From 751aa18afac8fc8751713fb7d996e2b39212bb8f Mon Sep 17 00:00:00 2001 From: Akihito Yokota Date: Sun, 9 Jul 2023 14:02:50 +0900 Subject: [PATCH] Enabled Undo --- GHScriptGPT/Scripts/CurrentEditor.cs | 129 +++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/GHScriptGPT/Scripts/CurrentEditor.cs b/GHScriptGPT/Scripts/CurrentEditor.cs index ccb5d1e..e75197b 100644 --- a/GHScriptGPT/Scripts/CurrentEditor.cs +++ b/GHScriptGPT/Scripts/CurrentEditor.cs @@ -1,9 +1,11 @@ using Grasshopper.GUI.Script; using Grasshopper.Kernel; using System; +using System.IO; using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Drawing; using System.Text; using System.Threading.Tasks; @@ -52,15 +54,142 @@ public SouceCode GetCode_RunScript() public void SetCode_RunScript(SouceCode code) { + /* GH_CodeBlock newCodeBlock = new GH_CodeBlock(); newCodeBlock.AddLines(code.CodeLines); _codeBlocks[3] = newCodeBlock; UpdateEditor(); + */ + InsertBlock(code.CodeText); } private void UpdateEditor() { _editor.SetSourceCode(_codeBlocks); + + } + + private void InsertBlock(string text) + { + + BindingFlags bind = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; + + // CodeEditor + PropertyInfo pInfo_CodeEditor = _editor.GetType().GetProperty("CodeEditor", bind); + object codeEditor = pInfo_CodeEditor.GetValue(_editor); + Type type_codeEditor = typeof(Grasshopper.GUI.GH_AlignWidgetSettingsUI).Assembly.GetType("Grasshopper.GUI.GH_WndProcOverridenCodeEditor"); + + + string systemPath = Path.GetDirectoryName(typeof(Rhino.RhinoDoc).Assembly.Location); + string assemblyPath = Path.Combine(systemPath, "QWhale.Editor.dll"); + Assembly assembly = Assembly.LoadFile(assemblyPath); + + + // Selection + PropertyInfo pInfo_Selection = type_codeEditor.GetProperty("Selection", bind); + object selection = pInfo_Selection.GetValue(codeEditor); + Type type_Selection = assembly.GetType("QWhale.Editor.Selection", true); + + // TextSource + PropertyInfo pInfo_Source = type_codeEditor.GetProperty("Source", bind); + object textSource = pInfo_Source.GetValue(codeEditor); + Type type_TextSource = assembly.GetType("QWhale.Editor.TextSource.TextSource", true); + + // Position + PropertyInfo pInfo_Position = type_TextSource.GetProperty("Position", bind); + + + + List mutableRecs = GetMutableBlockRec(_codeBlocks); + Rectangle rec = mutableRecs[1]; + + // Position Change + pInfo_Position.SetValue(textSource, new Point(rec.X, rec.Y)); + + // Set Selection + Type type_SelectionType = assembly.GetType("QWhale.Editor.SelectionType", true); + MethodInfo mInfo_SetSelection = type_Selection.GetMethod("SetSelection", new Type[]{ type_SelectionType, typeof(Rectangle)}); + mInfo_SetSelection.Invoke(selection, new object[] { 1, rec }); + + // Set Text + MethodInfo mInfo_SetSelectedText = type_Selection.GetMethod("SetSelectedText", new Type[] { typeof(string), type_SelectionType, typeof(bool) }); + mInfo_SetSelectedText.Invoke(selection, new object[] { text, 1, true }); + + // Smart Format + MethodInfo mInfo_SmartFormatDocument = type_Selection.GetMethod("SmartFormatDocument", bind); + mInfo_SmartFormatDocument.Invoke(selection, new object[] { }); + + // Clear + MethodInfo mInfo_Clear = type_Selection.GetMethod("Clear", bind); + mInfo_Clear.Invoke(selection, new object[] { }); + + + } + + private List GetMutableIndex(bool[] isReadOnlys) + { + List mutableIndexes = new List(); + + List startIndexes = new List(); + List endIndexes = new List(); + + int count = isReadOnlys.Length; + bool previous = false; + + for (int i = 0; i < count; i++) + { + bool isReadOnly = isReadOnlys[i]; + try + { + if (isReadOnly) continue; + if (isReadOnlys[i + 1]) endIndexes.Add(i); + if (!previous) continue; + + startIndexes.Add(i); + + } + finally + { + previous = isReadOnly; + } + } + + for (int i = 0; i < startIndexes.Count; i++) + { + int[] start_end = new int[2]; + start_end[0] = startIndexes[i]; + start_end[1] = endIndexes[i]; + mutableIndexes.Add(start_end); + } + + return mutableIndexes; + } + + private List GetMutableBlockRec(GH_CodeBlocks codeBlocks) + { + List recs = new List(); + + string[] lines = new string[] { }; + bool[] isReadOnlys = new bool[] { }; + + codeBlocks.GetAllLines(ref lines, ref isReadOnlys); + + List mutableIndexes = GetMutableIndex(isReadOnlys); + + foreach (int[] start_end in mutableIndexes) + { + int start = start_end[0]; + int end = start_end[1]; + + int height = end - start; + + int width = lines[end].Length; + + Rectangle rec = new Rectangle(0, start, width, height); + recs.Add(rec); + } + + return recs; } public IEnumerable GetErrors()