- Add tasks

- Add check if file exist
- Improved code structure
This commit is contained in:
Stone-Red-Code
2021-10-09 20:23:28 +02:00
parent c40d1f45e0
commit 68a9bde5d9
12 changed files with 520 additions and 246 deletions
+44 -17
View File
@@ -32,6 +32,7 @@ namespace YesNt.CodeEditor
yesNtInterpreter.OnDebugOutput += YesNtInterpreter_OnDebugOutput;
yesNtInterpreter.OnLineExecuted += YesNtInterpreter_OnLineExecuted;
syntaxHighlighter = new(yesNtInterpreter.StatementInformation);
Console.CancelKeyPress += Console_CancelKeyPress;
}
public void Run()
@@ -230,19 +231,32 @@ namespace YesNt.CodeEditor
case "run":
if (Save(input, true))
{
mode = Mode.Debug;
Console.Clear();
yesNtInterpreter.Execute(currentPath);
while (Console.KeyAvailable)
{
Console.ReadKey(true);
}
Console.ReadKey();
WriteStatus(string.Empty);
mode = Mode.Command;
}
break;
case "debug":
if (Save(input, true))
{
mode = Mode.Debug;
Console.Clear();
yesNtInterpreter.Execute(currentPath, true);
while (Console.KeyAvailable)
{
Console.ReadKey(true);
}
Console.ReadKey();
WriteStatus(string.Empty);
mode = Mode.Command;
}
break;
@@ -257,12 +271,6 @@ namespace YesNt.CodeEditor
break;
}
if (!File.Exists(path))
{
WriteStatus("File does not exist!");
break;
}
try
{
Load(path);
@@ -277,15 +285,13 @@ namespace YesNt.CodeEditor
break;
case "new":
if (Save(input, false))
{
lineOffset = 0;
cursorPosition.X = 0;
cursorPosition.Y = 0;
currentPath = string.Empty;
lines.Clear();
WriteStatus(string.Empty);
}
lineOffset = 0;
cursorPosition.X = 0;
cursorPosition.Y = 0;
currentPath = string.Empty;
lines.Clear();
WriteStatus(string.Empty);
break;
case "exit":
@@ -302,6 +308,11 @@ namespace YesNt.CodeEditor
private bool Load(string path)
{
if (string.IsNullOrEmpty(Path.GetExtension(path)))
{
path = Path.ChangeExtension(path, "ynt");
}
if (!File.Exists(path))
{
WriteStatus("File does not exist!");
@@ -321,7 +332,6 @@ namespace YesNt.CodeEditor
private bool Save(string input, bool loadIfExists)
{
string text = "";
string path = "";
foreach (string line in lines)
{
@@ -329,6 +339,7 @@ namespace YesNt.CodeEditor
}
text = text.TrimEnd('\n');
string path;
if (input.Split(' ').Length == 2)
{
path = input.Split(' ')[1];
@@ -361,6 +372,11 @@ namespace YesNt.CodeEditor
return false;
}
if (string.IsNullOrEmpty(Path.GetExtension(path)))
{
path = Path.ChangeExtension(path, "ynt");
}
try
{
File.WriteAllText(path, text);
@@ -404,6 +420,16 @@ namespace YesNt.CodeEditor
}
debugOutput.Clear();
}
private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
if (mode == Mode.Debug)
{
yesNtInterpreter.Stop();
mode = Mode.Command;
}
}
}
internal class Point
@@ -421,6 +447,7 @@ namespace YesNt.CodeEditor
internal enum Mode
{
Edit,
Command
Command,
Debug
}
}