Skip to content
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

selection_range: use merlin enclosing query instead of shape #1368

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Fix fd leak in running external processes for preprocessing (#1349)
- Fix prefix parsing for completion of object methods (#1363, fixes #1358)
- Remove some duplicates in the `selectionRange` answers (#1368)


# 1.19.0
Expand Down
42 changes: 14 additions & 28 deletions ocaml-lsp-server/src/ocaml_lsp_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -387,41 +387,27 @@ let selection_range
match Document.kind doc with
| `Other -> Fiber.return []
| `Merlin merlin ->
let selection_range_of_shapes
(cursor_position : Position.t)
(shapes : Query_protocol.shape list)
let selection_range_of_enclosings (enclosings : Warnings.loc list)
: SelectionRange.t option
=
let rec ranges_of_shape parent (s : Query_protocol.shape) =
let selectionRange =
let range = Range.of_loc s.shape_loc in
{ SelectionRange.range; parent }
in
match s.shape_sub with
| [] -> [ selectionRange ]
| xs -> List.concat_map xs ~f:(ranges_of_shape (Some selectionRange))
in
(* try to find the nearest range inside first, then outside *)
let nearest_range =
let ranges = List.concat_map ~f:(ranges_of_shape None) shapes in
List.min ranges ~f:(fun r1 r2 ->
let inc (r : SelectionRange.t) =
Position.compare_inclusion cursor_position r.range
in
match inc r1, inc r2 with
| `Outside x, `Outside y -> Position.compare x y
| `Outside _, `Inside -> Gt
| `Inside, `Outside _ -> Lt
| `Inside, `Inside -> Range.compare_size r1.range r2.range)
let ranges_of_enclosing parent (enclosing : Warnings.loc) =
let range = Range.of_loc enclosing in
{ SelectionRange.range; parent }
in
nearest_range
List.fold_left
~f:(fun parent enclosing -> Some (ranges_of_enclosing parent enclosing))
~init:None
@@ List.rev enclosings
in
let+ ranges =
Fiber.sequential_map positions ~f:(fun x ->
let+ shapes =
Document.Merlin.dispatch_exn ~name:"shape" merlin (Shape (Position.logical x))
let+ enclosings =
Document.Merlin.dispatch_exn
~name:"shape"
merlin
(Enclosing (Position.logical x))
in
selection_range_of_shapes x shapes)
selection_range_of_enclosings enclosings)
in
List.filter_opt ranges
;;
Expand Down
Loading
Loading