using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Text;
using var workspace = new AdhocWorkspace(MefHostServices.DefaultHost);
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, nullableContextOptions: NullableContextOptions.Annotations)
.WithUsings(new[]{ "System" });
var projectInfo = ProjectInfo.Create(
.WithMetadataReferences([MetadataReference.CreateFromFile(typeof(object).Assembly.Location)])
.WithCompilationOptions(compilationOptions)
var project = workspace.AddProject(projectInfo);
var combinedCode = "Guid.";
var documentInfo = DocumentInfo.Create(
DocumentId.CreateNewId(project.Id), "__Script.cs",
sourceCodeKind: SourceCodeKind.Script,
loader: TextLoader.From(TextAndVersion.Create(SourceText.From(combinedCode), VersionStamp.Default)));
var document = workspace.AddDocument(documentInfo);
var completionService = CompletionService.GetService(document);
if (completionService is null)
Console.WriteLine("CompletionService is null");
var completions = await completionService.GetCompletionsAsync(document, combinedCode.Length);
foreach (var completion in completions.ItemsList)
Console.WriteLine(completion.DisplayText);