chore(bridge): 删除桥接包的提交记录和Git属性配置 删除了 .codely-bridge-commit 文件中存储的提交哈希值 移除了 .gitattributes 文件中的行结束符配置规则 ```
42 lines
1.7 KiB
C#
42 lines
1.7 KiB
C#
using System.Linq;
|
|
using Codely.Microsoft.CodeAnalysis;
|
|
using Codely.Microsoft.CodeAnalysis.CSharp;
|
|
using Codely.Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
using Codely.Microsoft.CodeAnalysis.Text;
|
|
using SyntaxTree = Codely.Microsoft.CodeAnalysis.SyntaxTree;
|
|
|
|
namespace UnityTcp.Editor.Tools
|
|
{
|
|
internal static class ScriptSyntaxUtils
|
|
{
|
|
internal static SyntaxTree InsertTextAtLocation(this SyntaxTree tree, Location location, string text)
|
|
{
|
|
var lineSpan = location.GetLineSpan().Span;
|
|
var sourceText = SourceText.From(tree.ToString());
|
|
var position = sourceText.Lines.GetPosition(lineSpan.Start);
|
|
var newSource = tree.ToString().Insert(position, text);
|
|
return SyntaxFactory.ParseSyntaxTree(newSource);
|
|
}
|
|
|
|
internal static SyntaxTree AddUsingAliasDirective(this SyntaxTree tree, string namespaceName, string aliasName)
|
|
{
|
|
if (!(tree.GetRoot() is CompilationUnitSyntax root))
|
|
return tree;
|
|
|
|
bool exists = root.Usings.Any(u =>
|
|
u.Name.ToString() == namespaceName && u.Alias?.Name.ToString() == aliasName);
|
|
if (exists)
|
|
return tree;
|
|
|
|
var usingDirective = SyntaxFactory
|
|
.UsingDirective(SyntaxFactory.ParseName(namespaceName))
|
|
.WithAlias(SyntaxFactory.NameEquals(SyntaxFactory.IdentifierName(aliasName)))
|
|
.NormalizeWhitespace()
|
|
.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
|
|
|
|
var newRoot = root.AddUsings(usingDirective).NormalizeWhitespace();
|
|
return CSharpSyntaxTree.Create(newRoot);
|
|
}
|
|
}
|
|
}
|