Skip to content

Commit

Permalink
Debugger: Fixed/improved text highlighting issues for C code
Browse files Browse the repository at this point in the history
  • Loading branch information
SourMesen committed Sep 30, 2023
1 parent cf81247 commit 5f572d5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion UI/Debugger/Disassembly/BaseStyleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected void SetBreakpointLineProperties(LineProperties props, Breakpoint brea
props.Symbol = symbol;
}

public List<CodeColor> GetCodeColors(CodeLineData lineData, bool highlightCode, string addressFormat, Color? textColor, bool showMemoryValues)
public virtual List<CodeColor> GetCodeColors(CodeLineData lineData, bool highlightCode, string addressFormat, Color? textColor, bool showMemoryValues)
{
return CodeHighlighting.GetCpuHighlights(lineData, highlightCode, addressFormat, textColor, showMemoryValues);
}
Expand Down
70 changes: 70 additions & 0 deletions UI/Debugger/Disassembly/SourceViewStyleProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using Mesen.Interop;
using Mesen.Debugger.ViewModels;
using System.Collections.Generic;
using Mesen.Debugger.Controls;
using Avalonia.Media;
using System.Text.RegularExpressions;
using Mesen.Config;

namespace Mesen.Debugger.Disassembly
{
Expand Down Expand Up @@ -28,5 +33,70 @@ public override bool IsLineSelected(CodeLineData line, int lineIndex)
lineIndex += _model.ScrollPosition;
return lineIndex >= _model.SelectionStart && lineIndex <= _model.SelectionEnd;
}

public override List<CodeColor> GetCodeColors(CodeLineData lineData, bool highlightCode, string addressFormat, Color? textColor, bool showMemoryValues)
{
if(_model.SelectedFile?.IsAssembly != false) {
return base.GetCodeColors(lineData, highlightCode, addressFormat, textColor, showMemoryValues);
} else {
return GetCHighlights(lineData, highlightCode, addressFormat, textColor, showMemoryValues);
}
}

private static Regex _space = new Regex("^[ \t]+", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static Regex _comment = new Regex("^//.*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static Regex _keywords = new Regex("^(if|else|static|void|int|short|long|char|unsigned|signed|break|return|continue|switch|case|const|while|do|#define|#pragma|#include){1}([^a-z0-9_-]+|$)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static Regex _text = new Regex("^([a-z0-9_]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static Regex _syntax = new Regex("^[]([)!+,.|:<>?&^;{}\"'/*%=#-]{1}", RegexOptions.Compiled);
private static Regex _number = new Regex("^(0x[0-9a-f]+|0b[01]+|[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

public static List<CodeColor> GetCHighlights(CodeLineData lineData, bool highlightCode, string addressFormat, Color? textColor, bool showMemoryValues)
{
DebuggerConfig cfg = ConfigManager.Config.Debug.Debugger;
string codeString = lineData.Text.TrimEnd();
Color defaultColor = Color.FromRgb(60, 60, 60);

List<CodeColor> colors = new List<CodeColor>();
if(codeString.Length > 0 && highlightCode && !lineData.Flags.HasFlag(LineFlags.Label)) {
int pos = 0;
while(codeString.Length > 0) {
Match m;
if((m = _comment.Match(codeString)).Success) {
colors.Add(new CodeColor(m.Value, textColor ?? cfg.CodeCommentColor, CodeSegmentType.Comment, pos));
} else if((m = _number.Match(codeString)).Success) {
colors.Add(new CodeColor(m.Value, textColor ?? cfg.CodeImmediateColor, CodeSegmentType.ImmediateValue, pos));
} else if((m = _keywords.Match(codeString)).Success) {
colors.Add(new CodeColor(m.Groups[1].Value, textColor ?? cfg.CodeAddressColor, CodeSegmentType.OpCode, pos));
} else if((m = _text.Match(codeString)).Success) {
colors.Add(new CodeColor(m.Groups[1].Value, textColor ?? defaultColor, CodeSegmentType.OpCode, pos));
} else if((m = _syntax.Match(codeString)).Success) {
colors.Add(new CodeColor(m.Value, textColor ?? cfg.CodeEffectiveAddressColor, CodeSegmentType.Syntax, pos));
} else if((m = _space.Match(codeString)).Success) {
colors.Add(new CodeColor(m.Value, textColor ?? defaultColor, CodeSegmentType.None, pos));
}

if(m.Success) {
if(m.Groups.Count > 1) {
pos += m.Groups[1].Value.Length;
codeString = codeString.Substring(m.Groups[1].Value.Length);
} else {
pos += m.Value.Length;
codeString = codeString.Substring(m.Value.Length);
}
} else {
break;
}
}

//Display the rest of the line
if(codeString.Length > 0) {
colors.Add(new CodeColor(codeString, textColor ?? defaultColor, CodeSegmentType.None, pos));
}
} else {
colors.Add(new CodeColor(codeString, textColor ?? defaultColor, CodeSegmentType.None));
}

return colors;
}
}
}
8 changes: 7 additions & 1 deletion UI/Debugger/ViewModels/SourceViewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ public SourceViewViewModel(DebuggerWindowViewModel debugger, ISymbolProvider sym
StyleProvider = new SourceViewStyleProvider(cpuType, this);

SourceFiles = SymbolProvider.SourceFiles.Where(f => f.Data.Length > 0 && !f.Name.EndsWith(".chr", StringComparison.OrdinalIgnoreCase)).ToList();
SourceFiles.Sort((a, b) => a.ToString().CompareTo(b.ToString()));
SourceFiles.Sort((a, b) => {
int result = a.IsAssembly.CompareTo(b.IsAssembly);
if(result != 0) {
return result;
}
return a.ToString().CompareTo(b.ToString());
});
if(SourceFiles.Count > 0) {
SelectedFile = SourceFiles[0];
}
Expand Down

0 comments on commit 5f572d5

Please sign in to comment.