From 5a1cd9707f9a81ccfdc26e9b89c03a3e5b17d98b Mon Sep 17 00:00:00 2001 From: Martin Misol Monzo Date: Mon, 17 Aug 2020 11:30:03 -0400 Subject: [PATCH] Tab with spaces when using Python 3 The Python text editor now inserts spaces when the tab key is pressed if the engine is not 'IronPython2'. This is done in order to remain consistent with the reindent that occurs in 2to3 migration, which will avoid errors from Python 3 complaining about mixing spaces and tabs. --- .../ScriptEditorWindow.xaml.cs | 2 + .../PythonNodeCustomizationTests.cs | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/Libraries/PythonNodeModelsWpf/ScriptEditorWindow.xaml.cs b/src/Libraries/PythonNodeModelsWpf/ScriptEditorWindow.xaml.cs index 6f99ef6c14c..8328e31a16a 100644 --- a/src/Libraries/PythonNodeModelsWpf/ScriptEditorWindow.xaml.cs +++ b/src/Libraries/PythonNodeModelsWpf/ScriptEditorWindow.xaml.cs @@ -207,6 +207,8 @@ private void OnEngineChanged(object sender, System.Windows.Controls.SelectionCha { UpdateScript(editText.Text); } + + editText.Options.ConvertTabsToSpaces = nodeModel.Engine != PythonEngineVersion.IronPython2; } private void OnScriptEditorWindowClosed(object sender, EventArgs e) diff --git a/test/DynamoCoreWpfTests/PythonNodeCustomizationTests.cs b/test/DynamoCoreWpfTests/PythonNodeCustomizationTests.cs index 48ec6091ea7..b5c42e7f8e1 100644 --- a/test/DynamoCoreWpfTests/PythonNodeCustomizationTests.cs +++ b/test/DynamoCoreWpfTests/PythonNodeCustomizationTests.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; using Dynamo.Controls; using Dynamo.Graph.Workspaces; using Dynamo.Utilities; @@ -123,6 +124,16 @@ public void ChangingDropdownEngineSavesCodeBeforeRunning() DispatcherUtil.DoEvents(); } + private static ICSharpCode.AvalonEdit.TextEditor FindCodeEditor(ScriptEditorWindow view) + { + DispatcherUtil.DoEvents(); + var windowGrid = view.Content as Grid; + var codeEditor = windowGrid + .ChildrenOfType() + .First(); + return codeEditor; + } + private static ComboBox FindEditorDropDown(ScriptEditorWindow view) { DispatcherUtil.DoEvents(); @@ -303,5 +314,54 @@ public void WorkspaceWithMultiplePythonEnginesUpdatesCorrectlyViaContextHandler( Assert.AreEqual(new List { "2.7.9", "2.7.9" }, pynode2.CachedValue.GetElements().Select(x => x.Data)); DispatcherUtil.DoEvents(); } + + [Test] + public void TabWithSpacesMatchesEngine() + { + // Arrange + Open(@"core\python\python.dyn"); + + var nodeView = NodeViewWithGuid("3bcad14e-d086-4278-9e08-ed2759ef92f3"); + var nodeModel = nodeView.ViewModel.NodeModel as PythonNodeBase; + Assert.NotNull(nodeModel); + + var scriptWindow = EditPythonCode(nodeView, View); + var codeEditor = FindCodeEditor(scriptWindow); + var engineSelectorComboBox = FindEditorDropDown(scriptWindow); + + Assert.AreEqual(PythonEngineVersion.IronPython2, engineSelectorComboBox.SelectedItem); + + // Act + codeEditor.Focus(); + codeEditor.SelectionStart = 0; + var textArea = Keyboard.FocusedElement; + textArea.RaiseEvent(new KeyEventArgs( + Keyboard.PrimaryDevice, + PresentationSource.FromVisual(codeEditor), + 0, + Key.Tab) + { + RoutedEvent = Keyboard.KeyDownEvent + } + ); + DispatcherUtil.DoEvents(); + + engineSelectorComboBox.SelectedItem = PythonEngineVersion.CPython3; + + codeEditor.SelectionStart = 0; + textArea.RaiseEvent(new KeyEventArgs( + Keyboard.PrimaryDevice, + PresentationSource.FromVisual(codeEditor), + 0, + Key.Tab) + { + RoutedEvent = Keyboard.KeyDownEvent + } + ); + DispatcherUtil.DoEvents(); + + // Assert + StringAssert.StartsWith(" \t", codeEditor.Text); + } } }