public static IEnumerable<SyntaxNode> GetUnnecessaryImports(SemanticModel semanticModel, SyntaxNode root, CancellationToken cancellationToken)
		{
			var diagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken);
			if (!diagnostics.Any())
			{
				return null;
			}

			var unnecessaryImports = new HashSet<UsingDirectiveSyntax>();

			foreach (var diagnostic in diagnostics)
			{
				if (diagnostic.Id == "CS8019")
				{
					var node = root.FindNode(diagnostic.Location.SourceSpan) as UsingDirectiveSyntax;

					if (node != null)
					{
						unnecessaryImports.Add(node);
					}
				}
			}

			if (cancellationToken.IsCancellationRequested || !unnecessaryImports.Any())
			{
				return null;
			}

			return unnecessaryImports;
		}
        private static HashSet<SyntaxNode> GetUnusedImportDirectives(SemanticModel model, CancellationToken cancellationToken)
        {
            HashSet<SyntaxNode> unusedImportDirectives = new HashSet<SyntaxNode>();
            SyntaxNode root = model.SyntaxTree.GetRoot(cancellationToken);
            foreach (Diagnostic diagnostic in model.GetDiagnostics(null, cancellationToken).Where(d => d.Id == "CS8019" || d.Id == "CS0105"))
            {
                UsingDirectiveSyntax usingDirectiveSyntax = root.FindNode(diagnostic.Location.SourceSpan, false, false) as UsingDirectiveSyntax;
                if (usingDirectiveSyntax != null)
                {
                    unusedImportDirectives.Add(usingDirectiveSyntax);
                }
            }

            return unusedImportDirectives;
        }
Esempio n. 3
0
 public Visitor(SemanticModel model)
     : base(SyntaxWalkerDepth.Trivia)
 {
     _model = model;
     _modelDiagnostic = _model.GetDiagnostics();
 }
Esempio n. 4
0
        private async Task<ImmutableArray<Diagnostic>> GetPointDiagnostics(TextSpan location, List<string> diagnosticIds)
        {
            _document = _workspace.GetDocument(_document.FilePath);
            _semanticModel = await _document.GetSemanticModelAsync();
            var diagnostics = _semanticModel.GetDiagnostics();

            //Restrict diagnostics only to missing usings
            return diagnostics.Where(d => d.Location.SourceSpan.Contains(location) &&
                    diagnosticIds.Contains(d.Id)).ToImmutableArray();
        }
 private static bool HasErrors(SemanticModel semanticModel, SyntaxNode node)
 {
     var diags = semanticModel.GetDiagnostics(node.Span);
     if (diags.Any(d => d.Id.StartsWith("CS"))) return true;
     return false;
 }
        private void GetSemantic(string code, out SemanticModel model, out int pos)
        {
            var tree = CSharpSyntaxTree.ParseText(code);

            //var writer = new ConsoleDumpWalker();
            //writer.Visit(tree.GetRoot());

            var refs = AssemblyUtility.AllReferencedAssemblyLocations
                .Distinct()
                .Select(x => MetadataReference.CreateFromFile(x));

            var compilation = CSharpCompilation.Create(
                "MyCompilation",
                syntaxTrees: new[] { tree },
                references: refs);
            model = compilation.GetSemanticModel(tree);

            var diags = model.GetDiagnostics();
            foreach (var diag in diags)
            {
                if (diag.Id == "CS8019") continue;
                Console.WriteLine(diag);
                Assert.Fail();
            }

            var ns = tree.GetRoot().DescendantNodes().OfType<NamespaceDeclarationSyntax>().First();
            pos = ns.OpenBraceToken.SpanStart;

            // if we use that as a "container" then we get what's in the container *only*
            // not what we want here, then we have to use the position to determine *scope*
            //var ss = model.GetDeclaredSymbol(ns);
        }