feat: sub-info tree level hover
diff --git a/src/Lean/Parser/Term.lean b/src/Lean/Parser/Term.lean index 9d06b34..bf0f019 100644 --- a/src/Lean/Parser/Term.lean +++ b/src/Lean/Parser/Term.lean
@@ -84,12 +84,16 @@ @[builtinTermParser] def syntheticHole := leading_parser "?" >> (ident <|> hole) /-- A temporary placeholder for a missing proof or value. -/ @[builtinTermParser] def «sorry» := leading_parser "sorry" +/-- +A placeholder for an implicit lambda abstraction's variable. The lambda abstraction is scoped to the surrounding parentheses. +For example, `(· + ·)` is equivalent to `fun x y => x + y`. +-/ @[builtinTermParser] def cdot := leading_parser symbol "·" <|> "." def typeAscription := leading_parser " : " >> termParser def tupleTail := leading_parser ", " >> sepBy1 termParser ", " def parenSpecial : Parser := optional (tupleTail <|> typeAscription) /-- -You can use parentheses for +Parentheses, used for - Grouping expressions, e.g., `a * (b + c)`. - Creating tuples, e.g., `(a, b, c)` is notation for `Prod.mk a (Prod.mk b c)`. - Performing type ascription, e.g., `(0 : Int)` instructs Lean to process `0` as a value of type `Int`. @@ -140,16 +144,23 @@ `@e` for any term `e` also disables the insertion of implicit lambdas at this position. -/ @[builtinTermParser] def explicit := leading_parser "@" >> termParser maxPrec +/-- +`.(e)` marks an "inaccessible pattern", which does not influence evaluation of the pattern match, but may be necessary for type-checking. +In contrast to regular patterns, `e` may be an arbitrary term of the appropriate type. +-/ @[builtinTermParser] def inaccessible := leading_parser ".(" >> termParser >> ")" def binderIdent : Parser := ident <|> hole def binderType (requireType := false) : Parser := if requireType then node nullKind (" : " >> termParser) else optional (" : " >> termParser) def binderTactic := leading_parser atomic (symbol " := " >> " by ") >> Tactic.tacticSeq def binderDefault := leading_parser " := " >> termParser def explicitBinder (requireType := false) := ppGroup $ leading_parser "(" >> many1 binderIdent >> binderType requireType >> optional (binderTactic <|> binderDefault) >> ")" +/-- Implicit binder. In regular applications without `@`, it is automatically inserted and solved by unification whenever all explicit parameters before it are specified. -/ def implicitBinder (requireType := false) := ppGroup $ leading_parser "{" >> many1 binderIdent >> binderType requireType >> "}" def strictImplicitLeftBracket := atomic (group (symbol "{" >> "{")) <|> "⦃" def strictImplicitRightBracket := atomic (group (symbol "}" >> "}")) <|> "⦄" +/-- Strict-implicit binder. In contrast to `{ ... }` regular implicit binders, a strict-implicit binder is inserted automatically only when at least one subsequent explicit parameter is specified. -/ def strictImplicitBinder (requireType := false) := ppGroup $ leading_parser strictImplicitLeftBracket >> many1 binderIdent >> binderType requireType >> strictImplicitRightBracket +/-- Instance-implicit binder. In regular applications without `@`, it is automatically inserted and solved by typeclass inference of the specified class. -/ def instBinder := ppGroup $ leading_parser "[" >> optIdent >> termParser >> "]" def bracketedBinder (requireType := false) := withAntiquot (mkAntiquot "bracketedBinder" `Lean.Parser.Term.bracketedBinder (isPseudoKind := true)) <| explicitBinder requireType <|> strictImplicitBinder requireType <|> implicitBinder requireType <|> instBinder @@ -385,6 +396,15 @@ -- argument precedence is `arg` (i.e. does not accept `lead` precedence) @[builtinTermParser] def app := trailing_parser:leadPrec:maxPrec many1 argument +/-- +The *extended field notation* `e.f` is roughly short for `T.f e` where `T` is the type of `e`. +More precisely, +* if `e` is of a function type, `e.f` is translated to `Function.f (p := e)` where `p` is the first explicit parameter of function type +* if `e` is of a named type `T ...` and there is a declaration `T.f` (possibly from `export`), `e.f` is translated to `T.f (p := e)` where + `p` is the first explicit parameter of type `T ...` +* otherwise, if `e` is of a structure type, the above is repeated for every base type of the structure. + +The field index notation `e.i`, where `i` is a positive number, is short for accessing the `i`-th field (1-indexed) of `e` if it is of a structure type. -/ @[builtinTermParser] def proj := trailing_parser checkNoWsBefore >> "." >> checkNoWsBefore >> (fieldIdx <|> rawIdent) @[builtinTermParser] def completion := trailing_parser checkNoWsBefore >> "." @[builtinTermParser] def arrow := trailing_parser checkPrec 25 >> unicodeSymbol " → " " -> " >> termParser 25
diff --git a/src/Lean/Server/FileWorker/RequestHandling.lean b/src/Lean/Server/FileWorker/RequestHandling.lean index 717cd76..60a6e2b 100644 --- a/src/Lean/Server/FileWorker/RequestHandling.lean +++ b/src/Lean/Server/FileWorker/RequestHandling.lean
@@ -43,18 +43,34 @@ : RequestM (RequestTask (Option Hover)) := do let doc ← readDoc let text := doc.meta.text - let mkHover (s : String) (f : String.Pos) (t : String.Pos) : Hover := - { contents := { kind := MarkupKind.markdown - value := s } - range? := some { start := text.utf8PosToLspPos f - «end» := text.utf8PosToLspPos t } } + let mkHover (s : String) (r : String.Range) : Hover := { + contents := { + kind := MarkupKind.markdown + value := s + } + range? := r.toLspRange text + } let hoverPos := text.lspPosToUtf8Pos p.position withWaitFindSnap doc (fun s => s.endPos > hoverPos) (notFoundX := pure none) fun snap => do + -- try to find parser docstring from syntax tree + let stack? := snap.stx.findStack? (·.getRange?.any (·.contains hoverPos)) + let stxDoc? ← match stack? with + | some stack => stack.findSomeM? fun (stx, _) => do + return (← findDocString? snap.env stx.getKind).map (·, stx.getRange?.get!) + | none => pure none + + -- now try info tree if let some (ci, i) := snap.infoTree.hoverableInfoAt? hoverPos then - if let some hoverFmt ← i.fmtHover? ci then - return some <| mkHover (toString hoverFmt) i.pos?.get! i.tailPos?.get! + if let some range := i.range? then + -- prefer info tree if at least as specific as parser docstring + if stxDoc?.all fun (_, stxRange) => stxRange.includes range then + if let some hoverFmt ← i.fmtHover? ci then + return mkHover (toString hoverFmt) range + + if let some (doc, range) := stxDoc? then + return mkHover doc range return none
diff --git a/tests/lean/interactive/hover.lean b/tests/lean/interactive/hover.lean index ae0b3ef..18d2dff 100644 --- a/tests/lean/interactive/hover.lean +++ b/tests/lean/interactive/hover.lean
@@ -198,3 +198,6 @@ example : Nat := Id.run do (← 1) --^ textDocument/hover + +#check (· + ·) + --^ textDocument/hover
diff --git a/tests/lean/interactive/hover.lean.expected.out b/tests/lean/interactive/hover.lean.expected.out index aa67081..e02b5a4 100644 --- a/tests/lean/interactive/hover.lean.expected.out +++ b/tests/lean/interactive/hover.lean.expected.out
@@ -196,7 +196,13 @@ "contents": {"value": "My ultimate command ", "kind": "markdown"}} {"textDocument": {"uri": "file://hover.lean"}, "position": {"line": 115, "character": 10}} -null +{"range": + {"start": {"line": 115, "character": 8}, + "end": {"line": 115, "character": 14}}, + "contents": + {"value": + "Structure instance. `{ x := e, ... }` assigns `e` to field `x`, which may be\ninherited. If `e` is itself a variable called `x`, it can be elided:\n`fun y => { x := 1, y }`.\nA *structure update* of an existing value can be given via `with`:\n`{ point with x := 1 }`.\nThe structure type can be specified if not inferable:\n`{ x := 1, y := 2 : Point }`.\n", + "kind": "markdown"}} {"textDocument": {"uri": "file://hover.lean"}, "position": {"line": 119, "character": 8}} {"range": @@ -372,5 +378,13 @@ "end": {"line": 198, "character": 32}}, "contents": {"value": - "```lean\nId ℕ\n```\n***\nYou can use parentheses for\n- Grouping expressions, e.g., `a * (b + c)`.\n- Creating tuples, e.g., `(a, b, c)` is notation for `Prod.mk a (Prod.mk b c)`.\n- Performing type ascription, e.g., `(0 : Int)` instructs Lean to process `0` as a value of type `Int`.\n- Creating `Unit.unit`, `()` is just a shorthand for `Unit.unit`.\n- Creating simple functions when combined with `·`. Here are some examples:\n - `(· + 1)` is shorthand for `fun x => x + 1`\n - `(· + ·)` is shorthand for `fun x y => x + y`\n - `(f · a b)` is shorthand for `fun x => f x a b`\n - `(h (· + 1) ·)` is shorthand for `fun x => h (fun y => y + 1) x`\n", + "```lean\nId ℕ\n```\n***\nParentheses, used for\n- Grouping expressions, e.g., `a * (b + c)`.\n- Creating tuples, e.g., `(a, b, c)` is notation for `Prod.mk a (Prod.mk b c)`.\n- Performing type ascription, e.g., `(0 : Int)` instructs Lean to process `0` as a value of type `Int`.\n- Creating `Unit.unit`, `()` is just a shorthand for `Unit.unit`.\n- Creating simple functions when combined with `·`. Here are some examples:\n - `(· + 1)` is shorthand for `fun x => x + 1`\n - `(· + ·)` is shorthand for `fun x y => x + y`\n - `(f · a b)` is shorthand for `fun x => f x a b`\n - `(h (· + 1) ·)` is shorthand for `fun x => h (fun y => y + 1) x`\n", + "kind": "markdown"}} +{"textDocument": {"uri": "file://hover.lean"}, + "position": {"line": 201, "character": 8}} +{"range": + {"start": {"line": 201, "character": 8}, "end": {"line": 201, "character": 9}}, + "contents": + {"value": + "A placeholder for an implicit lambda abstraction's variable. The lambda abstraction is scoped to the surrounding parentheses.\nFor example, `(· + ·)` is equivalent to `fun x y => x + y`.\n", "kind": "markdown"}}