From 1ced6f393c9d8c93ae5570d9592fb4a139db212f Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Thu, 17 Nov 2022 11:40:51 +0100 Subject: [PATCH] Improve code formatting --- YesNt.CodeEditor/Editor.cs | 11 +---- YesNt.CodeEditor/InputHandler.cs | 25 +++++------- YesNt.CodeEditor/Program.cs | 12 +----- YesNt.Interpreter.Tests/CodeFowTests.cs | 2 +- YesNt.Interpreter.Tests/YesNtAssert.cs | 4 +- .../Runtime/RuntimeInformation.cs | 40 ++++--------------- .../Statements/SystemStatements.cs | 2 +- .../Statements/VariableStatements.cs | 4 +- YesNt.Interpreter/Utilities/Evaluator.cs | 28 ++----------- YesNt.Interpreter/Utilities/FixedProcess.cs | 18 ++++----- .../Utilities/StringExtentions.cs | 2 +- 11 files changed, 39 insertions(+), 109 deletions(-) diff --git a/YesNt.CodeEditor/Editor.cs b/YesNt.CodeEditor/Editor.cs index dee06c7..54c92cb 100644 --- a/YesNt.CodeEditor/Editor.cs +++ b/YesNt.CodeEditor/Editor.cs @@ -23,7 +23,7 @@ namespace YesNt.CodeEditor { if (File.Exists(path)) { - Load(path); + _ = Load(path); } } @@ -131,14 +131,7 @@ namespace YesNt.CodeEditor if (CurrentPath.Trim() != path.Trim() && loadIfExists) { - if (Load(path)) - { - return true; - } - else - { - return false; - } + return Load(path); } if (string.IsNullOrEmpty(Path.GetExtension(path))) { diff --git a/YesNt.CodeEditor/InputHandler.cs b/YesNt.CodeEditor/InputHandler.cs index 59a3024..3b31f46 100644 --- a/YesNt.CodeEditor/InputHandler.cs +++ b/YesNt.CodeEditor/InputHandler.cs @@ -16,7 +16,7 @@ namespace YesNt.CodeEditor { while (Console.KeyAvailable) { - Console.ReadKey(true); + _ = Console.ReadKey(true); } if (textEditor.EditMode == Mode.Edit) { @@ -50,14 +50,7 @@ namespace YesNt.CodeEditor return true; case ConsoleKey.E: - if (textEditor.Lines.Count > textEditor.CursorPosition.Y) - { - textEditor.CursorPosition.X = textEditor.Lines[textEditor.CursorPosition.Y].TrimEnd().Length; - } - else - { - textEditor.CursorPosition.X = 0; - } + textEditor.CursorPosition.X = textEditor.Lines.Count > textEditor.CursorPosition.Y ? textEditor.Lines[textEditor.CursorPosition.Y].TrimEnd().Length : 0; return true; } } @@ -128,7 +121,7 @@ namespace YesNt.CodeEditor StringBuilder lineBuilder = new StringBuilder(textEditor.Lines[textEditor.CursorPosition.Y]); while (lineBuilder.Length <= textEditor.CursorPosition.X) { - lineBuilder.Append(' '); + _ = lineBuilder.Append(' '); } textEditor.Lines[textEditor.CursorPosition.Y] = lineBuilder.ToString(); @@ -228,7 +221,7 @@ namespace YesNt.CodeEditor break; case "save": - textEditor.Save(input, false); + _ = textEditor.Save(input, false); break; case "run": @@ -240,9 +233,9 @@ namespace YesNt.CodeEditor textEditor.YesNtInterpreter.Execute(textEditor.CurrentPath); while (Console.KeyAvailable) { - Console.ReadKey(true); + _ = Console.ReadKey(true); } - Console.ReadKey(); + _ = Console.ReadKey(); WriteStatus(string.Empty); textEditor.EditMode = Mode.Command; } @@ -257,9 +250,9 @@ namespace YesNt.CodeEditor textEditor.YesNtInterpreter.Execute(textEditor.CurrentPath, true); while (Console.KeyAvailable) { - Console.ReadKey(true); + _ = Console.ReadKey(true); } - Console.ReadKey(); + _ = Console.ReadKey(); WriteStatus(string.Empty); textEditor.EditMode = Mode.Command; } @@ -278,7 +271,7 @@ namespace YesNt.CodeEditor try { - textEditor.Load(path); + _ = textEditor.Load(path); } catch (Exception ex) { diff --git a/YesNt.CodeEditor/Program.cs b/YesNt.CodeEditor/Program.cs index d95b4a7..ffa8ec7 100644 --- a/YesNt.CodeEditor/Program.cs +++ b/YesNt.CodeEditor/Program.cs @@ -4,17 +4,7 @@ { private static void Main(string[] args) { - TextEditor textEditor; - - if (args.Length > 0) - { - textEditor = new TextEditor(args[0]); - } - else - { - textEditor = new TextEditor(); - } - + TextEditor textEditor = args.Length > 0 ? new TextEditor(args[0]) : new TextEditor(); textEditor.Run(); } } diff --git a/YesNt.Interpreter.Tests/CodeFowTests.cs b/YesNt.Interpreter.Tests/CodeFowTests.cs index 105b253..23a715a 100644 --- a/YesNt.Interpreter.Tests/CodeFowTests.cs +++ b/YesNt.Interpreter.Tests/CodeFowTests.cs @@ -39,6 +39,6 @@ public class CodeFlowTests public void CalculationsTest() { Assert.Inconclusive(); - YesNtAssert.IsLineEqual("10 * 10 !calc", (20).ToString()); + YesNtAssert.IsLineEqual("10 * 10 !calc", 20.ToString()); } } \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/YesNtAssert.cs b/YesNt.Interpreter.Tests/YesNtAssert.cs index e7ae5f7..b7200a6 100644 --- a/YesNt.Interpreter.Tests/YesNtAssert.cs +++ b/YesNt.Interpreter.Tests/YesNtAssert.cs @@ -28,7 +28,7 @@ internal static class YesNtAssert if (er is null) { - onDone.Set(); + _ = onDone.Set(); } }; @@ -51,7 +51,7 @@ internal static class YesNtAssert yesNtInterpreter.OnLineExecuted += (er) => { debugEventArgs = er ?? debugEventArgs; - onDone.Set(); + _ = onDone.Set(); }; yesNtInterpreter.Execute(lines, true); diff --git a/YesNt.Interpreter/Runtime/RuntimeInformation.cs b/YesNt.Interpreter/Runtime/RuntimeInformation.cs index 9b5efc1..8113c6b 100644 --- a/YesNt.Interpreter/Runtime/RuntimeInformation.cs +++ b/YesNt.Interpreter/Runtime/RuntimeInformation.cs @@ -28,7 +28,7 @@ namespace YesNt.Interpreter.Runtime public bool Stop { get; private set; } = false; public bool StopAllTasks { get; private set; } = false; public bool IsDebugMode { get; set; } = false; - public string CurrentFilePath { get; set; } = string.Empty; + public string WorkingDirectory { get; set; } = string.Empty; public bool IsTask => ParentRuntimeInformation is not null; public int TaskId => IsTask ? taskId : 0; public bool InternalIsInFunction { get; set; } @@ -39,35 +39,9 @@ namespace YesNt.Interpreter.Runtime set => InternalIsInFunction = value; } - public Dictionary Variables - { - get - { - if (FunctionCallStack.Count == 0) - { - return topVariables; - } - else - { - return FunctionCallStack.Peek().Variables; - } - } - } + public Dictionary Variables => FunctionCallStack.Count == 0 ? topVariables : FunctionCallStack.Peek().Variables; - public Dictionary Labels - { - get - { - if (FunctionCallStack.Count == 0) - { - return topLabels; - } - else - { - return FunctionCallStack.Peek().Labels; - } - } - } + public Dictionary Labels => FunctionCallStack.Count == 0 ? topLabels : FunctionCallStack.Peek().Labels; public RuntimeInformation ParentRuntimeInformation { @@ -82,7 +56,7 @@ namespace YesNt.Interpreter.Runtime } } - public bool IsSearching => !string.IsNullOrWhiteSpace(SearchLabel + SearchFunction) || IsInFunction && FunctionCallStack.Count == 0; + public bool IsSearching => !string.IsNullOrWhiteSpace(SearchLabel + SearchFunction) || (IsInFunction && FunctionCallStack.Count == 0); public bool IsLocalSearch { get; set; } private event Action OnExit; @@ -98,7 +72,7 @@ namespace YesNt.Interpreter.Runtime public void WriteLine(string output, bool forceWrite = false) { - if (Stop && !forceWrite || parentRuntimeInformation?.StopAllTasks == true && !forceWrite) + if ((Stop && !forceWrite) || (parentRuntimeInformation?.StopAllTasks == true && !forceWrite)) { return; } @@ -122,7 +96,7 @@ namespace YesNt.Interpreter.Runtime public void Write(string output, bool forceWrite = false) { - if (Stop && !forceWrite || parentRuntimeInformation?.StopAllTasks == true && !forceWrite) + if ((Stop && !forceWrite) || (parentRuntimeInformation?.StopAllTasks == true && !forceWrite)) { return; } @@ -190,7 +164,7 @@ namespace YesNt.Interpreter.Runtime ParentRuntimeInformation = null; SearchLabel = string.Empty; SearchFunction = string.Empty; - CurrentFilePath = string.Empty; + WorkingDirectory = string.Empty; CurrentLine = string.Empty; Stop = false; StopAllTasks = false; diff --git a/YesNt.Interpreter/Statements/SystemStatements.cs b/YesNt.Interpreter/Statements/SystemStatements.cs index fb48103..ac357b7 100644 --- a/YesNt.Interpreter/Statements/SystemStatements.cs +++ b/YesNt.Interpreter/Statements/SystemStatements.cs @@ -76,7 +76,7 @@ namespace YesNt.Interpreter.Statements process.OutputDataReceived += Process_OutputDataReceived; process.ErrorDataReceived += Process_ErrorDataReceived; - process.Start(); + _ = process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); diff --git a/YesNt.Interpreter/Statements/VariableStatements.cs b/YesNt.Interpreter/Statements/VariableStatements.cs index 3d2e48a..06194a2 100644 --- a/YesNt.Interpreter/Statements/VariableStatements.cs +++ b/YesNt.Interpreter/Statements/VariableStatements.cs @@ -72,11 +72,11 @@ namespace YesNt.Interpreter.Statements if (RuntimeInfo.Variables.ContainsKey(key)) { - RuntimeInfo.Variables.Remove(key); + _ = RuntimeInfo.Variables.Remove(key); } else if (RuntimeInfo.GloablVariables.ContainsKey(key)) { - RuntimeInfo.GloablVariables.Remove(key); + _ = RuntimeInfo.GloablVariables.Remove(key); } else { diff --git a/YesNt.Interpreter/Utilities/Evaluator.cs b/YesNt.Interpreter/Utilities/Evaluator.cs index 1f0f239..aac7d50 100644 --- a/YesNt.Interpreter/Utilities/Evaluator.cs +++ b/YesNt.Interpreter/Utilities/Evaluator.cs @@ -38,12 +38,7 @@ namespace YesNt.Interpreter.Utilities { bool succ1 = parts[0].ToStandardizedNumber(out double part1); bool succ2 = parts[1].ToStandardizedNumber(out double part2); - if (!succ1 || !succ2) - { - return false; - } - - return part1 >= part2; + return succ1 && succ2 && part1 >= part2; } parts = input.Split("<="); @@ -51,12 +46,7 @@ namespace YesNt.Interpreter.Utilities { bool succ1 = parts[0].ToStandardizedNumber(out double part1); bool succ2 = parts[1].ToStandardizedNumber(out double part2); - if (!succ1 || !succ2) - { - return false; - } - - return part1 <= part2; + return succ1 && succ2 && part1 <= part2; } parts = input.Split(">"); @@ -64,12 +54,7 @@ namespace YesNt.Interpreter.Utilities { bool succ1 = parts[0].ToStandardizedNumber(out double part1); bool succ2 = parts[1].ToStandardizedNumber(out double part2); - if (!succ1 || !succ2) - { - return false; - } - - return part1 > part2; + return succ1 && succ2 && part1 > part2; } parts = input.Split("<"); @@ -77,12 +62,7 @@ namespace YesNt.Interpreter.Utilities { bool succ1 = parts[0].ToStandardizedNumber(out double part1); bool succ2 = parts[1].ToStandardizedNumber(out double part2); - if (!succ1 || !succ2) - { - return false; - } - - return part1 < part2; + return succ1 && succ2 && part1 < part2; } return null; diff --git a/YesNt.Interpreter/Utilities/FixedProcess.cs b/YesNt.Interpreter/Utilities/FixedProcess.cs index 65a32e2..9ee542d 100644 --- a/YesNt.Interpreter/Utilities/FixedProcess.cs +++ b/YesNt.Interpreter/Utilities/FixedProcess.cs @@ -42,7 +42,7 @@ namespace YesNt.Interpreter.Utilities DataReceivedEventArgs dataReceivedEventArgs = new DataReceivedEventArgs(data); if (SynchronizingObject != null && SynchronizingObject.InvokeRequired) { - SynchronizingObject.Invoke(outputDataReceived, new object[] + _ = SynchronizingObject.Invoke(outputDataReceived, new object[] { this, dataReceivedEventArgs @@ -61,7 +61,7 @@ namespace YesNt.Interpreter.Utilities DataReceivedEventArgs dataReceivedEventArgs = new DataReceivedEventArgs(data); if (SynchronizingObject != null && SynchronizingObject.InvokeRequired) { - SynchronizingObject.Invoke(errorDataReceived, new object[] + _ = SynchronizingObject.Invoke(errorDataReceived, new object[] { this, dataReceivedEventArgs @@ -160,7 +160,7 @@ namespace YesNt.Interpreter.Utilities if (sb == null) { sb = new StringBuilder(1024); - stream.BeginRead(byteBuffer, 0, byteBuffer.Length, new AsyncCallback(ReadBuffer), null); + _ = stream.BeginRead(byteBuffer, 0, byteBuffer.Length, new AsyncCallback(ReadBuffer), null); return; } FlushMessageQueue(); @@ -204,13 +204,13 @@ namespace YesNt.Interpreter.Utilities } finally { - eofEvent.Set(); + _ = eofEvent.Set(); } } int chars = decoder.GetChars(byteBuffer, 0, num, charBuffer, 0); - sb.Append(charBuffer, 0, chars); + _ = sb.Append(charBuffer, 0, chars); GetLinesFromStringBuilder(); - stream.BeginRead(byteBuffer, 0, byteBuffer.Length, new AsyncCallback(ReadBuffer), null); + _ = stream.BeginRead(byteBuffer, 0, byteBuffer.Length, new AsyncCallback(ReadBuffer), null); } private void GetLinesFromStringBuilder() @@ -227,7 +227,7 @@ namespace YesNt.Interpreter.Utilities while (i < length) { char c = sb[i]; - if (c == '\r' || c == '\n') + if (c is '\r' or '\n') { if (c == '\r' && i + 1 < length && sb[i + 1] == '\n') { @@ -261,7 +261,7 @@ namespace YesNt.Interpreter.Utilities } if (num < length) { - sb.Remove(0, num); + _ = sb.Remove(0, num); } else { @@ -292,7 +292,7 @@ namespace YesNt.Interpreter.Utilities { if (eofEvent != null) { - eofEvent.WaitOne(); + _ = eofEvent.WaitOne(); eofEvent.Close(); eofEvent = null; } diff --git a/YesNt.Interpreter/Utilities/StringExtentions.cs b/YesNt.Interpreter/Utilities/StringExtentions.cs index 6c81400..c8e615e 100644 --- a/YesNt.Interpreter/Utilities/StringExtentions.cs +++ b/YesNt.Interpreter/Utilities/StringExtentions.cs @@ -11,7 +11,7 @@ namespace YesNt.Interpreter.Utilities StringBuilder output = new StringBuilder(); foreach (char c in input) { - output.Append($"\v{c}\v"); + _ = output.Append($"\v{c}\v"); } return output.ToString(); }