public void SimpleTest(string expected) { var model = new TextDocumentEdit() { TextDocument = new VersionedTextDocumentIdentifier() { Version = 1, Uri = new Uri("file:///abc/123/d.cs"), }, Edits = new[] { new TextEdit() { NewText = "new text", Range = new Range(new Position(1, 1), new Position(2, 2)) }, new TextEdit() { NewText = "new text2", Range = new Range(new Position(3, 3), new Position(4, 4)) } } }; var result = Fixture.SerializeObject(model); result.Should().Be(expected); var deresult = JsonConvert.DeserializeObject <TextDocumentEdit>(expected); deresult.ShouldBeEquivalentTo(model); }
public void SimpleTest(string expected) { var model = new TextDocumentEdit { TextDocument = new OptionalVersionedTextDocumentIdentifier { Version = 1, Uri = new Uri("file:///abc/123/d.cs"), }, Edits = new[] { new TextEdit { NewText = "new text", Range = new Range(new Position(1, 1), new Position(2, 2)) }, new TextEdit { NewText = "new text2", Range = new Range(new Position(3, 3), new Position(4, 4)) } } }; var result = Fixture.SerializeObject(model); result.Should().Be(expected); var deresult = new LspSerializer(ClientVersion.Lsp3).DeserializeObject <TextDocumentEdit>(expected); deresult.Should().BeEquivalentTo(model, x => x.UsingStructuralRecordEquality()); }
public WorkspaceEditDocumentChange(TextDocumentEdit textDocumentEdit) { TextDocumentEdit = textDocumentEdit; CreateFile = null; RenameFile = null; DeleteFile = null; }
public async Task <WorkspaceEdit> HandleRequestAsync(Solution solution, RenameParams request, ClientCapabilities clientCapabilities, CancellationToken cancellationToken) { WorkspaceEdit workspaceEdit = null; var document = solution.GetDocumentFromURI(request.TextDocument.Uri); if (document != null) { var renameService = document.Project.LanguageServices.GetService <IEditorInlineRenameService>(); var position = await document.GetPositionFromLinePositionAsync(ProtocolConversions.PositionToLinePosition(request.Position), cancellationToken).ConfigureAwait(false); var renameInfo = await renameService.GetRenameInfoAsync(document, position, cancellationToken).ConfigureAwait(false); if (!renameInfo.CanRename) { return(workspaceEdit); } var renameLocationSet = await renameInfo.FindRenameLocationsAsync(solution.Workspace.Options, cancellationToken).ConfigureAwait(false); var renameReplacementInfo = await renameLocationSet.GetReplacementsAsync(request.NewName, solution.Workspace.Options, cancellationToken).ConfigureAwait(false); var newSolution = renameReplacementInfo.NewSolution; var solutionChanges = newSolution.GetChanges(solution); var changedDocuments = solutionChanges .GetProjectChanges() .SelectMany(p => p.GetChangedDocuments(onlyGetDocumentsWithTextChanges: true)); var documentEdits = new ArrayBuilder <TextDocumentEdit>(); foreach (var docId in changedDocuments) { var oldDoc = solution.GetDocument(docId); var newDoc = newSolution.GetDocument(docId); var textChanges = await newDoc.GetTextChangesAsync(oldDoc, cancellationToken).ConfigureAwait(false); var oldText = await oldDoc.GetTextAsync(cancellationToken).ConfigureAwait(false); var textDocumentEdit = new TextDocumentEdit { TextDocument = new VersionedTextDocumentIdentifier { Uri = newDoc.GetURI() }, Edits = textChanges.Select(tc => ProtocolConversions.TextChangeToTextEdit(tc, oldText)).ToArray() }; documentEdits.Add(textDocumentEdit); } workspaceEdit = new WorkspaceEdit { DocumentChanges = documentEdits.ToArrayAndFree() }; } return(workspaceEdit); }
public override async Task <WorkspaceEdit?> HandleRequestAsync(RenameParams request, RequestContext context, CancellationToken cancellationToken) { WorkspaceEdit?workspaceEdit = null; var document = SolutionProvider.GetDocument(request.TextDocument, context.ClientName); if (document != null) { var oldSolution = document.Project.Solution; var renameService = document.Project.LanguageServices.GetRequiredService <IEditorInlineRenameService>(); var position = await document.GetPositionFromLinePositionAsync(ProtocolConversions.PositionToLinePosition(request.Position), cancellationToken).ConfigureAwait(false); var renameInfo = await renameService.GetRenameInfoAsync(document, position, cancellationToken).ConfigureAwait(false); if (!renameInfo.CanRename) { return(workspaceEdit); } var renameLocationSet = await renameInfo.FindRenameLocationsAsync(oldSolution.Workspace.Options, cancellationToken).ConfigureAwait(false); var renameReplacementInfo = await renameLocationSet.GetReplacementsAsync(request.NewName, oldSolution.Workspace.Options, cancellationToken).ConfigureAwait(false); var renamedSolution = renameReplacementInfo.NewSolution; var solutionChanges = renamedSolution.GetChanges(oldSolution); // Linked files can correspond to multiple roslyn documents each with changes. Merge the changes in the linked files so that all linked documents have the same text. // Then we can just take the text changes from the first document to avoid returning duplicate edits. renamedSolution = await renamedSolution.WithMergedLinkedFileChangesAsync(oldSolution, solutionChanges, cancellationToken : cancellationToken).ConfigureAwait(false); solutionChanges = renamedSolution.GetChanges(oldSolution); var changedDocuments = solutionChanges .GetProjectChanges() .SelectMany(p => p.GetChangedDocuments(onlyGetDocumentsWithTextChanges: true)) .GroupBy(docId => renamedSolution.GetRequiredDocument(docId).FilePath, StringComparer.OrdinalIgnoreCase).Select(group => group.First()); using var _ = ArrayBuilder <TextDocumentEdit> .GetInstance(out var documentEdits); foreach (var docId in changedDocuments) { var oldDoc = oldSolution.GetRequiredDocument(docId); var newDoc = renamedSolution.GetRequiredDocument(docId); var textChanges = await newDoc.GetTextChangesAsync(oldDoc, cancellationToken).ConfigureAwait(false); var oldText = await oldDoc.GetTextAsync(cancellationToken).ConfigureAwait(false); var textDocumentEdit = new TextDocumentEdit { TextDocument = new VersionedTextDocumentIdentifier { Uri = newDoc.GetURI() }, Edits = textChanges.Select(tc => ProtocolConversions.TextChangeToTextEdit(tc, oldText)).ToArray() }; documentEdits.Add(textDocumentEdit); } workspaceEdit = new WorkspaceEdit { DocumentChanges = documentEdits.ToArray() }; } return(workspaceEdit); }
public async Task <WorkspaceEdit> HandleAsync(RenameParams request, RequestContext <Solution> requestContext, CancellationToken cancellationToken) { var solution = requestContext.Context; WorkspaceEdit workspaceEdit = null; var document = solution.GetDocumentFromURI(request.TextDocument.Uri); if (document != null) { var renameService = document.Project.LanguageServices.GetService <IEditorInlineRenameService>(); var position = await document.GetPositionFromLinePositionAsync(ProtocolConversions.PositionToLinePosition(request.Position), cancellationToken).ConfigureAwait(false); // We need to be on the UI thread to call GetRenameInfo which computes the rename locations. // This is because Roslyn reads the readonly regions of the buffer to compute the locations in the document. // This is typically quick. It's marked configureawait(false) so that the bulk of the rename operation can happen // in background threads. await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); var renameInfo = await renameService.GetRenameInfoAsync(document, position, cancellationToken).ConfigureAwait(false); if (!renameInfo.CanRename) { return(workspaceEdit); } var renameLocationSet = await renameInfo.FindRenameLocationsAsync(solution.Workspace.Options, cancellationToken).ConfigureAwait(false); var renameReplacementInfo = await renameLocationSet.GetReplacementsAsync(request.NewName, solution.Workspace.Options, cancellationToken).ConfigureAwait(false); var newSolution = renameReplacementInfo.NewSolution; var solutionChanges = newSolution.GetChanges(solution); var changedDocuments = solutionChanges .GetProjectChanges() .SelectMany(p => p.GetChangedDocuments(onlyGetDocumentsWithTextChanges: true)); var documentEdits = new ArrayBuilder <TextDocumentEdit>(); foreach (var docId in changedDocuments) { var oldDoc = solution.GetDocument(docId); var newDoc = newSolution.GetDocument(docId); var textChanges = await newDoc.GetTextChangesAsync(oldDoc, cancellationToken).ConfigureAwait(false); var oldText = await oldDoc.GetTextAsync(cancellationToken).ConfigureAwait(false); var textDocumentEdit = new TextDocumentEdit { TextDocument = new VersionedTextDocumentIdentifier { Uri = newDoc.GetURI() }, Edits = textChanges.Select(tc => ProtocolConversions.TextChangeToTextEdit(tc, oldText)).ToArray() }; documentEdits.Add(textDocumentEdit); } workspaceEdit = new WorkspaceEdit { DocumentChanges = documentEdits.ToArrayAndFree() }; } return(workspaceEdit); }