Code Assistant - Wrap in Quote Marks

I often start typing strings without enclosing them in quote marks, especially when using MessageBox, so I wrote my first Code Assistant to fix that.

// This script takes the current selection and wraps
// it in quote marks

Function Name () As String
  Return "Wrap In Quotes"
End Function

Function CanEdit (selection As String) As Boolean
  Return If(Selection = "", True, False)
End Function

Function Edit (selection As String) As String
  Var line As String
  Var a As String
  a = Chr(34) + selection + Chr(34)
  Return a
End Function
3 Likes

For a moment I said to myself “What’s he talking about?” and then I saw this blog post:

thanks for sharing.
just one thought.
it will not duplicate a quote if there is one inside the selected string.
is it the desired behavior ? I presume not …

1 Like

you could start from this former xojo script (made by Kem IIRC)

Const kQuote = """"

Dim s As String
s = SelText

Dim eol As String
If s.InStr( &u0D + &u0A ) <> 0 Then
  eol = &u0D + &u0A
Elseif s.InStr( &u0D ) <> 0 Then
  eol = &u0D
Elseif s.InStr( &u0A ) <> 0 Then
  eol = &u0A
End If

If eol <> "" Then
  Dim lines() As String
  lines = Split( s, eol )
  s = Join( lines, kQuote + " + EndOfLine + _" + eol + kQuote )
End If

s = kQuote + s + kQuote

Dim start As Integer
start = SelStart
SelText = s
SelStart = start + 1
SelLength = s.Len - 2

I still don’t use last xojo 2022 so I cannot test it.

I wish they would put button in the bar above the code editor to get drop down menu for those Code Assistants. Since their pretty nice except the click path to them is a bit long.

3 Likes

Being able to assign keyboard shortcuts for Code Assistants would awesome.

2 Likes

Here’s the new version that will wrap selected with with quote marks and fix straight double quote marks inside the string.

// This script wraps the current selection
// in quote marks

Function Name () As String
  Return "Wrap In Quotes"
End Function

Function CanEdit (selection As String) As Boolean
  Return If(Selection = "", False, True)
End Function

Function Edit (selection As String) As String
  Var a As String
  Var b As String = Chr(34)
  a = b + b + b + b
  selection = selection.ReplaceAll(b, a)
  a = b + selection + b
  Return a
End Function
1 Like

Looks good.

You seem to be replacing existing quotes with 4 quotes, but you only need two.

The line Return If ... can be shortened to Return selection <> ""

1 Like

I like this idea, so I created a case for it.

4 Likes