Skip to content

Commit

Permalink
WIP action to update sig
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed May 16, 2022
1 parent bce3cdb commit 07ab44e
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions src/requests/actions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -507,16 +507,20 @@ function is_parent_of(parent::EXPR, child::EXPR)
return false
end

function is_in_function_signature_without_docstring(x::EXPR, _)
function is_in_function_signature(x::EXPR, params; with_docstring=false)
func = _get_parent_fexpr(x, CSTParser.defines_function)
func === nothing && return false
sig = func.args[1]
hasdoc = func.parent isa EXPR && func.parent.head === :macrocall && func.parent.args[1] isa EXPR && func.parent.args[1].head === :globalrefdoc
return !hasdoc && (x.head === :FUNCTION || is_parent_of(sig, x))
if x.head === :FUNCTION || is_parent_of(sig, x)
hasdoc = func.parent isa EXPR && func.parent.head === :macrocall && func.parent.args[1] isa EXPR &&
func.parent.args[1].head === :globalrefdoc
return with_docstring == hasdoc
end
return false
end

function add_docstring_template(x, _, conn)
is_in_function_signature_without_docstring(x, nothing) || return
is_in_function_signature(x, nothing) || return
func = _get_parent_fexpr(x, CSTParser.defines_function)
func === nothing && return
file, func_offset = get_file_loc(func)
Expand All @@ -530,6 +534,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 = headof(docstr_expr) === :TRIPLESTRING ? "" : "\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 @@ -624,6 +656,14 @@ LSActions["AddDocstringTemplate"] = ServerAction(
"Add docstring template for this method",
missing,
missing,
is_in_function_signature_without_docstring,
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,
)

0 comments on commit 07ab44e

Please sign in to comment.