From 8071fbf466776efa79e6fbfb0dad61fd5060120c Mon Sep 17 00:00:00 2001 From: FrozenAssassine Date: Thu, 26 Jan 2023 07:23:38 +0100 Subject: [PATCH 1/2] This fixes #367 --- TextControlBox/TextControlBox.xaml.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/TextControlBox/TextControlBox.xaml.cs b/TextControlBox/TextControlBox.xaml.cs index c32f634..4eaf59c 100644 --- a/TextControlBox/TextControlBox.xaml.cs +++ b/TextControlBox/TextControlBox.xaml.cs @@ -704,11 +704,21 @@ private async void Safe_Paste(bool HandleException = true) DataPackageView dataPackageView = Clipboard.GetContent(); if (dataPackageView.Contains(StandardDataFormats.Text)) { - string Text = await dataPackageView.GetTextAsync(); - if (await Utils.IsOverTextLimit(Text.Length)) + string text = null; + try + { + text = await dataPackageView.GetTextAsync(); + } + catch(Exception ex) //When longer holding Ctrl + V the clipboard may throw an exception: + { + Debug.WriteLine("Clipboard exception: " + ex.Message); + return; + } + + if (await Utils.IsOverTextLimit(text.Length)) return; - AddCharacter(stringManager.CleanUpString(Text)); + AddCharacter(stringManager.CleanUpString(text)); } } catch (OutOfMemoryException) From 52b2ceb21ec2e064bf45137a022709cf473617f8 Mon Sep 17 00:00:00 2001 From: FrozenAssassine Date: Mon, 30 Jan 2023 10:25:23 +0100 Subject: [PATCH 2/2] This fixes #367 --- .gitignore | 1 + TextControlBox-TestApp/MainPage.xaml.cs | 11 +- .../TextControlBox-TestApp.csproj | 6 + TextControlBox/Languages/Batch.json | 48 --- TextControlBox/Languages/CSharp.json | 80 ----- TextControlBox/Languages/ConfigFile.json | 39 --- TextControlBox/Languages/Cpp.json | 61 ---- TextControlBox/Languages/GCode.json | 62 ---- TextControlBox/Languages/Hex.json | 59 ---- TextControlBox/Languages/Html.json | 62 ---- TextControlBox/Languages/Java.json | 56 ---- TextControlBox/Languages/Javascript.json | 74 ----- TextControlBox/Languages/Json.json | 55 ---- TextControlBox/Languages/Languages.cs | 283 ++++++++++++++++++ TextControlBox/Languages/PHP.json | 82 ----- TextControlBox/Languages/QSharp.json | 32 -- TextControlBox/Languages/Xml.json | 56 ---- TextControlBox/TextControlBox.csproj | 30 +- TextControlBox/TextControlBox.xaml.cs | 52 ++-- 19 files changed, 317 insertions(+), 832 deletions(-) delete mode 100644 TextControlBox/Languages/Batch.json delete mode 100644 TextControlBox/Languages/CSharp.json delete mode 100644 TextControlBox/Languages/ConfigFile.json delete mode 100644 TextControlBox/Languages/Cpp.json delete mode 100644 TextControlBox/Languages/GCode.json delete mode 100644 TextControlBox/Languages/Hex.json delete mode 100644 TextControlBox/Languages/Html.json delete mode 100644 TextControlBox/Languages/Java.json delete mode 100644 TextControlBox/Languages/Javascript.json delete mode 100644 TextControlBox/Languages/Json.json create mode 100644 TextControlBox/Languages/Languages.cs delete mode 100644 TextControlBox/Languages/PHP.json delete mode 100644 TextControlBox/Languages/QSharp.json delete mode 100644 TextControlBox/Languages/Xml.json diff --git a/.gitignore b/.gitignore index ff61fe7..c028245 100644 --- a/.gitignore +++ b/.gitignore @@ -365,3 +365,4 @@ FodyWeavers.xsd /TextControlBox/TextControlBox.nuspec /TextControlBox/icon1.png /TextControlBox/.nuget +/Cloned diff --git a/TextControlBox-TestApp/MainPage.xaml.cs b/TextControlBox-TestApp/MainPage.xaml.cs index 1fc97ef..5a97f74 100644 --- a/TextControlBox-TestApp/MainPage.xaml.cs +++ b/TextControlBox-TestApp/MainPage.xaml.cs @@ -37,19 +37,16 @@ public MainPage() } private void Load() { - textbox.CodeLanguage = TextControlBox.TextControlBox.GetCodeLanguageFromId("CSharp"); + textbox.CodeLanguage = TextControlBox.TextControlBox.GetCodeLanguageFromId("Json"); + textbox.SyntaxHighlighting = true; textbox.LoadLines(GenerateContent()); } private IEnumerable GenerateContent() { - int Length = 0; - int Limit = 50; - for (int i = 1; i < Limit; i++) + foreach (var language in TextControlBox.TextControlBox.CodeLanguages) { - Length += ("Line LOL: " + i).Length; - yield return "Line LOL: " + i; + yield return language.Value.Name; } - Debug.WriteLine("Length: " + Length); } private async void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args) diff --git a/TextControlBox-TestApp/TextControlBox-TestApp.csproj b/TextControlBox-TestApp/TextControlBox-TestApp.csproj index 23da5a5..968fbad 100644 --- a/TextControlBox-TestApp/TextControlBox-TestApp.csproj +++ b/TextControlBox-TestApp/TextControlBox-TestApp.csproj @@ -18,6 +18,12 @@ {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} true false + False + True + True + Always + x64 + 0 true diff --git a/TextControlBox/Languages/Batch.json b/TextControlBox/Languages/Batch.json deleted file mode 100644 index 2a56a1f..0000000 --- a/TextControlBox/Languages/Batch.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "Highlights": [ - { - //-/+ floating numbers - "Pattern": "\\b([+-]?(?=\\.\\d|\\d)(?:\\d+)?(?:\\.?\\d*))(?:[eE]([+-]?\\d+))?\\b", - "ColorDark": "#ff00ff", - "ColorLight": "#dd00dd" - }, - { - //keywords - "Pattern": "(?i)(set|echo|for|pushd|popd|pause|exit|cd|if|else|goto|del)\\s", - "ColorDark": "#dd00dd", - "ColorLight": "#dd00dd" - }, - { - //class names - "Pattern": "(:.*)", - "ColorDark": "#ffff00", - "ColorLight": "#00C000" - }, - { - //variables - "Pattern": "(\\\".+?\\\"|\\'.+?\\')", - "ColorDark": "#ff0000", - "ColorLight": "#ff0000" - }, - { - "Pattern": "(@|%)", - "ColorDark": "#dd0077", - "ColorLight": "#dd0077" - }, - { - "Pattern": "(\\*)", - "ColorDark": "#dd0077", - "ColorLight": "#dd0077" - }, - { - //Comments - "Pattern": "((?i)rem.*)", - "ColorDark": "#888888", - "ColorLight": "#888888" - } - ], - "Name": "Batch", - "Filter": ".bat", - "Description": "Syntax highlighting for Batch language", - "Author": "Julius Kirsch" -} \ No newline at end of file diff --git a/TextControlBox/Languages/CSharp.json b/TextControlBox/Languages/CSharp.json deleted file mode 100644 index 6417c5d..0000000 --- a/TextControlBox/Languages/CSharp.json +++ /dev/null @@ -1,80 +0,0 @@ -{ - "Highlights": [ - { - //-/+ floating numbers - "Pattern": "\\b([+-]?(?=\\.\\d|\\d)(?:\\d+)?(?:\\.?\\d*))(?:[eE]([+-]?\\d+))?\\b", - "ColorDark": "#ff00ff", - "ColorLight": "#ff00ff" - }, - { - //Functions - "Pattern": "(?", - "ColorDark": "#0099ff", - "ColorLight": "#00AADD" - }, - { - //Opening tag 2 - "Pattern": "[<]+[a-zA-Z0-9:]+\\s", - "ColorDark": "#0099ff", - "ColorLight": "#969696" - }, - { - //Opening tag 2 - "Pattern": "<+[/]+[a-zA-Z0-9:]+>", - "ColorDark": "#0099ff", - "ColorLight": "#969696" - }, - { - //strings: "TEST" - "Pattern": "\"[^\\n]*?\"", - "ColorDark": "#00FF00", - "ColorLight": "#00CA00" - }, - { - //strings: 'TEST' - "Pattern": "'[^\\n]*?'", - "ColorDark": "#00FF00", - "ColorLight": "#00CA00" - }, - { - //Units - "Pattern": "[0-9]+(px|rem|em|vh|vw|px|pt|pc|in|mm|cm|deg|%)", - "ColorDark": "#dd00dd", - "ColorLight": "#ff00ff" - }, - { - //Comment multiline /**/ - "Pattern": "", - "ColorDark": "#888888", - "ColorLight": "#888888" - } - ], - "Name": "Html", - "Filter": ".html|.htm", - "Description": "Syntax highlighting for Html language", - "Author": "Julius Kirsch" -} \ No newline at end of file diff --git a/TextControlBox/Languages/Java.json b/TextControlBox/Languages/Java.json deleted file mode 100644 index e556035..0000000 --- a/TextControlBox/Languages/Java.json +++ /dev/null @@ -1,56 +0,0 @@ -{ - "Highlights": [ - { - //-/+ floating numbers - "Pattern": "\\b([+-]?(?=\\.\\d|\\d)(?:\\d+)?(?:\\.?\\d*))(?:[eE]([+-]?\\d+))?\\b", - "ColorDark": "#ff00ff", - "ColorLight": "#ff00ff" - }, - { - //Functions - "Pattern": "(?|\\<|\\?|&|\\||\\~|\\^)", - "ColorDark": "#77FF77", - "ColorLight": "#77FF77" - }, - { - "Description": "-/+ floating numbers", - "Pattern": "\\b([+-]?(?=\\.\\d|\\d)(?:\\d+)?(?:\\.?\\d*))(?:[eE]([+-]?\\d+))?\\b", - "ColorDark": "#ff00ff", - "ColorLight": "#ff00ff" - }, - { - "Description": "functions", - "Pattern": "(?", "#00AADD", "#0099ff"), + new SyntaxHighlights("[<]+[a-zA-Z0-9:]+\\s", "#969696", "#0099ff"), + new SyntaxHighlights("<+[/]+[a-zA-Z0-9:]+>", "#969696", "#0099ff"), + new SyntaxHighlights("\"[^\\n]*?\"", "#00CA00", "#00FF00"), + new SyntaxHighlights("'[^\\n]*?'", "#00CA00", "#00FF00"), + new SyntaxHighlights("[0-9]+(px|rem|em|vh|vw|px|pt|pc|in|mm|cm|deg|%)", "#ff00ff", "#dd00dd"), + new SyntaxHighlights("", "#888888", "#888888"), + }; + } + } + internal class Java : CodeLanguage + { + public Java() + { + this.Name = "Java"; + this.Author = "Julius Kirsch"; + this.Filter = new string[2] { ".java", ".class" }; + this.Description = "Syntax highlighting for Java language"; + this.Highlights = new SyntaxHighlights[] + { + new SyntaxHighlights("\\b([+-]?(?=\\.\\d|\\d)(?:\\d+)?(?:\\.?\\d*))(?:[eE]([+-]?\\d+))?\\b", "#ff00ff", "#ff00ff"), + new SyntaxHighlights("(?|\\<|\\?|&|\\||\\~|\\^)", "#77FF77", "#77FF77"), + new SyntaxHighlights("\\b([+-]?(?=\\.\\d|\\d)(?:\\d+)?(?:\\.?\\d*))(?:[eE]([+-]?\\d+))?\\b", "#ff00ff", "#ff00ff"), + new SyntaxHighlights("(?", "#FF0000", "#FF0000"), + new SyntaxHighlights("(?|\\<|\\?|&|\\||\\~|\\^)", "#77FF77", "#77FF77"), + new SyntaxHighlights("\\$\\w+", "#440044", "#FFBBFF"), + new SyntaxHighlights("\"[^\\n]*?\"", "#ff5500", "#00FF00"), + new SyntaxHighlights("\\'[^\\n]*?\\'", "#ff5500", "#00FF00"), + new SyntaxHighlights("\"/[^\\n]*/i{0,1}\"", "#ff5500", "#00FF00"), + new SyntaxHighlights("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/", "#888888", "#646464"), + new SyntaxHighlights("//(.*?)\\r?\\n", "#888888", "#646464"), + }; + } + } + internal class QSharp : CodeLanguage + { + public QSharp() + { + this.Name = "QSharp"; + this.Author = "Finn Freitag"; + this.Filter = new string[1] { ".qs" }; + this.Description = "Syntax highlighting for QSharp language"; + this.Highlights = new SyntaxHighlights[] + { + new SyntaxHighlights("\\W", "#BB0000", "#BB0000"), + new SyntaxHighlights("//(.*?)\\r?\\n", "#888888", "#646464"), + new SyntaxHighlights("\\b(namespace|open|operation|using|let|H|M|Reset|return)\\b", "#0066bb", "#00ffff"), + new SyntaxHighlights("\\b(Qubit|Result)\\b", "#00bb66", "#00ff00"), + }; + } + } + internal class XML : CodeLanguage + { + public XML() + { + this.Name = "XML"; + this.Author = "Julius Kirsch"; + this.Filter = new string[2] { ".xml", ".xaml" }; + this.Description = "Syntax highlighting for Xml language"; + this.Highlights = new SyntaxHighlights[] + { + new SyntaxHighlights("\\b([+-]?(?=\\.\\d|\\d)(?:\\d+)?(?:\\.?\\d*))(?:[eE]([+-]?\\d+))?\\b", "#dd00dd", "#ff00ff"), + new SyntaxHighlights("<([^ >!\\/]+)[^>]*>", "#969696", "#0099ff"), + new SyntaxHighlights("<+[/]+[a-zA-Z0-9:?\\-]+>", "#969696", "#0099ff"), + new SyntaxHighlights("<[a-zA-Z0-9:?\\-]+?.*\\/>", "#969696", "#0099ff"), + new SyntaxHighlights("[-A-Za-z_]+\\=", "#00CA00", "#Ff0000"), + new SyntaxHighlights("\"[^\\n]*?\"", "#00CA00", "#00FF00"), + new SyntaxHighlights("'[^\\n]*?'", "#00CA00", "#00FF00"), + new SyntaxHighlights("", "#888888", "#888888"), + }; + } + } +} diff --git a/TextControlBox/Languages/PHP.json b/TextControlBox/Languages/PHP.json deleted file mode 100644 index de0d693..0000000 --- a/TextControlBox/Languages/PHP.json +++ /dev/null @@ -1,82 +0,0 @@ -{ - "Highlights": [ - { - "Description": "php start", - "Pattern": "\\<\\?php", - "ColorDark": "#FF0000", - "ColorLight": "#FF0000" - }, - { - "Description": "php end", - "Pattern": "\\?\\>", - "ColorDark": "#FF0000", - "ColorLight": "#FF0000" - }, - { - "Description": "functions", - "Pattern": "(?|\\<|\\?|&|\\||\\~|\\^)", - "ColorDark": "#77FF77", - "ColorLight": "#77FF77" - }, - - { - "Description": "variables", - "Pattern": "\\$\\w+", - "ColorDark": "#FFBBFF", - "ColorLight": "#440044" - }, - { - "Description": "strings", - "Pattern": "\"[^\\n]*?\"", - "ColorDark": "#00FF00", - "ColorLight": "#ff5500" - }, - { - "Description": "strings", - "Pattern": "\\'[^\\n]*?\\'", - "ColorDark": "#00FF00", - "ColorLight": "#ff5500" - }, - { - "Description": "regular expressions", - "Pattern": "\"/[^\\n]*/i{0,1}\"", - "ColorDark": "#00FF00", - "ColorLight": "#ff5500" - }, - { - "Description": "multiline comments", - "Pattern": "/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/", - "ColorDark": "#646464", - "ColorLight": "#888888" - }, - { - "Description": "single line comment", - "Pattern": "//(.*?)\\r?\\n", - "ColorDark": "#646464", - "ColorLight": "#888888" - } - ], - "Name": "PHP", - "Filter": ".php", - "Description": "Syntaxhighlighting for PHP language.", - "Author": "Finn Freitag" -} \ No newline at end of file diff --git a/TextControlBox/Languages/QSharp.json b/TextControlBox/Languages/QSharp.json deleted file mode 100644 index a7627af..0000000 --- a/TextControlBox/Languages/QSharp.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "Highlights": [ - { - "Description": "punctuation", - "Pattern": "\\W", - "ColorDark": "#BB0000", - "ColorLight": "#BB0000" - }, - { - "Description": "comment", - "Pattern": "//(.*?)\\r?\\n", - "ColorDark": "#646464", - "ColorLight": "#888888" - }, - { - "Description": "keywords", - "Pattern": "\\b(namespace|open|operation|using|let|H|M|Reset|return)\\b", - "ColorDark": "#00ffff", - "ColorLight": "#0066bb" - }, - { - "Description": "classes", - "Pattern": "\\b(Qubit|Result)\\b", - "ColorDark": "#00ff00", - "ColorLight": "#00bb66" - } - ], - "Name": "QSharp", - "Filter": ".qs", - "Description": "Syntaxhighlighting for QSharp language.", - "Author": "Finn Freitag" -} \ No newline at end of file diff --git a/TextControlBox/Languages/Xml.json b/TextControlBox/Languages/Xml.json deleted file mode 100644 index 0dca509..0000000 --- a/TextControlBox/Languages/Xml.json +++ /dev/null @@ -1,56 +0,0 @@ -{ - "Highlights": [ - { - //-/+ floating numbers - "Pattern": "\\b([+-]?(?=\\.\\d|\\d)(?:\\d+)?(?:\\.?\\d*))(?:[eE]([+-]?\\d+))?\\b", - "ColorDark": "#ff00ff", - "ColorLight": "#dd00dd" - }, - { - //Opening tag - "Pattern": "<([^ >!\/]+)[^>]*>", - "ColorDark": "#0099ff", - "ColorLight": "#969696" - }, - { - //Closing tag - "Pattern": "<+[/]+[a-zA-Z0-9:?\\-]+>", - "ColorDark": "#0099ff", - "ColorLight": "#969696" - }, - { - //Open-closing tag (oneline) - "Pattern": "<[a-zA-Z0-9:?\\-]+?.*\\/>", - "ColorDark": "#0099ff", - "ColorLight": "#969696" - }, - { - //Keywords - "Pattern": "[-A-Za-z_]+\\=", - "ColorDark": "#Ff0000", - "ColorLight": "#00CA00" - }, - { - //strings: "TEST" - "Pattern": "\"[^\\n]*?\"", - "ColorDark": "#00FF00", - "ColorLight": "#00CA00" - }, - { - //strings: 'TEST' - "Pattern": "'[^\\n]*?'", - "ColorDark": "#00FF00", - "ColorLight": "#00CA00" - }, - { - //Comment multiline /**/ - "Pattern": "", - "ColorDark": "#888888", - "ColorLight": "#888888" - } - ], - "Name": "Xml", - "Filter": ".xml|.xaml", - "Description": "Syntax highlighting for Xml language", - "Author": "Julius Kirsch" -} \ No newline at end of file diff --git a/TextControlBox/TextControlBox.csproj b/TextControlBox/TextControlBox.csproj index 2eabd51..5ef7311 100644 --- a/TextControlBox/TextControlBox.csproj +++ b/TextControlBox/TextControlBox.csproj @@ -132,6 +132,7 @@ + @@ -178,35 +179,6 @@ Designer - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 14.0 diff --git a/TextControlBox/TextControlBox.xaml.cs b/TextControlBox/TextControlBox.xaml.cs index 4eaf59c..928ea1b 100644 --- a/TextControlBox/TextControlBox.xaml.cs +++ b/TextControlBox/TextControlBox.xaml.cs @@ -15,14 +15,15 @@ using System.Threading.Tasks; using TextControlBox.Extensions; using TextControlBox.Helper; +using TextControlBox.Languages; using TextControlBox.Renderer; using TextControlBox.Text; using Windows.ApplicationModel.DataTransfer; using Windows.Foundation; -using Windows.Networking.Vpn; using Windows.Storage; using Windows.System; using Windows.UI.Core; +using Windows.UI.Popups; using Windows.UI.Text.Core; using Windows.UI.ViewManagement; using Windows.UI.Xaml; @@ -30,8 +31,6 @@ using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Shapes; -using static System.Net.Mime.MediaTypeNames; using Color = Windows.UI.Color; namespace TextControlBox @@ -2610,43 +2609,36 @@ public ScrollBarPosition ScrollBarPosition #region Static functions //static functions - private static void GetLanguagesFromBuffer() - { - _CodeLanguages = new Dictionary(); - - var files = Directory.GetFiles("TextControlBox/Languages"); - for (int i = 0; i < files.Length; i++) - { - var result = SyntaxHighlightingRenderer.GetCodeLanguageFromJson(File.ReadAllText(files[i])); - if(result.Succeed) - _CodeLanguages.Add(result.CodeLanguage.Name.ToLower(), result.CodeLanguage); - } - } - private static Dictionary _CodeLanguages = null; - /// - /// Get all the builtin codelanguages for syntaxhighlighting + /// Get all the builtin code languages for syntaxhighlighting /// - public static Dictionary CodeLanguages + public static Dictionary CodeLanguages => new Dictionary(StringComparer.OrdinalIgnoreCase) { - get - { - if (_CodeLanguages == null) - GetLanguagesFromBuffer(); - - return _CodeLanguages; - } - } + { "Batch", new Batch() }, + { "ConfigFile", new ConfigFile() }, + { "C++", new Cpp() }, + { "C#", new CSharp() }, + { "GCode", new GCode() }, + { "HexFile", new HexFile() }, + { "Html", new Html() }, + { "Java", new Java() }, + { "Javascript", new Javascript() }, + { "Json", new Json() }, + { "PHP", new PHP() }, + { "QSharp", new QSharp() }, + { "XML", new XML() }, + }; /// /// Get a CodeLanguage by the identifier from the list /// /// - /// + /// When found the Codelanguage. Otherwise null public static CodeLanguage GetCodeLanguageFromId(string Identifier) { - CodeLanguages.TryGetValue(Identifier.ToLower(), out CodeLanguage codelanguage); - return codelanguage; + if(CodeLanguages.TryGetValue(Identifier, out CodeLanguage codelanguage)) + return codelanguage; + return null; } ///