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

发布于 2024-07-04 13:17:41 字数 233 浏览 14 评论 0原文

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

我已经在使用词法分析器来进行自动语法突出显示,但现在我想标记搜索结果。 如果我只想标记一个命中,我可以在那里设置选择,但是,我想标记(例如用黄色背景)所有命中。

我用 Perl 写这篇文章,但如果你有其他语言的建议,那也很酷。

I am writing an editor using Scintilla.

I am already using a lexer to do automatic syntax highlighting but now I would like to mark search results. If I want to mark only one hit I can set the selection there, however, I would like to mark (e.g. with yellow background) all the hits.

I writing this in Perl but if you have suggestions in other languages that would be cool as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧伤还要旧人安 2024-07-11 13:17:41

“示例”编辑器站点使用书签功能为与搜索结果匹配的所有行添加书签。

The "sample" editor scite uses the bookmark feature to bookmark all the lines that match the search result.

孤者何惧 2024-07-11 13:17:41

此解决方案适用于 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);

This solution works in 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;
        }
    }

Call is simple, in this example the words beginning with @ will be highlighted:

        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);
清醇 2024-07-11 13:17:41

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

I used Indicators to highlight search results.

感情旳空白 2024-07-11 13:17:41

您是否阅读过 Scintilla 文档中的标记参考
该参考文献可能有点晦涩,因此我建议您也看一下 SciTE 的源代码。 该文本编辑器最初是 Scintilla 的测试平台。 它已发展成为一个成熟的编辑器,但对于 Scintilla 的所有内容来说,它仍然是一个很好的实现参考。

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

HTH。

Have you read the Markers reference in Scintilla doc?
This reference can be a bit obscure, so I advise to take a look at the source code of SciTE as well. This text editor was originally a testbed for Scintilla. It grown to a full fledged editor, but it is still a good implementation reference for all things Scintilla.

In our particular case, there is a Mark All button in the Find dialog. You can find its implementation in SciTEBase::MarkAll() method. This method only loops on search results (until it loops on the first search result, if any) and puts a bookmark on the found lines (and optionally set an indicator on the found items).
The found line is gotten using SCI_LINEFROMPOSITION(posFound), the bookmark is just a call to SCI_MARKERADD(lineno, markerBookmark).
Note that the mark can be symbol in a margin, or if not associated to a margin, it will highlight the whole line.

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文