Not able to format the document with '}'

SyntaxEditor for Windows Forms Forum

Posted 10 years ago by Cheshta Mittal - Software Developer, Beehive Systems
Version: 14.1.0320
Avatar

Hi,

I wanted the functionality of formatting the document and bringing all the lines in the document at correct indents when a closing curly bracket ('}') is added (like it works in Visual Studio). I could not find a suitable method, so I found the matching bracket token, their respective document line indexes and worked with that. It turns out that when I remove the last '}' of the document and insert it again, the syntax editor and its scroller slows down. Can you please tell me if there is any method or another way to format a document's indents?

Thanks in advance.

Comments (9)

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

Hello,

If you are doing formatting, I would recommend that you wrap the transactions with a document.UndoRedo.StartGroup() and .EndGroup() set of calls and then go through the lines and only make minimal changes as necessary.  For instance if you see the indent level of the line is fine, then leave it as is.  If you see that an additional tab is needed, just insert the single tab character.  That way you do the minimal number of whitespace edits and don't really alter the meaningful code like '}' characters.


Actipro Software Support

Posted 10 years ago by Cheshta Mittal - Software Developer, Beehive Systems
Avatar

Hi,

I am sorry, but I am still confused. At present, I find the line-indexes between a '{' and '}' . I then add indentation to those lines if required using  a 'for' loop (by comparing their tabstoplevel value).

If I use your approach, when am I supposed to use document.UndoRedo.StartGroup()?

Thanks

[Modified 10 years ago]

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

Hello,

You want to call StartGroup() right before your logic and then call the related EndGroup() method at the end of your logic.  That way all the modifications get wrapped into a single undo unit.


Actipro Software Support

Posted 10 years ago by Cheshta Mittal - Software Developer, Beehive Systems
Avatar

Hi,

I tried using StartGroup(), but if I have a class with around 8-9k number of lines, the editor gets hanged, applying changes in a really slow speed. I guess this is not a possible workaround. Can you suggest me something else please?

 

Thanks

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

Hello,

Oh if you have that many changes then the WinForms SyntaxEditor won't handle that number of modifications in a performant way.  That's something we specifically addressed when we wrote the newer WPF version's document object model.  The object model there allows you to combine many text change operations into a single atomic text change, meaning there is only one text change event that fires (and all UI pieces listen to) instead of hundreds or thousands of events firing, as in your case.  That means that the WPF version handles bulk changes much faster than the WinForms version can.

If you are using the WinForms SyntaxEditor and making that number of changes, the best thing would probably be to do a single string replacement that is a union of your modifications.  The Document.ReplaceText method has an overload where you can specify DocumentModificationOptions.  You'd want to pass in DocumentModficationOptions.RetainSelection.


Actipro Software Support

Posted 10 years ago by Cheshta Mittal - Software Developer, Beehive Systems
Avatar

Hi,

Do you people have any sample code using FormatTextRange() for C# language for formatting indents? I am not able to use FormatDocument and FormatSelection. It gives an error every time of missing implementation.

 

Thanks

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

Hello,

Formatting is different for each language, so it's up to the language author to implement (or not implement) the formatting logic.  If the language author does implement it, they must implement the SyntaxLanguage.FormatTextRange method and return true in the FormattingSupported property.  Our C# language in the .NET Languages Add-on doesn't implement code formatting at this time, and its FormattingSupported property should be returning false.  You would need to add that logic yourself for C#.


Actipro Software Support

Posted 10 years ago by Cheshta Mittal - Software Developer, Beehive Systems
Avatar

Hi,

I was checking out the actiprosoftware blog. There, a text formatter has been mentioned for WPF and Silverlight C# add-ons. And it is used for formatting the document. Is there anything similar to that present in Winforms Add-on? Or, will there be anything similar to it added in upcoming versions? 

There is an online sample of Silverlight as well with a FormatDocument and FomatSelection option. Is there a possibility to get its underlying code of formatting the indents?

 

Thanks

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

Hello,

Yes the WPF/Silverlight/WinRT versions of the .NET Languages Add-on do have a C# text formatter.  They all share a common object model.  But the WinForms version uses an older object model, so unfortunately the text formatter code isn't compatible with it, since the API is very different.  We eventually would like to get the WinForms version on the same general API as the other newer versions so that we could keep all four platforms in sync.  I'm not sure if/when that will happen though.


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.