Skip to content

Commit 5276056

Browse files
authored
feat: support renaming and finding references in lsp (#1700)
This adds support for finding all references of a given symbol. This also adds support for renaming symbols in the LSP server.
1 parent 4aaf2f6 commit 5276056

7 files changed

Lines changed: 683 additions & 17 deletions

File tree

pkg/cmd/zkc/lsp.go

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ func (s *zkcServer) Initialize(
248248
HoverProvider: true,
249249
DocumentSymbolProvider: true,
250250
DefinitionProvider: true,
251+
ReferencesProvider: true,
252+
RenameProvider: &protocol.RenameOptions{PrepareProvider: true},
251253
DocumentFormattingProvider: true,
252254
SignatureHelpProvider: &protocol.SignatureHelpOptions{
253255
TriggerCharacters: []string{"(", ","},
@@ -756,11 +758,20 @@ func (s *zkcServer) OnTypeFormatting(
756758
// rename is valid at the given position and, if so, what range should be
757759
// pre-selected in the rename input box. The server may reject the request if
758760
// the symbol cannot be renamed (e.g. it is a built-in).
759-
// Not yet implemented.
760761
func (s *zkcServer) PrepareRename(
761-
_ context.Context, _ *protocol.PrepareRenameParams,
762+
_ context.Context, params *protocol.PrepareRenameParams,
762763
) (*protocol.Range, error) {
763-
return nil, errNotImplemented
764+
s.mu.RLock()
765+
text, ok := s.compiler.Source(params.TextDocument.URI.Filename())
766+
program := s.compiler.Program()
767+
srcmaps := s.compiler.SourceMaps()
768+
s.mu.RUnlock()
769+
770+
if !ok {
771+
return nil, nil
772+
}
773+
774+
return lsp.PrepareRenameFor(params.TextDocument.URI, text, params.Position, program, srcmaps)
764775
}
765776

766777
// RangeFormatting handles a textDocument/rangeFormatting request. The client
@@ -778,22 +789,46 @@ func (s *zkcServer) RangeFormatting(
778789
// when the user invokes "find all references" on a symbol. The server returns
779790
// every location in the workspace where that symbol is used, optionally
780791
// including the declaration site itself.
781-
// Not yet implemented.
782792
func (s *zkcServer) References(
783-
_ context.Context, _ *protocol.ReferenceParams,
793+
_ context.Context, params *protocol.ReferenceParams,
784794
) ([]protocol.Location, error) {
785-
return nil, errNotImplemented
795+
s.mu.RLock()
796+
text, ok := s.compiler.Source(params.TextDocument.URI.Filename())
797+
program := s.compiler.Program()
798+
srcmaps := s.compiler.SourceMaps()
799+
s.mu.RUnlock()
800+
801+
if !ok {
802+
return nil, nil
803+
}
804+
805+
return lsp.ReferencesFor(
806+
params.TextDocument.URI, text, params.Position,
807+
params.Context.IncludeDeclaration, program, srcmaps,
808+
)
786809
}
787810

788811
// Rename handles a textDocument/rename request. The client sends this when
789812
// the user confirms a rename operation (following an optional PrepareRename).
790813
// The server returns a workspace edit — a set of text changes across all
791814
// affected files — that renames every occurrence of the symbol.
792-
// Not yet implemented.
793815
func (s *zkcServer) Rename(
794-
_ context.Context, _ *protocol.RenameParams,
816+
_ context.Context, params *protocol.RenameParams,
795817
) (*protocol.WorkspaceEdit, error) {
796-
return nil, errNotImplemented
818+
s.mu.RLock()
819+
text, ok := s.compiler.Source(params.TextDocument.URI.Filename())
820+
program := s.compiler.Program()
821+
srcmaps := s.compiler.SourceMaps()
822+
s.mu.RUnlock()
823+
824+
if !ok {
825+
return nil, nil
826+
}
827+
828+
return lsp.RenameFor(
829+
params.TextDocument.URI, text, params.Position, params.NewName,
830+
program, srcmaps,
831+
)
797832
}
798833

799834
// SignatureHelp handles a textDocument/signatureHelp request. The client

pkg/cmd/zkc/lsp/definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func DefinitionFor(
4242
// Lex the document to find the identifier token under the cursor.
4343
tokens := parser.Lex(*srcfile, false, false)
4444

45-
tok, ok := tokenAtOffset(tokens, offset)
45+
tok, _, ok := tokenAtOffset(tokens, offset)
4646
if !ok || tok.Kind != parser.IDENTIFIER {
4747
return nil, nil
4848
}

pkg/cmd/zkc/lsp/hover.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func HoverFor(
4141
// Lex the document to find the identifier token under the cursor.
4242
tokens := parser.Lex(*srcfile, false, false)
4343

44-
tok, ok := tokenAtOffset(tokens, offset)
44+
tok, _, ok := tokenAtOffset(tokens, offset)
4545
if !ok || tok.Kind != parser.IDENTIFIER {
4646
return nil, nil
4747
}

0 commit comments

Comments
 (0)