InsertText respecting indent level

SyntaxEditor for Windows Forms Forum

Posted 19 years ago by GeorgeNY - CTO, RatchetSoft, LLC
Avatar
When i do an SelectedView.InsertText, and insert a string like "foo\nbar", i would expect to have the "foo" appear at the caret and the bar appear directly below at the same indent level. This is the bahvior if i were to type foo(Enter)bar and it is the behavior when i cut a preexisting foo/nbar and paste it back in again. But it is not what happens when i use InsertText.

The inserted text is inserted at the caret and after the first \n is encountered the remaining lines are at column 0.

Any thoughts or help. I am using the current version

George

George P Weihs

Comments (13)

Posted 19 years ago by Boyd - Sr. Software Developer, Patterson Consulting, LLC
Avatar
The auto-indent you get when you press enter is caused by the <Enter> key triggering the smart indent. When you insert text with a newline character (programmatically or through paste), there is no tigger key for the smart indention. In this case, you'll need to insert the proper number of tabs in the insert text (i.e. "foo\n\t\t\tbar") based on the indention of the current line.
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
As Boyd said, you can check the current tabstop level before you insert the text and update your text to insert by adding tabs in. The tabstop level is available with Document.GetTabStopLevel().


Actipro Software Support

Posted 19 years ago by GeorgeNY - CTO, RatchetSoft, LLC
Avatar
I though I would provide the code to my solution for anyone down the road:

int iTabStopLevel = syntaxEditor1.SelectedView.CurrentDocumentLine.GetTabStopLevel();
string insertCode = "foo\nbar\rhello\nworld";

// in case \n is used:
insertCode = insertCode.Replace("\n", "\n".PadRight(iTabStopLevel+1, '\t'));

// in case \r is used:
insertCode = insertCode.Replace("\r", "\r".PadRight(iTabStopLevel+1, '\t'));

syntaxEditor1.SelectedView.InsertText(insertCode);

Thank you for your help.

George P Weihs

Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Thanks for the posting! Just as a reminder, the internal text is only stored with a \n character to indicate line feeds. This optimizes parsing so that you don't have to continuously check for \r or \n or both. So Document.Text and Document.GetSubstring both return text formatted only with \n.

However there is a Document.GetText method and are Document.GetSubstring overrides that take a LineEndStyle, allowing to you choose which line-end format to use for the returned text.

Make sure you always use one of those when saving your files if you use a custom file saving method. Alternatively, our SaveFile methods have LineEndStyle parameters that you can use.


Actipro Software Support

Posted 19 years ago by NSXDavid
Avatar
Does the \t convert to spaces if you are using spaces for tabs?
Posted 19 years ago by Boyd - Sr. Software Developer, Patterson Consulting, LLC
Avatar
When inserting text, you'll need to change the tabs to spaces if that's what you desire.
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
EditorView.UndoableInsert is where the conversion happens. Typing of keys and EditorView.InsertText and a couple other places end up calling that. So right now the conversion isn't part of the actual document model. Does anyone think that this capability should be moved to the document model instead of the UI model?


Actipro Software Support

Posted 19 years ago by GeorgeNY - CTO, RatchetSoft, LLC
Avatar
Being new to it, you should take my opinion with a grain of salt.... but i think it should be part of the Document model. The only downside - that i can see - is that these "conversion" features would then be the default behavior for all insertion into the document (UI or other). I could imagine a scenario where this would be undesireable and would like to see some flag to turn this "AutoConversion" on or off.

George P Weihs

Posted 19 years ago by Boyd - Sr. Software Developer, Patterson Consulting, LLC
Avatar
Quote:
<font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by George W from New York:
I could imagine a scenario where this would be undesireable and would like to see some flag to turn this "AutoConversion" on or off.<HR>


I agree. This would be a nice convenience, but could possibly be an undesired result. What if you just overload the UndoableInsert to allow for a flag to auto-convert?
Posted 17 years ago by Matthew Smith - Developer, One Plus One Solutions Pty Limited
Avatar
Hi,

Sorry to dig up an old discussion, but is this sort of thing still valid in v4??

int iTabStopLevel = syntaxEditor1.SelectedView.CurrentDocumentLine.GetTabStopLevel(); 
string insertCode = "foo\nbar\rhello\nworld"; 

// in case \n is used: 
insertCode = insertCode.Replace("\n", "\n".PadRight(iTabStopLevel+1, '\t')); 

// in case \r is used: 
insertCode = insertCode.Replace("\r", "\r".PadRight(iTabStopLevel+1, '\t')); 

syntaxEditor1.SelectedView.InsertText(insertCode); 
I currently tried using \n, \r etc, but the characters just print on screen. I'm trying to do automatically add the closing token for items such as the following:

Function - End Function
Select Case - End Select

I have a dynamic language and are intercepting the return key in OnSyntaxEditorKeyTyping.

Regards, Matt

Posted 17 years ago by Matthew Smith - Developer, One Plus One Solutions Pty Limited
Avatar
Ok, decided to go with the InsertSurrounding Text as follows:

syntaxEditor.SelectedView.InsertSurroundingText(DocumentModificationType.AutoIndent, Nothing, "my text")
and allow the return to be processed. This now give the what I want as well as smart indenting as follows:

Function Test1()
End Function

    Function Test2()
    End Function
Great! Now, If I want to now insert a line inbetween to give me a gap using the following extra code:

syntaxEditor.SelectedView.InsertSurroundingText(DocumentModificationType.AutoIndent, Nothing, vbCr)
I get the following:

Function Test1()

End Function

    Function Test2()

End Function
With no indenting it works great, but if there is the End Function text is left stranded at start of the line and not indented for me.

Anything I can do better??

Regards, Matt

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Matthew,

If you'd like, create a simple project with the editor and your indent code in it so that we can debug it to see what is happening. Thanks!


Actipro Software Support

Posted 17 years ago by Matthew Smith - Developer, One Plus One Solutions Pty Limited
Avatar
I'll try and put something together soon...

Regards, Matt

The latest build of this product (v24.1.0) was released 3 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.