Bracket matching in a temporary [not visible] editor

Posted 2 years ago by Avatar Mike Dempsey - Sr. Developer, Teradata Corp

Is it possible to use the parser, or other methods, to find the matching brackets when using a temporary editor or document. (one that is not attached to the UI.)

I tried to use the SelectedView.Selection to set the start/end offsets to point to a '(' and then tried using Selection.MoveToMatchingBracket() but it doesn't seem to move.

It works fine when I do the same thing with my real [visible] editor.

I also tried to use SelectedView.BracketHighlighting but both BracketToken and MatchingBracketToken remain null.

(Note that this is a Dynamic language - not sure if the BracketHighlighting property is supported for Dynamic languages.)

For performance reasons I am doing everything in temporary objects, or in a StringBuilder, but I dont seem to be able to determine the text range for a block within parens. I can find the "(" no problem but I cant find the matching ")" so that I can indent the block.
(Simpley finding the 'next' ")" is no good - I need the matching one.)  

Is this possible?

Thanks, Mike

Comments (5)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Mike,

Did you set "BracketHighlightingVisible" to true on the temporary editor you were using?  If that is false, I don't believe the bracket token properties get updated.

Actipro Software Support
Posted 2 years ago by Mike Dempsey - Sr. Developer, Teradata Corp

I thought I had set "BracketHighlightingVisible" but I guess it was possible I accidentally set "BracketHighlightingInclusive" instead.

However I realized that this last phase of formatting will only apply a few changes, and there will be no long lines of text at this point, so I can do the indentation in the main window without creating a temporary editor. 

The original performance issue was caused by the fact that we had very long lines of text in the earlier phases.

I did find that if I suspend events then the Selection.MoveToMatchingBracket() does not move at all. (So I think an Editor without a UI may act similarly) To avoid a lot of flickering from the select/indent steps I suspended painting instead of suspending events and that works OK. 

Posted 2 years ago by Mike Dempsey - Sr. Developer, Teradata Corp

I tried it again and I guess I must have set "BracketHighlightingInclusive" rather than  "BracketHighlightingVisible" since it is now working.

The bad news is that it does not work any faster than when I use the 'visible' editor - in fact it is very slightly slower. (Maybe because of the time required to create/initialize the temporary editor and load a dynamic language file into its' Document.)

The good news is that by using the temporary editor there is no flickering, and if the user then uses 'Undo' the original text is restored immediately. (When using the real editor it could take up to 30 seconds to undo)

I again tried to use SelectedView.BracketHighlighting but both BracketToken and MatchingBracketToken remain null.
(So I assume the BracketHighlighting property is not supported for Dynamic languages.)

Is there an alternative to Selection.MoveToMatchingBracket() if all I need to know is the offset of the matching bracket?

The information must be available somewhere since MoveToMatchingBracket() must use it. But is it exposed through the public interface?



Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Mike,

The "SyntaxEditor / General Features / Text Visualization Features / Bracket Highlighting" topic in the documentation that comes with the product talks about how to add support for custom syntax languages.  Both dynamic and non-dynamic languages support it, but in slightly different ways.

The MoveToMatchingBracket code has some complex logic in it that tries to determine if a token next to the caret is a bracket and then uses the view's BracketHighlighting object's public properties to access the match.  If that info isn't available, it will fall back to getting a TextStream and will use the GoToPreviousMatchingToken method to find a match.

Actipro Software Support
Posted 2 years ago by Mike Dempsey - Sr. Developer, Teradata Corp

Thanks. I think what I had missed is the overloaded version of TextStream.GoToNextMatchingToken() that takes both the 'open bracket' token and the tokenId of the matching 'end bracket' token.

I had previously tried to position the TextStream at the '(' and use GoToNextMatchingToken(ts.Token) to go to the matching ')' but that did not seem to work. When I added the TokenId of the ')' then it works. Since there is only one matching EndBracket TokenId (unless special customization has been added) I did not think it was required to specify the TokenId of the end bracket token. 

This should allow me to use only the TextStream in a temporary Document so that i can perform the updates in a StringBuilder, as I did for the first phase of the process. That should be a lot faster that creating an editor and using Selection.Indent().

Information The latest build of this product (2018.1 build 0341) was released 1 month ago, which was after the last post in this thread.

Add a Comment

Please log in to a validated account to post comments.