mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
feat(aliases): Add support for prepare rename
This commit is contained in:
parent
40a570809d
commit
ad1a8e0d95
45
handlers/aliases/lsp/text-document-prepare-rename.go
Normal file
45
handlers/aliases/lsp/text-document-prepare-rename.go
Normal file
@ -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
|
||||
}
|
@ -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{
|
||||
|
38
root-handler/text-document-prepare-rename.go
Normal file
38
root-handler/text-document-prepare-rename.go
Normal file
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user