ScintillaNET:如何获取单击单词的周围符号

发布于 2025-01-15 01:33:48 字数 480 浏览 2 评论 0原文

我在 VisualStudio/C# 中使用 ScintillaNET。 当用户单击(LMB 或 RMB)文本中的特定单词时,我需要获取周围的符号。例如:

This is <a test> to show my <problem>

在这种情况下,如果用户单击单词“test”,我想检索“<”之间的整个块。和“>”,所以我需要获取。 如果用户点击“问题”,我需要获取

我知道我可以获取插入符号位置,然后在该位置(向左)之前“导航”(for 循环)以查找第一次出现的“<”,然后在插入符号位置(向右)之后“导航”以找到第一次出现“>”。

但还有其他更好的方法来实现这一目标吗? Scintilla 是否提供一些找到它们的方法?

感谢您的帮助!

I'm using ScintillaNET in VisualStudio/C#.
When the user clicks (LMB or RMB) a specific word inside the text, I need to get the surrounding symbols. For example:

This is <a test> to show my <problem>

In this case, if the user clicks over the word "test", I want to retrieve the entire block between "<" and ">", so I need to get <a test>.
If the user clicks over "problem" I need to get <problem>.

I know that I can get the caret position then "navigate" (for loop) before the position (going left) to find the first occurence of "<", then "navigate" after the caret position (going right) to find the first occurrence of ">".

But is there any other better way to achieve this? Does Scintilla supply some methods to find them?

Thank you for your help!

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

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

发布评论

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

评论(1

热鲨 2025-01-22 01:33:48

我使用这段代码来找到解决方法,但坦率地说,我希望找到更好的解决方案:

const char CHAR_START_BLOCK = '[';
        const char CHAR_END_BLOCK = ']';

        if(e.Button == MouseButtons.Right) {
            int leftPos = -1;
            int rightPos = -1;

            //
            // Search for CHAR_START_BLOCK
            //
            for(int i = scintilla1.CurrentPosition; i >= 1; i--) {
                if(scintilla1.GetCharAt(i) == CHAR_START_BLOCK) {
                    leftPos = i;
                    break;
                }

                if(scintilla1.GetCharAt(i) == '\r') {
                    break;
                }
                
                if( (scintilla1.GetCharAt(i) == CHAR_END_BLOCK) && (i != scintilla1.CurrentPosition)) {
                    break;
                }
            }

            if(leftPos != -1) {

                //
                // Search for CHAR_END_BLOCK
                //
                string currentLine = scintilla1.Lines[scintilla1.CurrentLine].Text;
                for(int i = scintilla1.CurrentPosition; i <= (scintilla1.CurrentPosition + currentLine.len()); i++) {
                    if(scintilla1.GetCharAt(i) == CHAR_END_BLOCK) {
                        rightPos = i;
                        break;
                    }
                }

                LogManager.addLog("LEFT/RIGHT: " + scintilla1.GetTextRange(leftPos, (rightPos + 1 - leftPos)));
            }
        }

I used this code to find a workaround, but frankly speaking I wish to find a better solution:

const char CHAR_START_BLOCK = '[';
        const char CHAR_END_BLOCK = ']';

        if(e.Button == MouseButtons.Right) {
            int leftPos = -1;
            int rightPos = -1;

            //
            // Search for CHAR_START_BLOCK
            //
            for(int i = scintilla1.CurrentPosition; i >= 1; i--) {
                if(scintilla1.GetCharAt(i) == CHAR_START_BLOCK) {
                    leftPos = i;
                    break;
                }

                if(scintilla1.GetCharAt(i) == '\r') {
                    break;
                }
                
                if( (scintilla1.GetCharAt(i) == CHAR_END_BLOCK) && (i != scintilla1.CurrentPosition)) {
                    break;
                }
            }

            if(leftPos != -1) {

                //
                // Search for CHAR_END_BLOCK
                //
                string currentLine = scintilla1.Lines[scintilla1.CurrentLine].Text;
                for(int i = scintilla1.CurrentPosition; i <= (scintilla1.CurrentPosition + currentLine.len()); i++) {
                    if(scintilla1.GetCharAt(i) == CHAR_END_BLOCK) {
                        rightPos = i;
                        break;
                    }
                }

                LogManager.addLog("LEFT/RIGHT: " + scintilla1.GetTextRange(leftPos, (rightPos + 1 - leftPos)));
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文