如何在Scintilla中突出显示文本?

7

我正在使用Scintilla编写一个编辑器。

我已经使用了词法分析器来进行自动语法高亮,但现在我想标记搜索结果。如果我只想标记一个命中,我可以设置选区,然而,我想标记(例如黄色背景)所有的命中。

我是用Perl编写的,但如果你有其他语言的建议也可以。

4个回答

9

你读过Scintilla文档中的标记参考吗? 这个参考可能有点晦涩,所以建议您也查看SciTE的源代码。这个文本编辑器最初是Scintilla的测试平台。它已经成长为一个完整的编辑器,但对于所有Scintilla相关问题仍然是一个很好的实现参考。

在我们的特定情况下,在查找对话框中有一个“全部标记”按钮。 您可以在SciTEBase :: MarkAll()方法中找到其实现。 此方法仅循环搜索结果(直到循环到第一个搜索结果为止,如果有的话),并在找到的行上放置书签(并可选择在找到的项上设置指示符)。找到的行使用SCI_LINEFROMPOSITION(posFound)获取,书签只需调用SCI_MARKERADD(lineno,markerBookmark)即可。请注意,标记可以是边栏中的符号,或者如果不与边栏关联,则会突出显示整行。

希望对您有所帮助。


2
"sample"编辑器scite使用书签功能来标记所有与搜索结果匹配的行。

1
这个解决方案在C#中有效。
   private void HighlightWord(Scintilla scintilla, string text)
    {
        if (string.IsNullOrEmpty(text))
            return;

        // Indicators 0-7 could be in use by a lexer
        // so we'll use indicator 8 to highlight words.
        const int NUM = 8;

        // Remove all uses of our indicator
        scintilla.IndicatorCurrent = NUM;
        scintilla.IndicatorClearRange(0, scintilla.TextLength);

        // Update indicator appearance
        scintilla.Indicators[NUM].Style = IndicatorStyle.StraightBox;
        scintilla.Indicators[NUM].Under = true;
        scintilla.Indicators[NUM].ForeColor = Color.Green;
        scintilla.Indicators[NUM].OutlineAlpha = 50;
        scintilla.Indicators[NUM].Alpha = 30;

        // Search the document
        scintilla.TargetStart = 0;
        scintilla.TargetEnd = scintilla.TextLength;
        scintilla.SearchFlags = SearchFlags.None;
        while (scintilla.SearchInTarget(text) != -1)
        {
            // Mark the search results with the current indicator
            scintilla.IndicatorFillRange(scintilla.TargetStart, scintilla.TargetEnd - scintilla.TargetStart);

            // Search the remainder of the document
            scintilla.TargetStart = scintilla.TargetEnd;
            scintilla.TargetEnd = scintilla.TextLength;
        }
    }

调用很简单,在此示例中,以 @ 开头的单词将被突出显示:

        Regex regex = new Regex(@"@\w+");
        string[] operands = regex.Split(txtScriptText.Text);
        Match match = regex.Match(txtScriptText.Text);

        if (match.Value.Length > 0)
            HighlightWord(txtScriptText, match.Value);

1

我使用指标来突出显示搜索结果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接