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

Use Merlin Enclosing command instead of Shape to answer SelectionRange queries #860

Open
wants to merge 2 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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

- Fix signatureHelp on .mll files: avoid "Document.dune" exceptions

- Use Merlin's `enclosing` command instead of `shape` to answer `selectionRange`
queries since it is the more appropriate one. This notably removes the need
for checking which enclosing is the closest to the cursor since Merlin already
does it. (#860)

# 1.13.1

## Fixes
Expand Down
36 changes: 8 additions & 28 deletions ocaml-lsp-server/src/ocaml_lsp_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -538,39 +538,19 @@ let selection_range (state : State.t)
match Document.kind doc with
| `Other -> Fiber.return []
| `Merlin merlin ->
let selection_range_of_shapes (cursor_position : Position.t)
(shapes : Query_protocol.shape list) : SelectionRange.t option =
let rec ranges_of_shape parent s =
let selectionRange =
let range = Range.of_loc s.Query_protocol.shape_loc in
{ SelectionRange.range; parent }
in
match s.Query_protocol.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)
in
nearest_range
let rec selection_range_of_enclosings = function
| hd :: tl ->
let parent = selection_range_of_enclosings tl in
Some { SelectionRange.range = Range.of_loc hd; parent }
| [] -> None
in
let+ ranges =
Fiber.sequential_map positions ~f:(fun x ->
let+ shapes =
let command = Query_protocol.Shape (Position.logical x) in
let+ enclosings =
let command = Query_protocol.Enclosing (Position.logical x) in
Document.Merlin.dispatch_exn merlin command
in
selection_range_of_shapes x shapes)
selection_range_of_enclosings enclosings)
in
List.filter_opt ranges

Expand Down