Skip to content

Commit

Permalink
Draw IL disassembly with prismjs.
Browse files Browse the repository at this point in the history
  • Loading branch information
allisterb committed Jul 10, 2023
1 parent fb6c180 commit b0833ea
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
76 changes: 76 additions & 0 deletions src/Stratis.DevEx.Drawing/HighlightedCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Text;

namespace Stratis.DevEx.Drawing
{

public abstract class HighlightedCode
{
#region Constructors
public HighlightedCode(string lang, string code)
{
Lang = lang;
Code = code;
}
#endregion

#region Properties
public string Lang { get; set; }
public string Code { get; set; }
#endregion

#region Methods
public abstract string Draw(string id);
#endregion

#region Overriden members
public override string ToString()
{
return Code;
}
#endregion
}

public class TmHighlightedCode : HighlightedCode
{
#region Constructors
public TmHighlightedCode(string lang, string code) : base(lang, code)
{


}
#endregion

#region Methods
public override string Draw(string id)
{
var html = new StringBuilder();
html.AppendLine($"<div id=\"{id}\">");
html.AppendLine("</div>");
html.AppendLine($"<script>if (highlighter === undefined) {{console.error('Shiki not loaded yet!. Run the notebook init script.')}} else {{$('#{id}').html(highlighter.codeToHtml(\"{(this.Code)}\", \"{this.Lang}\", \"light-plus\"));}}</script>");
return html.ToString();
}
#endregion
}

public class PrismHighlightedCode : HighlightedCode
{
#region Constructors
public PrismHighlightedCode(string lang, string code) : base(lang, code)
{

}
#endregion

#region Methods
public override string Draw(string id)
{
var html = new StringBuilder();
html.AppendLine($"<div id=\"{id}\" class=\"language-{Lang}\">");
html.AppendLine(Code);
html.Append("</div>");
return html.ToString();
}
#endregion
}
}

15 changes: 14 additions & 1 deletion src/Stratis.DevEx.Drawing/Html.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static string DrawSummary(string summary)
var op = Begin("Drawing summary to HTML");
var stringBuilder = new StringBuilder();
var divId = Guid.NewGuid().ToString("N");
stringBuilder.AppendLine("<html lang=\"en\"><head><title>Title</title><body>");
stringBuilder.AppendLine("<html lang=\"en\"><head><title>Title</title></head><body>");
stringBuilder.AppendLine($"<pre id=\"{divId}\" class=\"mermaid\" style=\"height:1000px; width:100%\">");
stringBuilder.AppendLine(summary);
stringBuilder.AppendLine("</pre>");
Expand All @@ -75,6 +75,19 @@ public static string DrawSummary(string summary)
op.Complete();
return stringBuilder.ToString();
}

public static string DrawDisassembly(string il)
{
var op = Begin("Drawing disassembly");
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("<html lang=\"en\"><head><title>Title</title><link href=\"https://unpkg.com/prismjs@1.29.0/themes/prism.min.css\" rel=\"stylesheet\" /></head><body>");
stringBuilder.AppendLine("<script src=\"https://unpkg.com/prismjs@1.29.0/components/prism-core.min.js\"></script>");
stringBuilder.AppendLine("<script src=\"https://unpkg.com/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js\"></script>");
stringBuilder.AppendLine($"<pre><code class=\"language-cil\">{il}</code></pre>");
stringBuilder.AppendLine("</body>");
op.Complete();
return stringBuilder.ToString();
}
}
}

10 changes: 5 additions & 5 deletions src/Stratis.DevEx.Gui/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void ReadMessage(CompilationMessage m)
Info("Not processing message from unknown editor assembly for assembly {asm}.", m.AssemblyName);
return;
}
var projectid = m.EditorEntryAssembly + "_" + m.AssemblyName + "_" + m.CompilationId.ToString();
var projectid = m.EditorEntryAssembly + "_" + m.AssemblyName;
var projects = (TreeItem)navigation.DataStore[1];
if (projects.Children.Any(c => c.Key == projectid))
{
Expand Down Expand Up @@ -265,7 +265,7 @@ var x when x.StartsWith("JetBrains.Roslyn.Worker") => JetbrainsRider,

public void AddProjectToTree(CompilationMessage m)
{
var projectid = m.EditorEntryAssembly + "_" + m.AssemblyName + "_" + m.CompilationId.ToString();
var projectid = m.EditorEntryAssembly + "_" + m.AssemblyName;
var projectDir = Path.GetDirectoryName(m.ConfigFile)!;

projectViews.Add(projectid, @"<html><head><title>Summary</title></head><body><h1>" + m.AssemblyName + "</h1></body></html>");
Expand All @@ -276,7 +276,7 @@ public void AddProjectToTree(CompilationMessage m)
var output = new MonochromeSourceEmitterOutput();
Disassembler.Run(asm, output);
var docid = projectid + "_" + "Disassembly";
projectViews[docid] = output.Data;
projectViews[docid] = Html.DrawDisassembly(output.Data);
var child = new TreeItem()
{
Key = projectid,
Expand Down Expand Up @@ -344,7 +344,7 @@ public void UpdateProjectDocsTree(SummaryMessage m)

public void UpdateProjectDocsTree(CompilationMessage m)
{
var projectid = m.EditorEntryAssembly + "_" + m.AssemblyName + "_" + m.CompilationId.ToString();
var projectid = m.EditorEntryAssembly + "_" + m.AssemblyName;
var projectDir = Path.GetDirectoryName(m.ConfigFile)!;
var project = (TreeItem)Projects.Children.First(c => c.Key == projectid);
var asm = Path.Combine(Program.assemblyCacheDir.FullName, projectid + ".dll");
Expand All @@ -354,7 +354,7 @@ public void UpdateProjectDocsTree(CompilationMessage m)
var output = new MonochromeSourceEmitterOutput();
Disassembler.Run(asm, output);
var docid = projectid + "_" + "Disassembly";
projectViews[docid] = output.Data;
projectViews[docid] = Html.DrawDisassembly(output.Data);
}

#region Logging
Expand Down

0 comments on commit b0833ea

Please sign in to comment.