From a7e61a11c3a1aeb744778817cb47df3f36b431b3 Mon Sep 17 00:00:00 2001 From: Ntalumeso Date: Thu, 26 Sep 2024 10:14:27 +0300 Subject: [PATCH] Simplify the code --- instat/ucrScript.vb | 74 +++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 43 deletions(-) diff --git a/instat/ucrScript.vb b/instat/ucrScript.vb index 55e8f63b45..25ac4d3448 100644 --- a/instat/ucrScript.vb +++ b/instat/ucrScript.vb @@ -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 - - ''' ''' Removes the selected text from the active tab, and copies the removed text to the clipboard. ''' @@ -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