Skip to content

Commit 219e237

Browse files
authored
Merge branch 'master' into tests
2 parents 878e0c9 + d5fb184 commit 219e237

File tree

3 files changed

+33
-64
lines changed

3 files changed

+33
-64
lines changed

src/LanguageServer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ include("languageserverinstance.jl")
1919
include("runserver.jl")
2020
include("staticlint.jl")
2121

22-
include("requests/init.jl")
2322
include("requests/misc.jl")
2423
include("requests/textdocument.jl")
2524
include("requests/features.jl")
2625
include("requests/hover.jl")
2726
include("requests/completions.jl")
2827
include("requests/workspace.jl")
2928
include("requests/actions.jl")
29+
include("requests/init.jl")
3030
include("utilities.jl")
3131

3232
end

src/requests/actions.jl

Lines changed: 31 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
struct ServerAction
2+
command::Command
3+
when::Function
4+
handler::Function
5+
end
6+
17
function 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
3222
end
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
5632
end
5733

@@ -146,33 +122,6 @@ function expand_inline_func(x, server, conn)
146122
end
147123
end
148124

149-
150-
function add_default_constructor(x::EXPR, server, conn)
151-
sexpr = _get_parent_fexpr(x, CSTParser.defines_struct)
152-
!(sexpr.args isa Vector{EXPR}) && return
153-
ismutable = length(sexpr.args) == 5
154-
name = CSTParser.get_name(sexpr)
155-
sig = sexpr.args[2 + ismutable]
156-
block = sexpr.args[3 + ismutable]
157-
158-
isempty(block.args) && return
159-
any(CSTParser.defines_function(a) for a in block.args) && return # constructor already exists
160-
161-
newtext = string("\n function $(valof(name))(args...)\n\n new")
162-
# if DataType is parameterised do something here
163-
164-
newtext = string(newtext, "(")
165-
for i in 1:length(block.args)
166-
newtext = string(newtext, "", valof(CSTParser.get_arg_name(block.args[i])))
167-
newtext = string(newtext, i < length(block.args) ? ", " : ")\n end")
168-
end
169-
file, offset = get_file_loc(last(block.args))
170-
offset += last(block.args).span
171-
tde = TextDocumentEdit(VersionedTextDocumentIdentifier(file._uri, file._version), TextEdit[TextEdit(Range(file, offset:offset), newtext)])
172-
173-
JSONRPC.send(conn, workspace_applyEdit_request_type, ApplyWorkspaceEditParams(missing, WorkspaceEdit(missing, TextDocumentEdit[tde])))
174-
end
175-
176125
function is_in_fexpr(x::EXPR, f)
177126
if f(x)
178127
return true
@@ -204,6 +153,7 @@ function get_next_line_offset(x)
204153
end
205154

206155
function reexport_package(x::EXPR, server, conn)
156+
(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
207157
mod::SymbolServer.ModuleStore = refof(x).val
208158
using_stmt = parentof(x)
209159
file, offset = get_file_loc(x)
@@ -300,3 +250,22 @@ function applymissingreffix(x, server, conn)
300250
end
301251
end
302252
end
253+
254+
# Adding a CodeAction requires defining:
255+
# * a Command (title and description);
256+
# * a function (.when) called on the currently selected expression and parameters of the CodeAction call;
257+
# * a function (.handler) called on three arguments (current expression, server and the jr connection) to implement the command.
258+
const LSActions = Dict(
259+
"ExplicitPackageVarImport" => ServerAction(Command("Explicitly import used package variables.", "ExplicitPackageVarImport", missing),
260+
(x, params) -> refof(x) isa StaticLint.Binding && refof(x).val isa SymbolServer.ModuleStore,
261+
explicitly_import_used_variables),
262+
"ExpandFunction" => ServerAction(Command("Expand function definition.", "ExpandFunction", missing),
263+
(x, params) -> is_in_fexpr(x, is_single_line_func),
264+
expand_inline_func),
265+
"FixMissingRef" => ServerAction(Command("Fix missing reference", "FixMissingRef", missing),
266+
(x, params) -> is_fixable_missing_ref(x, params.context),
267+
applymissingreffix),
268+
"ReexportModule" => ServerAction(Command("Re-export package variables.", "ReexportModule", missing),
269+
(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),
270+
reexport_package)
271+
)

src/requests/init.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const serverCapabilities = ServerCapabilities(
2323
missing,
2424
true,
2525
false,
26-
ExecuteCommandOptions(missing, String["ExplicitPackageVarImport", "ExpandFunction", "AddDefaultConstructor", "ReexportModule", "FixMissingRef"]),
26+
ExecuteCommandOptions(missing, collect(keys(LSActions))),
2727
false,
2828
true,
2929
WorkspaceOptions(WorkspaceFoldersOptions(true, true)),

0 commit comments

Comments
 (0)