-
Notifications
You must be signed in to change notification settings - Fork 58
[WIP] Annotated completions #711
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
Changes from all commits
dc2472d
1d1f4f6
dd6b4fc
ee03963
34b52b8
5adfa08
339d30f
4c71789
b856a6d
9656b62
45c32a0
1eb870c
c595d20
20af7f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,7 +141,12 @@ let rec exprToContextPath (e : Parsetree.expression) = | |
| Pexp_constant (Pconst_string _) -> Some Completable.CPString | ||
| Pexp_constant (Pconst_integer _) -> Some CPInt | ||
| Pexp_constant (Pconst_float _) -> Some CPFloat | ||
| Pexp_array _ -> Some CPArray | ||
| Pexp_array exprs -> | ||
Some | ||
(CPArray | ||
(match exprs with | ||
| [] -> None | ||
| exp :: _ -> exprToContextPath exp)) | ||
| Pexp_ident {txt} -> Some (CPId (Utils.flattenLongIdent txt, Value)) | ||
| Pexp_field (e1, {txt = Lident name}) -> ( | ||
match exprToContextPath e1 with | ||
|
@@ -316,27 +321,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text = | |
| _ -> () | ||
in | ||
let scopeValueBinding (vb : Parsetree.value_binding) = | ||
scopePattern vb.pvb_pat; | ||
(* Identify relevant destructures for completion, like `let {<com>} = someVar` or `let (true, false) = someFn()`. *) | ||
match vb with | ||
| {pvb_pat; pvb_expr} when locHasCursor pvb_pat.ppat_loc -> ( | ||
match | ||
( pvb_pat | ||
|> CompletionPatterns.traversePattern ~patternPath:[] ~locHasCursor | ||
~firstCharBeforeCursorNoWhite ~posBeforeCursor, | ||
exprToContextPath pvb_expr ) | ||
with | ||
| Some (prefix, nestedPattern), Some ctxPath -> | ||
setResult | ||
(Completable.Cpattern | ||
{ | ||
contextPath = ctxPath; | ||
prefix; | ||
nested = List.rev nestedPattern; | ||
fallback = None; | ||
}) | ||
| _ -> ()) | ||
| _ -> () | ||
scopePattern vb.pvb_pat | ||
in | ||
let scopeTypeKind (tk : Parsetree.type_kind) = | ||
match tk with | ||
|
@@ -490,6 +475,58 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text = | |
(value_binding : Parsetree.value_binding) = | ||
let oldInJsxContext = !inJsxContext in | ||
if Utils.isReactComponent value_binding then inJsxContext := true; | ||
(match value_binding with | ||
| {pvb_pat = {ppat_desc = Ppat_constraint (_pat, coreType)}; pvb_expr} | ||
when locHasCursor pvb_expr.pexp_loc -> ( | ||
(* Expression with derivable type annotation. | ||
E.g: let x: someRecord = {<com>} *) | ||
match | ||
( TypeUtils.contextPathFromCoreType coreType, | ||
pvb_expr | ||
|> CompletionExpressions.traverseExpr ~exprPath:[] | ||
~pos:posBeforeCursor ~firstCharBeforeCursorNoWhite ) | ||
with | ||
| Some ctxPath, Some (prefix, nested) -> | ||
setResult | ||
(Completable.Cexpression | ||
{contextPath = ctxPath; prefix; nested = List.rev nested}) | ||
| _ -> ()) | ||
| { | ||
pvb_pat = {ppat_desc = Ppat_constraint (_pat, coreType); ppat_loc}; | ||
pvb_expr; | ||
} | ||
when locHasCursor value_binding.pvb_loc | ||
&& locHasCursor ppat_loc = false | ||
&& locHasCursor pvb_expr.pexp_loc = false | ||
&& CompletionExpressions.isExprHole pvb_expr -> ( | ||
(* Expression with derivable type annotation, when the expression is empty (expr hole). | ||
E.g: let x: someRecord = <com> *) | ||
match TypeUtils.contextPathFromCoreType coreType with | ||
| Some ctxPath -> | ||
setResult | ||
(Completable.Cexpression | ||
{contextPath = ctxPath; prefix = ""; nested = []}) | ||
| _ -> ()) | ||
| {pvb_pat; pvb_expr} when locHasCursor pvb_pat.ppat_loc -> ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it clear that this case cannot be an instance of the cases above? Are we relying on the order of cases? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fairly sure we're not depending on the ordering, but we're getting dangerously close... |
||
(* Completing a destructuring. | ||
E.g: let {<com>} = someVar *) | ||
match | ||
( pvb_pat | ||
|> CompletionPatterns.traversePattern ~patternPath:[] ~locHasCursor | ||
~firstCharBeforeCursorNoWhite ~posBeforeCursor, | ||
exprToContextPath pvb_expr ) | ||
with | ||
| Some (prefix, nested), Some ctxPath -> | ||
setResult | ||
(Completable.Cpattern | ||
{ | ||
contextPath = ctxPath; | ||
prefix; | ||
nested = List.rev nested; | ||
fallback = None; | ||
}) | ||
| _ -> ()) | ||
| _ -> ()); | ||
Ast_iterator.default_iterator.value_binding iterator value_binding; | ||
inJsxContext := oldInJsxContext | ||
in | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every case seems to return the same
env
. Factor out?