Skip to content

Instantly share code, notes, and snippets.

@natemcmaster
Created July 23, 2014 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natemcmaster/b44d8b0ef2dd4d363036 to your computer and use it in GitHub Desktop.
Save natemcmaster/b44d8b0ef2dd4d363036 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Recommendations;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var workspace = new CustomWorkspace();
var proj = workspace.AddProject("Program", LanguageNames.CSharp);
var documentText = @"
using System;
namespace Test{
class Program{
static void Main(string[] args)
{
var name = ""hwllo eorld"";
}
}
}
";
var doc = workspace.AddDocument(proj, "Program.cs", documentText);
SyntaxTree tree = CSharpSyntaxTree.ParseText(documentText);
var root = (CompilationUnitSyntax)tree.GetRoot();
var compilation = CSharpCompilation.Create("HelloWorld")
.AddReferences(
new MetadataFileReference(
typeof(object).Assembly.Location))
.AddSyntaxTrees(tree);
var model = compilation.GetSemanticModel(tree);
var rec =Recommender.GetRecommendedSymbolsAtPosition(model, documentText.IndexOf("eorld")+8, workspace);
foreach (var symbol in rec)
{
var parts = symbol.ToMinimalDisplayParts(model, documentText.IndexOf("eorld") + 8);
Console.WriteLine();
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment