Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ocaml-format the codebase #1586

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,9 @@ jobs:
opam exec -- dune clean
opam exec -- dune build
git diff --exit-code

- name: Check if formatted
if: matrix.os == 'ubuntu-latest'
run: |
opam install ocamlformat.0.24.1
opam exec dune build @fmt
16 changes: 16 additions & 0 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version=0.24.1
break-cases=fit-or-vertical
break-struct=natural
doc-comments=before
cases-exp-indent=2
doc-comments-tag-only=fit
dock-collection-brackets=false
# Preserve beign/end
voodoos marked this conversation as resolved.
Show resolved Hide resolved
exp-grouping=preserve
if-then-else=fit-or-vertical
module-item-spacing=preserve
parse-docstrings=false
space-around-lists=false
space-around-records=false
space-around-variants=false

4 changes: 4 additions & 0 deletions .ocamlformat-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src/ocaml/**
src/platform/**
tests/**
upstream/**
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
(using menhir 2.0)

(cram enable)
(formatting disabled)
(formatting (enabled_for ocaml))
(implicit_transitive_deps false)
186 changes: 100 additions & 86 deletions src/analysis/browse_misc.ml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
(* {{{ COPYING *(

This file is part of Merlin, an helper for ocaml editors
This file is part of Merlin, an helper for ocaml editors

Copyright (C) 2013 - 2015 Frédéric Bour <frederic.bour(_)lakaban.net>
Thomas Refis <refis.thomas(_)gmail.com>
Simon Castellan <simon.castellan(_)iuwt.fr>
Copyright (C) 2013 - 2015 Frédéric Bour <frederic.bour(_)lakaban.net>
Thomas Refis <refis.thomas(_)gmail.com>
Simon Castellan <simon.castellan(_)iuwt.fr>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

The Software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall
the authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising
from, out of or in connection with the software or the use or other dealings
in the Software.
The Software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall
the authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising
from, out of or in connection with the software or the use or other dealings
in the Software.

)* }}} *)
)* }}} *)

open Std

Expand All @@ -34,57 +34,67 @@ let dummy_type_scheme desc =
let print_constructor c =
let open Types in
match c.cstr_args with
| [] ->
Printtyp.tree_of_type_scheme
(dummy_type_scheme (get_desc c.cstr_res))
| [] -> Printtyp.tree_of_type_scheme (dummy_type_scheme (get_desc c.cstr_res))
| args ->
let desc = Tarrow (Ast_helper.no_label,
dummy_type_scheme (Ttuple args),
c.cstr_res, commu_ok)
let desc =
Tarrow
( Ast_helper.no_label,
dummy_type_scheme (Ttuple args),
c.cstr_res,
commu_ok )
in
Printtyp.tree_of_type_scheme (dummy_type_scheme desc)

let summary_prev = function
| Env.Env_empty -> None
| Env.Env_open (s,_) | Env.Env_value (s,_,_)
| Env.Env_type (s,_,_) | Env.Env_extension (s,_,_)
| Env.Env_module (s,_,_,_) | Env.Env_modtype (s,_,_)
| Env.Env_class (s,_,_) | Env.Env_cltype (s,_,_)
| Env.Env_functor_arg (s,_)
| Env.Env_constraints (s,_)
| Env.Env_open (s, _)
| Env.Env_value (s, _, _)
| Env.Env_type (s, _, _)
| Env.Env_extension (s, _, _)
| Env.Env_module (s, _, _, _)
| Env.Env_modtype (s, _, _)
| Env.Env_class (s, _, _)
| Env.Env_cltype (s, _, _)
| Env.Env_functor_arg (s, _)
| Env.Env_constraints (s, _)
| Env.Env_copy_types s
| Env.Env_persistent (s,_)
| Env.Env_value_unbound (s, _, _) | Env.Env_module_unbound (s, _, _) ->
Some s
| Env.Env_persistent (s, _)
| Env.Env_value_unbound (s, _, _)
| Env.Env_module_unbound (s, _, _) -> Some s

let signature_of_env ?(ignore_extensions=true) env =
let signature_of_env ?(ignore_extensions = true) env =
let signature_of_summary =
let open Env in
let open Types in
(* FIXME: the use of [Exported] here is wrong... The compiler should export
that information. *)
that information. *)
function
| Env_value (_,i,v) -> Some (Sig_value (i,v,Exported))
| Env_value (_, i, v) -> Some (Sig_value (i, v, Exported))
(* Trec_not == bluff, FIXME *)
| Env_type (_,i,t) -> Some (Sig_type (i,t,Trec_not,Exported))
| Env_type (_, i, t) -> Some (Sig_type (i, t, Trec_not, Exported))
(* Texp_first == bluff, FIXME *)
| Env_extension (_,i,e) ->
begin match e.ext_type_path with
| Env_extension (_, i, e) -> begin
match e.ext_type_path with
| Path.Pident id when Ident.name id = "exn" ->
Some (Sig_typext (i,e, Text_exception, Exported))
| _ ->
Some (Sig_typext (i,e, Text_first, Exported))
end
| Env_module (_,i,pr,m) -> Some (Sig_module (i,pr,m,Trec_not,Exported))
| Env_modtype (_,i,m) -> Some (Sig_modtype (i,m,Exported))
| Env_class (_,i,c) -> Some (Sig_class (i,c,Trec_not,Exported))
| Env_cltype (_,i,c) -> Some (Sig_class_type (i,c,Trec_not,Exported))
| Env_open _ | Env_empty | Env_functor_arg _
| Env_constraints _ | Env_copy_types _ | Env_persistent _
| Env_value_unbound _ | Env_module_unbound _ -> None
Some (Sig_typext (i, e, Text_exception, Exported))
| _ -> Some (Sig_typext (i, e, Text_first, Exported))
end
| Env_module (_, i, pr, m) ->
Some (Sig_module (i, pr, m, Trec_not, Exported))
| Env_modtype (_, i, m) -> Some (Sig_modtype (i, m, Exported))
| Env_class (_, i, c) -> Some (Sig_class (i, c, Trec_not, Exported))
| Env_cltype (_, i, c) -> Some (Sig_class_type (i, c, Trec_not, Exported))
| Env_open _
| Env_empty
| Env_functor_arg _
| Env_constraints _
| Env_copy_types _
| Env_persistent _
| Env_value_unbound _
| Env_module_unbound _ -> None
in
let summary_module_ident_opt = function
| Env.Env_module (_,i,_,_) -> Some i
| Env.Env_module (_, i, _, _) -> Some i
| _ -> None
in
let sg = ref [] in
Expand All @@ -98,54 +108,58 @@ let signature_of_env ?(ignore_extensions=true) env =
in
aux (Env.summary env);
(* Since 4.08 one can't simply call [simplify]. *)
(* Typemod.simplify_signature *) (!sg)
(* Typemod.simplify_signature *)
!sg

let dump_browse node =
let attr attr =
let ({Location . txt; loc},payload) = Ast_helper.Attr.as_tuple attr in
`Assoc [
"start" , Lexing.json_of_position loc.Location.loc_start;
"end" , Lexing.json_of_position loc.Location.loc_end;
"name" , `String (txt ^ if payload = Parsetree.PStr [] then "" else " _")
]
let {Location.txt; loc}, payload = Ast_helper.Attr.as_tuple attr in
`Assoc
[ ("start", Lexing.json_of_position loc.Location.loc_start);
("end", Lexing.json_of_position loc.Location.loc_end);
( "name",
`String (txt ^ if payload = Parsetree.PStr [] then "" else " _") ) ]
in
let rec append env node acc =
let loc = Mbrowse.node_loc node in
`Assoc [
"filename" , `String loc.Location.loc_start.Lexing.pos_fname;
"start" , Lexing.json_of_position loc.Location.loc_start;
"end" , Lexing.json_of_position loc.Location.loc_end;
"ghost" , `Bool loc.Location.loc_ghost;
"attrs" , `List (List.map ~f:attr (Browse_raw.node_attributes node));
"kind" , `String (Browse_raw.string_of_node node);
"children" , dump_list env node
] :: acc
`Assoc
[ ("filename", `String loc.Location.loc_start.Lexing.pos_fname);
("start", Lexing.json_of_position loc.Location.loc_start);
("end", Lexing.json_of_position loc.Location.loc_end);
("ghost", `Bool loc.Location.loc_ghost);
("attrs", `List (List.map ~f:attr (Browse_raw.node_attributes node)));
("kind", `String (Browse_raw.string_of_node node));
("children", dump_list env node) ]
:: acc
and dump_list env node =
`List (List.sort ~cmp:compare @@
Mbrowse.fold_node append env node [])
`List (List.sort ~cmp:compare @@ Mbrowse.fold_node append env node [])
in
`List (append Env.empty node [])

let annotate_tail_calls (ts : Mbrowse.t) :
(Env.t * Browse_raw.node * Query_protocol.is_tail_position) list =
(Env.t * Browse_raw.node * Query_protocol.is_tail_position) list =
let is_one_of candidates node = List.mem node ~set:candidates in
let find_entry_points candidates (env, node) =
Tail_analysis.entry_points node,
(env, node, is_one_of candidates node) in
(Tail_analysis.entry_points node, (env, node, is_one_of candidates node))
in
let _, entry_points = List.fold_n_map ts ~f:find_entry_points ~init:[] in
let propagate candidates (env, node, entry) =
let is_in_tail = entry || is_one_of candidates node in
(if is_in_tail
then Tail_analysis.tail_positions node
else []),
(env, node, is_in_tail) in
let _, tail_positions = List.fold_n_map entry_points ~f:propagate ~init:[] in
List.map ~f:(fun (env, node, tail) ->
env, node,
if not tail then
`No
else if Tail_analysis.is_call node then
`Tail_call
( (if is_in_tail then
Tail_analysis.tail_positions node
else
`Tail_position)
[]),
(env, node, is_in_tail) )
in
let _, tail_positions = List.fold_n_map entry_points ~f:propagate ~init:[] in
List.map
~f:(fun (env, node, tail) ->
( env,
node,
if not tail then
`No
else if Tail_analysis.is_call node then
`Tail_call
else
`Tail_position ))
tail_positions
Loading