Signature help, formatting, and a fix that writes a missing signature
- textDocument/signatureHelp: the function whose call the cursor is in, with the parameter that is being written marked. A definition shows its head as written; an import, a trait's method and a builtin show their type. It reads tokens as far as the cursor, so a call that is not closed has help. - textDocument/formatting: the whole text as bjo fmt lays it out. The server runs bjo -q fmt - on the buffer's text. - A quick fix at a function with no signature writes the one its body gives it, on a line above the definition (CheckDecl.observeMissingSignature, in the compiler). - The server says version 0.8.0. Needs the compiler at its commit "CheckDecl.observeMissingSignature". Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
parent
d421ec9363
commit
ce426b94df
8 changed files with 695 additions and 11 deletions
111
Program.fs
111
Program.fs
|
|
@ -515,6 +515,100 @@ let private actionsAt (uri: string) (first: Bjolang.Lexer.Position) (last: Bjola
|
|||
)
|
||||
:> JsonNode
|
||||
|
||||
/// `textDocument/signatureHelp`: the function whose call the position is in,
|
||||
/// its parameters, and the one that is being written.
|
||||
///
|
||||
/// - The function is the innermost form around the position whose head is a
|
||||
/// function this file sees: a definition of the file or of a module it
|
||||
/// sees, or a builtin. A special form or a macro is passed over, so that
|
||||
/// an argument written inside a `let` has the help of the call around it.
|
||||
/// - A definition shows its head as written, `(name a #:key default)`. An
|
||||
/// import of a .NET member, a method of a trait and a builtin show their
|
||||
/// type.
|
||||
/// - A local function has no help.
|
||||
let private signatureHelpAt (uri: string) (line: int) (character: int) : JsonNode =
|
||||
let path = uriToPath uri
|
||||
let text = (textOf uri).Replace("\r\n", "\n")
|
||||
|
||||
// The text as far as the position.
|
||||
let prefix =
|
||||
let lines = text.Split('\n')
|
||||
|
||||
if line >= lines.Length then
|
||||
text
|
||||
else
|
||||
let last = lines[line]
|
||||
|
||||
String.Join("\n", Array.append (Array.truncate line lines) [| last.Substring(0, min character last.Length) |])
|
||||
|
||||
let shapeOf (name: string) : SignatureHelp.Shape option =
|
||||
match Symbols.definition path text name with
|
||||
| Some found ->
|
||||
let m = found.Module
|
||||
let s = found.Symbol
|
||||
|
||||
SignatureHelp.formAt s.Signature m.Forms
|
||||
|> Option.bind (fun form ->
|
||||
if s.Kind = Symbols.Kind.Function && s.Detail.StartsWith "def" then
|
||||
SignatureHelp.ofHead m.Slice form
|
||||
else
|
||||
SignatureHelp.functionTypeIn form |> Option.bind (SignatureHelp.ofType m.Slice name))
|
||||
| None ->
|
||||
Hover.builtinDeclaration name
|
||||
|> Option.bind (fun declared ->
|
||||
Symbols.forms "" declared
|
||||
|> Option.bind List.tryHead
|
||||
|> Option.bind SignatureHelp.functionTypeIn
|
||||
|> Option.bind (SignatureHelp.ofType (Symbols.sliceOf declared) name))
|
||||
|
||||
let found =
|
||||
SignatureHelp.calls path prefix
|
||||
|> List.tryPick (fun call -> shapeOf call.Head |> Option.map (fun shape -> call, shape))
|
||||
|
||||
match found with
|
||||
| None -> null
|
||||
| Some(call, shape) ->
|
||||
let parameters =
|
||||
shape.Parameters
|
||||
|> List.map (fun (first, last) ->
|
||||
JsonObject(dict [ "label", JsonArray(JsonValue.Create first, JsonValue.Create last) :> JsonNode ]) :> JsonNode)
|
||||
|
||||
let signature =
|
||||
JsonObject(
|
||||
dict
|
||||
[ "label", JsonValue.Create shape.Label :> JsonNode
|
||||
"parameters", JsonArray(Array.ofList parameters) ]
|
||||
)
|
||||
|
||||
describe uri call.Head |> Option.iter (fun doc -> signature["documentation"] <- markdownContent doc)
|
||||
|
||||
let help =
|
||||
JsonObject(dict [ "signatures", JsonArray(signature) :> JsonNode; "activeSignature", JsonValue.Create 0 ])
|
||||
|
||||
// With no parameter to mark, as for an argument too many, the field
|
||||
// is left out. A client then marks the first one.
|
||||
SignatureHelp.active shape call |> Option.iter (fun index -> help["activeParameter"] <- JsonValue.Create index)
|
||||
help
|
||||
|
||||
/// `textDocument/formatting`: one edit that replaces the whole text with what
|
||||
/// `bjo fmt` makes of it, or no edit when the text is laid out already.
|
||||
let private formattingOf (uri: string) : Result<JsonNode, string> =
|
||||
let text = textOf uri
|
||||
|
||||
match Format.source text with
|
||||
| Error why -> Error why
|
||||
| Ok laidOut when laidOut = text -> Ok(JsonArray())
|
||||
| Ok laidOut ->
|
||||
let lines = text.Replace("\r\n", "\n").Split('\n')
|
||||
|
||||
let position (l: int) (c: int) =
|
||||
JsonObject(dict [ "line", JsonValue.Create l :> JsonNode; "character", JsonValue.Create c ]) :> JsonNode
|
||||
|
||||
let whole =
|
||||
JsonObject(dict [ "start", position 0 0; "end", position (lines.Length - 1) lines[lines.Length - 1].Length ])
|
||||
|
||||
Ok(JsonArray(JsonObject(dict [ "range", whole :> JsonNode; "newText", JsonValue.Create laidOut ])))
|
||||
|
||||
let private capabilities () : JsonNode =
|
||||
JsonNode.Parse
|
||||
"""
|
||||
|
|
@ -528,8 +622,10 @@ let private capabilities () : JsonNode =
|
|||
"referencesProvider": true,
|
||||
"documentHighlightProvider": true,
|
||||
"codeActionProvider": { "codeActionKinds": [ "quickfix" ] },
|
||||
"signatureHelpProvider": { "triggerCharacters": [ "(", " " ] },
|
||||
"documentFormattingProvider": true,
|
||||
"completionProvider": { "resolveProvider": true, "triggerCharacters": [ "(" ] } },
|
||||
"serverInfo": { "name": "bjolang-lsp", "version": "0.7.0" } }
|
||||
"serverInfo": { "name": "bjolang-lsp", "version": "0.8.0" } }
|
||||
"""
|
||||
|
||||
[<EntryPoint>]
|
||||
|
|
@ -662,6 +758,19 @@ let main _ =
|
|||
|
||||
Rpc.respond id (uri () |> Option.map (fun u -> completionAt u line character) |> Option.defaultValue null)
|
||||
| "completionItem/resolve" -> Rpc.respond id (resolve message["params"])
|
||||
| "textDocument/signatureHelp" ->
|
||||
let position = message["params"]["position"]
|
||||
let line = position["line"].GetValue<int>()
|
||||
let character = position["character"].GetValue<int>()
|
||||
|
||||
Rpc.respond id (uri () |> Option.map (fun u -> signatureHelpAt u line character) |> Option.defaultValue null)
|
||||
// A text that cannot be laid out is an error answer, with
|
||||
// what `bjo fmt` says of it.
|
||||
| "textDocument/formatting" ->
|
||||
match uri () |> Option.map formattingOf with
|
||||
| Some(Ok edits) -> Rpc.respond id edits
|
||||
| Some(Error why) -> Rpc.respondError id -32803 why
|
||||
| None -> Rpc.respond id null
|
||||
| "textDocument/references"
|
||||
| "textDocument/documentHighlight" ->
|
||||
let p = message["params"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue