Skip to content

Commit

Permalink
[B] Merge pull request ocaml#1778 from voodoos/find-command-opt
Browse files Browse the repository at this point in the history
Add a `find_command_opt` alternative to `find_command` that does not raise
  • Loading branch information
voodoos committed Jun 10, 2024
1 parent 89e00d2 commit 060404c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ unreleased
- Addition of a `merlin-lib.commands` library which disassociates the
execution of commands from the `new_protocol`, from the binary, allowing
it to be invoked from other projects (#1758)
- `merlin-lib.commands`: Add a `find_command_opt` alternative to
`find_command` that does not raise (#1778)

merlin 4.15
===========
Expand Down
15 changes: 7 additions & 8 deletions src/commands/new_commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ let marg_completion_kind f = Marg.param "completion-kind"
str
)

let rec find_command name = function
| [] -> raise Not_found
| (Command (name', _, _, _, _) as command) :: xs ->
if name = name' then
command
else find_command name xs
let command_is ~name (Command (name', _, _, _, _)) = String.equal name name'

let find_command name = List.find ~f:(command_is ~name)

let find_command_opt name = List.find_opt ~f:(command_is ~name)

let run pipeline query =
Logger.log ~section:"New_commands" ~title:"run(query)"
Expand Down Expand Up @@ -236,9 +235,9 @@ Otherwise, Merlin looks for the documentation for the entity under the cursor (a
]
~default: `None
begin fun buffer pos ->
match pos with
match pos with
| `None -> failwith "-position <pos> is mandatory"
| #Msource.position as pos ->
| #Msource.position as pos ->
run buffer (Query_protocol.Syntax_document pos)
end
;
Expand Down
6 changes: 6 additions & 0 deletions src/commands/new_commands.mli
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ type command =

val all_commands : command list

(** [find_command name cmds] returns the command with name [name] in the list
[cmds] if it exists. Raises [Not_found] if it does not. *)
val find_command : string -> command list -> command

(** [find_command name cmds] optionaly returns the command with name [name] if
it is in the list [cmds]. *)
val find_command_opt : string -> command list -> command option

0 comments on commit 060404c

Please sign in to comment.