package merlin-lib
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=82d648a4180d6d5aa3a083218e28bf197f4e9f147884de9302bb6811ad98b77f
sha512=e21c96db754a289462677e6985181902ddd90bab9e9cebbbb739d92d94b5aee7ef7a134da70ae384291d9bfaccfecd26d37b48f8d75c5d36b9eda18f6e5afd3d
doc/merlin-lib.utils/Merlin_utils/Misc/index.html
Module Merlin_utils.MiscSource
Miscellaneous useful types and functions
Reporting fatal errors
Raise the Fatal_error exception with the given string.
Format the arguments according to the given format string and raise Fatal_error with the resulting string.
Exceptions and finalization
val try_finally :
?always:(unit -> unit) ->
?exceptionally:(unit -> unit) ->
(unit -> 'a) ->
'atry_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.
reraise_preserving_backtrace e f is (f (); raise e) except that the current backtrace is preserved, even if f uses exceptions internally.
List operations
map_end f l t is map f l @ t, just more efficient.
map_end f l t is map f (rev l) @ t, just more efficient.
Like List.map, with guaranteed left-to-right evaluation order
Same as List.for_all but for a binary predicate. In addition, this for_all2 never fails: given two lists with different lengths, it returns false.
replicate_list elem n is the list with n elements all identical to elem.
list_remove x l returns a copy of l with the first element equal to x removed.
Return the last element and the other elements of the given list.
Detects a repeated label - for use with labeled tuples.
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.
Search a file in a list of directories.
Search a relative file in a list of directories.
Normalize file name Foo.ml to foo.ml
Same as find_in_path_rel , but search also for normalized unit filename, i.e. if name is Foo.ml, allow /path/Foo.ml and /path/foo.ml to match.
Delete the given file if it exists and is a regular file. Does nothing for other kinds of files. Never raises an error.
expand_directory alt file eventually expands a + at the beginning of file into alt (an alternate root directory)
val output_to_file_via_temporary :
?mode:open_flag list ->
string ->
(string -> out_channel -> 'a) ->
'afind_first_mono p takes an integer predicate p : int -> bool that we assume: 1. is monotonic on natural numbers: if a <= b then p a implies p b, 2. is satisfied for some natural numbers in range 0; max_int (this is equivalent to: p max_int = true).
find_first_mono p is the smallest natural number N that satisfies p, computed in O(log(N)) calls to p.
Our implementation supports two cases where the preconditions on p are not respected:
- If
pis alwaysfalse, we silently returnmax_intinstead of looping or crashing. - If
pis non-monotonic but eventually true, we return some satisfying value.
String operations
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.
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.
val align_hint :
prefix:string ->
main:Format_doc.t ->
hint:Format_doc.t ->
Format_doc.t * Format_doc.taligned_hint main hint vertically aligns a main message and a hint message. The vertical alignment is controlled by the use of @{<ralign> ... @} boxes: the start of one box, in either the hint or the main message, will be shifted on the left to ensure that the end of the two boxes are vertically aligned, taking in account a pre-existing prefix before the main message. For instance,
let main, sub =
align_hint
~prefix:"Error: "
(doc_printf "@{<ralign>The value @}%a is not an instance variable"
Style.inline_code "foobar"
)
(doc_printf
"@{<ralign>Did you mean @}%a" Style.inline_code "foobaz"
) in
printf "Error: %a%a" pp_doc main pp_doc subproduces the following text:
Error: The value "foobaz" is not an instance variable
Hint: Did you mean "foobar"?where the main message has been shifted to the left to align "foobaz" and "foobar".
Same as align_hint ~prefix:"Error: "
val aligned_hint :
prefix:string ->
Format_doc.formatter ->
('a, Format_doc.formatter, unit, Format_doc.t option -> unit) format4 ->
'aaligned_hint ~prefix fmt ... hint align the potential hint with the main error message generated by the format string fmt before printing the two message.
did_you_mean ~pp choices hints that the user may have meant one of the option in choices.
Each choice is printed with the pp function, or Style.inline_code if pp=None.
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
Returns a more precise measurement of resources usage than Sys.times/Unix.times. Both user and kernel cpu time is accounted.
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)
Return the name of the OCaml module matching a basename (filename without directory). Remove the extension and capitalize
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.
Styling handling for terminal output
See manual section