Repository navigation
Add ImportAdder API #1063
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
Add ImportAdder API #1063
Changes from 2 commits
33e7b59
00da542
c1c7c0d
f2bd6df
8a23bc0
58493b7
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,88 @@ | ||
| // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Composition; | ||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.CSharp.Utilities; | ||
| using Microsoft.CodeAnalysis.Editing; | ||
| using Microsoft.CodeAnalysis.Host.Mef; | ||
| using Microsoft.CodeAnalysis.Options; | ||
| using Microsoft.CodeAnalysis.Text; | ||
| using Roslyn.Utilities; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.Editing | ||
| { | ||
| [ExportLanguageService(typeof(ImportAdder), LanguageNames.CSharp), Shared] | ||
| internal class CSharpImportAdder : ImportAdder.ImportAdderBase | ||
| { | ||
| public CSharpImportAdder() | ||
|
Member
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. Delete? 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. The constructor looks redundant. The compiler generates the same by default. |
||
| { | ||
| } | ||
|
|
||
| protected override INamespaceSymbol GetImportedNamespaceSymbol(SyntaxNode import, SemanticModel model) | ||
| { | ||
| var @using = import as UsingDirectiveSyntax; | ||
| if (@using != null && @using.Alias == null) | ||
| { | ||
| return model.GetSymbolInfo(@using.Name).Symbol as INamespaceSymbol; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| protected override INamespaceSymbol GetExplicitNamespaceSymbol(SyntaxNode node, SemanticModel model) | ||
| { | ||
| var name = node as QualifiedNameSyntax; | ||
| if (name != null) | ||
| { | ||
| return GetExplicitNamespaceSymbol(name, name.Left, model); | ||
| } | ||
|
|
||
| var memberAccess = node as MemberAccessExpressionSyntax; | ||
| if (memberAccess != null) | ||
| { | ||
| return GetExplicitNamespaceSymbol(memberAccess, memberAccess.Expression, model); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private INamespaceSymbol GetExplicitNamespaceSymbol(ExpressionSyntax fullName, ExpressionSyntax namespacePart, SemanticModel model) | ||
| { | ||
| // name must refer to something that is not a namespace, but be qualified with a namespace. | ||
| var symbol = model.GetSymbolInfo(fullName).Symbol; | ||
| var nsSymbol = model.GetSymbolInfo(namespacePart).Symbol as INamespaceSymbol; | ||
| if (symbol != null && symbol.Kind != SymbolKind.Namespace && nsSymbol != null) | ||
| { | ||
| // use the symbols containing namespace, and not the potentially less than fully qualified namespace in the full name expression. | ||
| var ns = symbol.ContainingNamespace; | ||
| if (ns != null) | ||
| { | ||
| return model.Compilation.GetCompilationNamespace(ns); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| protected override SyntaxNode InsertNamespaceImport(SyntaxNode root, SyntaxGenerator gen, SyntaxNode import, OptionSet options) | ||
| { | ||
| var comparer = options.GetOption(GenerationOptions.PlaceSystemNamespaceFirst, LanguageNames.CSharp) | ||
| ? UsingsAndExternAliasesDirectiveComparer.SystemFirstInstance | ||
| : UsingsAndExternAliasesDirectiveComparer.NormalInstance; | ||
|
|
||
| // find insertion point | ||
| foreach (var existingImport in gen.GetNamespaceImports(root)) | ||
| { | ||
| if (comparer.Compare(import, existingImport) < 0) | ||
| { | ||
| return gen.InsertNodesBefore(root, existingImport, new[] { import }); | ||
| } | ||
| } | ||
|
|
||
| return gen.AddNamespaceImports(root, import); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,16 @@ | |
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
| using System.Composition; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.CSharp.Utilities; | ||
| using Microsoft.CodeAnalysis.Editing; | ||
| using Microsoft.CodeAnalysis.Formatting; | ||
| using Microsoft.CodeAnalysis.Host.Mef; | ||
| using Microsoft.CodeAnalysis.Internal.Log; | ||
| using Microsoft.CodeAnalysis.Simplification; | ||
| using Microsoft.CodeAnalysis.Text; | ||
| using Roslyn.Utilities; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.Simplification | ||
|
|
@@ -161,5 +164,24 @@ protected override bool CanNodeBeSimplifiedWithoutSpeculation(SyntaxNode node) | |
| { | ||
| return false; | ||
| } | ||
|
|
||
| protected override void GetUnusedNamespaceImports(SemanticModel model, HashSet<SyntaxNode> namespaceImports, CancellationToken cancellationToken) | ||
| { | ||
| var root = model.SyntaxTree.GetRoot(); | ||
| var diagnostics = model.GetDiagnostics(cancellationToken: cancellationToken); | ||
|
|
||
| foreach (var diagnostic in diagnostics) | ||
| { | ||
| if (diagnostic.Id == "CS8019") | ||
|
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. A comment explaining what the diagnostics with this Id is would be useful here. |
||
| { | ||
| var node = root.FindNode(diagnostic.Location.SourceSpan) as UsingDirectiveSyntax; | ||
|
|
||
| if (node != null) | ||
| { | ||
| namespaceImports.Add(node); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Consider swapping
using Microsoft.CodeAnalysis.Simplification;andusing Microsoft.CodeAnalysis.Shared.Extensions;to preserve lexical ordering.