|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using OmniSharp.Extensions.LanguageServer.Protocol; |
| 7 | +using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; |
| 8 | +using OmniSharp.Extensions.LanguageServer.Protocol.Document; |
| 9 | +using OmniSharp.Extensions.LanguageServer.Protocol.Models; |
| 10 | + |
| 11 | +namespace Yabal.LanguageServer.Handlers; |
| 12 | + |
| 13 | +public class RenameHandler(TextDocumentContainer documentContainer) : IRenameHandler |
| 14 | +{ |
| 15 | + public Task<WorkspaceEdit?> Handle(RenameParams request, CancellationToken cancellationToken) |
| 16 | + { |
| 17 | + if (!documentContainer.Documents.TryGetValue(request.TextDocument.Uri, out var document)) |
| 18 | + { |
| 19 | + return Task.FromResult<WorkspaceEdit?>(null); |
| 20 | + } |
| 21 | + |
| 22 | + var (_, variable) = document.Builder.Find(request.Position); |
| 23 | + |
| 24 | + if (variable == null) |
| 25 | + { |
| 26 | + return Task.FromResult<WorkspaceEdit?>(null); |
| 27 | + } |
| 28 | + |
| 29 | + return Task.FromResult<WorkspaceEdit?>(new WorkspaceEdit |
| 30 | + { |
| 31 | + Changes = new Dictionary<DocumentUri, IEnumerable<TextEdit>> |
| 32 | + { |
| 33 | + [request.TextDocument.Uri] = |
| 34 | + [ |
| 35 | + ..variable.References |
| 36 | + .OrderByDescending(i => i.Range) |
| 37 | + .DistinctBy(i => i.Range.Index) |
| 38 | + .Select(i => new TextEdit |
| 39 | + { |
| 40 | + Range = i.Range.ToRange(), |
| 41 | + NewText = request.NewName |
| 42 | + }), |
| 43 | + new TextEdit |
| 44 | + { |
| 45 | + Range = variable.Identifier.Range.ToRange(), |
| 46 | + NewText = request.NewName |
| 47 | + } |
| 48 | + ] |
| 49 | + } |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + public RenameRegistrationOptions GetRegistrationOptions(RenameCapability capability, ClientCapabilities clientCapabilities) |
| 54 | + { |
| 55 | + return new RenameRegistrationOptions |
| 56 | + { |
| 57 | + DocumentSelector = TextDocumentSelector.ForLanguage("yabal") |
| 58 | + }; |
| 59 | + } |
| 60 | +} |
0 commit comments