Skip to content

Improve autocomplete when several values have the same name, with a h… #342

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 1 commit into from
Jan 19, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Fix issue in record field autocomplete not working with type aliases.
- Support autocomplete of records for variables defined in other files.
- Fix issue where autocomplete for local values would not work in the presence of `@react.component` annotations.
- Improve autocomplete when several values have the same name, with a heuristic to approximate the correct scope.

## 1.1.3

Expand Down
17 changes: 16 additions & 1 deletion analysis/src/NewCompletions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,22 @@ let computeCompletions ~completable ~full ~pos ~rawOpens =
let declareds = getItems ~full ~rawOpens ~allFiles ~pos ~dotpath in
match dotpath |> List.rev with
| last :: _ when exact ->
declareds |> List.filter (fun {SharedTypes.name = {txt}} -> txt = last)
(* Heuristic to approximate scope.
Take the last position before pos if any, or just return the first element. *)
let rec prioritize decls =
match decls with
| d1 :: d2 :: rest ->
let pos2 = d2.extentLoc.loc_start |> Utils.tupleOfLexing in
if pos2 >= pos then prioritize (d1 :: rest)
else
let pos1 = d1.extentLoc.loc_start |> Utils.tupleOfLexing in
if pos1 <= pos2 then prioritize (d2 :: rest)
else prioritize (d1 :: rest)
| [] | [_] -> decls
in
declareds
|> List.filter (fun {SharedTypes.name = {txt}} -> txt = last)
|> prioritize
| _ -> declareds
in
completable |> processCompletable ~processDotPath ~full ~package ~rawOpens
8 changes: 8 additions & 0 deletions analysis/tests/src/CompletePrioritize1.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Test = {
type t = {name: int}
let add = (a: float) => a +. 1.0
}
let a: Test.t = {name: 4}
//^com a->


10 changes: 10 additions & 0 deletions analysis/tests/src/CompletePrioritize2.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let a = 4
let _ = a
let a = ""
let _ = a
module Test = {
type t = {name: int}
let add = (a: t) => a.name + 1
}
let a: Test.t = {name: 4}
//^com a->
9 changes: 9 additions & 0 deletions analysis/tests/src/expected/CompletePrioritize1.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Complete tests/src/CompletePrioritize1.res 4:2
[{
"label": "Test.add",
"kind": 12,
"tags": [],
"detail": "float => float",
"documentation": null
}]

9 changes: 9 additions & 0 deletions analysis/tests/src/expected/CompletePrioritize2.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Complete tests/src/CompletePrioritize2.res 8:2
[{
"label": "Test.add",
"kind": 12,
"tags": [],
"detail": "t => int",
"documentation": null
}]

4 changes: 3 additions & 1 deletion analysis/tests/src/expected/Debug.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ Dependencies: @rescript/react
Source directories: tests/node_modules/@rescript/react/./src tests/node_modules/@rescript/react/./src/legacy
Source files: tests/node_modules/@rescript/react/./src/React.res tests/node_modules/@rescript/react/./src/ReactDOM.res tests/node_modules/@rescript/react/./src/ReactDOMServer.res tests/node_modules/@rescript/react/./src/ReactDOMStyle.res tests/node_modules/@rescript/react/./src/ReactEvent.res tests/node_modules/@rescript/react/./src/ReactEvent.resi tests/node_modules/@rescript/react/./src/ReactTestUtils.res tests/node_modules/@rescript/react/./src/ReactTestUtils.resi tests/node_modules/@rescript/react/./src/RescriptReactErrorBoundary.res tests/node_modules/@rescript/react/./src/RescriptReactErrorBoundary.resi tests/node_modules/@rescript/react/./src/RescriptReactRouter.res tests/node_modules/@rescript/react/./src/RescriptReactRouter.resi tests/node_modules/@rescript/react/./src/legacy/ReactDOMRe.res tests/node_modules/@rescript/react/./src/legacy/ReasonReact.res
Source directories: tests/src
Source files: tests/src/Auto.res tests/src/Completion.res tests/src/Component.res tests/src/Component.resi tests/src/Cross.res tests/src/Debug.res tests/src/Definition.res tests/src/DefinitionWithInterface.res tests/src/DefinitionWithInterface.resi tests/src/Div.res tests/src/Fragment.res tests/src/Hover.res tests/src/Jsx.res tests/src/Jsx.resi tests/src/Obj.res tests/src/Patterns.res tests/src/RecModules.res tests/src/RecordCompletion.res tests/src/References.res tests/src/ReferencesWithInterface.res tests/src/ReferencesWithInterface.resi tests/src/Rename.res tests/src/RenameWithInterface.res tests/src/RenameWithInterface.resi tests/src/TypeDefinition.res
Source files: tests/src/Auto.res tests/src/CompletePrioritize1.res tests/src/CompletePrioritize2.res tests/src/Completion.res tests/src/Component.res tests/src/Component.resi tests/src/Cross.res tests/src/Debug.res tests/src/Definition.res tests/src/DefinitionWithInterface.res tests/src/DefinitionWithInterface.resi tests/src/Div.res tests/src/Fragment.res tests/src/Hover.res tests/src/Jsx.res tests/src/Jsx.resi tests/src/Obj.res tests/src/Patterns.res tests/src/RecModules.res tests/src/RecordCompletion.res tests/src/References.res tests/src/ReferencesWithInterface.res tests/src/ReferencesWithInterface.resi tests/src/Rename.res tests/src/RenameWithInterface.res tests/src/RenameWithInterface.resi tests/src/TypeDefinition.res
Impl cmt:tests/lib/bs/./src/Auto.cmt res:tests/src/Auto.res
Impl cmt:tests/lib/bs/./src/CompletePrioritize1.cmt res:tests/src/CompletePrioritize1.res
Impl cmt:tests/lib/bs/./src/CompletePrioritize2.cmt res:tests/src/CompletePrioritize2.res
Impl cmt:tests/lib/bs/./src/Completion.cmt res:tests/src/Completion.res
IntfAndImpl cmti:tests/lib/bs/./src/Component.cmti resi:tests/src/Component.resi cmt:tests/lib/bs/./src/Component.cmt res:tests/src/Component.res
Impl cmt:tests/lib/bs/./src/Cross.cmt res:tests/src/Cross.res
Expand Down