mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 15:30:27 +00:00
打开文件可视化窗口+CodeStudio代码高亮
This commit is contained in:
50
Gui/Apps/CodeStudio/CodeEditor.cs
Normal file
50
Gui/Apps/CodeStudio/CodeEditor.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using CMLeonOS.Gui.UILib;
|
||||
using Cosmos.System.Graphics;
|
||||
using System.Drawing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace CMLeonOS.Gui.Apps.CodeStudio
|
||||
{
|
||||
internal class CodeEditor : TextBox
|
||||
{
|
||||
private bool enableSyntaxHighlighting = false;
|
||||
private string fileExtension = "";
|
||||
|
||||
public CodeEditor(Window parent, int x, int y, int width, int height) : base(parent, x, y, width, height)
|
||||
{
|
||||
}
|
||||
|
||||
internal void SetSyntaxHighlighting(bool enable, string extension = "")
|
||||
{
|
||||
enableSyntaxHighlighting = enable;
|
||||
fileExtension = extension.ToLower();
|
||||
MarkAllLines();
|
||||
Render();
|
||||
}
|
||||
|
||||
internal override void RenderLine(int lineIndex, string lineText, int lineY, int xOffset)
|
||||
{
|
||||
if (enableSyntaxHighlighting && fileExtension == ".lua")
|
||||
{
|
||||
var tokens = LuaSyntaxHighlighter.HighlightLine(lineText);
|
||||
int currentXOffset = xOffset;
|
||||
|
||||
foreach (var token in tokens)
|
||||
{
|
||||
if (currentXOffset + token.Text.Length * 8 > 0 && currentXOffset < Width)
|
||||
{
|
||||
DrawString(token.Text, token.Color, currentXOffset, lineY);
|
||||
}
|
||||
currentXOffset += token.Text.Length * 8;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
base.RenderLine(lineIndex, lineText, lineY, xOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,12 +29,14 @@ namespace CMLeonOS.Gui.Apps.CodeStudio
|
||||
|
||||
Button runButton;
|
||||
|
||||
TextBox editor;
|
||||
CodeEditor editor;
|
||||
|
||||
TextBox problems;
|
||||
|
||||
TextBox output;
|
||||
|
||||
FileBrowser fileBrowser;
|
||||
|
||||
private const int headersHeight = 24;
|
||||
private const int problemsHeight = 128;
|
||||
private const int outputHeight = 128;
|
||||
@@ -90,6 +92,9 @@ namespace CMLeonOS.Gui.Apps.CodeStudio
|
||||
{
|
||||
editor.Text = File.ReadAllText(path);
|
||||
|
||||
string extension = Path.GetExtension(path).ToLower();
|
||||
editor.SetSyntaxHighlighting(extension == ".lua", extension);
|
||||
|
||||
editor.MarkAllLines();
|
||||
editor.Render();
|
||||
|
||||
@@ -101,35 +106,27 @@ namespace CMLeonOS.Gui.Apps.CodeStudio
|
||||
|
||||
private void OpenFilePrompt()
|
||||
{
|
||||
PromptBox prompt = new PromptBox(process, "Open File", "Enter the path to open.", "Path", (string newPath) =>
|
||||
fileBrowser = new FileBrowser(process, wm, (string selectedPath) =>
|
||||
{
|
||||
if (!newPath.Contains(':'))
|
||||
if (selectedPath != null)
|
||||
{
|
||||
newPath = $@"0:\{newPath}";
|
||||
Open(selectedPath);
|
||||
}
|
||||
Open(newPath);
|
||||
});
|
||||
prompt.Show();
|
||||
fileBrowser.Show();
|
||||
}
|
||||
|
||||
private void SaveAsPrompt()
|
||||
{
|
||||
PromptBox prompt = new PromptBox(process, "Save As", "Enter the path to save to.", "Path", (string newPath) =>
|
||||
fileBrowser = new FileBrowser(process, wm, (string selectedPath) =>
|
||||
{
|
||||
if (!newPath.Contains(':'))
|
||||
{
|
||||
newPath = $@"0:\{newPath}";
|
||||
}
|
||||
|
||||
Open(newPath, readFile: false);
|
||||
|
||||
// Check if open succeeded.
|
||||
if (path != null)
|
||||
if (selectedPath != null)
|
||||
{
|
||||
path = selectedPath;
|
||||
Save();
|
||||
}
|
||||
});
|
||||
prompt.Show();
|
||||
}, selectDirectoryOnly: true);
|
||||
fileBrowser.Show();
|
||||
}
|
||||
|
||||
private void Save()
|
||||
@@ -270,7 +267,7 @@ namespace CMLeonOS.Gui.Apps.CodeStudio
|
||||
runButton.OnClick = RunClicked;
|
||||
wm.AddWindow(runButton);
|
||||
|
||||
editor = new TextBox(mainWindow, 0, headersHeight, mainWindow.Width, mainWindow.Height - headersHeight - problemsHeight - outputHeight - (headersHeight * 3))
|
||||
editor = new CodeEditor(mainWindow, 0, headersHeight, mainWindow.Width, mainWindow.Height - headersHeight - problemsHeight - outputHeight - (headersHeight * 3))
|
||||
{
|
||||
Background = Theme.CodeBackground,
|
||||
Foreground = Color.White,
|
||||
@@ -278,6 +275,7 @@ namespace CMLeonOS.Gui.Apps.CodeStudio
|
||||
Changed = TextChanged,
|
||||
MultiLine = true
|
||||
};
|
||||
editor.SetSyntaxHighlighting(true, ".lua");
|
||||
wm.AddWindow(editor);
|
||||
|
||||
problems = new TextBox(mainWindow, 0, headersHeight + editor.Height + headersHeight, mainWindow.Width, problemsHeight + (headersHeight * 2))
|
||||
|
||||
150
Gui/Apps/CodeStudio/LuaSyntaxHighlighter.cs
Normal file
150
Gui/Apps/CodeStudio/LuaSyntaxHighlighter.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
|
||||
namespace CMLeonOS.Gui.Apps.CodeStudio
|
||||
{
|
||||
internal static class LuaSyntaxHighlighter
|
||||
{
|
||||
private static readonly List<string> Keywords = new List<string>
|
||||
{
|
||||
"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while"
|
||||
};
|
||||
|
||||
private static readonly List<string> Builtins = new List<string>
|
||||
{
|
||||
"print", "type", "tostring", "tonumber", "ipairs", "pairs", "table", "string", "math", "io", "os", "coroutine", "debug", "package", "utf8", "bit32"
|
||||
};
|
||||
|
||||
private static readonly List<string> Operators = new List<string>
|
||||
{
|
||||
"+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=", "<", ">", "=", "(", ")", "[", "]", "{", "}", ";", ":", ",", "..", "...", "."
|
||||
};
|
||||
|
||||
internal static class Colors
|
||||
{
|
||||
internal static Color Keyword = Color.FromArgb(255, 0, 128);
|
||||
internal static Color Builtin = Color.FromArgb(0, 128, 0);
|
||||
internal static Color String = Color.FromArgb(163, 21, 21);
|
||||
internal static Color Number = Color.FromArgb(0, 0, 255);
|
||||
internal static Color Comment = Color.FromArgb(128, 128, 128);
|
||||
internal static Color Operator = Color.FromArgb(0, 0, 0);
|
||||
internal static Color Default = Color.White;
|
||||
}
|
||||
|
||||
internal class HighlightedToken
|
||||
{
|
||||
internal string Text;
|
||||
internal Color Color;
|
||||
|
||||
internal HighlightedToken(string text, Color color)
|
||||
{
|
||||
Text = text;
|
||||
Color = color;
|
||||
}
|
||||
}
|
||||
|
||||
internal static List<HighlightedToken> HighlightLine(string line)
|
||||
{
|
||||
var tokens = new List<HighlightedToken>();
|
||||
int i = 0;
|
||||
|
||||
while (i < line.Length)
|
||||
{
|
||||
char c = line[i];
|
||||
|
||||
if (char.IsWhiteSpace(c))
|
||||
{
|
||||
int start = i;
|
||||
while (i < line.Length && char.IsWhiteSpace(line[i]))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
tokens.Add(new HighlightedToken(line.Substring(start, i - start), Colors.Default));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '-' && i + 1 < line.Length && line[i + 1] == '-')
|
||||
{
|
||||
int start = i;
|
||||
i += 2;
|
||||
while (i < line.Length && line[i] != '\n')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
tokens.Add(new HighlightedToken(line.Substring(start, i - start), Colors.Comment));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '"' || c == '\'')
|
||||
{
|
||||
int start = i;
|
||||
i++;
|
||||
while (i < line.Length && line[i] != c)
|
||||
{
|
||||
if (line[i] == '\\' && i + 1 < line.Length)
|
||||
{
|
||||
i += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
i++;
|
||||
}
|
||||
}
|
||||
if (i < line.Length)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
tokens.Add(new HighlightedToken(line.Substring(start, i - start), Colors.String));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char.IsDigit(c))
|
||||
{
|
||||
int start = i;
|
||||
while (i < line.Length && (char.IsDigit(line[i]) || line[i] == '.'))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
tokens.Add(new HighlightedToken(line.Substring(start, i - start), Colors.Number));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char.IsLetter(c) || c == '_')
|
||||
{
|
||||
int start = i;
|
||||
while (i < line.Length && (char.IsLetterOrDigit(line[i]) || line[i] == '_'))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
string word = line.Substring(start, i - start);
|
||||
|
||||
if (Keywords.Contains(word))
|
||||
{
|
||||
tokens.Add(new HighlightedToken(word, Colors.Keyword));
|
||||
}
|
||||
else if (Builtins.Contains(word))
|
||||
{
|
||||
tokens.Add(new HighlightedToken(word, Colors.Builtin));
|
||||
}
|
||||
else
|
||||
{
|
||||
tokens.Add(new HighlightedToken(word, Colors.Default));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Operators.Contains(c.ToString()))
|
||||
{
|
||||
tokens.Add(new HighlightedToken(c.ToString(), Colors.Operator));
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
tokens.Add(new HighlightedToken(c.ToString(), Colors.Default));
|
||||
i++;
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user