public string GetCallTipTextAtPosition(string code, int position) { SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code); var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); var compilation = CSharpCompilation.Create("output", options: options) .AddSyntaxTrees(syntaxTree) .AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(MongoSharpTextWriter).Assembly.Location), MetadataReference.CreateFromFile(typeof(IEnumerable <int>).Assembly.Location), MetadataReference.CreateFromFile(typeof(IQueryable).Assembly.Location), MetadataReference.CreateFromFile(typeof(MongoDB.Bson.BsonDocument).Assembly.Location), MetadataReference.CreateFromFile(typeof(MongoDB.Driver.MongoCollection).Assembly.Location), MetadataReference.CreateFromFile(typeof(MongoDB.Driver.MongoClient).Assembly.Location) ); var semanticModel = compilation.GetSemanticModel(syntaxTree); SyntaxToken token = syntaxTree.GetRoot().FindToken(position); SyntaxNode identifier = token.Parent; SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(identifier); TypeInfo typeInfo = semanticModel.GetTypeInfo(identifier); ITypeSymbol typeSymbol = typeInfo.Type; if (typeSymbol == null && symbolInfo.Symbol != null && symbolInfo.Symbol is ITypeSymbol) { typeSymbol = (ITypeSymbol)symbolInfo.Symbol.OriginalDefinition; } var err = (from e in semanticModel.GetDiagnostics() where (e.Severity == DiagnosticSeverity.Error || e.Severity == DiagnosticSeverity.Info) && position >= e.Location.SourceSpan.Start && position <= (e.Location.SourceSpan.Start + e.Location.SourceSpan.Length) select e.ToString()).ToList(); if (err.Any()) { return(err[0]); } if (typeSymbol != null) { string xmlComments = new XmlCommentsHelper().GetTypeComments(typeSymbol); string symbolName = symbolInfo.Symbol == null ? "" : symbolInfo.Symbol.Name + " "; string callTip = typeSymbol.ToString() + " " + symbolName + Environment.NewLine + " " + xmlComments; callTip = callTip.TrimEnd(new[] { ' ', '\t', '\n' }); return(Environment.NewLine + " " + callTip + " " + Environment.NewLine); } else if (symbolInfo.Symbol is IMethodSymbol) { var method = ((IMethodSymbol)symbolInfo.Symbol); string xmlComments = new XmlCommentsHelper().GetMethodComments(method); string callTip = method.ReturnType.Name + " " + symbolInfo.Symbol.ToDisplayString() + Environment.NewLine + " " + xmlComments; callTip = callTip.TrimEnd(new[] { ' ', '\t', '\n' }); return(Environment.NewLine + " " + callTip + " " + Environment.NewLine); } return(null); }
public string GetCallTipTextAtPosition(string code, int position) { var syntaxTree = CSharpSyntaxTree.ParseText(code); var semanticModel = GenerateSemanticModel(syntaxTree); SyntaxToken token = syntaxTree.GetRoot().FindToken(position); SyntaxNode identifier = token.Parent; SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(identifier); TypeInfo typeInfo = semanticModel.GetTypeInfo(identifier); ITypeSymbol typeSymbol = typeInfo.Type; if (typeSymbol == null && symbolInfo.Symbol != null && symbolInfo.Symbol is ITypeSymbol) { typeSymbol = (ITypeSymbol)symbolInfo.Symbol.OriginalDefinition; } var err = (from e in semanticModel.GetDiagnostics() where (e.Severity == DiagnosticSeverity.Error || e.Severity == DiagnosticSeverity.Info) && position >= e.Location.SourceSpan.Start && position <= (e.Location.SourceSpan.Start + e.Location.SourceSpan.Length) select e.ToString()).ToList(); if (err.Any()) { return(err[0]); } if (typeSymbol != null) { string xmlComments = new XmlCommentsHelper().GetTypeComments(typeSymbol); string symbolName = symbolInfo.Symbol == null ? "" : symbolInfo.Symbol.Name + " "; string callTip = typeSymbol.ToString() + " " + symbolName + Environment.NewLine + " " + xmlComments; callTip = callTip.TrimEnd(new[] { ' ', '\t', '\n' }); return(Environment.NewLine + " " + callTip + " " + Environment.NewLine); } else if (symbolInfo.Symbol is IMethodSymbol) { var method = ((IMethodSymbol)symbolInfo.Symbol); string xmlComments = new XmlCommentsHelper().GetMethodComments(method); string callTip = method.ReturnType.Name + " " + symbolInfo.Symbol.ToDisplayString() + Environment.NewLine + " " + xmlComments; callTip = callTip.TrimEnd(new[] { ' ', '\t', '\n' }); return(Environment.NewLine + " " + callTip + " " + Environment.NewLine); } return(null); }