ScintillaNET: how to get surrounding symbols of a clicked word

117 Views Asked by At

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!

1

There are 1 best solutions below

0
On

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)));
            }
        }