-
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.
Suggest Variable Names via Code Action
This commit introduces a new code action that suggests variable names based on the context and type annotations.
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
local function findup(node, kind) | ||
while node ~= nil and node:kind() ~= kind do | ||
node = node:parent() | ||
end | ||
return node | ||
end | ||
|
||
local M = { | ||
is_triggered = function(lsp_range) | ||
local start_node = active_doc:node_from_range(lsp_range) | ||
if start_node == nil then | ||
return nil | ||
end | ||
return findup(start_node, "identifier") | ||
end, | ||
|
||
action_name = function() | ||
return "List Identifier Name Options" | ||
end, | ||
|
||
process_answer = function(llm_response, lsp_range) | ||
return llm_response | ||
end, | ||
|
||
create_prompt = function(lsp_range) | ||
local start_node = active_doc:node_from_range(lsp_range) | ||
if start_node == nil then | ||
return nil | ||
end | ||
local var_node = findup(start_node, "identifier") | ||
local var_text = active_doc:text_from_node(var_node) | ||
local func_node = findup(start_node, "function_definition") | ||
local function_text = active_doc:text_from_node(func_node) | ||
|
||
return table.concat({ | ||
[=====[ Human: | ||
The variable <variable>]=====], var_text, [=====[</variable> | ||
is used in the following context | ||
<context> | ||
]=====], function_text, [=====[ | ||
</context> | ||
List me 20 Options of how I can name that identifer given the context. | ||
Assistant: ]=====] }) | ||
end, | ||
|
||
placement_range = function(lsp_range) | ||
-- place below | ||
return { | ||
start_line = lsp_range.end_line + 1, | ||
start_character = 0, | ||
end_line = lsp_range.end_line + 1, | ||
end_character = 0, | ||
} | ||
end | ||
} | ||
|
||
return M |