Skip to content

Explore type based autocomplete #493

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

Closed
wants to merge 81 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
7db9012
add base test case for completing jsx prop and labelled argument
zth Jul 14, 2022
4362bdf
commit the rest of the updated test result files
zth Jul 14, 2022
87508b3
fix accidentally broken completion example
zth Jul 14, 2022
5ed8bb4
identify typed context for labelled argument assignments
zth Jul 15, 2022
d121e73
remove special handling of equality char
zth Jul 16, 2022
57e7cce
use trick from JSX prop completion to identify empty assignment of la…
zth Jul 16, 2022
b125207
crude implementation of completing variant constructors
zth Jul 17, 2022
431ab48
rename and specialize test file so that it will only contain tests fo…
zth Jul 17, 2022
bdd2e11
generalize completion so it theoretically can handle more than variants
zth Jul 17, 2022
bea78fe
add assigned identifier to test file
zth Jul 17, 2022
5ebe602
add more args to test function in test file
zth Jul 17, 2022
b85c4ae
filter completed variant constructors on what the user has already wr…
zth Jul 17, 2022
dd8333e
add example completing variant from other file
zth Jul 17, 2022
ffa06f4
complete polyvariants in addition to regular variants
zth Jul 17, 2022
37e4f9b
clarifications and touch ups after pr comments
zth Jul 18, 2022
c2e2adf
add dedicated example file for JSX completions
zth Jul 18, 2022
d1d86e8
inline contextPath into NamedArg to pave way for JsxProp, that will n…
zth Jul 18, 2022
90ccf19
add jsx example completing across files
zth Jul 19, 2022
d54b562
basic JSX prop completion via type
zth Jul 18, 2022
2b29291
add completions from context in addition to the explicit type based c…
zth Jul 19, 2022
27532b0
bring contextual completions to labelled arguments as well
zth Jul 19, 2022
e87ad5a
remove redundant comment#
zth Jul 19, 2022
f79a38d
Merge branch 'master' into type-based-autocomplete
zth Jul 25, 2022
b260d38
take a step back and remove typed completions for now. focus on ident…
zth Jul 27, 2022
54e596c
add some completion cases for records
zth Jul 30, 2022
8c49fa5
refactor to continuously setting the typed context we're currently lo…
zth Jul 30, 2022
c8f112a
fix + debug util
zth Jul 30, 2022
49e45d8
identify and complete for record fields when destructuring
zth Jul 30, 2022
7c8a1ad
allow any expr, which makes fn returns also work
zth Jul 30, 2022
b34c35d
basic, slightly buggy completion for nested record fields
zth Jul 31, 2022
7b1b8cd
do not complete if we cant find the cursor inside of nested pattern
zth Aug 1, 2022
f79f21b
add more broken test cases
zth Aug 1, 2022
0df2b0c
ensure completing from empty root records work as expected
zth Aug 1, 2022
33cd645
look for ',' as first char before cursor to trigger completion in cas…
zth Aug 1, 2022
f21a06e
refactor
zth Aug 1, 2022
44698e4
refactor completion to use new way of recursively digging to the targ…
zth Aug 2, 2022
48b7bec
start refactoring the typed context
zth Aug 2, 2022
295be6b
add another label to the record so we can test prefix etc in a better…
zth Aug 2, 2022
5a2097e
bring back prefix and seen record fields filtering for destructuring …
zth Aug 2, 2022
da23ca8
change test message some
zth Aug 2, 2022
39a35ec
more completion exploration
zth Aug 4, 2022
f9515d1
cleanup and comments on where I'm at right now
zth Aug 4, 2022
a268d24
narrow the places where type based completion is invoked
zth Aug 9, 2022
9f511cd
complete record field destructuring in switch patterns
zth Aug 9, 2022
ef427d2
comment
zth Aug 9, 2022
555110f
wip
zth Aug 10, 2022
cf8b472
more work on nested completion in patterns
zth Aug 10, 2022
3a6a6e5
pick up already seen idents for constructors/record fields
zth Aug 11, 2022
339417c
basic completion of options
zth Aug 11, 2022
928e84e
complete empty | patterns
zth Aug 11, 2022
d134b8b
fix only descending to record field if valid
zth Aug 11, 2022
0d06fc6
more filtering of idents
zth Aug 11, 2022
d2ef230
fix record completion of empty ,
zth Aug 11, 2022
013680e
temp disable local completions + constructor completions in general s…
zth Aug 11, 2022
303eb57
commit test output from last commit
zth Aug 11, 2022
858be8f
handle completing empty patterns in records
zth Aug 11, 2022
d5ced33
complete booleans
zth Aug 11, 2022
1679769
more work, plugging holes etc
zth Aug 13, 2022
f262a50
complete patterns in arguments of fns passed to named arguments
zth Aug 13, 2022
28dddca
rename
zth Aug 14, 2022
8a98793
add debug tool and start setting up spec for typed context completion
zth Aug 15, 2022
5647a1e
start moving to rewrite of finding pattern completable
zth Aug 15, 2022
2c1b721
hook up completion backend with new pattern completion stuff
zth Aug 16, 2022
5d69adb
complete clean up of the old way of doing pattern completion
zth Aug 16, 2022
f061119
simplify
zth Aug 16, 2022
82796f9
complete empty tuples
zth Aug 16, 2022
6a0a884
introduce snippets
zth Aug 16, 2022
f98db04
properly apply command
zth Aug 16, 2022
5558995
implement support for tuple context paths in completion
zth Aug 16, 2022
5320d9e
dump pexp_field
zth Aug 16, 2022
36832cc
add typed expression completion type
zth Aug 17, 2022
eb3eb80
some cleanup and other fixes
zth Aug 17, 2022
859c3fd
track seen record fields when constructing a record
zth Aug 17, 2022
e67f63c
add a few test fixtures where the parser doesnt do quite what I need …
zth Aug 18, 2022
64f4f77
another broken case
zth Aug 20, 2022
40e3ec1
support completing expressions when assigning variables, even if the …
zth Aug 20, 2022
e330b68
pull put var from constraint pattern for now
zth Aug 20, 2022
fc48ca8
more cases
zth Aug 20, 2022
cd2161a
make sure the newest env is always propagated
zth Sep 2, 2022
0522d01
fix issue where Some variants wouldnt be handled properly
zth Sep 7, 2022
16a6f0f
complete inline records
zth Sep 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions analysis/tests/src/TypeContextCompletion.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type someVariant = One | Two | Three | Four

let someVariantToString = (~someVariant) =>
switch someVariant {
| One => "One"
| Two => "Two"
| Three => "Three"
| Four => "Four"
}

// let x = someVariantToString(~someVariant=
// ^com

module SomeComponent = {
@react.component
let make = (~whatever) => {
someVariantToString(~someVariant=whatever)->React.string
}
}

// let jsx = <SomeComponent whatever=
// ^com
6 changes: 6 additions & 0 deletions analysis/tests/src/expected/Completion.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,12 @@ Completable: Cpath Value[T]
"tags": [],
"detail": "file module",
"documentation": null
}, {
"label": "TypeContextCompletion",
"kind": 9,
"tags": [],
"detail": "file module",
"documentation": null
}, {
"label": "TypeDefinition",
"kind": 9,
Expand Down
2 changes: 1 addition & 1 deletion analysis/tests/src/expected/Dce.res.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
DCE src/Dce.res
issues:235
issues:241
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should get rid of these differences. This is testing several things at once and it's not necessary.


3 changes: 2 additions & 1 deletion analysis/tests/src/expected/Debug.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Dependencies: @rescript/react
Source directories: ./node_modules/@rescript/react/src ./node_modules/@rescript/react/src/legacy
Source files: ./node_modules/@rescript/react/src/React.res ./node_modules/@rescript/react/src/ReactDOM.res ./node_modules/@rescript/react/src/ReactDOMServer.res ./node_modules/@rescript/react/src/ReactDOMStyle.res ./node_modules/@rescript/react/src/ReactEvent.res ./node_modules/@rescript/react/src/ReactEvent.resi ./node_modules/@rescript/react/src/ReactTestUtils.res ./node_modules/@rescript/react/src/ReactTestUtils.resi ./node_modules/@rescript/react/src/RescriptReactErrorBoundary.res ./node_modules/@rescript/react/src/RescriptReactErrorBoundary.resi ./node_modules/@rescript/react/src/RescriptReactRouter.res ./node_modules/@rescript/react/src/RescriptReactRouter.resi ./node_modules/@rescript/react/src/legacy/ReactDOMRe.res ./node_modules/@rescript/react/src/legacy/ReasonReact.res
Source directories: ./src ./src/expected
Source files: ./src/Auto.res ./src/CompletePrioritize1.res ./src/CompletePrioritize2.res ./src/Completion.res ./src/Component.res ./src/Component.resi ./src/CreateInterface.res ./src/Cross.res ./src/Dce.res ./src/Debug.res ./src/Definition.res ./src/DefinitionWithInterface.res ./src/DefinitionWithInterface.resi ./src/Div.res ./src/DocumentSymbol.res ./src/Fragment.res ./src/Highlight.res ./src/Hover.res ./src/Jsx.res ./src/Jsx.resi ./src/LongIdentTest.res ./src/Object.res ./src/Patterns.res ./src/RecModules.res ./src/RecordCompletion.res ./src/RecoveryOnProp.res ./src/References.res ./src/ReferencesWithInterface.res ./src/ReferencesWithInterface.resi ./src/Rename.res ./src/RenameWithInterface.res ./src/RenameWithInterface.resi ./src/TableclothMap.ml ./src/TableclothMap.mli ./src/TypeDefinition.res ./src/Xform.res
Source files: ./src/Auto.res ./src/CompletePrioritize1.res ./src/CompletePrioritize2.res ./src/Completion.res ./src/Component.res ./src/Component.resi ./src/CreateInterface.res ./src/Cross.res ./src/Dce.res ./src/Debug.res ./src/Definition.res ./src/DefinitionWithInterface.res ./src/DefinitionWithInterface.resi ./src/Div.res ./src/DocumentSymbol.res ./src/Fragment.res ./src/Highlight.res ./src/Hover.res ./src/Jsx.res ./src/Jsx.resi ./src/LongIdentTest.res ./src/Object.res ./src/Patterns.res ./src/RecModules.res ./src/RecordCompletion.res ./src/RecoveryOnProp.res ./src/References.res ./src/ReferencesWithInterface.res ./src/ReferencesWithInterface.resi ./src/Rename.res ./src/RenameWithInterface.res ./src/RenameWithInterface.resi ./src/TableclothMap.ml ./src/TableclothMap.mli ./src/TypeContextCompletion.res ./src/TypeDefinition.res ./src/Xform.res
Impl cmt:./lib/bs/src/Auto.cmt res:./src/Auto.res
Impl cmt:./lib/bs/src/CompletePrioritize1.cmt res:./src/CompletePrioritize1.res
Impl cmt:./lib/bs/src/CompletePrioritize2.cmt res:./src/CompletePrioritize2.res
Expand Down Expand Up @@ -33,6 +33,7 @@ IntfAndImpl cmti:./lib/bs/src/ReferencesWithInterface.cmti resi:./src/References
Impl cmt:./lib/bs/src/Rename.cmt res:./src/Rename.res
IntfAndImpl cmti:./lib/bs/src/RenameWithInterface.cmti resi:./src/RenameWithInterface.resi cmt:./lib/bs/src/RenameWithInterface.cmt res:./src/RenameWithInterface.res
IntfAndImpl cmti:./lib/bs/src/TableclothMap.cmti resi:./src/TableclothMap.mli cmt:./lib/bs/src/TableclothMap.cmt res:./src/TableclothMap.ml
Impl cmt:./lib/bs/src/TypeContextCompletion.cmt res:./src/TypeContextCompletion.res
Impl cmt:./lib/bs/src/TypeDefinition.cmt res:./src/TypeDefinition.res
Impl cmt:./lib/bs/src/Xform.cmt res:./src/Xform.res
Dependency dirs: ./node_modules/@rescript/react/lib/bs/src ./node_modules/@rescript/react/lib/bs/src/legacy
Expand Down
12 changes: 12 additions & 0 deletions analysis/tests/src/expected/TypeContextCompletion.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Complete src/TypeContextCompletion.res 10:44
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see what's going on here first.
This is the position of the cursor: 10:44 unfortunately expressed in 0-based lsp format so you need to do some +1 gymnastics when looking in the actual editor

But if you look at 11:45 in the opened file in the editor that's where ^ is pointing.

posCursor:[10:44] posNoWhite:[10:43] Found expr:[10:11->18:1]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, since the cursor in autocomplete is always after what you want to complete, we need to go back to find anything interesting. And eat up any white space. In this case, just one character back to 10:43.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have hit an AST expression which goes from line 10 to 18. Next we'll dig deeper into that expression.

Why so many lines? That's parser error recovery, which apparently is taking in also the module definition.
Let's see if this gets in the way. If so, we'll remove the second part of the example and start again. As dealing with parser recovery is orthogonal to this.

Pexp_apply ...[10:11->10:30] (~someVariant10:32->10:43=...[13:0->17:3])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Digging inside, we found a sub-expression that is an application.
This is looking interesting. The cursor points right after the label someVariant.

Completable: CnamedArg(Value[someVariantToString], "", [someVariant])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is where it gets a bit hairy. Not sure why it decides it's a named arg completion given that it's in the expression assigned to the label. It could be because that expression is exactly where parser recovery happened and some heuristic is needed because of the uncertainty that parser recovery brings.
Something orthogonal, but need to figure it out in order to understand this example.

Anyhow the CnamedArg completion finds the function name, and the labels already visited: [someVariant] so they are not repeated. This is not going to find anything as it's the only label.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK looking at the code, I think we're hitting this case:

    | [] ->
      if posAfterFunExpr <= posBeforeCursor && posBeforeCursor < endPos then
        Some (CnamedArg (contextPath, "", allNames))

So we went through the entire application and did not find anything interesting. So we'll just default to named arg completion.
This can anyway be overridden by visiting sub-expressions later. The innermost AST expression where something interesting is found wins. Because the innermost is the most precise hit of the cursor.

To see why this case is needed one can just turn it off and see what tests fail.
Looks like this is one such example: let x = Lib.foo(~

Found type for function (~someVariant: someVariant) => string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to check where this is coming from: presumably named arg completion finding the type of the function.

[]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As predicted, no result.


Complete src/TypeContextCompletion.res 20:37
posCursor:[20:37] posNoWhite:[20:36] Found expr:[20:14->20:37]
JSX <SomeComponent:[20:14->20:27] whatever[20:28->20:36]=...__ghost__[0:-1->0:-1]> _children:None
[]