public static Document FindDocumentInProjectContext(this ImmutableArray <Document> documents, TextDocumentIdentifier documentIdentifier) { if (documents.Length > 1) { // We have more than one document; try to find the one that matches the right context if (documentIdentifier is VSTextDocumentIdentifier vsDocumentIdentifier && vsDocumentIdentifier.ProjectContext != null) { var projectId = ProtocolConversions.ProjectContextToProjectId(vsDocumentIdentifier.ProjectContext); var matchingDocument = documents.FirstOrDefault(d => d.Project.Id == projectId); if (matchingDocument != null) { return(matchingDocument); } } else { // We were not passed a project context. This can happen when the LSP powered NavBar is not enabled. // This branch should be removed when we're using the LSP based navbar in all scenarios. var solution = documents.First().Project.Solution; // Lookup which of the linked documents is currently active in the workspace. var documentIdInCurrentContext = solution.Workspace.GetDocumentIdInCurrentContext(documents.First().Id); return(solution.GetRequiredDocument(documentIdInCurrentContext)); } }
public static Document?GetDocument(this Solution solution, TextDocumentIdentifier documentIdentifier, string?clientName = null) { var documents = solution.GetDocuments(documentIdentifier.Uri, clientName); if (documents.Length == 0) { return(null); } if (documents.Length > 1) { // We have more than one document; try to find the one that matches the right context if (documentIdentifier is VSTextDocumentIdentifier vsDocumentIdentifier) { if (vsDocumentIdentifier.ProjectContext != null) { var projectId = ProtocolConversions.ProjectContextToProjectId(vsDocumentIdentifier.ProjectContext); var matchingDocument = documents.FirstOrDefault(d => d.Project.Id == projectId); if (matchingDocument != null) { return(matchingDocument); } } } } // We either have only one document or have multiple, but none of them matched our context. In the // latter case, we'll just return the first one arbitrarily since this might just be some temporary mis-sync // of client and server state. return(documents[0]); }
public static T FindDocumentInProjectContext <T>(this ImmutableArray <T> documents, TextDocumentIdentifier documentIdentifier) where T : TextDocument { if (documents.Length > 1) { // We have more than one document; try to find the one that matches the right context if (documentIdentifier is VSTextDocumentIdentifier vsDocumentIdentifier) { if (vsDocumentIdentifier.ProjectContext != null) { var projectId = ProtocolConversions.ProjectContextToProjectId(vsDocumentIdentifier.ProjectContext); var matchingDocument = documents.FirstOrDefault(d => d.Project.Id == projectId); if (matchingDocument != null) { return(matchingDocument); } } } } // We either have only one document or have multiple, but none of them matched our context. In the // latter case, we'll just return the first one arbitrarily since this might just be some temporary mis-sync // of client and server state. return(documents[0]); }