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
266
SignatureHelp.fs
Normal file
266
SignatureHelp.fs
Normal file
|
|
@ -0,0 +1,266 @@
|
|||
/// Signature help: the parameters of the function whose call the cursor is in,
|
||||
/// and which of them is being written.
|
||||
///
|
||||
/// It reads the text as far as the cursor as tokens, not as forms, so that a
|
||||
/// call that is not closed yet, which is the usual case, has help.
|
||||
module BjolangLsp.SignatureHelp
|
||||
|
||||
open System
|
||||
open System.Text
|
||||
open System.Text.RegularExpressions
|
||||
open Bjolang
|
||||
open Bjolang.Lexer
|
||||
open Bjolang.Ast
|
||||
|
||||
/// A call around the cursor.
|
||||
type Call =
|
||||
{ /// The symbol at the head of the form.
|
||||
Head: string
|
||||
/// The arguments before the one that is being written: for `#:name`
|
||||
/// its name, for each other argument `None`.
|
||||
Before: string option list
|
||||
/// The argument that is being written, when the cursor is at the end
|
||||
/// of `#:name`: that name.
|
||||
Writing: string option }
|
||||
|
||||
type private Frame =
|
||||
{ Open: Token
|
||||
mutable HeadSeen: bool
|
||||
mutable Head: string option
|
||||
Items: ResizeArray<string option> }
|
||||
|
||||
/// The calls around the end of `prefix`, the text of a file as far as the
|
||||
/// cursor: the innermost first. A form whose head is not a symbol, and a
|
||||
/// `[...]` or `{...}`, is not a call, and is the argument that is being
|
||||
/// written of the call around it.
|
||||
let calls (path: string) (prefix: string) : Call list =
|
||||
let tokens =
|
||||
try
|
||||
Session.isolated (fun () -> Lexer.tokenize path prefix)
|
||||
with _ ->
|
||||
// The cursor is in a string that is not closed, or in text that
|
||||
// the tokenizer refuses.
|
||||
[]
|
||||
|
||||
let frames = Collections.Generic.Stack<Frame>()
|
||||
|
||||
// Whether the last token is an argument that the cursor can be at the
|
||||
// end of, and of the innermost form.
|
||||
let mutable lastIsArgument = false
|
||||
|
||||
let item (kind: string option) (isSymbol: string option) =
|
||||
if frames.Count > 0 then
|
||||
let f = frames.Peek()
|
||||
|
||||
if not f.HeadSeen then
|
||||
f.HeadSeen <- true
|
||||
f.Head <- isSymbol
|
||||
lastIsArgument <- false
|
||||
else
|
||||
f.Items.Add kind
|
||||
lastIsArgument <- true
|
||||
|
||||
for t in tokens do
|
||||
match t.Token with
|
||||
| LParen
|
||||
| LBracket
|
||||
| LBrace ->
|
||||
frames.Push
|
||||
{ Open = t.Token
|
||||
HeadSeen = false
|
||||
Head = None
|
||||
Items = ResizeArray() }
|
||||
|
||||
lastIsArgument <- false
|
||||
| RParen
|
||||
| RBracket
|
||||
| RBrace ->
|
||||
if frames.Count > 0 then
|
||||
frames.Pop() |> ignore
|
||||
// A closed form is one argument of the form around it, and
|
||||
// the cursor after it starts the next one.
|
||||
item None None
|
||||
lastIsArgument <- false
|
||||
// A prefix of the form after it.
|
||||
| Hash
|
||||
| Quote
|
||||
| SynQuote
|
||||
| Comma
|
||||
| CommaAt
|
||||
| Spread -> ()
|
||||
| Keyword k -> item (Some k) None
|
||||
| Symbol n -> item None (Some n)
|
||||
| _ -> item None None
|
||||
|
||||
// The cursor is at the end of the last token when no space is between
|
||||
// them.
|
||||
let atEndOfLast =
|
||||
match List.tryLast tokens with
|
||||
| Some t ->
|
||||
let lines = prefix.Replace("\r\n", "\n").Split('\n')
|
||||
lastIsArgument && t.Range.End.Line = lines.Length && t.Range.End.Column = lines[lines.Length - 1].Length
|
||||
| None -> false
|
||||
|
||||
frames
|
||||
|> Seq.mapi (fun depth f ->
|
||||
match f.Open, f.Head with
|
||||
| LParen, Some head ->
|
||||
let items = List.ofSeq f.Items
|
||||
|
||||
// Only the innermost form has an argument that the cursor is at
|
||||
// the end of. In each form around it, the form inside is the
|
||||
// argument that is being written.
|
||||
if depth = 0 && atEndOfLast then
|
||||
let before = List.truncate (items.Length - 1) items
|
||||
|
||||
Some
|
||||
{ Head = head
|
||||
Before = before
|
||||
Writing = List.last items }
|
||||
else
|
||||
Some
|
||||
{ Head = head
|
||||
Before = items
|
||||
Writing = None }
|
||||
| _ -> None)
|
||||
|> Seq.choose id
|
||||
|> List.ofSeq
|
||||
|
||||
/// The parameters of a function, as a label that shows them and where each
|
||||
/// one is in it.
|
||||
type Shape =
|
||||
{ Label: string
|
||||
/// Each parameter: where it starts in the label and where it ends.
|
||||
Parameters: (int * int) list
|
||||
/// The positional parameters, as indexes of `Parameters`, in order.
|
||||
Positional: int list
|
||||
/// The keyword parameters, by name, as indexes of `Parameters`.
|
||||
Keywords: Map<string, int>
|
||||
/// The `#:rest` parameter, as an index of `Parameters`.
|
||||
Rest: int option }
|
||||
|
||||
let private oneLine (text: string) = Regex.Replace(text.Trim(), @"\s+", " ")
|
||||
|
||||
/// A shape from `parts`: the text before the parameters, each parameter as
|
||||
/// its text and what it is, and the text after them.
|
||||
type private Part =
|
||||
| Positional of string
|
||||
| Keyword of string * string
|
||||
| Rest of string
|
||||
|
||||
let private shape (before: string) (parts: Part list) (after: string) : Shape =
|
||||
let label = StringBuilder(before)
|
||||
let parameters = ResizeArray<int * int>()
|
||||
let positional = ResizeArray<int>()
|
||||
let mutable keywords = Map.empty
|
||||
let mutable rest = None
|
||||
|
||||
for part in parts do
|
||||
label.Append ' ' |> ignore
|
||||
let start = label.Length
|
||||
|
||||
let text =
|
||||
match part with
|
||||
| Positional t
|
||||
| Keyword(_, t)
|
||||
| Rest t -> t
|
||||
|
||||
label.Append text |> ignore
|
||||
let index = parameters.Count
|
||||
parameters.Add((start, label.Length))
|
||||
|
||||
match part with
|
||||
| Positional _ -> positional.Add index
|
||||
| Keyword(name, _) -> keywords <- Map.add name index keywords
|
||||
| Rest _ -> rest <- Some index
|
||||
|
||||
label.Append after |> ignore
|
||||
|
||||
{ Label = label.ToString()
|
||||
Parameters = List.ofSeq parameters
|
||||
Positional = List.ofSeq positional
|
||||
Keywords = keywords
|
||||
Rest = rest }
|
||||
|
||||
/// The shape of a definition's head, `(name a (: b int) #:key default #:rest
|
||||
/// more)`, with each parameter as it is written. `slice` gives the source of
|
||||
/// a range.
|
||||
let ofHead (slice: Range -> string) (head: SExpr) : Shape option =
|
||||
match head with
|
||||
| SList(SAtom { Token = Symbol name } :: items, _) ->
|
||||
let text (s: SExpr) = oneLine (slice (getRange s))
|
||||
|
||||
let rec parts (items: SExpr list) =
|
||||
match items with
|
||||
| SAtom { Token = Lexer.Keyword "rest" } :: more :: tail -> Rest("#:rest " + text more) :: parts tail
|
||||
| SAtom { Token = Lexer.Keyword k } :: fallback :: tail ->
|
||||
Keyword(k, $"#:%s{k} " + text fallback) :: parts tail
|
||||
| one :: tail -> Positional(text one) :: parts tail
|
||||
| [] -> []
|
||||
|
||||
Some(shape ("(" + name) (parts items) ")")
|
||||
| _ -> None
|
||||
|
||||
/// The shape of a function type, `(-> a (#:key T) #:rest T result)`, for a
|
||||
/// function that has no head to show: an import of a .NET member, a method of
|
||||
/// a trait, a builtin. `None` for a type that is not a function's.
|
||||
let ofType (slice: Range -> string) (name: string) (functionType: SExpr) : Shape option =
|
||||
match functionType with
|
||||
| SList(SAtom { Token = Symbol("->" | "-bjo->" | "-?->" as arrow) } :: items, _) when not items.IsEmpty ->
|
||||
let text (s: SExpr) = oneLine (slice (getRange s))
|
||||
let result = List.last items
|
||||
|
||||
let rec parts (items: SExpr list) =
|
||||
match items with
|
||||
| SAtom { Token = Lexer.Keyword "rest" } :: more :: tail -> Rest("#:rest " + text more) :: parts tail
|
||||
| (SList(SAtom { Token = Lexer.Keyword k } :: _, _) as one) :: tail -> Keyword(k, text one) :: parts tail
|
||||
| one :: tail -> Positional(text one) :: parts tail
|
||||
| [] -> []
|
||||
|
||||
Some(shape $"(: %s{name} (%s{arrow}" (parts (List.truncate (items.Length - 1) items)) $" %s{text result}))")
|
||||
| _ -> None
|
||||
|
||||
/// The first function type in `form`, outermost first.
|
||||
let rec functionTypeIn (form: SExpr) : SExpr option =
|
||||
match form with
|
||||
| SList(SAtom { Token = Symbol("->" | "-bjo->" | "-?->") } :: _, _) -> Some form
|
||||
| SList(items, _) -> items |> List.tryPick functionTypeIn
|
||||
| SAtom _ -> None
|
||||
|
||||
/// The form of `forms` that has the range `r`.
|
||||
let rec formAt (r: Range) (forms: SExpr list) : SExpr option =
|
||||
forms
|
||||
|> List.tryPick (fun form ->
|
||||
let fr = getRange form
|
||||
|
||||
if fr.Start = r.Start && fr.End = r.End && fr.File = r.File then
|
||||
Some form
|
||||
else
|
||||
match form with
|
||||
| SList(items, _) -> formAt r items
|
||||
| SAtom _ -> None)
|
||||
|
||||
/// The parameter of `shape` that `call` is writing, as an index of the
|
||||
/// shape's parameters. `None` when the call has more arguments than the
|
||||
/// function has parameters, or a keyword that the function does not have.
|
||||
let active (shape: Shape) (call: Call) : int option =
|
||||
// The arguments before: how many are positional, and the keyword whose
|
||||
// value is not written yet, if the last of them is one.
|
||||
let rec walk (items: string option list) (positional: int) =
|
||||
match items with
|
||||
| Some k :: [] -> positional, Some k
|
||||
| Some _ :: _ :: tail -> walk tail positional
|
||||
| None :: tail -> walk tail (positional + 1)
|
||||
| [] -> positional, None
|
||||
|
||||
let positional, pending = walk call.Before 0
|
||||
|
||||
match pending, call.Writing with
|
||||
// The value of a keyword.
|
||||
| Some k, _ -> Map.tryFind k shape.Keywords
|
||||
// The keyword itself.
|
||||
| None, Some k -> Map.tryFind k shape.Keywords
|
||||
| None, None ->
|
||||
match List.tryItem positional shape.Positional with
|
||||
| Some index -> Some index
|
||||
| None -> shape.Rest
|
||||
Loading…
Add table
Add a link
Reference in a new issue