-
Notifications
You must be signed in to change notification settings - Fork 418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: structure auto-completion & partial InfoTrees #5835
base: master
Are you sure you want to change the base?
feat: structure auto-completion & partial InfoTrees #5835
Conversation
Mathlib CI status (docs):
|
Does this work after |
"Yes" to the first question, see the test file: https://github.com/mhuisi/lean4/blob/mhuisi/constructor-field-completion-2/tests/lean/interactive/completionStructureInstance.lean It doesn't work in the second case because Lake config declarations are a separate command with its own syntax and its own elaborator. For now, whitespace completions are specific to certain syntaxes (before macro expansion). |
Does this make it easy to implement the corresponding feature in lake? This has been a long standing feature request. |
The whitespace completion triggers on the |
@mhuisi It don't think using |
No, there is not. |
@mhuisi Ah, so this new completion is hardcoded to the parser and is not a matter of creating the right info-tree nodes? (I guess that is what you meant by "whitespace completions are specific to certain syntax" -- I was not fully sure whether that was simply due to support rather than hardcoding.) |
13ae691
to
f1801c5
Compare
This PR adds auto-completion for the fields of structure instance notation. Specifically, querying the completions via
Ctrl+Space
in the whitespace of a structure instance notation will now bring up the full list of fields. Whitespace structure completion can be enabled for custom syntax by wrapping the parser for the list of fields in astructInstFields
parser.This PR also makes completions of partial structure fields in the bracketed structure instance notation work again (e.g. completing
a
in{ a }
). These were previously broken by the introduction of set notation.In order to facilitate language server support for ambiguous syntax where all associated elaborators fail, this PR introduces the notion of an
InfoTree
choice node and anInfoTree
PartialTermInfo
node. Choice nodes are produced by the elaborator when several overloaded elaborators fail and contain theInfoTree
s of each failed elaborator as subtrees.PartialTermInfo
s are generated when an individual elaborator fails to produce anExpr
so that the subtrees produced by that elaborator can be retained as children of thatPartialTermInfo
.Since the
InfoTree
contains more similarCompletionInfo
s after the introduction of choice nodes, this PR reworks theCompletionInfo
selection to be more effective at selecting the bestCompletionInfo
amongst multiple similar ones.Finally, this PR splits
Completion.lean
into several files and fixes #5807.Breaking changes
Lean.Elab.withInfoContext'
andLean.Elab.Term.withInfoContext'
gained a new parametermkInfoOnError
that is used to produce anInfoTree
node when the elaboratorx
fails. In cases wheremkInfo
produces aTermInfo
usingLean.Elab.Term.mkTermInfo
,mkInfoOnError
should produce an equivalentPartialTermInfo
without anExpr
usingLean.Elab.Term.mkPartialTermInfo
.Lean.Elab.Info
gained two new alternatives:ofPartialTermInfo
andofChoiceInfo
.id
field of thefieldId
alternative ofLean.Elab.CompletionInfo
has been turned into anOption
withnone
representing a full field completion.structInst
andwhereStructInst
have been adjusted to use thestructInstFields
parser.Details of other changes
Init/Prelude.lean
andInit/Meta.lean
: Add two new functionsgetTrailing?
andgetTrailingTailPos?
to work with the trailing whitespace of aSourceInfo
/Syntax
.Lean/Elab/App.lean
:a.b.c
so that theCompletionInfo
s fora.b.c
reference the syntaxa
,a.b
anda.b.c
respectively, instead of all referencinga.b.c
. Before, theseCompletionInfo
s were indistinguishable and we only chose the right one by accident.mergeFailures
to produce anInfoTree
with anofChoiceInfo
root node and theInfoTree
s of all failed elaborators as subtrees instead of throwing them away.Lean/Elab/BuiltinTerm.lean
: AdjustelabCompletion
so that it always produces an identifierCompletionInfo
, even when elaborating the left hand side of a dot completion succeeds and we also add a dotCompletionInfo
to theInfoTree
. With the changes to theCompletionInfo
selection in the language server, the language server will now always pick the correct one of these two. Before, we had a special case in the language server dot completion where under certain circumstances we would treat it a dot completion as an identifier completion instead - with this change, this is now not necessary anymore.Lean/Elab/MutualDef.lean
: AdjustexpandWhereStructInst
to set reasonableSourceInfo
s for the produced bracketed structure instance notation. The structure field completion benefits from thisSourceInfo
.Lean/Server/RequestHandling.lean
: AdjusthandleCompletion
so that in the-
completion case, we also emitCompletionInfoData
. This fixes PANIC at Lean.Lsp.CompletionItem.getFileSource! Lean.Server.CompletionItemData:34:22: no data param on completion item #5807.Due to the changes to the
structInst
andwhereStructInst
parsers, this PR needs two stage0 updates.