protected async Task <LSP.TextEdit[]> GetTextEditsAsync(LSP.TextDocumentIdentifier documentIdentifier, LSP.FormattingOptions formattingOptions, RequestContext context, CancellationToken cancellationToken, LSP.Range?range = null) { using var _ = ArrayBuilder <LSP.TextEdit> .GetInstance(out var edits); var document = context.Document; var formattingService = document?.Project.LanguageServices.GetService <IXamlFormattingService>(); if (document != null && formattingService != null) { var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); TextSpan?textSpan = null; if (range != null) { textSpan = ProtocolConversions.RangeToTextSpan(range, text); } var options = new XamlFormattingOptions { InsertSpaces = formattingOptions.InsertSpaces, TabSize = formattingOptions.TabSize, OtherOptions = formattingOptions.OtherOptions }; var textChanges = await formattingService.GetFormattingChangesAsync(document, options, textSpan, cancellationToken).ConfigureAwait(false); edits.AddRange(textChanges.Select(change => ProtocolConversions.TextChangeToTextEdit(change, text))); } return(edits.ToArray()); }
public CodeActionResolveData(string uniqueIdentifier, ImmutableArray <string> customTags, LSP.Range range, LSP.TextDocumentIdentifier textDocument) { UniqueIdentifier = uniqueIdentifier; CustomTags = customTags; Range = range; TextDocument = textDocument; }
public async Task TestLooseFile_MovedToRegisteredWorkspace() { var source = @"class A { void M() { } }"; // Create the server and verify no misc files present. using var testLspServer = await CreateTestLspServerAsync(string.Empty); Assert.Null(GetMiscellaneousDocument(testLspServer)); // Open an empty loose file and make a request to verify it gets added to the misc workspace. var looseFileUri = new Uri(@"C:\SomeFile.cs"); var looseFileTextDocumentIdentifier = new LSP.TextDocumentIdentifier { Uri = looseFileUri }; await testLspServer.OpenDocumentAsync(looseFileUri, source).ConfigureAwait(false); // Verify that the file returned by the manager is in the lsp misc files workspace. await AssertFileInMiscWorkspaceAsync(testLspServer, looseFileUri).ConfigureAwait(false); // Update the workspace to contain the loose file. var project = testLspServer.GetCurrentSolution().Projects.Single(); var documentInfo = DocumentInfo.Create( DocumentId.CreateNewId(project.Id), looseFileUri.AbsolutePath, sourceCodeKind: SourceCodeKind.Regular, loader: new TestTextLoader(source), filePath: looseFileUri.AbsolutePath); testLspServer.TestWorkspace.OnDocumentAdded(documentInfo); await WaitForWorkspaceOperationsAsync(testLspServer.TestWorkspace); Assert.Contains(looseFileUri.AbsolutePath, testLspServer.GetCurrentSolution().Projects.Single().Documents.Select(d => d.FilePath)); // Verify that the manager returns the file that has been added to the main workspace. await AssertFileInMainWorkspaceAsync(testLspServer, looseFileUri).ConfigureAwait(false); }
protected async Task <LSP.TextEdit[]> GetTextEditsAsync(LSP.TextDocumentIdentifier documentIdentifier, RequestContext context, CancellationToken cancellationToken, LSP.Range?range = null) { var edits = new ArrayBuilder <LSP.TextEdit>(); var document = SolutionProvider.GetDocument(documentIdentifier, context.ClientName); if (document != null) { var formattingService = document.Project.LanguageServices.GetRequiredService <IEditorFormattingService>(); var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); TextSpan?textSpan = null; if (range != null) { textSpan = ProtocolConversions.RangeToTextSpan(range, text); } var textChanges = await GetFormattingChangesAsync(formattingService, document, textSpan, cancellationToken).ConfigureAwait(false); edits.AddRange(textChanges.Select(change => ProtocolConversions.TextChangeToTextEdit(change, text))); } return(edits.ToArrayAndFree()); }
public async Task <IEnumerable <CodeAction> > GetCodeActionsAsync(Solution solution, LSP.TextDocumentIdentifier documentIdentifier, LSP.Range selection, string?clientName, CancellationToken cancellationToken) { var document = solution.GetDocument(documentIdentifier, clientName); if (document == null) { return(ImmutableArray <CodeAction> .Empty); } var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); var textSpan = ProtocolConversions.RangeToTextSpan(selection, text); var codeFixCollections = await _codeFixService.GetFixesAsync(document, textSpan, true, cancellationToken).ConfigureAwait(false); var codeRefactorings = await _codeRefactoringService.GetRefactoringsAsync(document, textSpan, cancellationToken).ConfigureAwait(false); var codeActions = codeFixCollections.SelectMany(c => c.Fixes.Select(f => f.Action)).Concat( codeRefactorings.SelectMany(r => r.CodeActions.Select(ca => ca.action))); // Flatten out the nested codeactions. var nestedCodeActions = codeActions.Where(c => c is CodeAction.CodeActionWithNestedActions nc && nc.IsInlinable).SelectMany(nc => nc.NestedCodeActions); codeActions = codeActions.Where(c => !(c is CodeAction.CodeActionWithNestedActions)).Concat(nestedCodeActions); return(codeActions); }
public CodeActionResolveData(string uniqueIdentifier, LSP.Range range, LSP.TextDocumentIdentifier textDocument) { UniqueIdentifier = uniqueIdentifier; Range = range; TextDocument = textDocument; }