diff --git a/CHANGELOG.md b/CHANGELOG.md index b43d920dd..66b338549 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Fix issue in JSX autocompletion where the `key` label would always appear. - 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. ## 1.1.3 diff --git a/analysis/src/ProcessCmt.ml b/analysis/src/ProcessCmt.ml index b226e79ca..9e5c8a7dc 100644 --- a/analysis/src/ProcessCmt.ml +++ b/analysis/src/ProcessCmt.ml @@ -1,32 +1,25 @@ open Typedtree open SharedTypes -let itemsExtent items = - match items with +let locsExtent locs = + let locs = locs |> List.filter (fun loc -> not loc.Location.loc_ghost) in + (* This filters out ghost locs, but still assumes positions are ordered. + Perhaps compute min/max. *) + match locs with | [] -> Location.none | first :: _ -> - let last = List.nth items (List.length items - 1) in + let last = List.nth locs (List.length locs - 1) in let first, last = - if first.str_loc.loc_start.pos_cnum < last.str_loc.loc_start.pos_cnum then - (first, last) + if first.loc_start.pos_cnum < last.loc_start.pos_cnum then (first, last) else (last, first) in - { - loc_ghost = true; - loc_start = first.str_loc.loc_start; - loc_end = last.str_loc.loc_end; - } + {loc_ghost = true; loc_start = first.loc_start; loc_end = last.loc_end} + +let impItemsExtent items = + items |> List.map (fun item -> item.Typedtree.str_loc) |> locsExtent let sigItemsExtent items = - match items with - | [] -> Location.none - | first :: _ -> - let last = List.nth items (List.length items - 1) in - { - Location.loc_ghost = true; - loc_start = first.sig_loc.loc_start; - loc_end = last.sig_loc.loc_end; - } + items |> List.map (fun item -> item.Typedtree.sig_loc) |> locsExtent let addItem ~name ~extent ~stamp ~env ~item attributes exported stamps = let declared = @@ -409,7 +402,7 @@ and forModule env mod_desc moduleName = let env = { env with - scope = itemsExtent structure.str_items; + scope = impItemsExtent structure.str_items; modulePath = ExportedModule (moduleName, env.modulePath); } in @@ -482,7 +475,7 @@ let forCmt ~moduleName ~uri ({cmt_modname; cmt_annots} : Cmt_format.cmt_infos) = | _ -> None) |> List.concat in - let extent = itemsExtent items in + let extent = impItemsExtent items in let extent = { extent with @@ -525,7 +518,7 @@ let forCmt ~moduleName ~uri ({cmt_modname; cmt_annots} : Cmt_format.cmt_infos) = | Implementation structure -> let env = { - scope = itemsExtent structure.str_items; + scope = impItemsExtent structure.str_items; stamps = initStamps (); modulePath = File (uri, moduleName); } @@ -1044,7 +1037,7 @@ end let extraForStructureItems ~(file : File.t) (items : Typedtree.structure_item list) parts = let extra = extraForFile ~file in - let extent = itemsExtent items in + let extent = impItemsExtent items in let extent = { extent with diff --git a/analysis/tests/src/Completion.res b/analysis/tests/src/Completion.res index 442082250..2a93f1010 100644 --- a/analysis/tests/src/Completion.res +++ b/analysis/tests/src/Completion.res @@ -89,4 +89,12 @@ type rAlias = r let r:rAlias = assert false // ^com r. -// ^com Obj.Rec.recordVal. \ No newline at end of file +// ^com Obj.Rec.recordVal. + +let myAmazingFunction = (x,y) => x+y + +@react.component +let make = () => { +// ^com my + <> +} diff --git a/analysis/tests/src/expected/Completion.res.txt b/analysis/tests/src/expected/Completion.res.txt index bf27ee163..d31b2e349 100644 --- a/analysis/tests/src/expected/Completion.res.txt +++ b/analysis/tests/src/expected/Completion.res.txt @@ -569,6 +569,16 @@ DocumentSymbol tests/src/Completion.res "name": "r", "kind": 13, "location": {"uri": "Completion.res", "range": {"start": {"line": 88, "character": 4}, "end": {"line": 88, "character": 5}}} +}, +{ + "name": "myAmazingFunction", + "kind": 12, + "location": {"uri": "Completion.res", "range": {"start": {"line": 93, "character": 4}, "end": {"line": 93, "character": 21}}} +}, +{ + "name": "make", + "kind": 12, + "location": {"uri": "Completion.res", "range": {"start": {"line": 96, "character": 4}, "end": {"line": 96, "character": 8}}} } ] @@ -710,3 +720,12 @@ Complete tests/src/Completion.res 90:3 "documentation": null }] +Complete tests/src/Completion.res 96:3 +[{ + "label": "myAmazingFunction", + "kind": 12, + "tags": [], + "detail": "(int, int) => int", + "documentation": null + }] +