From 0f6963ee540000b4e1caa108626b9b420a28b9d0 Mon Sep 17 00:00:00 2001 From: ALittlePatate Date: Sun, 25 Aug 2024 18:37:19 +0200 Subject: [PATCH] add: syntax highlight & save file popup in pasm editor --- Server/Menu.cs | 2 +- Server/PasmEditor.cs | 95 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/Server/Menu.cs b/Server/Menu.cs index ff74747..7b31e77 100644 --- a/Server/Menu.cs +++ b/Server/Menu.cs @@ -29,7 +29,7 @@ namespace Server public static int localport; public static void ServerStart() { - localip = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString(); + localip = "192.168.1.20";//Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString(); localport = 4444; server = new TcpListener(System.Net.IPAddress.Parse(localip), localport); server.Start(); diff --git a/Server/PasmEditor.cs b/Server/PasmEditor.cs index 1e6348f..a81c285 100644 --- a/Server/PasmEditor.cs +++ b/Server/PasmEditor.cs @@ -14,6 +14,7 @@ using static System.Windows.Forms.VisualStyles.VisualStyleElement; using System.Runtime.InteropServices; using System.Text.RegularExpressions; using System.Xml; +using static System.Net.Mime.MediaTypeNames; namespace Server { @@ -21,6 +22,13 @@ namespace Server { private static string filepath = ""; private static bool is_executing = false; + List instructions = new List { + "add", "sub", "mul", "div", "mov", + "cmp", "je", "jne", "jb", "jnb", + "ja", "jna", "jmp", "ret", "pop", + "push", "call", "sqrt", "neg", + "and", "xor", "end" + }; public PasmEditor(bool is_from_right_click) { InitializeComponent(); @@ -28,8 +36,26 @@ namespace Server richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop); } + private bool SpawnSaveDialog() + { + if (this.Text.Contains("*") || (filepath != "" && this.Text.Contains("*"))) + { + DialogResult messageResult = MessageBox.Show("Save this file?", "Save", MessageBoxButtons.YesNoCancel); + if (messageResult == DialogResult.Yes) + { + saveToolStripMenuItem_Click(null, null); + } + else if (messageResult == DialogResult.Cancel) + { + return true; + } + } + return false; + } private void newCTRLNToolStripMenuItem_Click(object sender, EventArgs e) { + if (SpawnSaveDialog()) + return; richTextBox1.Text = ""; filepath = ""; this.Text = "pasm editor - New File"; @@ -37,6 +63,9 @@ namespace Server private void openToolStripMenuItem_Click(object sender, EventArgs e) { + if (SpawnSaveDialog()) + return; + OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "PASM source files (*.pasm)|*.pasm", @@ -85,6 +114,9 @@ namespace Server private void closeCTRLToolStripMenuItem_Click(object sender, EventArgs e) { + if (SpawnSaveDialog()) + return; + richTextBox1.Text = ""; filepath = ""; this.Text = "pasm editor - New File"; @@ -184,6 +216,69 @@ namespace Server { this.Text += "*"; } + + // took from https://codingvision.net/c-simple-syntax-highlighting + Regex semicolonRegex = new Regex(@";.*"); + Regex numberRegex = new Regex(@"\b\d+\b"); + Regex labelsRegex = new Regex(@"(?:\n|\A)\w+:"); + + // saving the original caret position + forecolor + int originalIndex = richTextBox1.SelectionStart; + int originalLength = richTextBox1.SelectionLength; + Color originalColor = Color.Black; + + // MANDATORY - focuses a label before highlighting (avoids blinking) + splitter1.Focus(); + + // removes any previous highlighting (so modified words won't remain highlighted) + richTextBox1.SelectionStart = 0; + richTextBox1.SelectionLength = richTextBox1.Text.Length; + richTextBox1.SelectionColor = originalColor; + + // scanning... + foreach(Match match in semicolonRegex.Matches(richTextBox1.Text)) + { + richTextBox1.SelectionStart = match.Index; + richTextBox1.SelectionLength = match.Length; + richTextBox1.SelectionColor = Color.Green; + } + + foreach (Match match in numberRegex.Matches(richTextBox1.Text)) + { + richTextBox1.SelectionStart = match.Index; + richTextBox1.SelectionLength = match.Length; + if (richTextBox1.SelectionColor == Color.Black) + richTextBox1.SelectionColor = Color.Blue; + } + + foreach (Match match in labelsRegex.Matches(richTextBox1.Text)) + { + richTextBox1.SelectionStart = match.Index; + richTextBox1.SelectionLength = match.Length; + if (richTextBox1.SelectionColor == Color.Black) + richTextBox1.SelectionColor = Color.DarkOrange; + } + + foreach (string ins in instructions) + { + string pattern = @"\b" + Regex.Escape(ins) + @"\b"; + Regex regex = new Regex(pattern); + + foreach (Match match in regex.Matches(richTextBox1.Text)) + { + richTextBox1.SelectionStart = match.Index; + richTextBox1.SelectionLength = match.Length; + if (richTextBox1.SelectionColor == Color.Black) + richTextBox1.SelectionColor = Color.DarkRed; + } + } + + // restoring the original colors, for further writing + richTextBox1.SelectionStart = originalIndex; + richTextBox1.SelectionLength = originalLength; + richTextBox1.SelectionColor = originalColor; + + richTextBox1.Focus(); } } }