<commit_before>(* {{{ COPYING *(

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

  Copyright (C) 2013 - 2015  <NAME>  <frederic.bour(_)lakaban.net>
                             <NAME>  <refis.thomas(_)gmail.com>
                             <NAME>  <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:

  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.

)* }}} *)

open Std

type ('item, 'ast) step = {
  ast: 'ast;
  env: Env.t;
  result: 'item * Types.signature;
  delayed_checks: Typecore.delayed_check list;
  errors: exn list;
  snapshot: Btype.snapshot;
}

type steps = [
  | `Signature of
      (Typedtree.signature_item list, Parsetree.signature_item) step list *
      Parsetree.signature_item list
  | `Structure of
      (Typedtree.structure_item list, Parsetree.structure_item) step list *
      Parsetree.structure_item list
]

type tree = [
  | `Signature of Typedtree.signature
  | `Structure of Typedtree.structure
]

type t = {
  reader: Merlin_reader.t;
  mutable steps: steps;
  extensions: String.Set.t;
  btype_cache: Btype.cache;
  env_cache: Env.cache;
}

let type_signature env sg =
  let sg = Typemod.transl_signature env [sg] in
  (sg.Typedtree.sig_items,
   sg.Typedtree.sig_type),
  sg.Typedtree.sig_final_env

let type_structure env str =
  let str, _, env = Typemod.type_structure env [str] Location.none in
  (str.Typedtree.str_items,
   str.Typedtree.str_type),
  env

let type_steps type_fun items env =
  let caught = ref [] in
  Parsing_aux.catch_warnings caught @@ fun () ->
  Typing_aux.catch_errors caught    @@ fun () ->
  Typecore.delayed_checks := [];
  let renv = ref env in
  let type_item ast =
    let result, env = type_fun !renv ast in
    let item = {
      ast; result; env;
      delayed_checks = !Typecore.delayed_checks;
      errors = !caught;
      snapshot = Btype.snapshot ();
    } in
    renv := env;
    caught := [];
    Typecore.delayed_checks := [];
    item
  in
  List.map ~f:type_item items

let rec update_steps acc = function
  | step :: steps, item :: items
    when Btype.is_valid step.snapshot && step.ast = item ->
    update_steps (step :: acc) (steps, items)
  | _, items -> List.rev acc, items

let update_steps steps = function
  | `Structure items ->
    let steps = match steps with
      | `Structure (steps, _) -> steps
      | _ -> []
    in
    `Structure (update_steps [] (steps, items))
  | `Signature items ->
    let steps = match steps with
      | `Signature (steps, _) -> steps
      | _ -> []
    in
    `Signature (update_steps [] (steps, items))

let is_valid _ = true

let make reader extensions =
  let btype_cache = Btype.new_cache () in
  let env_cache = Env.new_cache
      ~unit_name:(Merlin_source.name (Merlin_reader.source reader)) in
  { reader; extensions; btype_cache; env_cache;
    steps = update_steps `None (Merlin_reader.result reader) }

let update reader t =
  if not (is_valid t) then
    make reader t.extensions
  else if t.reader == reader then
    t
  else if Merlin_reader.compare reader t.reader = 0 then
    {t with reader}
  else
    {t with reader; steps = update_steps t.steps (Merlin_reader.result reader)}

let with_typer t f =
  let open Fluid in
  let' (from_ref Btype.cache) t.btype_cache @@ fun () ->
  let' (from_ref Env.cache)   t.env_cache f

let sig_loc item = item.Parsetree.psig_loc
let str_loc item = item.Parsetree.pstr_loc

let select_items loc_fun offset =
  let rec aux acc = function
    | item :: items
      when (loc_fun item).Location.loc_start.Lexing.pos_cnum <= offset ->
      aux (item :: acc) items
    | rest -> List.rev acc, rest
  in
  aux []

let resume_steps t steps =
  match List.last steps with
  | Some step ->
    Btype.backtrack step.snapshot;
    step.env
  | None -><commit_msg>Typer: reintroduce consistency checking<commit_after>(* {{{ COPYING *(

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

  Copyright (C) 2013 - 2015  <NAME>  <frederic.bour(_)lakaban.net>
                             <NAME>  <refis.thomas(_)gmail.com>
                             <NAME>  <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:

  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.

)* }}} *)

open Std

type ('item, 'ast) step = {
  ast: 'ast;
  env: Env.t;
  result: 'item * Types.signature;
  delayed_checks: Typecore.delayed_check list;
  errors: exn list;
  snapshot: Btype.snapshot;
}

type steps = [
  | `Signature of
      (Typedtree.signature_item list, Parsetree.signature_item) step list *
      Parsetree.signature_item list
  | `Structure of
      (Typedtree.structure_item list, Parsetree.structure_item) step list *
      Parsetree.structure_item list
]

type tree = [
  | `Signature of Typedtree.signature
  | `Structure of Typedtree.structure
]

type t = {
  reader: Merlin_reader.t;
  mutable steps: steps;
  extensions: String.Set.t;
  btype_cache: Btype.cache;
  env_cache: Env.cache;
}

let type_signature env sg =
  let sg = Typemod.transl_signature env [sg] in
  (sg.Typedtree.sig_items,
   sg.Typedtree.sig_type),
  sg.Typedtree.sig_final_env

let type_structure env str =
  let str, _, env = Typemod.type_structure env [str] Location.none in
  (str.Typedtree.str_items,
   str.Typedtree.str_type),
  env

let type_steps type_fun items env =
  let caught = ref [] in
  Parsing_aux.catch_warnings caught @@ fun () ->
  Typing_aux.catch_errors caught    @@ fun () ->
  Typecore.delayed_checks := [];
  let renv = ref env in
  let type_item ast =
    let result, env = type_fun !renv ast in
    let item = {
      ast; result; env;
      delayed_checks = !Typecore.delayed_checks;
      errors = !caught;
      snapshot = Btype.snapshot ();
    } in
    renv := env;
    caught := [];
    Typecore.delayed_checks := [];
    item
  in
  List.map ~f:type_item items

let rec update_steps acc = function
  | step :: steps, item :: items
    when Btype.is_valid step.snapshot && step.ast = item ->
    update_steps (step :: acc) (steps, items)
  | _, items -> List.rev acc, items

let update_steps steps = function
  | `Structure items ->
    let steps = match steps with
      | `Structure (steps, _) -> steps
      | _ -> []
    in
    `Structure (update_steps [] (steps, items))
  | `Signature items ->
    let steps = match steps with
      | `Signature (steps, _) -> steps
      | _ -> []
    in
    `Signature (update_steps [] (steps, items))

let with_typer t f =
  let open Fluid in
  let' (from_ref Btype.cache) t.btype_cache @@ fun () ->
  let' (from_ref Env.cache)   t.env_cache f

let is_valid t =
  with_typer t Env.check_cache_consistency

let make reader extensions =
  let btype_cache = Btype.new_cache () in
  let env_cache = Env.new_cache
      ~unit_name:(Merlin_source.name (Merlin_reader.source reader)) in
  { reader; extensions; btype_cache; env_cache;
    steps = update_steps `None (Merlin_reader.result reader) }

let update reader t =
  if not (is_valid t) then
    make reader t.extensions
  else if t.reader == reader then
    t
  else if Merlin_reader.compare reader t.reader = 0 then
    {t with reader}
  else
    {t with reader; steps = update_steps t.steps (Merlin_reader.result reader)}

let sig_loc item = item.Parsetree.psig_loc
let str_loc item = item.Parsetree.pstr_loc

let select_items loc_fun offset =
  let rec aux acc = function
    | item :: items
      when (loc_fun item).Location.loc_start.Lexing.pos_cnum <= offset ->
      aux (item :: acc) items
    | rest -> List.rev acc, rest
  in
  aux []

let resume_steps t steps =
  match List.last steps with
  | Some step ->
    Btype.backtrack step.snapshot;
    step.env
  | None ->
{'fraction_non_alphanumeric': 0.09155816883662327, 'fraction_numerical': 0.0018899622007559848, 'mean_word_length': 3.1431056981296215, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4008978', 'n_tokens_mistral': 3038, 'n_tokens_neox': 3009, 'n_words': 1032}