fix(aliases): Improve rename when definition entry is not available

This commit is contained in:
Myzel394 2024-09-18 20:46:33 +02:00
parent 590786e844
commit 1a70a5ad57
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
2 changed files with 22 additions and 13 deletions

View File

@ -1,7 +1,6 @@
package handlers
import (
"config-lsp/handlers/aliases/ast"
"config-lsp/handlers/aliases/indexes"
protocol "github.com/tliron/glsp/protocol_3_16"
@ -9,17 +8,22 @@ import (
func RenameAlias(
i indexes.AliasesIndexes,
oldEntry *ast.AliasEntry,
oldName string,
newName string,
) []protocol.TextEdit {
occurrences := i.UserOccurrences[indexes.NormalizeKey(oldEntry.Key.Value)]
normalizedName := indexes.NormalizeKey(oldName)
definitionEntry := i.Keys[normalizedName]
occurrences := i.UserOccurrences[normalizedName]
changes := make([]protocol.TextEdit, 0, len(occurrences))
// Own rename
changes = append(changes, protocol.TextEdit{
Range: oldEntry.Key.Location.ToLSPRange(),
NewText: newName,
})
if definitionEntry != nil {
// Own rename
changes = append(changes, protocol.TextEdit{
Range: definitionEntry.Key.Location.ToLSPRange(),
NewText: newName,
})
}
// Other AliasValueUser occurrences
for _, value := range occurrences {

View File

@ -4,7 +4,6 @@ import (
"config-lsp/handlers/aliases"
"config-lsp/handlers/aliases/ast"
"config-lsp/handlers/aliases/handlers"
"config-lsp/handlers/aliases/indexes"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
@ -24,7 +23,11 @@ func TextDocumentRename(context *glsp.Context, params *protocol.RenameParams) (*
entry := rawEntry.(*ast.AliasEntry)
if character >= entry.Key.Location.Start.Character && character <= entry.Key.Location.End.Character {
changes := handlers.RenameAlias(*d.Indexes, entry, params.NewName)
changes := handlers.RenameAlias(
*d.Indexes,
entry.Key.Value,
params.NewName,
)
return &protocol.WorkspaceEdit{
Changes: map[protocol.DocumentUri][]protocol.TextEdit{
@ -44,9 +47,11 @@ func TextDocumentRename(context *glsp.Context, params *protocol.RenameParams) (*
case ast.AliasValueUser:
userValue := (*rawValue).(ast.AliasValueUser)
definitionEntry := d.Indexes.Keys[indexes.NormalizeKey(userValue.Value)]
changes := handlers.RenameAlias(*d.Indexes, definitionEntry, params.NewName)
changes := handlers.RenameAlias(
*d.Indexes,
userValue.Value,
params.NewName,
)
return &protocol.WorkspaceEdit{
Changes: map[protocol.DocumentUri][]protocol.TextEdit{