Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Node Editor Visual Update - part #3 #13783

Merged
merged 9 commits into from
Mar 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@
<End>"</End>
</Span>
<!--<MarkPrevious color="#417693">(</MarkPrevious>-->
<KeyWords name="BuiltInStatements" color="#8C5EAA">
<KeyWords name="BuiltInStatements" color="#F2A9F2">
<Key word="local" />
<Key word="static" />
<Key word="this" />
</KeyWords>
<KeyWords name="ClassStatement" color="#8C5EAA">
<KeyWords name="ClassStatement" color="#F2A9F2">
<Key word="class" />
<Key word="extends" />
</KeyWords>
<KeyWords name="ExceptionHandlingStatements" color="#8C5EAA">
<KeyWords name="ExceptionHandlingStatements" color="#F2A9F2">
<Key word="throw" />
<Key word="catch" />
<Key word="try" />
Expand All @@ -58,7 +58,7 @@
<Key word="function" />
<Key word="constructor" />
</KeyWords>
<KeyWords name="Imports" color="#8C5EAA">
<KeyWords name="Imports" color="#F2A9F2">
<Key word="import" />
<Key word="from" />
</KeyWords>
Expand All @@ -72,7 +72,7 @@
<Key word="continue" />
<Key word="return" />
</KeyWords>
<KeyWords name="AccessSpecifiers" color="#8C5EAA">
<KeyWords name="AccessSpecifiers" color="#F2A9F2">
<Key word="public" />
<Key word="private" />
<Key word="protected" />
Expand All @@ -96,7 +96,7 @@
<Key word="Imperative" />
<Key word="Options" />
</KeyWords>
<KeyWords name="SelectionStatements" color="#8C5EAA">
<KeyWords name="SelectionStatements" color="#F2A9F2">
<Key word="elseif" />
<Key word="else" />
<Key word="if" />
Expand Down
20 changes: 16 additions & 4 deletions src/DynamoCoreWpf/Views/CodeBlocks/CodeBlockEditorUtils.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Media;
using System.Xml;
using Dynamo.Configuration;
using Dynamo.Engine;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using ICSharpCode.AvalonEdit.Rendering;
Expand Down Expand Up @@ -132,19 +133,30 @@ public static void CreateHighlightingRules(ICSharpCode.AvalonEdit.TextEditor edi
var stream = typeof(CodeHighlightingRuleFactory).Assembly.GetManifestResourceStream(
"Dynamo.Wpf.UI.Resources." + Configurations.HighlightingFile);

// Hyperlink color
editor.TextArea.TextView.LinkTextForegroundBrush = new SolidColorBrush(Color.FromArgb(255, 106, 192, 231));

editor.SyntaxHighlighting = HighlightingLoader.Load(
new XmlTextReader(stream), HighlightingManager.Instance);

AddCommonHighlighingRules(editor, controller);
}

// Assigning custom highlighting rules moved to a separate method
// So we can reuse for both design script and python script
// Allows each individual eidtor to have their own set of rules, and aligns the common ones
internal static void AddCommonHighlighingRules(TextEditor editor, EngineController controller)
{
// Highlighting Digits
var rules = editor.SyntaxHighlighting.MainRuleSet.Rules;

rules.Add(CodeHighlightingRuleFactory.CreateNumberHighlightingRule());

var classRule = CodeHighlightingRuleFactory.CreateClassHighlightRule(controller);
if(classRule != null) rules.Add(classRule);
if (classRule != null) rules.Add(classRule);

var methodRule = CodeHighlightingRuleFactory.CreateMethodHighlightRule(controller);
if(methodRule != null) rules.Add(methodRule);
if (methodRule != null) rules.Add(methodRule);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
Expand Down Expand Up @@ -418,4 +418,4 @@ internal void Dispose()
this.dynamoViewModel.PreferencesViewModel.PropertyChanged -= OnPreferencesViewModelPropertyChanged;
}
}
}
dnenov marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,79 +18,83 @@
<Properties>
<Property name="LineComment" value="#" />
</Properties>
<Digits name="Digits" color="#AE81FF" />
<Digits name="Digits" color="#6AC0E7" />
<RuleSets>
<RuleSet ignorecase="false">
<Delimiters>()[]{}@,:.`=;+-*/% &amp;|^&gt;&lt;</Delimiters>
<Span name="Char" stopateol="true" color="#E6DB74" escapecharacter="\">
<Span name="Char" stopateol="true" color="#E0A86F" escapecharacter="\">
<Begin>'</Begin>
<End>'</End>
</Span>
<Span name="DocComment" color="#a8a8a8">
<Span name="DocComment" color="#B7D78C">
<Begin>"""</Begin>
<End>"""</End>
</Span>
<Span name="LineComment" stopateol="true" color="#a8a8a8">
<Span name="LineComment" stopateol="true" color="#B7D78C">
<Begin>#</Begin>
</Span>
<Span name="String" stopateol="true" color="#E6DB74" escapecharacter="\">
<Span name="String" stopateol="true" color="#E0A86F" escapecharacter="\">
<Begin>"</Begin>
<End>"</End>
</Span>
<MarkPrevious bold="true" color="White">(</MarkPrevious>
<KeyWords name="BuiltInStatements" bold="false" color="#B7D78C">
</Span>
<MarkPrevious bold="false" color="#84d7ce">(</MarkPrevious>
<KeyWords name="BuiltInStatements" bold="false" color="#F2A9F2">
<Key word="assert" />
<Key word="del" />
<Key word="exec" />
<Key word="global" />
<Key word="lambda" />
<Key word="print" />
</KeyWords>
<KeyWords name="ClassStatement" color="#E0A86F" bold="false">
<KeyWords name="ClassStatement" color="#6AC0E7" bold="false">
<Key word="class" />
</KeyWords>
<KeyWords name="ExceptionHandlingStatements" bold="false" color="#BC8CDF">
<KeyWords name="ExceptionHandlingStatements" bold="false" color="#F2A9F2">
<Key word="except" />
<Key word="finally" />
<Key word="raise" />
<Key word="try" />
</KeyWords>
<KeyWords name="FunctionDefinition" italic="true" color="#66D9EF">
<KeyWords name="FunctionDefinition" italic="false" color="#6AC0E7">
<Key word="def" />
</KeyWords>
<KeyWords name="Imports" bold="false" color="#F5C1D1">
<Key word="import" />
<Key word="from" />
</KeyWords>
<KeyWords name="IterationStatements" bold="false" color="#BC8CDF">
<KeyWords name="IterationStatements" bold="false" color="#F2A9F2">
<Key word="for" />
<Key word="in" />
<Key word="while" />
</KeyWords>
<KeyWords name="JumpStatements" color="#BC8CDF">
<KeyWords name="JumpStatements" color="#F2A9F2">
<Key word="break" />
<Key word="continue" />
<Key word="yield" />
<Key word="return" />
</KeyWords>
<KeyWords name="OperatorStatements" bold="true" color="#BC8CDF">
<KeyWords name="OperatorStatements" bold="true" color="#F2A9F2">
<Key word="and" />
<Key word="as" />
<Key word="is" />
<Key word="not" />
<Key word="or" />
</KeyWords>
<KeyWords name="PassStatement" color="#BC8CDF">
<KeyWords name="PassStatement" color="#F2A9F2">
<Key word="pass" />
</KeyWords>
<KeyWords name="SelectionStatements" bold="true" color="#BC8CDF">
<KeyWords name="SelectionStatements" bold="false" color="#F2A9F2">
<Key word="elif" />
<Key word="else" />
<Key word="if" />
</KeyWords>
<KeyWords name="WithStatement" color="#BC8CDF">
<KeyWords name="WithStatement" color="#F2A9F2">
<Key word="with" />
</KeyWords>
<KeyWords name="Constants" color="#F9F9A5">
<Key word="True" />
<Key word="False" />
</KeyWords>
</RuleSet>
</RuleSets>
</SyntaxDefinition>
9 changes: 9 additions & 0 deletions src/Libraries/PythonNodeModelsWpf/ScriptEditorWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using System.Text.RegularExpressions;
using System.Windows.Media;
using Dynamo.PythonServices;
using Dynamo.Wpf.Views;
using System.Windows.Media;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Editing;
Expand Down Expand Up @@ -129,6 +131,9 @@ internal void Initialize(Guid workspaceGuid, Guid nodeGuid, string propName, str
// Initialize editor with global settings for show/hide tabs and spaces
editText.Options = dynamoViewModel.PythonScriptEditorTextOptions.GetTextOptions();

// Hyperlink color
editText.TextArea.TextView.LinkTextForegroundBrush = new SolidColorBrush(Color.FromArgb(255, 106, 192, 231));

// Set options to reflect global settings when python script editor in initialized for the first time.
editText.Options.ShowSpaces = dynamoViewModel.ShowTabsAndSpacesInScriptEditor;
editText.Options.ShowTabs = dynamoViewModel.ShowTabsAndSpacesInScriptEditor;
Expand All @@ -144,6 +149,9 @@ internal void Initialize(Guid workspaceGuid, Guid nodeGuid, string propName, str
editText.SyntaxHighlighting = HighlightingLoader.Load(
new XmlTextReader(elem), HighlightingManager.Instance);

// Add custom highlighting rules consistent with DesignScript
CodeHighlightingRuleFactory.AddCommonHighlighingRules(editText, dynamoViewModel.EngineController);

AvailableEngines =
new ObservableCollection<string>(PythonEngineManager.Instance.AvailableEngines.Select(x => x.Name));
// Add the serialized Python Engine even if it is missing (so that the user does not see an empty slot)
Expand Down Expand Up @@ -402,6 +410,7 @@ private void UpdateScript(string scriptText)
dynamoViewModel.ExecuteCommand(command);
this.Focus();
nodeWasModified = true;
IsSaved = true;
NodeModel.OnNodeModified();
}

Expand Down