package pfff

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

Source file graph_code_tags.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
(* Yoann Padioleau
 *
 * Copyright (C) 2012 Facebook
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation, with the
 * special exception on linking described in file license.txt.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the file
 * license.txt for more details.
 *)
open Common

module G = Graph_code
module E = Entity_code

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* Generating a set of TAGS from a graph_code.
 * 
 * alternatives:
 *  - could start from the prolog facts (themselves generated from graph_code)
 *    to factorize some code, but for TAGS we are only interested
 *    in position and the only predicate that matters, at/3, does not 
 *    actually contain enough information such as the byte offset in the file,
 *    so let's copy paste for now.
 *)

(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)

(*****************************************************************************)
(* Main entry point *)
(*****************************************************************************)

(* quite similar to graph_code_prolog *)
let defs_of_graph_code ?(verbose=false) g =
  ignore(verbose);

  (* we use the multi-values-to-same-key property of Hashtbl.add and
   * Hashtbl.find_all
   *)
  let hfile_to_tags = Hashtbl.create 101 in

  let hmemo_file_array = Hashtbl.create 101 in
  

  g +> G.iter_nodes (fun n ->
    let (str, kind) = n in
    (try 
      let nodeinfo = G.nodeinfo n g in
      let file = nodeinfo.G.pos.Parse_info.file in
      let line = nodeinfo.G.pos.Parse_info.line in
      let text = 
        try 
          let array = Common.memoized hmemo_file_array file (fun () ->
            Common2.cat_array file
          )
          in
          (* not sure why, but can't put an empty string for 
           * tag_definition_text; Emacs is then getting really confused
           *)
          array.(line)
        with
        | Invalid_argument _out_of_bound ->
          pr2 (spf "PB accessing line %d of %s" line file);
          ""
        | Sys_error _no_such_file ->
          pr2_once (spf "PB accessing file %s" file);
          ""
      in
      let tag = { Tags_file.
         tagname = str;
         line_number = nodeinfo.G.pos.Parse_info.line;
         byte_offset = nodeinfo.G.pos.Parse_info.charpos;
         kind = kind;
         tag_definition_text = text;
      }
      in
      Hashtbl.add hfile_to_tags file tag;
      (* when add a tag for List.foo, also add foo.List *)
      let reversed_tagname = 
        Common.split "\\." str +> List.rev +> Common.join "." in
      Hashtbl.add hfile_to_tags file 
        { tag with Tags_file.tagname = reversed_tagname }

    with Not_found -> 
      (match kind with
      | E.Package | E.File | E.Dir | E.TopStmts | E.Module -> ()
      | _ -> 
        if List.mem G.not_found (G.parents n g)
        then ()
        else pr2 (spf "PB, nodeinfo not found for %s" str);
      )
    )
  );
  Common2.hkeys hfile_to_tags +> List.map (fun file ->
    file, 
    Hashtbl.find_all hfile_to_tags file 
    +> List.map (fun tag -> tag.Tags_file.byte_offset, tag)
    +> Common.sort_by_key_lowfirst
    +> List.map snd
  )