Skip to content

Commit

Permalink
Merge pull request #1834 from voodoos/ocamlformat-again
Browse files Browse the repository at this point in the history
Ocamlformat the codebase
  • Loading branch information
voodoos authored Sep 24, 2024
2 parents 86b7ae9 + 7c5da09 commit 53ac829
Show file tree
Hide file tree
Showing 145 changed files with 10,466 additions and 10,769 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# git config blame.ignoreRevsFile .git-blame-ignore-revs
40e0cdb95ff31b81212109fada6e0ff93b6eefee
7 changes: 7 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,10 @@ jobs:
opam exec -- dune clean
opam exec -- dune build
git diff --exit-code
- name: Check that the changes are correctly formatted
if: matrix.os == 'ubuntu-latest'
run: |
opam install ocamlformat.0.26.2
opam exec -- dune build @fmt
11 changes: 11 additions & 0 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version=0.26.2
disable=false

break-cases=fit-or-vertical
doc-comments=before
cases-exp-indent=2
dock-collection-brackets=false
# Preserve begin/end
exp-grouping=preserve
module-item-spacing=preserve
parse-docstrings=false
1 change: 1 addition & 0 deletions .ocamlformat-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
upstream/**
8 changes: 3 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ issue has not already been submitted.

### Styleguides

As of today, Merlin's codebase does not use a code formatter. When contributing
code to an existing module, one should adopt the style of the surrounding code.
Please keep lines under 80 characters.

We plan to move the codebase to ocamlformat in a near future.
Merlin uses ocamlformat. When contributing code, please always format it by
running `dune fmt` before submitting a PR. The required version of ocamlformat
can be found in the `.ocamlformat` configuration file.

Changes unrelated to the issue addressed by a PR should be made in a separate
PR. Additionally, formatting changes in parts of the code not concerned by a
Expand Down
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
(using menhir 2.0)

(cram enable)
(formatting disabled)
(formatting (enabled_for ocaml))
(implicit_transitive_deps false)
(use_standard_c_and_cxx_flags true)
236 changes: 129 additions & 107 deletions src/analysis/ast_iterators.ml
Original file line number Diff line number Diff line change
@@ -1,144 +1,166 @@
open Std
open Typedtree

let {Logger. log} = Logger.for_section "iterators"
let { Logger.log } = Logger.for_section "iterators"

(* Sometimes we do not want to iterate on nodes that do not correspond to actual
syntax such as the ones introduced by PPXes with the `merlin.hide`
attribute. *)
let iter_only_visible iter =
let has_attribute ~name attrs =
List.exists ~f:(fun a ->
let (str,_) = Ast_helper.Attr.as_tuple a in
str.Location.txt = name
) attrs
in
let not_hidden attrs =
not (has_attribute ~name:"merlin.hide" attrs)
List.exists
~f:(fun a ->
let str, _ = Ast_helper.Attr.as_tuple a in
str.Location.txt = name)
attrs
in
let not_hidden attrs = not (has_attribute ~name:"merlin.hide" attrs) in
let not_hidden_node node =
not (Browse_raw.has_attr ~name:"merlin.hide" node)
in
Tast_iterator.{ iter with
class_declaration = (fun sub ({ ci_attributes; _ } as cl) ->
if not_hidden ci_attributes then iter.class_declaration sub cl);
class_description = (fun sub ({ ci_attributes; _ } as cl) ->
if not_hidden ci_attributes then iter.class_description sub cl);
class_expr = (fun sub ({ cl_attributes; _ } as cl) ->
if not_hidden cl_attributes then iter.class_expr sub cl);
class_field = (fun sub ({ cf_attributes; _ } as cl) ->
if not_hidden cf_attributes then iter.class_field sub cl);
class_type = (fun sub ({ cltyp_attributes; _ } as cl) ->
if not_hidden cltyp_attributes then iter.class_type sub cl);
class_type_declaration = (fun sub ({ ci_attributes; _ } as cl) ->
if not_hidden ci_attributes then iter.class_type_declaration sub cl);
class_type_field = (fun sub ({ ctf_attributes; _ } as cl) ->
if not_hidden ctf_attributes then iter.class_type_field sub cl);

expr = (fun sub ({ exp_attributes; _ } as e) ->
if not_hidden exp_attributes then iter.expr sub e);
extension_constructor = (fun sub ({ ext_attributes; _ } as e) ->
if not_hidden ext_attributes then iter.extension_constructor sub e);

include_description = (fun sub ({ incl_attributes; _ } as incl) ->
if not_hidden incl_attributes then iter.include_description sub incl);
include_declaration = (fun sub ({ incl_attributes; _ } as incl) ->
if not_hidden incl_attributes then iter.include_declaration sub incl);

module_binding = (fun sub ({ mb_attributes; _ } as mb) ->
if not_hidden mb_attributes then iter.module_binding sub mb);
module_declaration = (fun sub ({ md_attributes; _ } as m) ->
if not_hidden md_attributes then iter.module_declaration sub m);
module_substitution = (fun sub ({ ms_attributes; _ } as m) ->
if not_hidden ms_attributes then iter.module_substitution sub m);
module_expr = (fun sub ({ mod_attributes; _ } as m) ->
if not_hidden mod_attributes then iter.module_expr sub m);
module_type = (fun sub ({ mty_attributes; _ } as m) ->
if not_hidden mty_attributes then iter.module_type sub m);
module_type_declaration = (fun sub ({ mtd_attributes; _ } as m) ->
if not_hidden mtd_attributes then iter.module_type_declaration sub m);

pat = (fun sub ({ pat_attributes; _ } as p) ->
if not_hidden pat_attributes then iter.pat sub p);
row_field = (fun sub ({ rf_attributes; _ } as p) ->
if not_hidden rf_attributes then iter.row_field sub p);
object_field = (fun sub ({ of_attributes; _ } as p) ->
if not_hidden of_attributes then iter.object_field sub p);

open_declaration = (fun sub ({ open_attributes; _ } as p) ->
if not_hidden open_attributes then iter.open_declaration sub p);
open_description = (fun sub ({ open_attributes; _ } as p) ->
if not_hidden open_attributes then iter.open_description sub p);

signature_item = (fun sub si ->
if not_hidden_node (Signature_item (si, Env.empty)) then
iter.signature_item sub si);
structure_item = (fun sub si ->
if not_hidden_node (Structure_item (si, Env.empty)) then
iter.structure_item sub si);

typ = (fun sub ({ ctyp_attributes; _ } as t) ->
if not_hidden ctyp_attributes then iter.typ sub t);
type_declaration = (fun sub ({ typ_attributes; _ } as t) ->
if not_hidden typ_attributes then iter.type_declaration sub t);
type_extension = (fun sub ({ tyext_attributes; _ } as t) ->
if not_hidden tyext_attributes then iter.type_extension sub t);
type_exception = (fun sub ({ tyexn_attributes; _ } as t) ->
if not_hidden tyexn_attributes then iter.type_exception sub t);

value_binding = (fun sub ({ vb_attributes; _ } as vb) ->
if not_hidden vb_attributes then iter.value_binding sub vb);
value_description = (fun sub ({ val_attributes; _ } as vb) ->
if not_hidden val_attributes then iter.value_description sub vb);
}
Tast_iterator.
{ iter with
class_declaration =
(fun sub ({ ci_attributes; _ } as cl) ->
if not_hidden ci_attributes then iter.class_declaration sub cl);
class_description =
(fun sub ({ ci_attributes; _ } as cl) ->
if not_hidden ci_attributes then iter.class_description sub cl);
class_expr =
(fun sub ({ cl_attributes; _ } as cl) ->
if not_hidden cl_attributes then iter.class_expr sub cl);
class_field =
(fun sub ({ cf_attributes; _ } as cl) ->
if not_hidden cf_attributes then iter.class_field sub cl);
class_type =
(fun sub ({ cltyp_attributes; _ } as cl) ->
if not_hidden cltyp_attributes then iter.class_type sub cl);
class_type_declaration =
(fun sub ({ ci_attributes; _ } as cl) ->
if not_hidden ci_attributes then iter.class_type_declaration sub cl);
class_type_field =
(fun sub ({ ctf_attributes; _ } as cl) ->
if not_hidden ctf_attributes then iter.class_type_field sub cl);
expr =
(fun sub ({ exp_attributes; _ } as e) ->
if not_hidden exp_attributes then iter.expr sub e);
extension_constructor =
(fun sub ({ ext_attributes; _ } as e) ->
if not_hidden ext_attributes then iter.extension_constructor sub e);
include_description =
(fun sub ({ incl_attributes; _ } as incl) ->
if not_hidden incl_attributes then iter.include_description sub incl);
include_declaration =
(fun sub ({ incl_attributes; _ } as incl) ->
if not_hidden incl_attributes then iter.include_declaration sub incl);
module_binding =
(fun sub ({ mb_attributes; _ } as mb) ->
if not_hidden mb_attributes then iter.module_binding sub mb);
module_declaration =
(fun sub ({ md_attributes; _ } as m) ->
if not_hidden md_attributes then iter.module_declaration sub m);
module_substitution =
(fun sub ({ ms_attributes; _ } as m) ->
if not_hidden ms_attributes then iter.module_substitution sub m);
module_expr =
(fun sub ({ mod_attributes; _ } as m) ->
if not_hidden mod_attributes then iter.module_expr sub m);
module_type =
(fun sub ({ mty_attributes; _ } as m) ->
if not_hidden mty_attributes then iter.module_type sub m);
module_type_declaration =
(fun sub ({ mtd_attributes; _ } as m) ->
if not_hidden mtd_attributes then iter.module_type_declaration sub m);
pat =
(fun sub ({ pat_attributes; _ } as p) ->
if not_hidden pat_attributes then iter.pat sub p);
row_field =
(fun sub ({ rf_attributes; _ } as p) ->
if not_hidden rf_attributes then iter.row_field sub p);
object_field =
(fun sub ({ of_attributes; _ } as p) ->
if not_hidden of_attributes then iter.object_field sub p);
open_declaration =
(fun sub ({ open_attributes; _ } as p) ->
if not_hidden open_attributes then iter.open_declaration sub p);
open_description =
(fun sub ({ open_attributes; _ } as p) ->
if not_hidden open_attributes then iter.open_description sub p);
signature_item =
(fun sub si ->
if not_hidden_node (Signature_item (si, Env.empty)) then
iter.signature_item sub si);
structure_item =
(fun sub si ->
if not_hidden_node (Structure_item (si, Env.empty)) then
iter.structure_item sub si);
typ =
(fun sub ({ ctyp_attributes; _ } as t) ->
if not_hidden ctyp_attributes then iter.typ sub t);
type_declaration =
(fun sub ({ typ_attributes; _ } as t) ->
if not_hidden typ_attributes then iter.type_declaration sub t);
type_extension =
(fun sub ({ tyext_attributes; _ } as t) ->
if not_hidden tyext_attributes then iter.type_extension sub t);
type_exception =
(fun sub ({ tyexn_attributes; _ } as t) ->
if not_hidden tyexn_attributes then iter.type_exception sub t);
value_binding =
(fun sub ({ vb_attributes; _ } as vb) ->
if not_hidden vb_attributes then iter.value_binding sub vb);
value_description =
(fun sub ({ val_attributes; _ } as vb) ->
if not_hidden val_attributes then iter.value_description sub vb)
}

(* The compiler contains an iterator that aims to gather definitions but
ignores local values like let-in expressions and local type definition. To
provide occurrences in the active buffer we extend the compiler's iterator with
these cases. *)
ignores local values like let-in expressions and local type definition. To
provide occurrences in the active buffer we extend the compiler's iterator with
these cases. *)
let iter_on_defs ~uid_to_locs_tbl =
let log = log ~title:"iter_on_defs" in
let register_uid uid fragment =
let loc = Typedtree_utils.location_of_declaration ~uid fragment in
Option.iter loc ~f:(fun loc ->
Types.Uid.Tbl.add uid_to_locs_tbl uid loc)
Option.iter loc ~f:(fun loc -> Types.Uid.Tbl.add uid_to_locs_tbl uid loc)
in
let iter_decl = Cmt_format.iter_on_declarations ~f:register_uid in
let register_uid uid loc =
Types.Uid.Tbl.add uid_to_locs_tbl uid loc
in
let register_uid uid loc = Types.Uid.Tbl.add uid_to_locs_tbl uid loc in
{ iter_decl with
expr = (fun sub ({ exp_extra; _ } as expr) ->
List.iter exp_extra ~f:(fun (exp_extra, _loc, _attr) ->
match exp_extra with
| Texp_newtype' (typ_id, typ_name, uid) ->
log "Found newtype %s wit id %a (%a)\n%!" typ_name.txt
Logger.fmt (Fun.flip Ident.print_with_scope typ_id)
Logger.fmt (fun fmt -> Location.print_loc fmt typ_name.loc);
register_uid uid typ_name;
()
| _ -> ());
iter_decl.expr sub expr);
expr =
(fun sub ({ exp_extra; _ } as expr) ->
List.iter exp_extra ~f:(fun (exp_extra, _loc, _attr) ->
match exp_extra with
| Texp_newtype' (typ_id, typ_name, uid) ->
log "Found newtype %s wit id %a (%a)\n%!" typ_name.txt Logger.fmt
(Fun.flip Ident.print_with_scope typ_id) Logger.fmt (fun fmt ->
Location.print_loc fmt typ_name.loc);
register_uid uid typ_name;
()
| _ -> ());
iter_decl.expr sub expr)
}

let build_uid_to_locs_tbl ~(local_defs : Mtyper.typedtree) () =
let uid_to_locs_tbl : string Location.loc Types.Uid.Tbl.t =
Types.Uid.Tbl.create 64
in
let iter = iter_on_defs ~uid_to_locs_tbl in
begin match local_defs with
| `Interface sign ->
iter.signature iter sign
| `Implementation str ->
iter.structure iter str end;
begin
match local_defs with
| `Interface sign -> iter.signature iter sign
| `Implementation str -> iter.structure iter str
end;
uid_to_locs_tbl

let iter_on_usages ~f (local_defs : Mtyper.typedtree) =
let occ_iter = Cmt_format.iter_on_occurrences ~f in
let iter = iter_only_visible occ_iter in
begin match local_defs with
| `Interface signature -> iter.signature iter signature
| `Implementation structure -> iter.structure iter structure end
begin
match local_defs with
| `Interface signature -> iter.signature iter signature
| `Implementation structure -> iter.structure iter structure
end

let iterator_on_usages ~f =
let occ_iter = Cmt_format.iter_on_occurrences ~f in
Expand Down
Loading

0 comments on commit 53ac829

Please sign in to comment.