Skip to content

Commit

Permalink
Simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
N-thony committed Sep 26, 2024
1 parent f900948 commit a7e61a1
Showing 1 changed file with 31 additions and 43 deletions.
74 changes: 31 additions & 43 deletions instat/ucrScript.vb
Original file line number Diff line number Diff line change
Expand Up @@ -120,48 +120,6 @@ Public Class ucrScript
EnableDisableButtons()
End Sub

Function EscapeDoubleQuotes(ByVal input As String) As String
' Replace each double quote with an escaped double quote
Return input.Replace("""", "\""")
End Function

Public Sub FormatRCode()

Try
' Your R script text from Scintilla
Dim scriptText As String = EscapeDoubleQuotes(clsScriptActive.SelectedText)
Dim clsStylerFunction As New RFunction

clsStylerFunction.SetPackageName("styler")
clsStylerFunction.SetRCommand("style_text")
clsStylerFunction.AddParameter("text", Chr(34) & scriptText & Chr(34), bIncludeArgumentName:=False)

Dim expTemp As SymbolicExpression
Dim formattedCode As String() = Nothing
expTemp = frmMain.clsRLink.RunInternalScriptGetValue(clsStylerFunction.ToScript(), bSilent:=True)
If expTemp IsNot Nothing AndAlso expTemp.Type <> Internals.SymbolicExpressionType.Null Then
formattedCode = expTemp.AsCharacter().ToArray
End If

' Join the formattedCode array into a single string
Dim formattedText As String = String.Join(Environment.NewLine, formattedCode)

' Check if there is any selected text
If clsScriptActive.SelectionStart <> clsScriptActive.SelectionEnd Then
' Replace the selected text with the formatted text
clsScriptActive.ReplaceSelection(formattedText)
Else
' If no text is selected, you could decide what to do (e.g., insert the text at the current caret position)
Dim currentPos As Integer = clsScriptActive.CurrentPosition
clsScriptActive.InsertText(currentPos, formattedText)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub


''' <summary>
''' Removes the selected text from the active tab, and copies the removed text to the clipboard.
''' </summary>
Expand Down Expand Up @@ -1069,6 +1027,36 @@ Public Class ucrScript
End Sub

Private Sub mnuReformatCode_Click(sender As Object, e As EventArgs) Handles mnuReformatCode.Click
FormatRCode()
Try
' Your R script text from Scintilla
Dim scriptText As String = clsScriptActive.SelectedText.Replace("""", "\""")
Dim clsStylerFunction As New RFunction

clsStylerFunction.SetPackageName("styler")
clsStylerFunction.SetRCommand("style_text")
clsStylerFunction.AddParameter("text", Chr(34) & scriptText & Chr(34), bIncludeArgumentName:=False)

Dim expTemp As SymbolicExpression
Dim formattedCode As String() = Nothing
expTemp = frmMain.clsRLink.RunInternalScriptGetValue(clsStylerFunction.ToScript(), bSilent:=True)
If expTemp IsNot Nothing AndAlso expTemp.Type <> Internals.SymbolicExpressionType.Null Then
formattedCode = expTemp.AsCharacter().ToArray
End If

' Join the formattedCode array into a single string
Dim formattedText As String = String.Join(Environment.NewLine, formattedCode)

' Check if there is any selected text
If clsScriptActive.SelectionStart <> clsScriptActive.SelectionEnd Then
' Replace the selected text with the formatted text
clsScriptActive.ReplaceSelection(formattedText)
Else
' If no text is selected, you could decide what to do (e.g., insert the text at the current caret position)
Dim currentPos As Integer = clsScriptActive.CurrentPosition
clsScriptActive.InsertText(currentPos, formattedText)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class

0 comments on commit a7e61a1

Please sign in to comment.