Skip to content

Heuristic for JSX completion happening at the very end of a component with children #875

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

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
Some text.[offsetNoWhite]
else None
in
let charAtCursor =
if offset < String.length text then text.[offset] else '\n'
in
let posBeforeCursor = Pos.posBeforeCursor posCursor in
let charBeforeCursor, blankAfterCursor =
match Pos.positionToOffset text posCursor with
| Some offset when offset > 0 -> (
let charBeforeCursor = text.[offset - 1] in
let charAtCursor =
if offset < String.length text then text.[offset] else '\n'
in
match charAtCursor with
| ' ' | '\t' | '\r' | '\n' ->
(Some charBeforeCursor, Some charBeforeCursor)
Expand Down Expand Up @@ -918,7 +918,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
CompletionJsx.findJsxPropsCompletable ~jsxProps
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
~posAfterCompName:(Loc.end_ compName.loc)
~firstCharBeforeCursorNoWhite
~firstCharBeforeCursorNoWhite ~charAtCursor
in
if jsxCompletable <> None then setResultOpt jsxCompletable
else if compName.loc |> Loc.hasPos ~pos:posBeforeCursor then
Expand Down
30 changes: 24 additions & 6 deletions analysis/src/CompletionJsx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -802,12 +802,17 @@ type jsxProps = {
}

let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
~firstCharBeforeCursorNoWhite ~posAfterCompName =
~firstCharBeforeCursorNoWhite ~charAtCursor ~posAfterCompName =
let allLabels =
List.fold_right
(fun prop allLabels -> prop.name :: allLabels)
jsxProps.props []
in
let beforeChildrenStart =
match jsxProps.childrenStart with
| Some childrenPos -> posBeforeCursor < childrenPos
| None -> posBeforeCursor <= endPos
in
let rec loop props =
match props with
| prop :: rest ->
Expand Down Expand Up @@ -860,13 +865,26 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
nested = [];
})
else None
else if rest = [] && beforeChildrenStart && charAtCursor = '>' then
(* This is a special case for: <SomeComponent someProp=> (completing directly after the '=').
The completion comes at the end of the component, after the equals sign, but before any
children starts, and '>' marks that it's at the end of the component JSX.
This little heuristic makes sure we pick up this special case. *)
Some
(Cexpression
{
contextPath =
CJsxPropValue
{
pathToComponent =
Utils.flattenLongIdent ~jsx:true jsxProps.compName.txt;
propName = prop.name;
};
prefix = "";
nested = [];
})
else loop rest
| [] ->
let beforeChildrenStart =
match jsxProps.childrenStart with
| Some childrenPos -> posBeforeCursor < childrenPos
| None -> posBeforeCursor <= endPos
in
let afterCompName = posBeforeCursor >= posAfterCompName in
if afterCompName && beforeChildrenStart then
Some
Expand Down
3 changes: 3 additions & 0 deletions analysis/tests/src/CompletionJsx.res
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ module CompWithoutJsxPpx = {

// <CompWithoutJsxPpx n
// ^com

// <SomeComponent someProp=>
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this also work when there are children present?

// ^com
19 changes: 19 additions & 0 deletions analysis/tests/src/expected/CompletionJsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,22 @@ Path CompWithoutJsxPpx.make
"documentation": null
}]

Complete src/CompletionJsx.res 48:27
posCursor:[48:27] posNoWhite:[48:26] Found expr:[48:4->48:28]
JSX <SomeComponent:[48:4->48:17] someProp[48:18->48:26]=...[48:18->48:26]> _children:None
Completable: Cexpression CJsxPropValue [SomeComponent] someProp
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
ContextPath CJsxPropValue [SomeComponent] someProp
Path SomeComponent.make
[{
"label": "\"\"",
"kind": 12,
"tags": [],
"detail": "string",
"documentation": null,
"sortText": "A",
"insertText": "{\"$0\"}",
"insertTextFormat": 2
}]