示例#1
0
        public async Task SingleDocumentReturnsSingleContext()
        {
            var workspaceXml =
                @"<Workspace>
    <Project Language=""C#"" CommonReferences=""true"" AssemblyName=""CSProj"">
        <Document FilePath = ""C:\C.cs"">{|caret:|}</Document>
    </Project>
</Workspace>";

            using var testLspServer = CreateXmlTestLspServer(workspaceXml, out var locations);
            var documentUri = locations["caret"].Single().Uri;
            var result      = await RunGetProjectContext(testLspServer, documentUri);

            Assert.NotNull(result);
            Assert.Equal(0, result !.DefaultIndex);
            var context = Assert.Single(result.ProjectContexts);

            Assert.Equal(
                ProtocolConversions.ProjectIdToProjectContextId(
                    testLspServer.GetCurrentSolution().ProjectIds.Single()
                    ),
                context.Id
                );
            Assert.Equal(LSP.ProjectContextKind.CSharp, context.Kind);
            Assert.Equal("CSProj", context.Label);
        }
示例#2
0
        public async Task SwitchingContextsChangesDefaultContext()
        {
            var workspaceXml =
                @"<Workspace>
    <Project Language=""C#"" CommonReferences=""true"" AssemblyName=""CSProj1"">
        <Document FilePath=""C:\C.cs"">{|caret:|}</Document>
    </Project>
    <Project Language=""C#"" CommonReferences=""true"" AssemblyName=""CSProj2"">
        <Document IsLinkFile=""true"" LinkFilePath=""C:\C.cs"" LinkAssemblyName=""CSProj1""></Document>
    </Project>
</Workspace>";

            using var testLspServer = CreateXmlTestLspServer(workspaceXml, out var locations);

            // Ensure the documents are open so we can change contexts
            foreach (var document in testLspServer.TestWorkspace.Documents)
            {
                _ = document.GetOpenTextContainer();
            }

            var documentUri = locations["caret"].Single().Uri;

            foreach (var project in testLspServer.GetCurrentSolution().Projects)
            {
                testLspServer.TestWorkspace.SetDocumentContext(project.DocumentIds.Single());
                var result = await RunGetProjectContext(testLspServer, documentUri);

                Assert.Equal(
                    ProtocolConversions.ProjectIdToProjectContextId(project.Id),
                    result !.ProjectContexts[result.DefaultIndex].Id
                    );
                Assert.Equal(project.Name, result !.ProjectContexts[result.DefaultIndex].Label);
            }
        }
示例#3
0
        public override Task <ActiveProjectContexts?> HandleRequestAsync(
            GetTextDocumentWithContextParams request,
            ClientCapabilities clientCapabilities,
            string?clientName,
            CancellationToken cancellationToken)
        {
            var documents = SolutionProvider.GetDocuments(request.TextDocument.Uri, clientName);

            if (!documents.Any())
            {
                return(Task.FromResult <ActiveProjectContexts?>(null));
            }

            var contexts = new List <ProjectContext>();

            foreach (var document in documents)
            {
                var project = document.Project;
                var context = new ProjectContext
                {
                    Id    = ProtocolConversions.ProjectIdToProjectContextId(project.Id),
                    Label = project.Name
                };

                if (project.Language == LanguageNames.CSharp)
                {
                    context.Kind = ProjectContextKind.CSharp;
                }
                else if (project.Language == LanguageNames.VisualBasic)
                {
                    context.Kind = ProjectContextKind.VisualBasic;
                }

                contexts.Add(context);
            }

            // If the document is open, it doesn't matter which DocumentId we pass to GetDocumentIdInCurrentContext since
            // all the documents are linked at that point, so we can just pass the first arbitrarily. If the document is closed
            // GetDocumentIdInCurrentContext will just return the same ID back, which means we're going to pick the first
            // ID in GetDocumentIdsWithFilePath, but there's really nothing we can do since we don't have contexts for
            // close documents anyways.
            var openDocument             = documents.First();
            var currentContextDocumentId = openDocument.Project.Solution.Workspace.GetDocumentIdInCurrentContext(openDocument.Id);

            return(Task.FromResult <ActiveProjectContexts?>(new ActiveProjectContexts
            {
                ProjectContexts = contexts.ToArray(),
                DefaultIndex = documents.IndexOf(d => d.Id == currentContextDocumentId)
            }));
        }
        public override Task <VSProjectContextList?> HandleRequestAsync(VSGetProjectContextsParams request, RequestContext context, CancellationToken cancellationToken)
        {
            Contract.ThrowIfNull(context.Solution);

            // We specifically don't use context.Document here because we want multiple
            var documents = context.Solution.GetDocuments(request.TextDocument.Uri, context.ClientName);

            if (!documents.Any())
            {
                return(SpecializedTasks.Null <VSProjectContextList>());
            }

            var contexts = new List <VSProjectContext>();

            foreach (var document in documents)
            {
                var project        = document.Project;
                var projectContext = new VSProjectContext
                {
                    Id    = ProtocolConversions.ProjectIdToProjectContextId(project.Id),
                    Label = project.Name
                };

                if (project.Language == LanguageNames.CSharp)
                {
                    projectContext.Kind = VSProjectKind.CSharp;
                }
                else if (project.Language == LanguageNames.VisualBasic)
                {
                    projectContext.Kind = VSProjectKind.VisualBasic;
                }

                contexts.Add(projectContext);
            }

            // If the document is open, it doesn't matter which DocumentId we pass to GetDocumentIdInCurrentContext since
            // all the documents are linked at that point, so we can just pass the first arbitrarily. If the document is closed
            // GetDocumentIdInCurrentContext will just return the same ID back, which means we're going to pick the first
            // ID in GetDocumentIdsWithFilePath, but there's really nothing we can do since we don't have contexts for
            // close documents anyways.
            var openDocument             = documents.First();
            var currentContextDocumentId = openDocument.Project.Solution.Workspace.GetDocumentIdInCurrentContext(openDocument.Id);

            return(Task.FromResult <VSProjectContextList?>(new VSProjectContextList
            {
                ProjectContexts = contexts.ToArray(),
                DefaultIndex = documents.IndexOf(d => d.Id == currentContextDocumentId)
            }));
        }
示例#5
0
        protected static LSP.TextDocumentIdentifier CreateTextDocumentIdentifier(Uri uri, ProjectId?projectContext = null)
        {
            var documentIdentifier = new LSP.VSTextDocumentIdentifier {
                Uri = uri
            };

            if (projectContext != null)
            {
                documentIdentifier.ProjectContext =
                    new LSP.ProjectContext {
                    Id = ProtocolConversions.ProjectIdToProjectContextId(projectContext)
                };
            }

            return(documentIdentifier);
        }
        protected static LSP.TextDocumentIdentifier CreateTextDocumentIdentifier(Uri uri, ProjectId?projectContext = null)
        {
            var documentIdentifier = new LSP.VSTextDocumentIdentifier {
                Uri = uri
            };

            if (projectContext != null)
            {
                documentIdentifier.ProjectContext =
                    new LSP.VSProjectContext {
                    Id = ProtocolConversions.ProjectIdToProjectContextId(projectContext), Label = projectContext.DebugName !, Kind = LSP.VSProjectKind.CSharp
                };
            }

            return(documentIdentifier);
        }
    public async Task TestExternalAccessTypeScriptHandlerInvoked()
    {
        var workspaceXml =
            @$ "<Workspace>
    <Project Language=" "TypeScript" " CommonReferences=" "true" " AssemblyName=" "TypeScriptProj" ">
        <Document FilePath=" "C:\T.ts" "></Document>
    </Project>
</Workspace>";

        using var testLspServer = await CreateTsTestLspServerAsync(workspaceXml);

        var document = testLspServer.GetCurrentSolution().Projects.Single().Documents.Single();
        var request  = new TSRequest(document.GetURI(), ProtocolConversions.ProjectIdToProjectContextId(document.Project.Id));

        var response = await testLspServer.ExecuteRequestAsync <TSRequest, int>(TypeScriptHandler.MethodName, request, CancellationToken.None);

        Assert.Equal(TypeScriptHandler.Response, response);
    }