Adding text at beginning and end of each line (selection)

SyntaxEditor for Windows Forms Forum

Posted 7 years ago by Radjesh Klauke - X-impress
Version: 12.1.0300
Avatar

Hi,

How do I add text to each line at the beginning and end of a selection? I have done it in a richtextbox, but I can't seem to 'convert' it to syntaxeditor.

With RichTextBox1
Dim startAt As Integer = .GetFirstCharIndexFromLine(.GetLineFromCharIndex(.SelectionStart))
Dim endAt As Integer = .Text.IndexOf(vbLf, .SelectionStart + .SelectionLength) - 1
endAt = If(endAt < 0, .Text.Length, endAt)
.SelectionStart = startAt
.SelectionLength = endAt - startAt + 1

'format selected lines

Dim lines As New List(Of String)(.SelectedText.Split(New String() {vbLf}, StringSplitOptions.RemoveEmptyEntries))
If lines.Count > 0 Then
lines = Array.ConvertAll(lines.ToArray, Function(s) "   [line]" & s & "[/line]").ToList
lines.Insert(0, "[start]")
lines.Add("[/start]")
End If

.SelectedText = String.Join(vbLf, lines.ToArray)
End With

Comments (6)

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

Hi Radjesh,

I think something like this will work...

Dim firstLine As DocumentLine = editor.Document.Lines(editor.SelectedView.Selection.FirstDocumentPosition.Line)
Dim lastLine As DocumentLine = editor.Document.Lines(editor.SelectedView.Selection.LastDocumentPosition.Line)
Dim text As String = editor.Document.GetSubString(New TextRange(firstLine.StartOffset, lastLine.EndOffset))
' Convert here
editor.SelectedView.SelectedText = text


Actipro Software Support

Posted 7 years ago by Radjesh Klauke - X-impress
Avatar

Hmmm... Thanks, but the "conversion" is the issue. Should've been clearer. Example:

Radjesh
Radjesh
Radjesh
Radjesh

When I select the list above in the editor and perform the action, the result conversion should be:

[mycode]
   [start]
     [line]Radjesh[/line]
     [line]Radjesh[/line]
     [line]Radjesh[/line]
     [line]Radjesh[/line]
   [/start]
[/mycodecode]

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

The code we posted should give you the four Radjesh lines in one string.  So you could split those up by vbLf characters like you did before in your original post.  That code goes where we commented "Convert here" and is why we put that comment there.  Put the String.Join result in the "text" variable then and you're done.


Actipro Software Support

Posted 7 years ago by Radjesh Klauke - X-impress
Avatar

The problem is that it returns

[mycode]
[start]
[line]radjesh
[/line]
[line] radjesh
[/line]
[line] radjesh
[/line]
[line] radjesh[/line]
[/start]
[/mycode]

For Each xsyn As SyntaxEditor In document_tab.SelectedTab.AttachedControl.Controls

            Dim firstLine As DocumentLine = xsyn.Document.Lines(xsyn.SelectedView.Selection.FirstDocumentPosition.Line)
            Dim lastLine As DocumentLine = xsyn.Document.Lines(xsyn.SelectedView.Selection.LastDocumentPosition.Line)
            Dim text As String = xsyn.Document.GetSubstring(New TextRange(firstLine.StartOffset, lastLine.EndOffset))

            ' Convert here

            Dim lines As New List(Of String)(xsyn.SelectedView.SelectedText.Split(New String() {vbLf}, StringSplitOptions.RemoveEmptyEntries))
            If lines.Count > 0 Then
                lines = Array.ConvertAll(lines.ToArray, Function(s) "[line]" & Trim(s) & "[/line]").ToList
                lines.Insert(0, "[mycode]")
                lines.Insert(1, "[start]")
                lines.Add("[/start]")
                lines.Add("[/mycode]")
            End If

            xsyn.SelectedView.SelectedText = String.Join(vbLf, lines.ToArray)
Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

The Document.GetSubstring method returns CR/LF line endings by default.  You can use this overload to get it with just LF line endings:

Dim text As String = xsyn.Document.GetSubstring(New TextRange(firstLine.StartOffset, lastLine.EndOffset), LineTerminator.Newline) 


Actipro Software Support

Posted 7 years ago by Radjesh Klauke - X-impress
Avatar

Yes. that was. it. Thanks.

The latest build of this product (v2018.1 build 0341) 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.