瀏覽代碼

Editor: support highlighting tabs

Le Tan 7 年之前
父節點
當前提交
68dc2425a9
共有 7 個文件被更改,包括 127 次插入30 次删除
  1. 3 0
      src/resources/vnote.ini
  2. 4 0
      src/vconfigmanager.cpp
  3. 23 0
      src/vconfigmanager.h
  4. 2 2
      src/vedit.cpp
  5. 70 22
      src/veditor.cpp
  6. 13 6
      src/veditor.h
  7. 12 0
      src/vmainwindow.cpp

+ 3 - 0
src/resources/vnote.ini

@@ -289,6 +289,9 @@ enable_smart_im_in_vim_mode=true
 ; Should be one character long
 vim_leader_key=" "
 
+; Enable tab highlight
+enable_tab_highlight=false
+
 [export]
 ; Path of the wkhtmltopdf tool
 wkhtmltopdf=wkhtmltopdf

+ 4 - 0
src/vconfigmanager.cpp

@@ -353,6 +353,10 @@ void VConfigManager::initEditorConfigs()
             m_vimLeaderKey = QChar(' ');
         }
     }
+
+    m_enableTabHighlight = getConfigFromSettings("editor",
+                                                 "enable_tab_highlight").toBool();
+
 }
 
 void VConfigManager::initSettings()

+ 23 - 0
src/vconfigmanager.h

@@ -314,6 +314,9 @@ public:
     bool getEnableTrailingSpaceHighlight() const;
     void setEnableTrailingSapceHighlight(bool p_enabled);
 
+    bool getEnableTabHighlight() const;
+    void setEnableTabHighlight(bool p_enabled);
+
     KeyMode getKeyMode() const;
     void setKeyMode(KeyMode p_mode);
 
@@ -804,6 +807,9 @@ private:
     // Enable trailing-space highlight.
     bool m_enableTrailingSpaceHighlight;
 
+    // Enable tab highlight.
+    bool m_enableTabHighlight;
+
     // Editor key mode.
     KeyMode m_keyMode;
 
@@ -1818,6 +1824,23 @@ inline void VConfigManager::setEnableTrailingSapceHighlight(bool p_enabled)
                         m_enableTrailingSpaceHighlight);
 }
 
+inline bool VConfigManager::getEnableTabHighlight() const
+{
+    return m_enableTabHighlight;
+}
+
+inline void VConfigManager::setEnableTabHighlight(bool p_enabled)
+{
+    if (m_enableTabHighlight == p_enabled) {
+        return;
+    }
+
+    m_enableTabHighlight = p_enabled;
+    setConfigToSettings("editor",
+                        "enable_tab_highlight",
+                        m_enableTabHighlight);
+}
+
 inline KeyMode VConfigManager::getKeyMode() const
 {
     return m_keyMode;

+ 2 - 2
src/vedit.cpp

@@ -604,7 +604,7 @@ static void trailingSpaceFilter(VEdit *p_editor, QList<QTextEdit::ExtraSelection
 void VEdit::highlightTrailingSpace()
 {
     if (!g_config->getEnableTrailingSpaceHighlight()) {
-        QList<QTextEdit::ExtraSelection> &selects = m_extraSelections[(int)SelectionId::TrailingSapce];
+        QList<QTextEdit::ExtraSelection> &selects = m_extraSelections[(int)SelectionId::TrailingSpace];
         if (!selects.isEmpty()) {
             selects.clear();
             highlightExtraSelections(true);
@@ -616,7 +616,7 @@ void VEdit::highlightTrailingSpace()
     format.setBackground(m_trailingSpaceColor);
     QString text("\\s+$");
     highlightTextAll(text, FindOption::RegularExpression,
-                     SelectionId::TrailingSapce, format,
+                     SelectionId::TrailingSpace, format,
                      trailingSpaceFilter);
 }
 

+ 70 - 22
src/veditor.cpp

@@ -26,7 +26,9 @@ VEditor::VEditor(VFile *p_file,
       m_enableInputMethod(true),
       m_timeStamp(0),
       m_trailingSpaceSelectionTS(0),
-      m_completer(p_completer)
+      m_completer(p_completer),
+      m_trailingSpaceHighlightEnabled(false),
+      m_tabHighlightEnabled(false)
 {
 }
 
@@ -73,6 +75,8 @@ void VEditor::init()
 
     m_trailingSpaceColor = QColor(g_config->getEditorTrailingSpaceBg());
 
+    m_tabColor = g_config->getMdEditPalette().color(QPalette::Text);
+
     QPixmap wrapPixmap(":/resources/icons/search_wrap.svg");
     m_wrapLabel = new QLabel(m_editor);
     m_wrapLabel->setPixmap(wrapPixmap.scaled(labelSize, labelSize));
@@ -93,7 +97,7 @@ void VEditor::init()
     m_trailingSpaceTimer->setSingleShot(true);
     m_trailingSpaceTimer->setInterval(trailingSpaceUpdateTimer);
     QObject::connect(m_trailingSpaceTimer, &QTimer::timeout,
-                     m_object, &VEditorObject::doUpdateTrailingSpaceHighlights);
+                     m_object, &VEditorObject::doUpdateTrailingSpaceAndTabHighlights);
 
     m_extraSelections.resize((int)SelectionId::MaxSelection);
 
@@ -108,9 +112,31 @@ void VEditor::labelTimerTimeout()
     m_wrapLabel->hide();
 }
 
-void VEditor::updateTrailingSpaceHighlights()
+bool VEditor::needUpdateTrailingSpaceAndTabHighlights()
 {
+    bool ret = false;
+    bool space = g_config->getEnableTrailingSpaceHighlight();
+    if (m_trailingSpaceHighlightEnabled != space) {
+        m_trailingSpaceHighlightEnabled = space;
+        ret = true;
+    }
+
+    bool tab = g_config->getEnableTabHighlight();
+    if (m_tabHighlightEnabled != tab) {
+        m_tabHighlightEnabled = tab;
+        ret = true;
+    }
+
     if (m_trailingSpaceSelectionTS != m_timeStamp) {
+        ret = true;
+    }
+
+    return ret;
+}
+
+void VEditor::updateTrailingSpaceAndTabHighlights()
+{
+    if (needUpdateTrailingSpaceAndTabHighlights()) {
         m_trailingSpaceTimer->start();
     } else {
         highlightExtraSelections(false);
@@ -123,7 +149,7 @@ void VEditor::doHighlightExtraSelections()
     Q_ASSERT(nrExtra == (int)SelectionId::MaxSelection);
     QList<QTextEdit::ExtraSelection> extraSelects;
     for (int i = 0; i < nrExtra; ++i) {
-        if (i == (int)SelectionId::TrailingSapce) {
+        if (i == (int)SelectionId::TrailingSpace) {
             filterTrailingSpace(extraSelects, m_extraSelections[i]);
         } else {
             extraSelects.append(m_extraSelections[i]);
@@ -133,29 +159,51 @@ void VEditor::doHighlightExtraSelections()
     setExtraSelectionsW(extraSelects);
 }
 
-void VEditor::doUpdateTrailingSpaceHighlights()
+void VEditor::doUpdateTrailingSpaceAndTabHighlights()
 {
-    if (!g_config->getEnableTrailingSpaceHighlight()) {
-        QList<QTextEdit::ExtraSelection> &selects = m_extraSelections[(int)SelectionId::TrailingSapce];
+    bool needHighlight = false;
+
+    // Trailing space.
+    if (!m_trailingSpaceHighlightEnabled) {
+        QList<QTextEdit::ExtraSelection> &selects = m_extraSelections[(int)SelectionId::TrailingSpace];
         if (!selects.isEmpty()) {
             selects.clear();
-            highlightExtraSelections(true);
+            needHighlight = true;
         }
-
-        m_trailingSpaceSelectionTS = m_timeStamp;
-        return;
+    } else {
+        needHighlight = true;
+        QTextCharFormat format;
+        format.setBackground(m_trailingSpaceColor);
+        QString text("\\s+$");
+        highlightTextAll(text,
+                         FindOption::RegularExpression,
+                         SelectionId::TrailingSpace,
+                         format);
+    }
+
+    // Tab.
+    if (!m_tabHighlightEnabled) {
+        QList<QTextEdit::ExtraSelection> &selects = m_extraSelections[(int)SelectionId::Tab];
+        if (!selects.isEmpty()) {
+            selects.clear();
+            needHighlight = true;
+        }
+    } else {
+        needHighlight = true;
+        QTextCharFormat format;
+        format.setBackground(m_tabColor);
+        QString text("\\t");
+        highlightTextAll(text,
+                         FindOption::RegularExpression,
+                         SelectionId::Tab,
+                         format);
     }
 
-    QTextCharFormat format;
-    format.setBackground(m_trailingSpaceColor);
-    QString text("\\s+$");
-    highlightTextAll(text,
-                     FindOption::RegularExpression,
-                     SelectionId::TrailingSapce,
-                     format);
-
     m_trailingSpaceSelectionTS = m_timeStamp;
-    highlightExtraSelections(true);
+
+    if (needHighlight) {
+        highlightExtraSelections(true);
+    }
 }
 
 void VEditor::updateEditConfig()
@@ -175,7 +223,7 @@ void VEditor::highlightOnCursorPositionChanged()
 
     QTextCursor cursor = textCursorW();
     if (lastCursor.isNull() || cursor.blockNumber() != lastCursor.blockNumber()) {
-        updateTrailingSpaceHighlights();
+        updateTrailingSpaceAndTabHighlights();
         highlightCurrentLine();
     } else {
         // Judge whether we have trailing space at current line.
@@ -196,7 +244,7 @@ void VEditor::highlightOnCursorPositionChanged()
             }
 
             if (needUpdate) {
-                updateTrailingSpaceHighlights();
+                updateTrailingSpaceAndTabHighlights();
             }
         }
 

+ 13 - 6
src/veditor.h

@@ -31,7 +31,8 @@ enum class SelectionId {
     SearchedKeyword,
     SearchedKeywordUnderCursor,
     IncrementalSearchedKeyword,
-    TrailingSapce,
+    TrailingSpace,
+    Tab,
     MaxSelection
 };
 
@@ -490,6 +491,10 @@ private:
                          int p_start = 0,
                          int p_end = -1);
 
+    void updateTrailingSpaceAndTabHighlights();
+
+    bool needUpdateTrailingSpaceAndTabHighlights();
+
     QLabel *m_wrapLabel;
     QTimer *m_labelTimer;
 
@@ -512,6 +517,7 @@ private:
     QColor m_incrementalSearchedWordBg;
 
     QColor m_trailingSpaceColor;
+    QColor m_tabColor;
 
     // Timer for extra selections highlight.
     QTimer *m_highlightTimer;
@@ -538,6 +544,9 @@ private:
 
     FindInfo m_findInfo;
 
+    bool m_trailingSpaceHighlightEnabled;
+    bool m_tabHighlightEnabled;
+
 // Functions for private slots.
 private:
     void labelTimerTimeout();
@@ -545,9 +554,7 @@ private:
     // Do the real work to highlight extra selections.
     void doHighlightExtraSelections();
 
-    void updateTrailingSpaceHighlights();
-
-    void doUpdateTrailingSpaceHighlights();
+    void doUpdateTrailingSpaceAndTabHighlights();
 
     void clearFindCache();
 };
@@ -615,9 +622,9 @@ private slots:
         m_editor->doHighlightExtraSelections();
     }
 
-    void doUpdateTrailingSpaceHighlights()
+    void doUpdateTrailingSpaceAndTabHighlights()
     {
-        m_editor->doUpdateTrailingSpaceHighlights();
+        m_editor->doUpdateTrailingSpaceAndTabHighlights();
     }
 
     void clearFindCache()

+ 12 - 0
src/vmainwindow.cpp

@@ -1236,6 +1236,15 @@ void VMainWindow::initEditMenu()
     connect(trailingSapceAct, &QAction::triggered,
             this, &VMainWindow::changeHighlightTrailingSapce);
 
+    // Highlight tab.
+    QAction *tabAct = new QAction(tr("Highlight Tabs"), this);
+    tabAct->setToolTip(tr("Highlight all the tabs"));
+    tabAct->setCheckable(true);
+    connect(tabAct, &QAction::triggered,
+            this, [this](bool p_checked) {
+                g_config->setEnableTabHighlight(p_checked);
+            });
+
     QMenu *findReplaceMenu = editMenu->addMenu(tr("Find/Replace"));
     findReplaceMenu->setToolTipsVisible(true);
     findReplaceMenu->addAction(m_findReplaceAct);
@@ -1311,6 +1320,9 @@ void VMainWindow::initEditMenu()
 
     editMenu->addAction(trailingSapceAct);
     trailingSapceAct->setChecked(g_config->getEnableTrailingSpaceHighlight());
+
+    editMenu->addAction(tabAct);
+    tabAct->setChecked(g_config->getEnableTabHighlight());
 }
 
 void VMainWindow::initDockWindows()