diff --git a/CHANGELOG.md b/CHANGELOG.md index 66b338549..66e30aa8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/analysis/src/NewCompletions.ml b/analysis/src/NewCompletions.ml index 6e26029b8..3ec5a1d7b 100644 --- a/analysis/src/NewCompletions.ml +++ b/analysis/src/NewCompletions.ml @@ -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 diff --git a/analysis/tests/src/CompletePrioritize1.res b/analysis/tests/src/CompletePrioritize1.res new file mode 100644 index 000000000..79174e6bb --- /dev/null +++ b/analysis/tests/src/CompletePrioritize1.res @@ -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-> + + diff --git a/analysis/tests/src/CompletePrioritize2.res b/analysis/tests/src/CompletePrioritize2.res new file mode 100644 index 000000000..ddeaf3038 --- /dev/null +++ b/analysis/tests/src/CompletePrioritize2.res @@ -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-> \ No newline at end of file diff --git a/analysis/tests/src/expected/CompletePrioritize1.res.txt b/analysis/tests/src/expected/CompletePrioritize1.res.txt new file mode 100644 index 000000000..7642c950e --- /dev/null +++ b/analysis/tests/src/expected/CompletePrioritize1.res.txt @@ -0,0 +1,9 @@ +Complete tests/src/CompletePrioritize1.res 4:2 +[{ + "label": "Test.add", + "kind": 12, + "tags": [], + "detail": "float => float", + "documentation": null + }] + diff --git a/analysis/tests/src/expected/CompletePrioritize2.res.txt b/analysis/tests/src/expected/CompletePrioritize2.res.txt new file mode 100644 index 000000000..e024878a3 --- /dev/null +++ b/analysis/tests/src/expected/CompletePrioritize2.res.txt @@ -0,0 +1,9 @@ +Complete tests/src/CompletePrioritize2.res 8:2 +[{ + "label": "Test.add", + "kind": 12, + "tags": [], + "detail": "t => int", + "documentation": null + }] + diff --git a/analysis/tests/src/expected/Debug.res.txt b/analysis/tests/src/expected/Debug.res.txt index 6185387e3..60c47fed5 100644 --- a/analysis/tests/src/expected/Debug.res.txt +++ b/analysis/tests/src/expected/Debug.res.txt @@ -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