0

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!

Alessandro
  • 11
  • 2
  • [SCI_BRACEMATCH](https://www.scintilla.org/ScintillaDoc.html#SCI_BRACEMATCH) / [SCI_BRACEMATCHNEXT](https://www.scintilla.org/ScintillaDoc.html#SCI_BRACEMATCHNEXT). – ekhumoro Mar 16 '22 at 12:30
  • Thank you, this message helps very much. But a part of the problem is still there, since from the middle of the word (inside the braces) I still need to find the opening one (starting point) and then SCI_BRACEMATCH will find the closing brace for me: ^ Caret Then I need to loop back up to < then using SCI_BRACEMATCH to find the position of >. Is it correct? More workarounds? Best ways? – Alessandro Mar 16 '22 at 13:32
  • You could use [SCI_SEARCHINTARGET](https://www.scintilla.org/ScintillaDoc.html#SCI_SEARCHINTARGET), and then `SCI_BRACEMATCH`. – ekhumoro Mar 16 '22 at 17:25

1 Answers1

0

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)));
            }
        }
Alessandro
  • 11
  • 2