mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 15:30:27 +00:00
语法高亮
This commit is contained in:
@@ -1 +1 @@
|
|||||||
2026-02-24 20:06:20
|
2026-02-24 20:31:52
|
||||||
@@ -1 +1 @@
|
|||||||
0386511
|
e0e7da7
|
||||||
105
editor/Nano.cs
105
editor/Nano.cs
@@ -97,7 +97,6 @@ namespace CMLeonOS
|
|||||||
{
|
{
|
||||||
int y = i - scrollY + TITLEBAR_HEIGHT;
|
int y = i - scrollY + TITLEBAR_HEIGHT;
|
||||||
if (y < TITLEBAR_HEIGHT || y >= consoleHeight - SHORTCUT_BAR_HEIGHT) continue;
|
if (y < TITLEBAR_HEIGHT || y >= consoleHeight - SHORTCUT_BAR_HEIGHT) continue;
|
||||||
|
|
||||||
Console.SetCursorPosition(0, y);
|
Console.SetCursorPosition(0, y);
|
||||||
|
|
||||||
if (i >= lines.Count || scrollX >= lines[i].Length)
|
if (i >= lines.Count || scrollX >= lines[i].Length)
|
||||||
@@ -107,9 +106,17 @@ namespace CMLeonOS
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
string line = lines[i].Substring(scrollX, Math.Min(consoleWidth, lines[i].Length - scrollX));
|
string line = lines[i].Substring(scrollX, Math.Min(consoleWidth, lines[i].Length - scrollX));
|
||||||
|
|
||||||
|
if (IsLuaFile())
|
||||||
|
{
|
||||||
|
RenderLuaLine(line, consoleWidth);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Console.Write(line + new string(' ', Math.Max(0, consoleWidth - line.Length)));
|
Console.Write(line + new string(' ', Math.Max(0, consoleWidth - line.Length)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
updatedLinesStart = null;
|
updatedLinesStart = null;
|
||||||
updatedLinesEnd = null;
|
updatedLinesEnd = null;
|
||||||
@@ -118,6 +125,102 @@ namespace CMLeonOS
|
|||||||
Console.SetCursorPosition(linePos - scrollX, currentLine + TITLEBAR_HEIGHT - scrollY);
|
Console.SetCursorPosition(linePos - scrollX, currentLine + TITLEBAR_HEIGHT - scrollY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool IsLuaFile()
|
||||||
|
{
|
||||||
|
if (path != null)
|
||||||
|
{
|
||||||
|
string extension = System.IO.Path.GetExtension(path)?.ToLower();
|
||||||
|
return extension == ".lua";
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RenderLuaLine(string line, int consoleWidth)
|
||||||
|
{
|
||||||
|
int pos = 0;
|
||||||
|
bool inString = false;
|
||||||
|
bool inComment = false;
|
||||||
|
|
||||||
|
while (pos < line.Length && pos < consoleWidth)
|
||||||
|
{
|
||||||
|
if (inComment)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
Console.Write(line[pos]);
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
else if (line.Substring(pos).StartsWith("--"))
|
||||||
|
{
|
||||||
|
inComment = true;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
Console.Write(line.Substring(pos));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (inString)
|
||||||
|
{
|
||||||
|
if (line[pos] == '"' || line[pos] == '\'')
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
|
Console.Write(line[pos]);
|
||||||
|
inString = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
|
Console.Write(line[pos]);
|
||||||
|
}
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (line[pos] == '"' || line[pos] == '\'')
|
||||||
|
{
|
||||||
|
inString = true;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
|
Console.Write(line[pos]);
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
else if (IsLuaKeyword(line, pos))
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||||
|
Console.Write(line[pos]);
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
Console.Write(line[pos]);
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pos < consoleWidth)
|
||||||
|
{
|
||||||
|
Console.Write(new string(' ', consoleWidth - pos));
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsLuaKeyword(string line, int pos)
|
||||||
|
{
|
||||||
|
string[] keywords = { "function", "end", "if", "then", "else", "elseif", "while", "do", "for", "return", "local", "true", "false", "nil", "and", "or", "not", "break", "repeat", "until" };
|
||||||
|
|
||||||
|
foreach (string keyword in keywords)
|
||||||
|
{
|
||||||
|
if (pos + keyword.Length <= line.Length && line.Substring(pos, keyword.Length) == keyword)
|
||||||
|
{
|
||||||
|
char nextChar = pos + keyword.Length < line.Length ? line[pos + keyword.Length] : ' ';
|
||||||
|
if (!char.IsLetterOrDigit(nextChar))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Insert a new line at the cursor.
|
// Insert a new line at the cursor.
|
||||||
private void InsertLine()
|
private void InsertLine()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user