Skip to content

Commit bc3d6bd

Browse files
committed
Improve autocomplete when several values have the same name, with a heuristic to approximate the correct scope.
Fixes #335 Fixes #336
1 parent 9fbfbf9 commit bc3d6bd

7 files changed

+56
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Fix issue in record field autocomplete not working with type aliases.
1010
- Support autocomplete of records for variables defined in other files.
1111
- Fix issue where autocomplete for local values would not work in the presence of `@react.component` annotations.
12+
- Improve autocomplete when several values have the same name, with a heuristic to approximate the correct scope.
1213

1314
## 1.1.3
1415

analysis/src/NewCompletions.ml

+16-1
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,22 @@ let computeCompletions ~completable ~full ~pos ~rawOpens =
12351235
let declareds = getItems ~full ~rawOpens ~allFiles ~pos ~dotpath in
12361236
match dotpath |> List.rev with
12371237
| last :: _ when exact ->
1238-
declareds |> List.filter (fun {SharedTypes.name = {txt}} -> txt = last)
1238+
(* Heuristic to approximate scope.
1239+
Take the last position before pos if any, or just return the first element. *)
1240+
let rec prioritize decls =
1241+
match decls with
1242+
| d1 :: d2 :: rest ->
1243+
let pos2 = d2.extentLoc.loc_start |> Utils.tupleOfLexing in
1244+
if pos2 >= pos then prioritize (d1 :: rest)
1245+
else
1246+
let pos1 = d1.extentLoc.loc_start |> Utils.tupleOfLexing in
1247+
if pos1 <= pos2 then prioritize (d2 :: rest)
1248+
else prioritize (d1 :: rest)
1249+
| [] | [_] -> decls
1250+
in
1251+
declareds
1252+
|> List.filter (fun {SharedTypes.name = {txt}} -> txt = last)
1253+
|> prioritize
12391254
| _ -> declareds
12401255
in
12411256
completable |> processCompletable ~processDotPath ~full ~package ~rawOpens
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Test = {
2+
type t = {name: int}
3+
let add = (a: float) => a +. 1.0
4+
}
5+
let a: Test.t = {name: 4}
6+
//^com a->
7+
8+
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let a = 4
2+
let _ = a
3+
let a = ""
4+
let _ = a
5+
module Test = {
6+
type t = {name: int}
7+
let add = (a: t) => a.name + 1
8+
}
9+
let a: Test.t = {name: 4}
10+
//^com a->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Complete tests/src/CompletePrioritize1.res 4:2
2+
[{
3+
"label": "Test.add",
4+
"kind": 12,
5+
"tags": [],
6+
"detail": "float => float",
7+
"documentation": null
8+
}]
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Complete tests/src/CompletePrioritize2.res 8:2
2+
[{
3+
"label": "Test.add",
4+
"kind": 12,
5+
"tags": [],
6+
"detail": "t => int",
7+
"documentation": null
8+
}]
9+

analysis/tests/src/expected/Debug.res.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ Dependencies: @rescript/react
44
Source directories: tests/node_modules/@rescript/react/./src tests/node_modules/@rescript/react/./src/legacy
55
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
66
Source directories: tests/src
7-
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
7+
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
88
Impl cmt:tests/lib/bs/./src/Auto.cmt res:tests/src/Auto.res
9+
Impl cmt:tests/lib/bs/./src/CompletePrioritize1.cmt res:tests/src/CompletePrioritize1.res
10+
Impl cmt:tests/lib/bs/./src/CompletePrioritize2.cmt res:tests/src/CompletePrioritize2.res
911
Impl cmt:tests/lib/bs/./src/Completion.cmt res:tests/src/Completion.res
1012
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
1113
Impl cmt:tests/lib/bs/./src/Cross.cmt res:tests/src/Cross.res

0 commit comments

Comments
 (0)