From ad1a8e0d95c6171ab28ce27f794ebe9afe260a3a Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 7 Sep 2024 21:02:43 +0200 Subject: [PATCH] feat(aliases): Add support for prepare rename --- .../lsp/text-document-prepare-rename.go | 45 +++++++++++++++++++ root-handler/handler.go | 9 ++++ root-handler/text-document-prepare-rename.go | 38 ++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 handlers/aliases/lsp/text-document-prepare-rename.go create mode 100644 root-handler/text-document-prepare-rename.go diff --git a/handlers/aliases/lsp/text-document-prepare-rename.go b/handlers/aliases/lsp/text-document-prepare-rename.go new file mode 100644 index 0000000..2b2d03e --- /dev/null +++ b/handlers/aliases/lsp/text-document-prepare-rename.go @@ -0,0 +1,45 @@ +package lsp + +import ( + "config-lsp/handlers/aliases" + "config-lsp/handlers/aliases/ast" + "config-lsp/handlers/aliases/handlers" + + "github.com/tliron/glsp" + protocol "github.com/tliron/glsp/protocol_3_16" +) + +func TextDocumentPrepareRename(context *glsp.Context, params *protocol.PrepareRenameParams) (any, error) { + d := aliases.DocumentParserMap[params.TextDocument.URI] + character := params.Position.Character + line := params.Position.Line + + rawEntry, found := d.Parser.Aliases.Get(line) + + if !found { + return nil, nil + } + + entry := rawEntry.(*ast.AliasEntry) + + if character >= entry.Key.Location.Start.Character && character <= entry.Key.Location.End.Character { + return entry.Key.Location.ToLSPRange(), nil + } + + if entry.Values != nil && character >= entry.Values.Location.Start.Character && character <= entry.Values.Location.End.Character { + rawValue := handlers.GetValueAtCursor(character, entry) + + if rawValue == nil { + return nil, nil + } + + switch (*rawValue).(type) { + case ast.AliasValueUser: + userValue := (*rawValue).(ast.AliasValueUser) + + return userValue.Location.ToLSPRange(), nil + } + } + + return nil, nil +} diff --git a/root-handler/handler.go b/root-handler/handler.go index 9b74ea4..9dff13c 100644 --- a/root-handler/handler.go +++ b/root-handler/handler.go @@ -30,6 +30,7 @@ func SetUpRootHandler() { TextDocumentDefinition: TextDocumentDefinition, WorkspaceExecuteCommand: WorkspaceExecuteCommand, TextDocumentRename: TextDocumentRename, + TextDocumentPrepareRename: TextDocumentPrepareRename, } server := server.NewServer(&lspHandler, lsName, false) @@ -41,6 +42,14 @@ func initialize(context *glsp.Context, params *protocol.InitializeParams) (any, capabilities := lspHandler.CreateServerCapabilities() capabilities.TextDocumentSync = protocol.TextDocumentSyncKindFull + if (*params.Capabilities.TextDocument.Rename.PrepareSupport) == true { + // Client supports rename preparation + prepareRename := true + capabilities.RenameProvider = protocol.RenameOptions{ + PrepareProvider: &prepareRename, + } + } + return protocol.InitializeResult{ Capabilities: capabilities, ServerInfo: &protocol.InitializeResultServerInfo{ diff --git a/root-handler/text-document-prepare-rename.go b/root-handler/text-document-prepare-rename.go new file mode 100644 index 0000000..fc4489b --- /dev/null +++ b/root-handler/text-document-prepare-rename.go @@ -0,0 +1,38 @@ +package roothandler + +import ( + "github.com/tliron/glsp" + aliases "config-lsp/handlers/aliases/lsp" + + protocol "github.com/tliron/glsp/protocol_3_16" +) + +func TextDocumentPrepareRename(context *glsp.Context, params *protocol.PrepareRenameParams) (any, error) { + language := rootHandler.GetLanguageForDocument(params.TextDocument.URI) + + if language == nil { + showParseError( + context, + params.TextDocument.URI, + undetectableError, + ) + + return nil, undetectableError.Err + } + + switch *language { + case LanguageHosts: + return nil, nil + case LanguageSSHDConfig: + return nil, nil + case LanguageFstab: + return nil, nil + case LanguageWireguard: + return nil, nil + case LanguageAliases: + return aliases.TextDocumentPrepareRename(context, params) + } + + panic("root-handler/TextDocumentPrepareRename: unexpected language" + *language) +} +