Format Selection Possible?

SyntaxEditor for Windows Forms Forum

Posted 12 years ago by Cody Thompson - Space Systems Engineer, a.i. solutions
Version: 12.1.0302
Avatar

Hello again!

I am looking to implement a 'Format Selection' option into my language similar to what Visual Studio currently has when you press Control+K followed by Control+F with text selected. What it does is it runs through selection formatting code when you press this key combination. I haven't been able to find anything in the help documentation for support of this sort of thing, however, so I am wondering if it is possible to implement and if so, how?

Thanks in advance.

Comments (2)

Posted 12 years ago by Tobias Lingemann - Software Devolpment Engineer, Vector Informatik GmbH
Avatar

Well, here is what I did.

I have made two steps. The first step analyzes per token. I use the token stream to get the previous and/or next token. Depending on that I insert or remove whitespaces.

The second step only trims whitespaces and uses the correct indentation.

My biggest problem is the performance. In my editor a lot of things happen during the DocumentChanged event, so I try to minimize the modifications.

My method lookes like this:

    public override void FormatTextRange(ActiproSoftware.SyntaxEditor.Document document, TextRange textRange)
    {
      document.UndoRedo.StartGroup(DocumentModificationType.AutoFormat);

      // 1 - analyze per token
      FormatTokens(document, textRange);

      // 2 - analyze per line (indentation)
      FormatLines(document, textRange);

      document.UndoRedo.EndGroup();
    }

 And inside the methods you do your logic and replace the text like that:

document.ReplaceText(DocumentModificationType.AutoFormat, currentToken.TextRange, newText, DocumentModificationOptions.RetainSelection);

[Modified 12 years ago]


Best regards, Tobias Lingemann.

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Cody,

Yes it is supported but you'd need to write the code in your language to do the actual formatting.  What Tobias suggested is the right direction of what you would want to do.

The "Language Definition Guide / Other Features / Code Formatting" topic talks about it a bit.  Basically you'd have your language return true from a FormattingSelected property.  Then you'd need to implement the FormatTextRange method.  It's probably easiest if you have the logic in that method build up a StringBuilder of the formatted result then do a document.ReplaceText at the end with the sum result.


Actipro Software Support

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

Add Comment

Please log in to a validated account to post comments.