-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fbeced
commit d79882e
Showing
6 changed files
with
126 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from bolt import AstIdentifier, AstTargetIdentifier | ||
from lsprotocol import types as lsp | ||
|
||
from .helpers import fetch_compilation_data, get_node_at_position, node_location_to_range, search_scope_for_binding | ||
|
||
from .. import MechaLanguageServer | ||
|
||
|
||
def get_references(ls: MechaLanguageServer, params: lsp.ReferenceParams): | ||
compiled_doc = fetch_compilation_data(ls, params) | ||
|
||
if compiled_doc is None or compiled_doc.compiled_module is None: | ||
return | ||
|
||
ast = compiled_doc.compiled_module.ast | ||
scope = compiled_doc.compiled_module.lexical_scope | ||
|
||
node = get_node_at_position(ast, params.position) | ||
if isinstance(node, AstIdentifier) or isinstance(node, AstTargetIdentifier): | ||
var_name = node.value | ||
|
||
binding = search_scope_for_binding(var_name, node, scope) | ||
if not (result := search_scope_for_binding(var_name, node, scope)): | ||
return | ||
|
||
binding, _ = result | ||
|
||
locations = [] | ||
for reference in binding.references: | ||
range = node_location_to_range(reference) | ||
locations.append(lsp.Location(params.text_document.uri, range)) | ||
|
||
return locations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from bolt import AstIdentifier, AstTargetIdentifier | ||
from lsprotocol import types as lsp | ||
|
||
from .helpers import ( | ||
fetch_compilation_data, | ||
get_node_at_position, | ||
node_location_to_range, | ||
search_scope_for_binding, | ||
) | ||
|
||
from .. import MechaLanguageServer | ||
|
||
|
||
def rename_variable(ls: MechaLanguageServer, params: lsp.RenameParams): | ||
compiled_doc = fetch_compilation_data(ls, params) | ||
|
||
if compiled_doc is None or compiled_doc.compiled_module is None: | ||
return | ||
|
||
ast = compiled_doc.compiled_module.ast | ||
scope = compiled_doc.compiled_module.lexical_scope | ||
|
||
node = get_node_at_position(ast, params.position) | ||
if isinstance(node, AstIdentifier) or isinstance(node, AstTargetIdentifier): | ||
var_name = node.value | ||
|
||
|
||
|
||
if not (result := search_scope_for_binding(var_name, node, scope)): | ||
return | ||
binding, _ = result | ||
|
||
edits = [] | ||
edits.append( | ||
lsp.TextEdit(node_location_to_range(binding.origin), params.new_name) | ||
) | ||
|
||
for reference in binding.references: | ||
edits.append( | ||
lsp.TextEdit(node_location_to_range(reference), params.new_name) | ||
) | ||
|
||
return lsp.WorkspaceEdit(changes={params.text_document.uri: edits}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters