Skip to content

Commit

Permalink
Code analysis optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
tareqimbasher committed Jun 8, 2024
1 parent dfa270c commit d6e2192
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 115 deletions.
115 changes: 48 additions & 67 deletions src/Apps/NetPad.Apps.App/App/src/core/@domain/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2669,12 +2669,19 @@ export interface IAppDependencyCheckResult {
isSupportedDotNetEfToolInstalled: boolean;
}

/** An implementation of semantic versioning (https://semver.org) */
export class SemanticVersion implements ISemanticVersion {
/** The major version number, never negative. */
major!: number;
/** The minor version number, never negative. */
minor!: number;
/** The patch version, -1 if not specified. */
patch!: number;
/** PreReleaseLabel position in the SymVer string 'major.minor.patch-PreReleaseLabel+BuildLabel'. */
preReleaseLabel?: string | undefined;
/** BuildLabel position in the SymVer string 'major.minor.patch-PreReleaseLabel+BuildLabel'. */
buildLabel?: string | undefined;
/** String representation. */
string!: string;

constructor(data?: ISemanticVersion) {
Expand Down Expand Up @@ -2723,12 +2730,19 @@ export class SemanticVersion implements ISemanticVersion {
}
}

/** An implementation of semantic versioning (https://semver.org) */
export interface ISemanticVersion {
/** The major version number, never negative. */
major: number;
/** The minor version number, never negative. */
minor: number;
/** The patch version, -1 if not specified. */
patch: number;
/** PreReleaseLabel position in the SymVer string 'major.minor.patch-PreReleaseLabel+BuildLabel'. */
preReleaseLabel?: string | undefined;
/** BuildLabel position in the SymVer string 'major.minor.patch-PreReleaseLabel+BuildLabel'. */
buildLabel?: string | undefined;
/** String representation. */
string: string;
}

Expand Down Expand Up @@ -3188,6 +3202,7 @@ export class SyntaxNodeOrTokenSlim implements ISyntaxNodeOrTokenSlim {
isToken!: boolean;
isNode!: boolean;
kind!: SyntaxKind;
type!: string;
span!: LinePositionSpan;
isMissing!: boolean;
valueText?: string | undefined;
Expand Down Expand Up @@ -3215,6 +3230,7 @@ export class SyntaxNodeOrTokenSlim implements ISyntaxNodeOrTokenSlim {
this.isToken = _data["isToken"];
this.isNode = _data["isNode"];
this.kind = _data["kind"];
this.type = _data["type"];
this.span = _data["span"] ? LinePositionSpan.fromJS(_data["span"]) : new LinePositionSpan();
this.isMissing = _data["isMissing"];
this.valueText = _data["valueText"];
Expand Down Expand Up @@ -3248,6 +3264,7 @@ export class SyntaxNodeOrTokenSlim implements ISyntaxNodeOrTokenSlim {
data["isToken"] = this.isToken;
data["isNode"] = this.isNode;
data["kind"] = this.kind;
data["type"] = this.type;
data["span"] = this.span ? this.span.toJSON() : <any>undefined;
data["isMissing"] = this.isMissing;
data["valueText"] = this.valueText;
Expand Down Expand Up @@ -3281,6 +3298,7 @@ export interface ISyntaxNodeOrTokenSlim {
isToken: boolean;
isNode: boolean;
kind: SyntaxKind;
type: string;
span: LinePositionSpan;
isMissing: boolean;
valueText?: string | undefined;
Expand Down Expand Up @@ -4346,7 +4364,6 @@ export interface ICreateScriptDto {

export class RunOptionsDto implements IRunOptionsDto {
specificCodeToRun?: string | undefined;
additionalCode?: SourceCodeDto[] | undefined;

constructor(data?: IRunOptionsDto) {
if (data) {
Expand All @@ -4360,11 +4377,6 @@ export class RunOptionsDto implements IRunOptionsDto {
init(_data?: any) {
if (_data) {
this.specificCodeToRun = _data["specificCodeToRun"];
if (Array.isArray(_data["additionalCode"])) {
this.additionalCode = [] as any;
for (let item of _data["additionalCode"])
this.additionalCode!.push(SourceCodeDto.fromJS(item));
}
}
}

Expand All @@ -4378,11 +4390,6 @@ export class RunOptionsDto implements IRunOptionsDto {
toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
data["specificCodeToRun"] = this.specificCodeToRun;
if (Array.isArray(this.additionalCode)) {
data["additionalCode"] = [];
for (let item of this.additionalCode)
data["additionalCode"].push(item.toJSON());
}
return data;
}

Expand All @@ -4396,62 +4403,6 @@ export class RunOptionsDto implements IRunOptionsDto {

export interface IRunOptionsDto {
specificCodeToRun?: string | undefined;
additionalCode?: SourceCodeDto[] | undefined;
}

export class SourceCodeDto implements ISourceCodeDto {
usings?: string[] | undefined;
code?: string | undefined;

constructor(data?: ISourceCodeDto) {
if (data) {
for (var property in data) {
if (data.hasOwnProperty(property))
(<any>this)[property] = (<any>data)[property];
}
}
}

init(_data?: any) {
if (_data) {
if (Array.isArray(_data["usings"])) {
this.usings = [] as any;
for (let item of _data["usings"])
this.usings!.push(item);
}
this.code = _data["code"];
}
}

static fromJS(data: any): SourceCodeDto {
data = typeof data === 'object' ? data : {};
let result = new SourceCodeDto();
result.init(data);
return result;
}

toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
if (Array.isArray(this.usings)) {
data["usings"] = [];
for (let item of this.usings)
data["usings"].push(item);
}
data["code"] = this.code;
return data;
}

clone(): SourceCodeDto {
const json = this.toJSON();
let result = new SourceCodeDto();
result.init(json);
return result;
}
}

export interface ISourceCodeDto {
usings?: string[] | undefined;
code?: string | undefined;
}

export type OptimizationLevel = "Debug" | "Release";
Expand Down Expand Up @@ -5163,6 +5114,7 @@ export interface IKeyboardShortcutConfiguration {

export type KeyCode = "Unknown" | "Backspace" | "Tab" | "Enter" | "ShiftLeft" | "ShiftRight" | "ControlLeft" | "ControlRight" | "AltLeft" | "AltRight" | "Pause" | "CapsLock" | "Escape" | "Space" | "PageUp" | "PageDown" | "End" | "Home" | "ArrowLeft" | "ArrowUp" | "ArrowRight" | "ArrowDown" | "PrintScreen" | "Insert" | "Delete" | "Digit0" | "Digit1" | "Digit2" | "Digit3" | "Digit4" | "Digit5" | "Digit6" | "Digit7" | "Digit8" | "Digit9" | "KeyA" | "KeyB" | "KeyC" | "KeyD" | "KeyE" | "KeyF" | "KeyG" | "KeyH" | "KeyI" | "KeyJ" | "KeyK" | "KeyL" | "KeyM" | "KeyN" | "KeyO" | "KeyP" | "KeyQ" | "KeyR" | "KeyS" | "KeyT" | "KeyU" | "KeyV" | "KeyW" | "KeyX" | "KeyY" | "KeyZ" | "MetaLeft" | "MetaRight" | "ContextMenu" | "Numpad0" | "Numpad1" | "Numpad2" | "Numpad3" | "Numpad4" | "Numpad5" | "Numpad6" | "Numpad7" | "Numpad8" | "Numpad9" | "NumpadMultiply" | "NumpadAdd" | "NumpadSubtract" | "NumpadDecimal" | "NumpadDivide" | "F1" | "F2" | "F3" | "F4" | "F5" | "F6" | "F7" | "F8" | "F9" | "F10" | "F11" | "F12" | "NumLock" | "ScrollLock" | "Semicolon" | "Equal" | "Comma" | "Minus" | "Period" | "Slash" | "Backquote" | "BracketLeft" | "Backslash" | "BracketRight" | "Quote";

/** This should be moved to the OmniSharp plugin */
export class OmniSharpOptions implements IOmniSharpOptions {
enabled!: boolean;
executablePath?: string | undefined;
Expand Down Expand Up @@ -5227,6 +5179,7 @@ export class OmniSharpOptions implements IOmniSharpOptions {
}
}

/** This should be moved to the OmniSharp plugin */
export interface IOmniSharpOptions {
enabled: boolean;
executablePath?: string | undefined;
Expand Down Expand Up @@ -5713,8 +5666,11 @@ export interface IErrorResult {
details?: string | undefined;
}

/** A base class for all script output */
export abstract class ScriptOutput implements IScriptOutput {
/** The body of the output. */
body?: any | undefined;
/** The order this output was emitted. A value of 0 indicates no order. */
order!: number;

constructor(data?: IScriptOutput) {
Expand Down Expand Up @@ -5750,11 +5706,15 @@ export abstract class ScriptOutput implements IScriptOutput {
}
}

/** A base class for all script output */
export interface IScriptOutput {
/** The body of the output. */
body?: any | undefined;
/** The order this output was emitted. A value of 0 indicates no order. */
order: number;
}

/** A base class for script output with an HTML-formatted string as the body. */
export abstract class HtmlScriptOutput extends ScriptOutput implements IHtmlScriptOutput {
body?: string | undefined;

Expand Down Expand Up @@ -5786,10 +5746,12 @@ export abstract class HtmlScriptOutput extends ScriptOutput implements IHtmlScri
}
}

/** A base class for script output with an HTML-formatted string as the body. */
export interface IHtmlScriptOutput extends IScriptOutput {
body?: string | undefined;
}

/** Results script output represented as HTML. */
export class HtmlResultsScriptOutput extends HtmlScriptOutput implements IHtmlResultsScriptOutput {

constructor(data?: IHtmlResultsScriptOutput) {
Expand Down Expand Up @@ -5821,9 +5783,11 @@ export class HtmlResultsScriptOutput extends HtmlScriptOutput implements IHtmlRe
}
}

/** Results script output represented as HTML. */
export interface IHtmlResultsScriptOutput extends IHtmlScriptOutput {
}

/** Error script output represented as HTML. */
export class HtmlErrorScriptOutput extends HtmlScriptOutput implements IHtmlErrorScriptOutput {

constructor(data?: IHtmlErrorScriptOutput) {
Expand Down Expand Up @@ -5855,9 +5819,11 @@ export class HtmlErrorScriptOutput extends HtmlScriptOutput implements IHtmlErro
}
}

/** Error script output represented as HTML. */
export interface IHtmlErrorScriptOutput extends IHtmlScriptOutput {
}

/** Raw script output represented as HTML. */
export class HtmlRawScriptOutput extends HtmlScriptOutput implements IHtmlRawScriptOutput {

constructor(data?: IHtmlRawScriptOutput) {
Expand Down Expand Up @@ -5889,9 +5855,11 @@ export class HtmlRawScriptOutput extends HtmlScriptOutput implements IHtmlRawScr
}
}

/** Raw script output represented as HTML. */
export interface IHtmlRawScriptOutput extends IHtmlScriptOutput {
}

/** SQL script output represented as HTML. */
export class HtmlSqlScriptOutput extends HtmlScriptOutput implements IHtmlSqlScriptOutput {

constructor(data?: IHtmlSqlScriptOutput) {
Expand Down Expand Up @@ -5923,6 +5891,7 @@ export class HtmlSqlScriptOutput extends HtmlScriptOutput implements IHtmlSqlScr
}
}

/** SQL script output represented as HTML. */
export interface IHtmlSqlScriptOutput extends IHtmlScriptOutput {
}

Expand Down Expand Up @@ -6018,11 +5987,17 @@ export interface IAppStatusMessagePublishedEvent {
message: AppStatusMessage;
}

/** Represents a status change in the application. */
export class AppStatusMessage implements IAppStatusMessage {
/** The ID of the script this message relates to. */
scriptId?: string | undefined;
/** The text of this message. */
text!: string;
/** The priority of this message. */
priority!: AppStatusMessagePriority;
/** Whether this status message should be persistant or it should clear out after a timeout. */
persistant!: boolean;
/** The DateTime of when this message was created. */
createdDate!: Date;

constructor(data?: IAppStatusMessage) {
Expand Down Expand Up @@ -6069,11 +6044,17 @@ export class AppStatusMessage implements IAppStatusMessage {
}
}

/** Represents a status change in the application. */
export interface IAppStatusMessage {
/** The ID of the script this message relates to. */
scriptId?: string | undefined;
/** The text of this message. */
text: string;
/** The priority of this message. */
priority: AppStatusMessagePriority;
/** Whether this status message should be persistant or it should clear out after a timeout. */
persistant: boolean;
/** The DateTime of when this message was created. */
createdDate: Date;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
<i class="fa-fw fa-solid ${syntaxNode.isNode ? 'fa-code-commit text-blue' : 'fa-minus text-yellow'}"
title="${syntaxNode.isNode ? 'Node' : 'Token'}"></i>

<span class="${syntaxNode.isMissing ? 'text-danger' : ''}">${syntaxNode.kind}</span>
<span class="${syntaxNode.isMissing ? 'text-danger' : ''}"
title.bind="syntaxNode.type">
${syntaxNode.kind}
</span>

<span class="tree-node-header-right">
<span class="value-text"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -236,16 +237,13 @@ private async Task<ScaffoldedSourceFile> ParseScaffoldedSourceFileAsync(FileInfo
var syntaxTreeRoot = CSharpSyntaxTree.ParseText(scaffoldedCode).GetRoot();
var nodes = syntaxTreeRoot.DescendantNodes().ToArray();

var usings = new HashSet<string>();

foreach (var usingDirective in nodes.OfType<UsingDirectiveSyntax>())
{
var ns = scaffoldedCode.Substring(usingDirective.Span.Start, usingDirective.Span.Length)
.Split(' ')[1]
.TrimEnd(';');

usings.Add(ns);
}
var usings = nodes
.OfType<UsingDirectiveSyntax>()
.Select(u => string.Join(
' ',
u.NormalizeWhitespace().ChildNodes().Select(x => x.ToFullString()))
)
.ToArray();

var classDeclaration = nodes.OfType<ClassDeclarationSyntax>().Single();
var classCode = scaffoldedCode.Substring(classDeclaration.Span.Start, classDeclaration.Span.Length);
Expand Down
15 changes: 10 additions & 5 deletions src/Core/NetPad.Runtime/CodeAnalysis/CodeAnalysisService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ private static SyntaxNodeOrTokenSlim Build(SyntaxNodeOrToken nodeOrToken, Cancel

var kind = nodeOrToken.Kind();

var element = new SyntaxNodeOrTokenSlim(nodeOrToken.IsToken, nodeOrToken.IsNode, kind, nodeOrToken.GetLocation()!.GetLineSpan().Span, nodeOrToken.IsMissing);
var element = new SyntaxNodeOrTokenSlim(
nodeOrToken.IsToken,
nodeOrToken.IsNode,
kind,
nodeOrToken.IsNode ? nodeOrToken.AsNode()!.GetType().Name : string.Empty,
nodeOrToken.GetLocation()!.GetLineSpan().Span,
nodeOrToken.IsMissing);

if (nodeOrToken.IsToken)
{
Expand Down Expand Up @@ -78,12 +84,11 @@ private static SyntaxNodeOrTokenSlim Build(SyntaxNodeOrToken nodeOrToken, Cancel
return element;
}

private static SyntaxTriviaSlim ToTrivia(Microsoft.CodeAnalysis.SyntaxTrivia trivia)
private static SyntaxTriviaSlim ToTrivia(SyntaxTrivia trivia)
{
var kind = trivia.Kind();
string? displayValue;// = kind.ToString();//.Token.ValueText; //TriviaDisplayValues.GetValueOrDefault(t.Kind());

if (!TriviaDisplayValues.TryGetValue(trivia.Kind(), out displayValue))
if (!_triviaDisplayValues.TryGetValue(trivia.Kind(), out var displayValue))
{
if (trivia.IsKind(SyntaxKind.WhitespaceTrivia))
{
Expand All @@ -99,7 +104,7 @@ private static SyntaxTriviaSlim ToTrivia(Microsoft.CodeAnalysis.SyntaxTrivia tri
return new SyntaxTriviaSlim(kind, trivia.GetLocation()!.GetLineSpan().Span, displayValue.Truncate(50, true));
}

private static Dictionary<SyntaxKind, string> TriviaDisplayValues = new Dictionary<SyntaxKind, string>
private static readonly Dictionary<SyntaxKind, string> _triviaDisplayValues = new Dictionary<SyntaxKind, string>
{
[SyntaxKind.SemicolonToken] = ";",
[SyntaxKind.EndOfLineTrivia] = "\\n",
Expand Down
Loading

0 comments on commit d6e2192

Please sign in to comment.