Skip to content

Fix issue in the calculation of struct/signature scopes. #327

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 7, 2021
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 @@ -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

Expand Down
39 changes: 16 additions & 23 deletions analysis/src/ProcessCmt.ml
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion analysis/tests/src/Completion.res
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,12 @@ type rAlias = r
let r:rAlias = assert false
// ^com r.

// ^com Obj.Rec.recordVal.
// ^com Obj.Rec.recordVal.

let myAmazingFunction = (x,y) => x+y

@react.component
let make = () => {
// ^com my
<> </>
}
19 changes: 19 additions & 0 deletions analysis/tests/src/expected/Completion.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}
}
]

Expand Down Expand Up @@ -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
}]