Skip to content

Commit

Permalink
Add an action to update the method signature in a docstring attached …
Browse files Browse the repository at this point in the history
…to the method.
  • Loading branch information
fredrikekre committed May 20, 2022
1 parent 3ed0d38 commit 500605b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/requests/actions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,34 @@ function add_docstring_template(x, _, conn)
return
end

function update_docstring_sig(x, _, conn)
is_in_function_signature(x, nothing; with_docstring=true) || return
func = _get_parent_fexpr(x, CSTParser.defines_function)
# Current docstring
docstr_expr = func.parent.args[3]
docstr = valof(docstr_expr)
file, docstr_offset = get_file_loc(docstr_expr)
# New signature in the code
sig = func.args[1]
_, sig_offset = get_file_loc(sig)
sig_str = get_text(file)[sig_offset .+ (1:sig.span)]
# Heuristic for finding a signature in the current docstring
reg = r"\A .*$"m
if (m = match(reg, valof(docstr_expr)); m !== nothing)
docstr = replace(docstr, reg => string(" ", sig_str))
else
docstr = string(" ", sig_str, "\n\n", docstr)
end
newline = endswith(docstr, "\n") ? "" : "\n"
# Rewrap in """"
docstr = string("\"\"\"\n", docstr, newline, "\"\"\"")
tde = TextDocumentEdit(VersionedTextDocumentIdentifier(get_uri(file), get_version(file)), TextEdit[
TextEdit(Range(file, docstr_offset .+ (0:docstr_expr.span)), docstr)
])
JSONRPC.send(conn, workspace_applyEdit_request_type, ApplyWorkspaceEditParams(missing, WorkspaceEdit(missing, TextDocumentEdit[tde])))
return
end

# Adding a CodeAction requires defining:
# * a unique id
# * a description
Expand Down Expand Up @@ -730,3 +758,12 @@ LSActions["AddDocstringTemplate"] = ServerAction(
is_in_function_signature,
add_docstring_template,
)

LSActions["UpdateDocstringSignature"] = ServerAction(
"UpdateDocstringSignature",
"Update method signature in docstring",
missing,
missing,
(args...) -> is_in_function_signature(args...; with_docstring=true),
update_docstring_sig,
)
28 changes: 28 additions & 0 deletions test/requests/actions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,31 @@ end
c = filter(c -> c.command == "AddDocstringTemplate", action_request_test(0, 1))[1]
LanguageServer.workspace_executeCommand_request(LanguageServer.ExecuteCommandParams(missing, c.command, c.arguments), server, server.jr_endpoint)
end

@testset "Update docstring signature" begin
doc = settestdoc("""
"hello"
f(x) = x
\"\"\"hello\"\"\"
g(x) = x
\"\"\"
h()
hello
\"\"\"
function h(x)
end
i(x) = x
""")

@test any(c.command == "UpdateDocstringSignature" for c in action_request_test(1, 0))
@test any(c.command == "UpdateDocstringSignature" for c in action_request_test(4, 0))
@test any(c.command == "UpdateDocstringSignature" for c in action_request_test(11, 0))
@test !any(c.command == "UpdateDocstringSignature" for c in action_request_test(14, 0))

c = filter(c -> c.command == "UpdateDocstringSignature", action_request_test(1, 0))[1]
LanguageServer.workspace_executeCommand_request(LanguageServer.ExecuteCommandParams(missing, c.command, c.arguments), server, server.jr_endpoint)
end

0 comments on commit 500605b

Please sign in to comment.