金庆的专栏

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  344 随笔 :: 0 文章 :: 454 评论 :: 0 Trackbacks
为 LiteIDE 添加选中标记

(金庆的专栏 2018.1)

"[Eclipse Mark Occurrences](http://www.eclipse.org/pdt/help/html/mark_occurrences.htm)"
可以在滚动条旁边显示文中所有选中的标记,
可以选中某个变量显示有多少引用。不过 GoClipse 没有 "Mark Occurrences" 功能。
VS code 有此功能。

LiteIDE 对于build错误会显示这种标记,所以感觉可以依此实现"Mark Occurrences"。

首先找到了打标记的功能。标记用到颜色,所以搜'QColor', 找到了:
```c++
QColor markTypeColor(LiteApi::EditorNaviagteType type)
```

相关的接口会用到:
```c++
    void insertNavigateMark(int line, LiteApi::EditorNaviagteType type, const QString &msg, const char* tag);
    void clearAllNavigateMark(LiteApi::EditorNaviagteType types, const char *tag);
```

寻找选中时触发标记的代码,选中或搜索时会有圆角框圈出,搜"find", 找到:
```c++
if (!m_findExpression.isEmpty()) {
    if (!findInBlock(block,m_findExpression,pos,m_findFlags,cur)) {
        break;
    }
    ...
    painter.drawRoundedRect(offsetX+left,r.top()+l.y(),right-left,l.height(),3,3);
} else if (!m_selectionExpression.isEmpty()) {
    if (!findInBlock(block,m_selectionExpression,pos,QTextDocument::FindWholeWords,cur)) {
        break;
    }
    ...
    painter.drawRoundedRect(offsetX+left,r.top()+l.y(),right-left,l.height(),3,3);
}
```

发现画方框仅在可视区域。续继查找 m_findExpression 和 m_selectionExpression 更改处,
添加 'updateNavigateMarks()'

```c++
void LiteEditorWidgetBase::setFindOption(LiteApi::FindOption *opt)
{
    ...
+   updateNavigateMarks(LiteApi::EditorNavigateFind);
    viewport()->update();
}
```

```c++
void LiteEditorWidgetBase::slotSelectionChanged()
{
    ...
        m_selectionExpression.setPattern(pattern);
+       updateNavigateMarks(LiteApi::EditorNavigateSelection);
        viewport()->update();
    ...
}
```

须添加2个新的类型:
```c++
enum EditorNaviagteType{
    EditorNavigateNormal = 1,
    EditorNavigateWarning = 2,
    EditorNavigateError = 4,
    EditorNavigateReload = 8,
+   EditorNavigateFind = 16,
+   EditorNavigateSelection = 32,
    EditorNavigateBad = EditorNavigateWarning|EditorNavigateError
};
```

搜索 `EditorNavigateWarning`, 找到因新增类型须更改优先级表和颜色函数。

```c++
const int PRIORITYLIST_LENGTH = 7;
const LiteApi::EditorNaviagteType MARKTYPE_PRIORITYLIST[PRIORITYLIST_LENGTH] = {
        ..., LiteApi::EditorNavigateFind, LiteApi::EditorNavigateSelection, ...
    };
...
inline QColor markTypeColor(LiteApi::EditorNaviagteType type) {
    switch(type) {
    ...
    case LiteApi::EditorNavigateNormal:
        return Qt::darkGreen;
    case LiteApi::EditorNavigateReload:
        return Qt::darkBlue;
    }
}
```

更新标记时先清空,然后逐行搜索添加标记:
```c++
// Update selections or find marks.
void LiteEditorWidgetBase::updateNavigateMarks(LiteApi::EditorNaviagteType type)
{
    clearAllNavigateMark(type, "");
    ...

    QTextDocument *doc = this->document();
    for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
    {
        if (!needToMarkBlock(it, type))
            continue;
        int lineNumber = it.blockNumber() + 1;
        insertNavigateMark(lineNumber, type, QString("%1: %2").arg(lineNumber).arg(it.text()), "");
    }
}

bool LiteEditorWidgetBase::needToMarkBlock(
    const QTextBlock &block, LiteApi::EditorNaviagteType type) const
{
    ...
    if (LiteApi::EditorNavigateFind == type)
        return findInBlock(block, m_findExpression, pos, m_findFlags, cur);
    if (LiteApi::EditorNavigateSelection == type)
        return findInBlock(block, m_selectionExpression, pos,
                           QTextDocument::FindWholeWords, cur);
    return false;
}
```

已合并主干:
```
Revision: 43f4954b0b802eccbbf451136be600bfcec71f27
Author: Jin Qing <jinq0123@163.com>
Date: 18.1.5 19:08:26
Message:
Add "Mark Occurrences" function that marks selections and findings.

----
Modified: liteidex/src/api/liteeditorapi/liteeditorapi.h
Modified: liteidex/src/plugins/liteeditor/liteeditorwidgetbase.cpp
Modified: liteidex/src/plugins/liteeditor/liteeditorwidgetbase.h
```
posted on 2018-01-06 11:05 金庆 阅读(60) 评论(0)  编辑 收藏 引用 所属分类: 1. C/C++

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理