private async Task <IReadOnlyList <CompletionItem> > GetCompletionItems(string testCode)
        {
            var index = testCode.IndexOf("$$");

            testCode = testCode.Remove(index, 2);

            var workspace = new TestWorkspace();

            var document = workspace.OpenDocument(
                DocumentId.CreateNewId(),
                SourceText.From(testCode),
                LanguageNames.Hlsl);

            var completionProvider = new SymbolCompletionProvider();

            var completionContext = new CompletionContext(
                completionProvider,
                document.LogicalDocuments[0],
                index,
                new TextSpan(),
                new CompletionTrigger(CompletionTriggerKind.Invoke),
                await document.GetOptionsAsync(),
                CancellationToken.None);

            await completionProvider.ProvideCompletionsAsync(completionContext);

            return(completionContext.Items);
        }
        public async Task CanCreateLogicalDocuments(string shader, int logicalDocumentsCount)
        {
            var workspace = new TestWorkspace();

            var testFile = Path.GetFullPath(Path.Combine("TestAssets", shader));

            var document = workspace.OpenDocument(
                DocumentId.CreateNewId(),
                SourceText.From(File.ReadAllText(testFile), testFile),
                LanguageNames.ShaderLab);

            Assert.Equal(logicalDocumentsCount, document.LogicalDocuments.Length);

            foreach (var logicalDocument in document.LogicalDocuments)
            {
                var syntaxTree = await logicalDocument.GetSyntaxTreeAsync(CancellationToken.None);

                var syntaxTreeDiagnostics = syntaxTree.GetDiagnostics();
                Assert.Empty(syntaxTreeDiagnostics);

                if (logicalDocument.SupportsSemanticModel)
                {
                    var semanticModel = await logicalDocument.GetSemanticModelAsync(CancellationToken.None);

                    var semanticModelDiagnostics = semanticModel.GetDiagnostics();
                    Assert.Empty(semanticModelDiagnostics.Where(x => x.Severity == CodeAnalysis.Diagnostics.DiagnosticSeverity.Error));
                }
            }
        }
        public async Task CanGetTargets()
        {
            // Arrange.
            const string sourceCode = @"
void Func1(int a, bool b, float c = 1.0);

int Func2();

struct Struct1 {
    float StructField1;
};

class Class1 {};

float Variable1, Variable2;";
            var          workspace  = new TestWorkspace();
            var          document   = workspace.OpenDocument(DocumentId.CreateNewId(), new Text.SourceFile(SourceText.From(sourceCode)), LanguageNames.Hlsl);
            var          service    = new HlslNavigationBarItemService();

            // Act.
            var targets = await service.GetItemsAsync(document, CancellationToken.None);

            // Assert.
            Assert.Equal(3, targets.Count);

            Assert.Equal("(Global Scope)", targets[0].Text);
            Assert.Equal(Glyph.Namespace, targets[0].Glyph);

            Assert.Equal(4, targets[0].ChildItems.Count);
            Assert.Equal("Func1(int a, bool b, float c = 1.0)", targets[0].ChildItems[0].Text);
            Assert.Equal(Glyph.Method, targets[0].ChildItems[0].Glyph);
            Assert.Equal("Func2()", targets[0].ChildItems[1].Text);
            Assert.Equal(Glyph.Method, targets[0].ChildItems[1].Glyph);
            Assert.Equal("Variable1", targets[0].ChildItems[2].Text);
            Assert.Equal(Glyph.Local, targets[0].ChildItems[2].Glyph);
            Assert.Equal("Variable2", targets[0].ChildItems[3].Text);
            Assert.Equal(Glyph.Local, targets[0].ChildItems[3].Glyph);

            Assert.Equal("Class1", targets[1].Text);
            Assert.Equal(Glyph.Class, targets[1].Glyph);

            Assert.Equal(0, targets[1].ChildItems.Count);

            Assert.Equal("Struct1", targets[2].Text);
            Assert.Equal(Glyph.Structure, targets[2].Glyph);

            Assert.Equal(1, targets[2].ChildItems.Count);
            Assert.Equal("StructField1", targets[2].ChildItems[0].Text);
            Assert.Equal(Glyph.Field, targets[2].ChildItems[0].Glyph);
        }
Exemple #4
0
        public void Document_InvocationReasons()
        {
            using (var workspace = new TestWorkspace(TestExportProvider.CreateExportProviderWithCSharpAndVisualBasic(), SolutionCrawler))
            {
                var solution = GetInitialSolutionInfo(workspace);
                workspace.OnSolutionAdded(solution);
                WaitWaiter(workspace.ExportProvider);

                var id = workspace.CurrentSolution.Projects.First().DocumentIds[0];

                var analyzer   = new Analyzer(blockedRun: true);
                var lazyWorker = new Lazy <IIncrementalAnalyzerProvider, IncrementalAnalyzerProviderMetadata>(() => new AnalyzerProvider(analyzer), Metadata.Crawler);
                var service    = new SolutionCrawlerRegistrationService(new[] { lazyWorker }, GetListeners(workspace.ExportProvider));

                service.Register(workspace);

                // first invocation will block worker
                workspace.ChangeDocument(id, SourceText.From("//"));
                analyzer.RunningEvent.Wait();

                var openReady  = new ManualResetEventSlim(initialState: false);
                var closeReady = new ManualResetEventSlim(initialState: false);

                workspace.DocumentOpened += (o, e) => openReady.Set();
                workspace.DocumentClosed += (o, e) => closeReady.Set();

                // cause several different request to queue up
                workspace.ChangeDocument(id, SourceText.From("// "));
                workspace.OpenDocument(id);
                workspace.CloseDocument(id);

                openReady.Set();
                closeReady.Set();
                analyzer.BlockEvent.Set();

                Wait(service, workspace);

                service.Unregister(workspace);

                Assert.Equal(1, analyzer.SyntaxDocumentIds.Count);
                Assert.Equal(5, analyzer.DocumentIds.Count);
            }
        }