使用语句 Roslyn 脚本/代码排序和删除(未使用)?我正在寻找一些可以在项目中运行并对未使用的 using 语句进行排序和删除的 .NET/Roslyn(编译器即服务)代码。我相信罗斯林有可能吗?谁能指出我可以重写的代码?
5 回答
这是 Visual Studio 中的一个功能,但在学术上我认为你会使用 SyntaxTree 中的语句来收集,如下所示:
var usings = syntaxTree.Root.DescendentNodes().Where(node is UsingDirectiveSyntax);
...并将其与符号表解析的命名空间进行比较,如下所示:
private static IEnumerable<INamespaceSymbol> GetNamespaceSymbol(ISymbol symbol)
{
if (symbol != null && symbol.ContainingNamespace != null)
yield return symbol.ContainingNamespace;
}
var ns = semanticModel.SyntaxTree.Root.DescendentNodes().SelectMany(node =>
GetNamespaceSymbol(semanticModel.GetSemanticInfo(node).Symbol)).Distinct();
Roslyn CTP September 2012 提供了一种GetUnusedImportDirectives()
方法,在这里很有用。
通过修改 Matt 引用的OrganizeSolution示例项目,您可以使用指令实现(未使用)的排序和删除。可以在此处找到此项目的(过时)版本:http: //go.microsoft.com/fwlink/ ?LinkId=263977 。它已经过时了,因为
document.GetUpdatedDocument()
不再存在,
var document = newSolution.GetDocument(documentId);
var transformation = document.OrganizeImports();
var newDocument = transformation.GetUpdatedDocument();
可以简化为
var document = newSolution.GetDocument(documentId);
var newDocument = document.OrganizeImports();
添加newDocument = RemoveUnusedImportDirectives(newDocument);
并提供以下方法可以解决问题。
private static IDocument RemoveUnusedImportDirectives(IDocument document)
{
var root = document.GetSyntaxRoot();
var semanticModel = document.GetSemanticModel();
// An IDocument can refer to both a CSharp as well as a VisualBasic source file.
// Therefore we need to distinguish those cases and provide appropriate casts.
// Since the question was tagged c# only the CSharp way is provided.
switch (document.LanguageServices.Language)
{
case LanguageNames.CSharp:
var oldUsings = ((CompilationUnitSyntax)root).Usings;
var unusedUsings = ((SemanticModel)semanticModel).GetUnusedImportDirectives();
var newUsings = Syntax.List(oldUsings.Where(item => !unusedUsings.Contains(item)));
root = ((CompilationUnitSyntax)root).WithUsings(newUsings);
document = document.UpdateSyntaxRoot(root);
break;
case LanguageNames.VisualBasic:
// TODO
break;
}
return document;
}
我使用以下扩展方法对 usings 进行排序
internal static SyntaxList<UsingDirectiveSyntax> Sort(this SyntaxList<UsingDirectiveSyntax> usingDirectives, bool placeSystemNamespaceFirst = false) =>
SyntaxFactory.List(
usingDirectives
.OrderBy(x => x.StaticKeyword.IsKind(SyntaxKind.StaticKeyword) ? 1 : x.Alias == null ? 0 : 2)
.ThenBy(x => x.Alias?.ToString())
.ThenByDescending(x => placeSystemNamespaceFirst && x.Name.ToString().StartsWith(nameof(System) + "."))
.ThenBy(x => x.Name.ToString()));
和
compilationUnit = compilationUnit.WithUsings(SortUsings(compilationUnit.Usings))
查看 Roslyn 附带的 OrganizeSolution 示例项目。它做的事情类似于你想要的。它执行排序部分。您还必须使用 Jeff 展示的 SemanticModel 来确定源中是否没有对特定命名空间的引用。
For removing statements, check out the FAQ item 30 in the solution in the following directory, in the FAQ.cs file: (Note this is for the June 2012 CTP version of Roslyn).
%userprofile%\Documents\Microsoft Roslyn CTP - June 2012\CSharp\APISampleUnitTestsCS
There is also a FAQ which refers to this project:
http://www.codeplex.com/Download?ProjectName=dlr&DownloadId=386858
Here is the sample rewriter from the sample code, which removes assignment statements.
// Below SyntaxRewriter removes multiple assignement statements from under the
// SyntaxNode being visited.
public class AssignmentStatementRemover : SyntaxRewriter
{
public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax node)
{
SyntaxNode updatedNode = base.VisitExpressionStatement(node);
if (node.Expression.Kind == SyntaxKind.AssignExpression)
{
if (node.Parent.Kind == SyntaxKind.Block)
{
// There is a parent block so it is ok to remove the statement completely.
updatedNode = null;
}
else
{
// The parent context is some statement like an if statement without a block.
// Return an empty statement.
updatedNode = Syntax.EmptyStatement()
.WithLeadingTrivia(updatedNode.GetLeadingTrivia())
.WithTrailingTrivia(updatedNode.GetTrailingTrivia());
}
}
return updatedNode;
}
}