diff --git a/CHANGES.md b/CHANGES.md index 260aa1eed..3c86229c5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 =========== diff --git a/src/commands/new_commands.ml b/src/commands/new_commands.ml index 81e5ff1bc..4491ae9f2 100644 --- a/src/commands/new_commands.ml +++ b/src/commands/new_commands.ml @@ -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)" @@ -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 is mandatory" - | #Msource.position as pos -> + | #Msource.position as pos -> run buffer (Query_protocol.Syntax_document pos) end ; diff --git a/src/commands/new_commands.mli b/src/commands/new_commands.mli index e8f766f82..7c62b49d8 100644 --- a/src/commands/new_commands.mli +++ b/src/commands/new_commands.mli @@ -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