package merlin-lib

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Merlin_utils.MiscSource

Miscellaneous useful types and functions

Sourceval fatal_error : string -> 'a
Sourceval fatal_errorf : ('a, Format.formatter, unit, 'b) format4 -> 'a
Sourceexception Fatal_error of string * Printexc.raw_backtrace
Sourceval try_finally : ?always:(unit -> unit) -> ?exceptionally:(unit -> unit) -> (unit -> 'a) -> 'a

try_finally work ~always ~exceptionally is designed to run code in work that may fail with an exception, and has two kind of cleanup routines: always, that must be run after any execution of the function (typically, freeing system resources), and exceptionally, that should be run only if work or always failed with an exception (typically, undoing user-visible state changes that would only make sense if the function completes correctly). For example:

  let objfile = outputprefix ^ ".cmo" in
  let oc = open_out_bin objfile in
  Misc.try_finally
    (fun () ->
       bytecode
       ++ Timings.(accumulate_time (Generate sourcefile))
           (Emitcode.to_file oc modulename objfile);
       Warnings.check_fatal ())
    ~always:(fun () -> close_out oc)
    ~exceptionally:(fun _exn -> remove_file objfile);

If exceptionally fail with an exception, it is propagated as usual.

If always or exceptionally use exceptions internally for control-flow but do not raise, then try_finally is careful to preserve any exception backtrace coming from work or always for easier debugging.

Sourceval reraise_preserving_backtrace : exn -> (unit -> unit) -> 'a

reraise_preserving_backtrace e f is (f (); raise e) except that the current backtrace is preserved, even if f uses exceptions internally.

Sourceval map_end : ('a -> 'b) -> 'a list -> 'b list -> 'b list
Sourceval map_left_right : ('a -> 'b) -> 'a list -> 'b list
Sourceval for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
Sourceval replicate_list : 'a -> int -> 'a list
Sourceval list_remove : 'a -> 'a list -> 'a list
Sourceval split_last : 'a list -> 'a list * 'a
Sourceval may : ('a -> unit) -> 'a option -> unit
Sourceval may_map : ('a -> 'b) -> 'a option -> 'b option
Sourcetype ref_and_value =
  1. | R : 'a ref * 'a -> ref_and_value
Sourceval protect_refs : ref_and_value list -> (unit -> 'a) -> 'a

protect_refs l f temporarily sets r to v for each R (r, v) in l while executing f. The previous contents of the references is restored even if f raises an exception.

Sourceval exact_file_exists : dirname:string -> basename:string -> bool
Sourceval find_in_path : string list -> string -> string
Sourceval find_in_path_rel : string list -> string -> string
Sourceval find_in_path_uncap : ?fallback:string -> string list -> string -> string
Sourceval canonicalize_filename : ?cwd:string -> string -> string
Sourceval expand_glob : ?filter:(string -> bool) -> string -> string list -> string list
Sourceval split_path : string -> string list -> string list
Sourceval remove_file : string -> unit
Sourceval expand_directory : string -> string -> string
Sourceval create_hashtable : int -> ('a * 'b) list -> ('a, 'b) Hashtbl.t
Sourceval copy_file : in_channel -> out_channel -> unit
Sourceval copy_file_chunk : in_channel -> out_channel -> int -> unit
Sourceval string_of_file : in_channel -> string
Sourceval output_to_file_via_temporary : ?mode:open_flag list -> string -> (string -> out_channel -> 'a) -> 'a
Sourceval input_bytes : in_channel -> int -> bytes
Sourceval log2 : int -> int
Sourceval align : int -> int -> int
Sourceval no_overflow_add : int -> int -> bool
Sourceval no_overflow_sub : int -> int -> bool
Sourceval no_overflow_mul : int -> int -> bool
Sourceval no_overflow_lsl : int -> int -> bool
Sourcemodule Int_literal_converter : sig ... end
Sourceval chop_extension_if_any : string -> string
Sourceval chop_extensions : string -> string
Sourceval search_substring : string -> string -> int -> int
Sourceval replace_substring : before:string -> after:string -> string -> string
Sourceval rev_split_words : string -> string list
Sourceval rev_string_split : on:char -> string -> string list
Sourceval get_ref : 'a list ref -> 'a list
Sourceval set_or_ignore : ('a -> 'b option) -> 'b option ref -> 'a -> unit
Sourceval fst3 : ('a * 'b * 'c) -> 'a
Sourceval snd3 : ('a * 'b * 'c) -> 'b
Sourceval thd3 : ('a * 'b * 'c) -> 'c
Sourceval fst4 : ('a * 'b * 'c * 'd) -> 'a
Sourceval snd4 : ('a * 'b * 'c * 'd) -> 'b
Sourceval thd4 : ('a * 'b * 'c * 'd) -> 'c
Sourceval for4 : ('a * 'b * 'c * 'd) -> 'd
Sourceval modules_in_path : ext:string -> string list -> string list
Sourceval file_contents : string -> string
Sourcemodule LongString : sig ... end
Sourceval edit_distance : string -> string -> int -> int option

edit_distance a b cutoff computes the edit distance between strings a and b. To help efficiency, it uses a cutoff: if the distance d is smaller than cutoff, it returns Some d, else None.

The distance algorithm currently used is Damerau-Levenshtein: it computes the number of insertion, deletion, substitution of letters, or swapping of adjacent letters to go from one word to the other. The particular algorithm may change in the future.

Sourceval spellcheck : string list -> string -> string list

spellcheck env name takes a list of names env that exist in the current environment and an erroneous name, and returns a list of suggestions taken from env, that are close enough to name that it may be a typo for one of them.

Sourceval did_you_mean : Format.formatter -> (unit -> string list) -> unit

did_you_mean ppf get_choices hints that the user may have meant one of the option returned by calling get_choices. It does nothing if the returned list is empty.

The unit -> ... thunking is meant to delay any potentially-slow computation (typically computing edit-distance with many things from the current environment) to when the hint message is to be printed. You should print an understandable error message before calling did_you_mean, so that users get a clear notification of the failure even if producing the hint is slow.

Sourceval cut_at : string -> char -> string * string

String.cut_at s c returns a pair containing the sub-string before the first occurrence of c in s, and the sub-string after the first occurrence of c in s. let (before, after) = String.cut_at s c in before ^ String.make 1 c ^ after is the identity if s contains c.

Raise Not_found if the character does not appear in the string

  • since 4.01
Sourceval time_spent : unit -> float

Returns a more precise measurement of resources usage than Sys.times/Unix.times. Both user and kernel cpu time is accounted.

Sourcemodule String : sig ... end
Sourceval normalise_eol : string -> string

normalise_eol s returns a fresh copy of s with any '\r' characters removed. Intended for pre-processing text which will subsequently be printed on a channel which performs EOL transformations (i.e. Windows)

Sourceval unitname : string -> string

Return the name of the OCaml module matching a basename (filename without directory). Remove the extension and capitalize

Sourcetype filepath = string
Sourcetype modname = string
Sourcetype crcs = (modname * Digest.t option) list
Sourcetype alerts = string String.Map.t
Sourceval ordinal_suffix : int -> string

ordinal_suffix n is the appropriate suffix to append to the numeral n as an ordinal number: 1 -> "st", 2 -> "nd", 3 -> "rd", 4 -> "th", and so on. Handles larger numbers (e.g., 42 -> "nd") and the numbers 11--13 (which all get "th") correctly.

Sourcemodule Color : sig ... end
OCaml

Innovation. Community. Security.