1+ struct ServerAction
2+ command:: Command
3+ when:: Function
4+ handler:: Function
5+ end
6+
17function textDocument_codeAction_request (params:: CodeActionParams , server:: LanguageServerInstance , conn)
28 commands = Command[]
39 doc = getdocument (server, URI2 (params. textDocument. uri))
@@ -6,28 +12,12 @@ function textDocument_codeAction_request(params::CodeActionParams, server::Langu
612 x = get_expr (getcst (doc), offset)
713 arguments = Any[params. textDocument. uri, offset, offset1] # use the same arguments for all commands
814 if x isa EXPR
9- if refof (x) isa StaticLint. Binding && refof (x). val isa SymbolServer. ModuleStore
10- push! (commands, Command (" Explicitly import used package variables." , " ExplicitPackageVarImport" , arguments))
11- end
12- if parentof (x) isa EXPR && typof (parentof (x)) === CSTParser. Using && refof (x) isa StaticLint. Binding
13- if refof (x). type === StaticLint. CoreTypes. Module || (refof (x). val isa StaticLint. Binding && refof (x). val. type === StaticLint. CoreTypes. Module) || refof (x). val isa SymbolServer. ModuleStore
14- push! (commands, Command (" Re-export package variables." , " ReexportModule" , arguments))
15+ for (_,sa) in LSActions
16+ if sa. when (x, params)
17+ push! (commands, Command (sa. command. title, sa. command. command, arguments))
1518 end
1619 end
17- if is_in_fexpr (x, is_single_line_func)
18- push! (commands, Command (" Expand function definition." , " ExpandFunction" , arguments))
19- end
20- if is_in_fexpr (x, CSTParser. defines_struct)
21- push! (commands, Command (" Add default constructor" , " AddDefaultConstructor" , arguments))
22- end
23- if is_fixable_missing_ref (x, params. context)
24- push! (commands, Command (" Fix missing reference" , " FixMissingRef" , arguments))
25- end
26- # if params.range.start.line != params.range.stop.line # selection across _line_offsets
27- # push!(commands, Command("Wrap in `if` block.", "WrapIfBlock", arguments))
28- # end
2920 end
30-
3121 return commands
3222end
3323
@@ -36,22 +26,8 @@ function workspace_executeCommand_request(params::ExecuteCommandParams, server::
3626 offset = params. arguments[2 ]
3727 doc = getdocument (server, URI2 (uri))
3828 x = get_expr (getcst (doc), offset)
39- if params. command == " ExplicitPackageVarImport"
40- explicitly_import_used_variables (x, server, conn)
41- elseif params. command == " ExpandFunction"
42- expand_inline_func (x, server, conn)
43- elseif params. command == " AddDefaultConstructor"
44- add_default_constructor (x, server, conn)
45- elseif params. command == " ReexportModule"
46- if refof (x). type === StaticLint. CoreTypes. Module || (refof (x). val isa StaticLint. Binding && refof (x). val. type === StaticLint. CoreTypes. Module)
47- reexport_module (x, server, conn)
48- elseif refof (x). val isa SymbolServer. ModuleStore
49- reexport_package (x, server, conn)
50- end
51- elseif params. command == " WrapIfBlock"
52- wrap_block (get_expr (getcst (doc), params. arguments[2 ]: params. arguments[3 ]), server, :if , conn)
53- elseif params. command == " FixMissingRef"
54- applymissingreffix (x, server, conn)
29+ if haskey (LSActions, params. command)
30+ LSActions[params. command]. handler (x, server, conn)
5531 end
5632end
5733
@@ -147,33 +123,6 @@ function expand_inline_func(x, server, conn)
147123 end
148124end
149125
150-
151- function add_default_constructor (x:: EXPR , server, conn)
152- sexpr = _get_parent_fexpr (x, CSTParser. defines_struct)
153- ! (sexpr. args isa Vector{EXPR}) && return
154- ismutable = length (sexpr. args) == 5
155- name = CSTParser. get_name (sexpr)
156- sig = sexpr. args[2 + ismutable]
157- block = sexpr. args[3 + ismutable]
158-
159- isempty (block. args) && return
160- any (CSTParser. defines_function (a) for a in block. args) && return # constructor already exists
161-
162- newtext = string (" \n function $(valof (name)) (args...)\n\n new" )
163- # if DataType is parameterised do something here
164-
165- newtext = string (newtext, " (" )
166- for i in 1 : length (block. args)
167- newtext = string (newtext, " " , valof (CSTParser. get_arg_name (block. args[i])))
168- newtext = string (newtext, i < length (block. args) ? " , " : " )\n end" )
169- end
170- file, offset = get_file_loc (last (block. args))
171- offset += last (block. args). span
172- tde = TextDocumentEdit (VersionedTextDocumentIdentifier (file. _uri, file. _version), TextEdit[TextEdit (Range (file, offset: offset), newtext)])
173-
174- JSONRPC. send (conn, workspace_applyEdit_request_type, ApplyWorkspaceEditParams (missing , WorkspaceEdit (missing , TextDocumentEdit[tde])))
175- end
176-
177126function is_in_fexpr (x:: EXPR , f)
178127 if f (x)
179128 return true
@@ -205,6 +154,7 @@ function get_next_line_offset(x)
205154end
206155
207156function reexport_package (x:: EXPR , server, conn)
157+ (refof (x). type === StaticLint. CoreTypes. Module || (refof (x). val isa StaticLint. Binding && refof (x). val. type === StaticLint. CoreTypes. Module)) || (refof (x). val isa SymbolServer. ModuleStore) || return
208158 mod:: SymbolServer.ModuleStore = refof (x). val
209159 using_stmt = parentof (x)
210160 file, offset = get_file_loc (x)
@@ -301,3 +251,22 @@ function applymissingreffix(x, server, conn)
301251 end
302252 end
303253end
254+
255+ # Adding a CodeAction requires defining:
256+ # * a Command (title and description);
257+ # * a function (.when) called on the currently selected expression and parameters of the CodeAction call;
258+ # * a function (.handler) called on three arguments (current expression, server and the jr connection) to implement the command.
259+ const LSActions = Dict (
260+ " ExplicitPackageVarImport" => ServerAction (Command (" Explicitly import used package variables." , " ExplicitPackageVarImport" , missing ),
261+ (x, params) -> refof (x) isa StaticLint. Binding && refof (x). val isa SymbolServer. ModuleStore,
262+ explicitly_import_used_variables),
263+ " ExpandFunction" => ServerAction (Command (" Expand function definition." , " ExpandFunction" , missing ),
264+ (x, params) -> is_in_fexpr (x, is_single_line_func),
265+ expand_inline_func),
266+ " FixMissingRef" => ServerAction (Command (" Fix missing reference" , " FixMissingRef" , missing ),
267+ (x, params) -> is_fixable_missing_ref (x, params. context),
268+ applymissingreffix),
269+ " ReexportModule" => ServerAction (Command (" Re-export package variables." , " ReexportModule" , missing ),
270+ (x, params) -> parentof (x) isa EXPR && typof (parentof (x)) === CSTParser. Using && refof (x) isa StaticLint. Binding && (refof (x). type === StaticLint. CoreTypes. Module || (refof (x). val isa StaticLint. Binding && refof (x). val. type === StaticLint. CoreTypes. Module) || refof (x). val isa SymbolServer. ModuleStore),
271+ reexport_package)
272+ )
0 commit comments