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 4 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
1 change: 1 addition & 0 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,7 @@ let processCompletable ~debug ~package ~scope ~env ~pos ~forHover
in
match completable with
| Cnone -> []
| CtypedContext _contextPath -> []
| Cpath contextPath ->
contextPath
|> getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
Expand Down
14 changes: 12 additions & 2 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ type label = labelled option
type arg = {label: label; exp: Parsetree.expression}

let findNamedArgCompletable ~(args : arg list) ~endPos ~posBeforeCursor
~(contextPath : Completable.contextPath) ~posAfterFunExpr =
~(contextPath : Completable.contextPath) ~posAfterFunExpr ~charBeforeCursor
~debug =
let allNames =
List.fold_right
(fun arg allLabels ->
Expand All @@ -149,11 +150,19 @@ let findNamedArgCompletable ~(args : arg list) ~endPos ~posBeforeCursor
let rec loop args =
match args with
| {label = Some labelled; exp} :: rest ->
(* Figure out if we're completing the labelled argument name, or assigning to the labelled argument *)
if
labelled.posStart <= posBeforeCursor
&& posBeforeCursor < labelled.posEnd
&& charBeforeCursor <> Some '='
(* We need to account for empty assignments being parsed as regular labelled arguments without an assignment *)
then Some (Completable.CnamedArg (contextPath, labelled.name, allNames))
else if exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then None
else if
exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor
|| charBeforeCursor = Some '='
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 go slowly and stick to the script.
This is what you do when there is no other choice.
I would like to see how far we can go before trying to do this.

Also, we don't know if it's the character before the cursor.

then (
if debug then Printf.printf "found typed context \n";
Some (Completable.CtypedContext contextPath))
else loop rest
| {label = None; exp} :: rest ->
if exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then None
Expand Down Expand Up @@ -592,6 +601,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
| Some contextPath ->
findNamedArgCompletable ~contextPath ~args
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
~charBeforeCursor ~debug
~posAfterFunExpr:(Loc.end_ funExpr.pexp_loc)
| None -> None
in
Expand Down
2 changes: 2 additions & 0 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ module Completable = struct
| Cpath of contextPath
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you start adding comments to Cpath and contextPath?
There's the question of whether there should be an entirely different case of type t, or whether typed context info should be integrated into the existing ones.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Would you mind helping me out with adding comments to those that exist already? I have a hard time parsing what they are/mean.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Cpath is for complex expressions

Copy link
Collaborator

Choose a reason for hiding this comment

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

CPString is for string constants, used in pipe expressions 34 -> ...
CPArray is for arrays used in pipe expressions [] -> ....
CPApply of contextPath * Asttypes.arg_label list is application with a list of labels

CPId of string list * completionContext is for a path A. or A.B.C. and the rhs is the rest to be completed

CPField of contextPath * string is the .z of record access expr.z
CPObj of contextPath * string is the ["hello"] of object access expr["hello"]
CPPipe of contextPath * string is expr->foo where foo is the function whose first argument is expr.

| Cjsx of string list * string * string list
(** E.g. (["M", "Comp"], "id", ["id1", "id2"]) for <M.Comp id1=... id2=... ... id *)
| CtypedContext of contextPath (** WIP, just a dummy arg for now *)

let toString =
let str s = if s = "" then "\"\"" else s in
Expand Down Expand Up @@ -471,4 +472,5 @@ module Completable = struct
| Cnone -> "Cnone"
| Cjsx (sl1, s, sl2) ->
"Cjsx(" ^ (sl1 |> list) ^ ", " ^ str s ^ ", " ^ (sl2 |> list) ^ ")"
| CtypedContext cp -> "CtypedContext(" ^ (cp |> contextPathToString) ^ ")"
end
28 changes: 28 additions & 0 deletions analysis/tests/src/TypeContextCompletion.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type someVariant = One | Two | Three | Four

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

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

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

// let x = someVariantToString(~someVariant=T
// ^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
32 changes: 32 additions & 0 deletions analysis/tests/src/expected/TypeContextCompletion.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Complete src/TypeContextCompletion.res 10:41
posCursor:[10:41] posNoWhite:[10:40] Found expr:[10:11->24:1]
Pexp_apply ...[10:11->10:30] (~someVaria10:32->10:41=...[10:32->10:41], ...[19:0->23:3])
Completable: CnamedArg(Value[someVariantToString], someVaria, [someVaria])
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.

[{
"label": "someVariant",
"kind": 4,
"tags": [],
"detail": "someVariant",
"documentation": null
}]

Complete src/TypeContextCompletion.res 13:44
posCursor:[13:44] posNoWhite:[13:43] Found expr:[13:11->24:1]
Pexp_apply ...[13:11->13:30] (~someVariant13:32->13:43=...[19:0->23:3])
found typed context
Completable: CtypedContext(Value[someVariantToString])
[]

Complete src/TypeContextCompletion.res 16:45
posCursor:[16:45] posNoWhite:[16:44] Found expr:[16:11->24:1]
Pexp_apply ...[16:11->16:30] (~someVariant16:32->16:43=...[16:44->16:45], ...[19:0->23:3])
found typed context
Completable: CtypedContext(Value[someVariantToString])
[]
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 26:37
posCursor:[26:37] posNoWhite:[26:36] Found expr:[26:14->26:37]
JSX <SomeComponent:[26:14->26:27] whatever[26:28->26:36]=...__ghost__[0:-1->0:-1]> _children:None
[]