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

View File

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