package ocamlformat-lib

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

Placing and formatting comments in a parsetree.

This module provides an interface to the global mutable data structure that maintains the relationship between comments and Ast terms within a parsetree.

Each comment is placed, by one of the init functions, either before or after a location appearing in the parsetree. The relocate function can be used to adjust this placement.

When comments are formatted by one of the fmt functions, they are removed from the data structure. This is significant in cases where there are multiple Ast terms with the same location.

type t

init fragment source x comments associates each comment in comments with a source location appearing in x. It uses Source to help resolve ambiguities. Initializes the state used by the fmt functions.

relocate src before after moves (changes the association with locations) comments before src to before and comments after src to after.

val relocate_wrongfully_attached_cmts : t -> Source.t -> Extended_ast.expression -> Ocamlformat_stdlib.unit

relocate_wrongfully_attached_cmts relocates wrongfully attached comments, e.g. comments that should be attached to the whole pattern-matching expressions (match-with or try-with expressions) but are wrongfully attached to the matched expression.

val fmt_before : t -> Conf.t -> fmt_code:Fmt_odoc.fmt_code -> ?pro:Fmt.t -> ?epi:Fmt.t -> ?eol:Fmt.t -> ?adj:Fmt.t -> Ocaml_common.Location.t -> Fmt.t

fmt_before loc formats the comments associated with loc that appear before loc.

val fmt_after : t -> Conf.t -> fmt_code:Fmt_odoc.fmt_code -> ?pro:Fmt.t -> ?epi:Fmt.t -> ?filter:(Cmt.t -> Ocamlformat_stdlib.bool) -> Ocaml_common.Location.t -> Fmt.t

fmt_after loc formats the comments associated with loc that appear after loc.

val fmt_within : t -> Conf.t -> fmt_code:Fmt_odoc.fmt_code -> ?pro:Fmt.t -> ?epi:Fmt.t -> Ocaml_common.Location.t -> Fmt.t

fmt_within loc formats the comments associated with loc that appear within loc.

module Toplevel : sig ... end
val drop_before : t -> Ocaml_common.Location.t -> t

has_before t loc holds if t contains some comment before loc.

has_within t loc holds if t contains some comment within loc.

has_after t loc holds if t contains some comment after loc.

val remaining_comments : t -> Cmt.t Ocamlformat_stdlib.list

Returns comments that have not been formatted yet.

remaining_before c loc returns the comments before loc

preserve f t formats like f () but returns a string and does not consume comments from t.

val is_docstring : Conf.t -> Cmt.t -> (Cmt.t, Cmt.t) Ocamlformat_stdlib.Either.t

is_docstring conf cmt returns:

  • First c when cmt is a docstring, where c is its content stripped of the leading *;
  • Second c when cmt is a regular comment, where c is its content.
OCaml

Innovation. Community. Security.