add: syntax highlight & save file popup in pasm editor

This commit is contained in:
2024-08-25 18:37:19 +02:00
parent ac43486a82
commit 0f6963ee54
2 changed files with 96 additions and 1 deletions

View File

@@ -29,7 +29,7 @@ namespace Server
public static int localport; public static int localport;
public static void ServerStart() 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; localport = 4444;
server = new TcpListener(System.Net.IPAddress.Parse(localip), localport); server = new TcpListener(System.Net.IPAddress.Parse(localip), localport);
server.Start(); server.Start();

View File

@@ -14,6 +14,7 @@ using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Xml; using System.Xml;
using static System.Net.Mime.MediaTypeNames;
namespace Server namespace Server
{ {
@@ -21,6 +22,13 @@ namespace Server
{ {
private static string filepath = ""; private static string filepath = "";
private static bool is_executing = false; private static bool is_executing = false;
List<string> instructions = new List<string> {
"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) public PasmEditor(bool is_from_right_click)
{ {
InitializeComponent(); InitializeComponent();
@@ -28,8 +36,26 @@ namespace Server
richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop); 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) private void newCTRLNToolStripMenuItem_Click(object sender, EventArgs e)
{ {
if (SpawnSaveDialog())
return;
richTextBox1.Text = ""; richTextBox1.Text = "";
filepath = ""; filepath = "";
this.Text = "pasm editor - New File"; this.Text = "pasm editor - New File";
@@ -37,6 +63,9 @@ namespace Server
private void openToolStripMenuItem_Click(object sender, EventArgs e) private void openToolStripMenuItem_Click(object sender, EventArgs e)
{ {
if (SpawnSaveDialog())
return;
OpenFileDialog openFileDialog = new OpenFileDialog OpenFileDialog openFileDialog = new OpenFileDialog
{ {
Filter = "PASM source files (*.pasm)|*.pasm", Filter = "PASM source files (*.pasm)|*.pasm",
@@ -85,6 +114,9 @@ namespace Server
private void closeCTRLToolStripMenuItem_Click(object sender, EventArgs e) private void closeCTRLToolStripMenuItem_Click(object sender, EventArgs e)
{ {
if (SpawnSaveDialog())
return;
richTextBox1.Text = ""; richTextBox1.Text = "";
filepath = ""; filepath = "";
this.Text = "pasm editor - New File"; this.Text = "pasm editor - New File";
@@ -184,6 +216,69 @@ namespace Server
{ {
this.Text += "*"; 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();
} }
} }
} }