From 85da6f67ee29d80192fc63112761b4aadc64ef5e Mon Sep 17 00:00:00 2001 From: RLittlesII Date: Wed, 25 Oct 2017 22:17:12 -0500 Subject: [PATCH] Adds RenameHandler --- .../Handlers/RenameHandler.cs | 90 +++++++++++++++++++ .../LanguageServerHost.cs | 1 + 2 files changed, 91 insertions(+) create mode 100644 src/OmniSharp.LanguageServerProtocol/Handlers/RenameHandler.cs diff --git a/src/OmniSharp.LanguageServerProtocol/Handlers/RenameHandler.cs b/src/OmniSharp.LanguageServerProtocol/Handlers/RenameHandler.cs new file mode 100644 index 0000000000..698484f43e --- /dev/null +++ b/src/OmniSharp.LanguageServerProtocol/Handlers/RenameHandler.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Composition; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using OmniSharp.Extensions.JsonRpc; +using OmniSharp.Extensions.LanguageServer.Capabilities.Client; +using OmniSharp.Extensions.LanguageServer.Models; +using OmniSharp.Extensions.LanguageServer.Protocol; +using OmniSharp.Models.Rename; + +namespace OmniSharp.LanguageServerProtocol.Handlers +{ + internal class RenameHandler : IRenameHandler + { + private readonly Mef.IRequestHandler _renameHandler; + private readonly DocumentSelector _documentSelector; + private RenameCapability _capability; + + public RenameHandler(Mef.IRequestHandler renameHandler, DocumentSelector documentSelector) + { + _renameHandler = renameHandler; + _documentSelector = documentSelector; + } + + public static IEnumerable Enumerate(RequestHandlers handlers) + { + foreach (var (selector, handler) in handlers + .OfType>()) + if (handler != null) + yield return new RenameHandler(handler, selector); + } + + public async Task Handle(RenameParams request, CancellationToken token) + { + var omnisharpRequest = new RenameRequest + { + FileName = Helpers.FromUri(request.TextDocument.Uri), + RenameTo = request.NewName, + Column = Convert.ToInt32(request.Position.Character), + Line = Convert.ToInt32(request.Position.Line), + Buffer = request.NewName + }; + + var omnisharpResponse = await _renameHandler.Handle(omnisharpRequest); + + if (omnisharpResponse.ErrorMessage != null) + { + } + + var changes = omnisharpResponse.Changes.ToDictionary(change => Helpers.ToUri(change.FileName), + x => x.Changes.Select(edit => new TextEdit + { + NewText = edit.NewText, + Range = Helpers.ToRange((edit.StartColumn, edit.StartLine), (edit.EndColumn, edit.EndLine)) + })); + + var edits = changes.Values.SelectMany(edit => edit.ToList()); + + var documentEdits = omnisharpResponse.Changes.Select(x => new TextDocumentEdit + { + Edits = new Container(edits), + TextDocument = new VersionedTextDocumentIdentifier + { + Uri = Helpers.ToUri(x.FileName) + } + }); + + return new WorkspaceEdit + { + Changes = changes, + DocumentChanges = new Container(documentEdits) + }; + } + + public TextDocumentRegistrationOptions GetRegistrationOptions() + { + return new TextDocumentRegistrationOptions + { + DocumentSelector = _documentSelector + }; + } + + public void SetCapability(RenameCapability capability) + { + _capability = capability; + } + } +} diff --git a/src/OmniSharp.LanguageServerProtocol/LanguageServerHost.cs b/src/OmniSharp.LanguageServerProtocol/LanguageServerHost.cs index f242821054..3044cc9475 100644 --- a/src/OmniSharp.LanguageServerProtocol/LanguageServerHost.cs +++ b/src/OmniSharp.LanguageServerProtocol/LanguageServerHost.cs @@ -154,6 +154,7 @@ private Task Initialize(InitializeParams initializeParams) _server.AddHandlers(HoverHandler.Enumerate(_handlers)); _server.AddHandlers(CompletionHandler.Enumerate(_handlers)); _server.AddHandlers(SignatureHelpHandler.Enumerate(_handlers)); + _server.AddHandlers(RenameHandler.Enumerate(_handlers)); _server.LogMessage(new LogMessageParams() {