-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update HE 使用 rewriter plugin 重构语法处理部分 #348
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
namespace Natasha.CSharp.HotExecutor.Component | ||
{ | ||
internal class HEMethodTriviaRewriter : CSharpSyntaxRewriter | ||
{ | ||
public readonly HashSet<TriviaSyntaxPluginBase> _methodTriviaSyntaxPlugins; | ||
public HEMethodTriviaRewriter(HashSet<TriviaSyntaxPluginBase> methodTriviaSyntaxPluginBases) | ||
{ | ||
_methodTriviaSyntaxPlugins = methodTriviaSyntaxPluginBases; | ||
} | ||
public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia) | ||
{ | ||
if (trivia.IsKind(SyntaxKind.SingleLineCommentTrivia)) | ||
{ | ||
var commentText = trivia.ToString(); | ||
var commentLowerText = commentText.ToLower(); | ||
if (commentText.Length > 2) | ||
{ | ||
foreach (var plugin in _methodTriviaSyntaxPlugins) | ||
{ | ||
if (plugin.IsMatch(commentText,commentLowerText)) | ||
{ | ||
var result = plugin.Handle(commentText, commentLowerText); | ||
if (result != null) | ||
{ | ||
return SyntaxFactory.Comment(result); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return base.VisitTrivia(trivia); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,84 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
using Microsoft.CodeAnalysis; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
using Microsoft.CodeAnalysis.CSharp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
using System; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
using System.Collections.Concurrent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
using System.Collections.Generic; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
using System.Runtime.CompilerServices; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
using System.Text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
using System.Xml.Linq; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
namespace Natasha.CSharp.HotExecutor.Component | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
internal class HETreeMethodRewriter : CSharpSyntaxRewriter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private readonly ConcurrentDictionary<string, HashSet<MethodSyntaxPluginBase>> _methodPlugins; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private readonly ConcurrentDictionary<string, HashSet<TriviaSyntaxPluginBase>> _methodTriviaPlugins; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public HETreeMethodRewriter() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_methodPlugins = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_methodTriviaPlugins = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public HETreeMethodRewriter RegistePlugin(MethodSyntaxPluginBase methodSyntaxPlugin) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (_methodPlugins.TryGetValue(methodSyntaxPlugin.MethodName, out var methodPlugins)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
methodPlugins.Add(methodSyntaxPlugin); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_methodPlugins[methodSyntaxPlugin.MethodName] = [methodSyntaxPlugin]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (_methodTriviaPlugins.TryGetValue(methodSyntaxPlugin.MethodName, out var triviaPlugins)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (methodSyntaxPlugin.TriviaSyntaxPluginBases.Count>0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
triviaPlugins.UnionWith(methodSyntaxPlugin.TriviaSyntaxPluginBases); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_methodTriviaPlugins[methodSyntaxPlugin.MethodName] = new(methodSyntaxPlugin.TriviaSyntaxPluginBases); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return this; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+23
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the use of array brackets for initializing collections. The method incorrectly uses array brackets for initializing collections instead of - _methodPlugins[methodSyntaxPlugin.MethodName] = [methodSyntaxPlugin];
- _methodTriviaPlugins[methodSyntaxPlugin.MethodName] = new(methodSyntaxPlugin.TriviaSyntaxPluginBases);
+ _methodPlugins[methodSyntaxPlugin.MethodName] = new HashSet<MethodSyntaxPluginBase> { methodSyntaxPlugin };
+ _methodTriviaPlugins[methodSyntaxPlugin.MethodName] = new HashSet<TriviaSyntaxPluginBase>(methodSyntaxPlugin.TriviaSyntaxPluginBases); Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax methodNode) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var name = methodNode.Identifier.Text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (_methodTriviaPlugins.TryGetValue(name,out var triviaPlugins)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foreach (var plugin in triviaPlugins) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
plugin.Initialize(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var methodTriviaRewriter = new HEMethodTriviaRewriter(triviaPlugins); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
methodTriviaRewriter.Visit(methodNode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (_methodPlugins.TryGetValue(name, out var plugins)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var body = methodNode.Body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foreach (var plugin in plugins) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var result = plugin.Handle(body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (result != null) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
body = result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (body != methodNode.Body) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var node = methodNode.WithBody(body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Console.WriteLine(node.ToFullString()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return methodNode.WithBody(body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return base.VisitMethodDeclaration(methodNode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,57 @@ | ||||||||||||||||||||||
using Microsoft.CodeAnalysis; | ||||||||||||||||||||||
using Microsoft.CodeAnalysis.CSharp; | ||||||||||||||||||||||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||||||||||||||||||||||
using System; | ||||||||||||||||||||||
using System.Collections.Concurrent; | ||||||||||||||||||||||
using System.Collections.Generic; | ||||||||||||||||||||||
using System.Runtime.CompilerServices; | ||||||||||||||||||||||
using System.Text; | ||||||||||||||||||||||
using System.Xml.Linq; | ||||||||||||||||||||||
|
||||||||||||||||||||||
namespace Natasha.CSharp.HotExecutor.Component | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
internal class HETreeTriviaRewriter : CSharpSyntaxRewriter | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
private readonly HashSet<TriviaSyntaxPluginBase> _triviaPlugins; | ||||||||||||||||||||||
public HETreeTriviaRewriter() | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
_triviaPlugins = []; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public HETreeTriviaRewriter RegistePlugin(TriviaSyntaxPluginBase triviaSyntaxPluginBase) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
_triviaPlugins.Add(triviaSyntaxPluginBase); | ||||||||||||||||||||||
return this; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
Comment on lines
+21
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename method to The method correctly adds the plugin to - public HETreeTriviaRewriter RegistePlugin(TriviaSyntaxPluginBase triviaSyntaxPluginBase)
+ public HETreeTriviaRewriter RegisterPlugin(TriviaSyntaxPluginBase triviaSyntaxPluginBase) Committable suggestion
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
if (trivia.IsKind(SyntaxKind.SingleLineCommentTrivia)) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
var commentText = trivia.ToString(); | ||||||||||||||||||||||
var commentLowerText = commentText.ToLower(); | ||||||||||||||||||||||
string? newComment = null; | ||||||||||||||||||||||
if (commentText.Length > 2) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
foreach (var plugin in _triviaPlugins) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
if (plugin.IsMatch(commentText, commentLowerText)) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
var result= plugin.Handle(commentText, commentLowerText); | ||||||||||||||||||||||
if (result != null) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
newComment = result; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
if (newComment!=null) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
return SyntaxFactory.Comment(newComment); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return base.VisitTrivia(trivia); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Natasha.CSharp.HotExecutor.Component | ||
{ | ||
internal static class OnceHandler | ||
{ | ||
private static string _onceComment = "//HE:Once".ToLower(); | ||
public static void SetOnceComment(string onceComment) | ||
{ | ||
_onceComment = onceComment.ToLower(); | ||
} | ||
public static CompilationUnitSyntax Handle(CompilationUnitSyntax root) | ||
{ | ||
|
||
var statementSyntaxes = root | ||
.DescendantNodesAndSelf() | ||
.OfType<StatementSyntax>() | ||
.Where(statement=>statement.HasLeadingTrivia || statement.HasTrailingTrivia); | ||
|
||
var removeNodes = new HashSet<SyntaxNode>(); | ||
if (statementSyntaxes!=null) | ||
{ | ||
foreach (var statement in statementSyntaxes) | ||
{ | ||
|
||
if (statement.HasLeadingTrivia) | ||
{ | ||
if (ShouldRemoveStatmentSyntax(statement.GetLeadingTrivia())) | ||
{ | ||
removeNodes.Add(statement); | ||
continue; | ||
} | ||
} | ||
if (statement.HasTrailingTrivia) | ||
{ | ||
if (ShouldRemoveStatmentSyntax(statement.GetTrailingTrivia())) | ||
{ | ||
removeNodes.Add(statement); | ||
continue; | ||
} | ||
} | ||
} | ||
if (removeNodes.Count > 0) | ||
{ | ||
root = root.RemoveNodes(removeNodes, SyntaxRemoveOptions.KeepExteriorTrivia)!; | ||
} | ||
} | ||
|
||
return root; | ||
} | ||
|
||
private static bool ShouldRemoveStatmentSyntax(SyntaxTriviaList leadingTrivias) | ||
{ | ||
var trivias = leadingTrivias.Where(trivia => trivia.IsKind(SyntaxKind.SingleLineCommentTrivia)); | ||
|
||
if (trivias != null) | ||
{ | ||
foreach (var trivia in trivias) | ||
{ | ||
var commentText = trivia.ToString(); | ||
if (commentText.Length > 2) | ||
{ | ||
var commentLowerText = commentText.ToLower(); | ||
if (commentLowerText.StartsWith(_onceComment)) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace Natasha.CSharp.HotExecutor.Component | ||
{ | ||
internal class CS0104TriviaPlugin : TriviaSyntaxPluginBase | ||
{ | ||
public string _excludeUsingComment; | ||
public HashSet<string> ExcludeUsings; | ||
public CS0104TriviaPlugin(string excludeUsingComment) | ||
{ | ||
ExcludeUsings = []; | ||
_excludeUsingComment = excludeUsingComment.ToLower(); | ||
} | ||
|
||
public void SetMatchComment(string excludeUsingComment) | ||
{ | ||
_excludeUsingComment = excludeUsingComment.ToLower(); | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
ExcludeUsings.Clear(); | ||
} | ||
|
||
public override bool IsMatch(string comment, string lowerComment) | ||
{ | ||
return lowerComment.StartsWith(_excludeUsingComment); | ||
} | ||
|
||
public override string? Handle(string comment, string lowerComment) | ||
{ | ||
var result = CommentHelper.GetCommentScript(comment, _excludeUsingComment.Length); | ||
var usingStrings = result.Split(';', StringSplitOptions.RemoveEmptyEntries); | ||
ExcludeUsings.UnionWith(usingStrings); | ||
return null; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
namespace Natasha.CSharp.HotExecutor.Component | ||
{ | ||
internal class OutputTriviaPlugin : TriviaSyntaxPluginBase | ||
{ | ||
private string _debugOutputComment; | ||
private string _releaseOutputComment; | ||
private readonly Func<bool> _isRelease; | ||
public readonly string CommentPrefix; | ||
public OutputTriviaPlugin(string debugOutputComment, string releaseOutputComment, Func<bool> isRelease) | ||
{ | ||
_debugOutputComment = debugOutputComment.ToLower(); | ||
_releaseOutputComment = releaseOutputComment.ToLower(); | ||
_isRelease = isRelease; | ||
CommentPrefix = "//NMSAzulx Natasha HE RID: " + Guid.NewGuid().ToString("N") + " "; | ||
} | ||
|
||
public void SetDebugOutputCommentTag(string debugOutputComment) | ||
{ | ||
_debugOutputComment = debugOutputComment.ToLower(); | ||
} | ||
|
||
public void SetReleaseOutputCommentTag(string releaseOutputComment) | ||
{ | ||
_releaseOutputComment = releaseOutputComment.ToLower(); | ||
} | ||
|
||
public override string? Handle(string comment, string lowerComment) | ||
{ | ||
if (_isRelease()) | ||
{ | ||
return CommentPrefix + CreatePreprocessorReplaceScript(CommentHelper.GetCommentScript(comment,_releaseOutputComment.Length)); | ||
} | ||
return CommentPrefix + CreatePreprocessorReplaceScript(CommentHelper.GetCommentScript(comment,_debugOutputComment.Length)); | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
|
||
} | ||
|
||
public override bool IsMatch(string comment, string lowerComment) | ||
{ | ||
if (_isRelease() && lowerComment.StartsWith(_releaseOutputComment)) | ||
{ | ||
return true; | ||
} | ||
else if (!_isRelease() && lowerComment.StartsWith(_debugOutputComment)) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static string CreatePreprocessorReplaceScript(string content) | ||
{ | ||
//if (_projectKind == HEProjectKind.AspnetCore || _projectKind == HEProjectKind.Console) | ||
//{ | ||
// return $"Console.WriteLine({content});"; | ||
//} | ||
return $"HEProxy.ShowMessage(({content}).ToString());"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace Natasha.CSharp.HotExecutor.Component | ||
{ | ||
internal class AsyncTriviaPlugin : TriviaSyntaxPluginBase | ||
{ | ||
private string _asyncCommentTag; | ||
public bool IsAsync; | ||
public AsyncTriviaPlugin(string asyncComment) | ||
{ | ||
_asyncCommentTag = asyncComment.ToLower(); | ||
} | ||
public void SetAsyncCommentTag(string asyncComment) | ||
{ | ||
_asyncCommentTag = asyncComment.ToLower(); | ||
} | ||
public override string? Handle(string comment, string lowerComment) | ||
{ | ||
IsAsync = true; | ||
return null; | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
IsAsync = false; | ||
} | ||
|
||
public override bool IsMatch(string comment, string lowerComment) | ||
{ | ||
return lowerComment.StartsWith(_asyncCommentTag); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the initialization of
_methodPlugins
and_methodTriviaPlugins
.The constructor incorrectly initializes
_methodPlugins
and_methodTriviaPlugins
with empty array brackets instead ofnew ConcurrentDictionary<>()
.Committable suggestion