Beispiel #1
0
        public void AllClassFields(OmniSharpWorkspace ws, string sourceFile)
        {
            var doc   = ws.GetDocument(sourceFile);
            var tree  = doc.GetSyntaxTreeAsync().Result;
            var model = doc.GetSemanticModelAsync().Result;

            foreach (var c in tree.GetRoot().DescendantNodes().OfType <ClassDeclarationSyntax>())
            {
                foreach (var f in c.DescendantNodes().OfType <FieldDeclarationSyntax>())
                {
                    foreach (var v in f.Declaration.Variables)
                    {
                        var fieldSymbol = model.GetDeclaredSymbol(v);

                        foreach (var l in fieldSymbol.Locations)
                        {
                            FindSymbolAtOffset(ws, sourceFile, l.SourceSpan.Start);
                        }

                        // Console.WriteLine($"{v.SourceSpan.Start} {v.ToString()}");
                        // Console.WriteLine($"{v.Identifier.SpanStart} - {v.Identifier.Text}");
                    }
                }
            }
        }
Beispiel #2
0
        public void AllLocalVariables(OmniSharpWorkspace ws, string sourceFile)
        {
            var doc   = ws.GetDocument(sourceFile);
            var tree  = doc.GetSyntaxTreeAsync().Result;
            var model = doc.GetSemanticModelAsync().Result;

            foreach (var method in tree.GetRoot().DescendantNodes().OfType <MethodDeclarationSyntax>())
            {
                var methodBody = method.Body;
                var result     = model.AnalyzeDataFlow(methodBody);

                var variableDeclarationAndUsages = result.VariablesDeclared.
                                                   Union(result.ReadInside).
                                                   Union(result.ReadOutside);

                foreach (var v in variableDeclarationAndUsages)
                {
                    foreach (var l in v.Locations)
                    {
                        // Console.WriteLine($"{l.SourceSpan.Start} {v.ToString()}");
                        FindSymbolAtOffset(ws, sourceFile, l.SourceSpan.Start);
                    }
                }
            }
        }
Beispiel #3
0
        public Symbol FindSymbolAtOffset(OmniSharpWorkspace ws, string path, int offset)
        {
            var doc   = ws.GetDocument(path);
            var model = doc.GetSemanticModelAsync().Result;

            var symbol = Microsoft.CodeAnalysis.FindSymbols.SymbolFinder.FindSymbolAtPositionAsync(model, offset, ws).Result;

            ITypeSymbol type;

            // Must be a bette way
            if (symbol is ILocalSymbol)
            {
                type = ((ILocalSymbol)symbol).Type;
            }
            else if (symbol is IParameterSymbol)
            {
                type = ((IParameterSymbol)symbol).Type;
            }
            else if (symbol is ITypeSymbol)
            {
                type = symbol as ITypeSymbol;
            }
            else
            {
                type = symbol.ContainingType;
                // throw new Exception($"Unsupported symbol at offset {offset} [{symbol?.Name}] {symbol.GetType()}");
            }

            var needCompletions = type;

            var typeName = $"{needCompletions.Name}";

            if (String.IsNullOrWhiteSpace(typeName))
            {
                needCompletions = needCompletions.BaseType as ITypeSymbol;
                typeName        = needCompletions.Name;
            }

            Console.WriteLine($"{symbol.Name}\t{path.Replace(Directory.GetCurrentDirectory(), "").Replace("/", "")}\t:normal {offset}go ;\"");
            // App.Log($"Looking Up: {typeName} {needCompletions.ContainingAssembly.ToDisplayString()} {offset} [{symbol.Name}]");

            return(new Symbol()
            {
                SymbolName = symbol.Name,
                AssemblyName = new AssemblyName(needCompletions.ContainingAssembly.ToDisplayString()),
                TypeName = typeName,
            });
        }
Beispiel #4
0
        public void AllClasses(OmniSharpWorkspace ws, string sourceFile)
        {
            var doc   = ws.GetDocument(sourceFile);
            var tree  = doc.GetSyntaxTreeAsync().Result;
            var model = doc.GetSemanticModelAsync().Result;

            foreach (var c in tree.GetRoot().DescendantNodes().OfType <ClassDeclarationSyntax>())
            {
                // Console.WriteLine(c.Identifier.Text);
                var fieldSymbol = model.GetDeclaredSymbol(c);

                foreach (var l in fieldSymbol.Locations)
                {
                    // Console.WriteLine(l);
                    FindSymbolAtOffset(ws, sourceFile, l.SourceSpan.Start);
                }
            }
        }