From 50cab9996fa1c0c7a08dd412d43e2e133ada3172 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Sun, 5 Jul 2020 23:49:22 -0700 Subject: [PATCH] Remove unnecessary local function --- .../CodeActions/CodeActionResolveHandler.cs | 99 ++++++++----------- 1 file changed, 42 insertions(+), 57 deletions(-) diff --git a/src/Features/LanguageServer/Protocol/Handler/CodeActions/CodeActionResolveHandler.cs b/src/Features/LanguageServer/Protocol/Handler/CodeActions/CodeActionResolveHandler.cs index 20f4aabee2e..af67babda61 100644 --- a/src/Features/LanguageServer/Protocol/Handler/CodeActions/CodeActionResolveHandler.cs +++ b/src/Features/LanguageServer/Protocol/Handler/CodeActions/CodeActionResolveHandler.cs @@ -89,43 +89,11 @@ internal class CodeActionResolveHandler : AbstractRequestHandler(); if (applyChangesOperations.Any()) - { - var workspaceEdits = await ComputeWorkspaceEdits(applyChangesOperations, document!, cancellationToken).ConfigureAwait(false); - if (workspaceEdits != null) - { - codeAction.Edit = new LSP.WorkspaceEdit { DocumentChanges = workspaceEdits }; - } - else - { - // The workspace edit is something we don't currently support, like adding a new document. - runAsCommand = true; - } - } - - // Set up to run as command on the server instead of using WorkspaceEdits. - var commandOperations = operations.All(operation => !(operation is ApplyChangesOperation)); - if (commandOperations || runAsCommand) - { - codeAction.Command = new LSP.Command - { - CommandIdentifier = CodeActionsHandler.RunCodeActionCommandName, - Title = codeAction.Title, - Arguments = new object[] { data } - }; - } - - return codeAction; - - // Local functions - static async Task ComputeWorkspaceEdits( - IEnumerable applyChangesOperations, - Document document, - CancellationToken cancellationToken) { using var _ = ArrayBuilder.GetInstance(out var textDocumentEdits); foreach (var applyChangesOperation in applyChangesOperations) { - var solution = document.Project.Solution; + var solution = document!.Project.Solution; var changes = applyChangesOperation.ChangedSolution.GetChanges(solution); var projectChanges = changes.GetProjectChanges(); @@ -136,7 +104,8 @@ internal class CodeActionResolveHandler : AbstractRequestHandler pc.GetAddedDocuments().Concat(pc.GetAddedAdditionalDocuments().Concat(pc.GetAddedAnalyzerConfigDocuments()))); if (addedDocuments.Any()) { - return null; + runAsCommand = true; + break; } var changedDocuments = projectChanges.SelectMany(pc => pc.GetChangedDocuments()); @@ -151,7 +120,8 @@ internal class CodeActionResolveHandler : AbstractRequestHandler( - ArrayBuilder textDocumentEdits, - ApplyChangesOperation applyChangesOperation, - Solution solution, - IEnumerable changedDocuments, - Func getNewDocumentFunc, - Func getOldDocumentFunc, - CancellationToken cancellationToken) - where T : TextDocument + codeAction.Edit = new LSP.WorkspaceEdit { DocumentChanges = textDocumentEdits.ToArray() }; + } + + // Set up to run as command on the server instead of using WorkspaceEdits. + var commandOperations = operations.All(operation => !(operation is ApplyChangesOperation)); + if (commandOperations || runAsCommand) + { + codeAction.Command = new LSP.Command { - foreach (var docId in changedDocuments) - { - var newDoc = getNewDocumentFunc(docId); - var oldDoc = getOldDocumentFunc(docId); + CommandIdentifier = CodeActionsHandler.RunCodeActionCommandName, + Title = codeAction.Title, + Arguments = new object[] { data } + }; + } - var oldText = await oldDoc!.GetTextAsync(cancellationToken).ConfigureAwait(false); - var newText = await newDoc!.GetTextAsync(cancellationToken).ConfigureAwait(false); + return codeAction; - var textChanges = newText.GetTextChanges(oldText).ToList(); + // Local functions + static async Task AddTextDocumentEdits( + ArrayBuilder textDocumentEdits, + ApplyChangesOperation applyChangesOperation, + Solution solution, + IEnumerable changedDocuments, + Func getNewDocumentFunc, + Func getOldDocumentFunc, + CancellationToken cancellationToken) + where T : TextDocument + { + foreach (var docId in changedDocuments) + { + var newDoc = getNewDocumentFunc(docId); + var oldDoc = getOldDocumentFunc(docId); - var edits = textChanges.Select(tc => ProtocolConversions.TextChangeToTextEdit(tc, oldText)).ToArray(); - var documentIdentifier = new VersionedTextDocumentIdentifier { Uri = newDoc.GetURI() }; - textDocumentEdits.Add(new TextDocumentEdit { TextDocument = documentIdentifier, Edits = edits.ToArray() }); - } + var oldText = await oldDoc!.GetTextAsync(cancellationToken).ConfigureAwait(false); + var newText = await newDoc!.GetTextAsync(cancellationToken).ConfigureAwait(false); + + var textChanges = newText.GetTextChanges(oldText).ToList(); + + var edits = textChanges.Select(tc => ProtocolConversions.TextChangeToTextEdit(tc, oldText)).ToArray(); + var documentIdentifier = new VersionedTextDocumentIdentifier { Uri = newDoc.GetURI() }; + textDocumentEdits.Add(new TextDocumentEdit { TextDocument = documentIdentifier, Edits = edits.ToArray() }); } } } -- GitLab