documentSymbol emits span of entire construct
[hiphop-php.git] / hphp / hack / src / utils / symbolDefinition.ml
blob7b41fd45415a1470979afcf77c738460d74dc945
1 (**
2 * Copyright (c) 2015, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
8 *)
10 open Hh_core
12 type kind =
13 | Function
14 | Class
15 | Method
16 | Property
17 | Const
18 | Enum
19 | Interface
20 | Trait
21 | LocalVar
22 | Typeconst
23 | Param
24 | Typedef
26 and modifier =
27 | Final
28 | Static
29 | Abstract
30 | Private
31 | Public
32 | Protected
33 | Async
34 | Inout
36 and 'a t = {
37 kind : kind;
38 name : string;
39 full_name : string;
40 id : string option;
41 pos : 'a Pos.pos; (* covers the span of just the identifier *)
42 span : 'a Pos.pos; (* covers the span of the entire construct, including children *)
43 modifiers : modifier list;
44 children : 'a t list option;
45 params : 'a t list option;
46 docblock : string option;
49 let rec to_absolute x = {
50 kind = x.kind;
51 name = x.name;
52 full_name = x.full_name;
53 id = x.id;
54 pos = Pos.to_absolute x.pos;
55 span = Pos.to_absolute x.span;
56 modifiers = x.modifiers;
57 children = Option.map x.children (fun x -> List.map x to_absolute);
58 params = Option.map x.params (fun x -> List.map x to_absolute);
59 docblock = x.docblock;
62 let string_of_kind = function
63 | Function -> "function"
64 | Class -> "class"
65 | Method -> "method"
66 | Property -> "property"
67 | Const -> "const"
68 | Enum -> "enum"
69 | Interface -> "interface"
70 | Trait -> "trait"
71 | Typeconst -> "typeconst"
72 | LocalVar -> "local"
73 | Param -> "param"
74 | Typedef -> "typedef"
76 let string_of_modifier = function
77 | Final -> "final"
78 | Static -> "static"
79 | Abstract -> "abstract"
80 | Private -> "private"
81 | Public -> "public"
82 | Protected -> "protected"
83 | Async -> "async"
84 | Inout -> "inout"
86 let function_kind_name = "function"
87 let type_id_kind_name = "type_id"
88 let method_kind_name = "method"
89 let property_kind_name = "property"
90 let class_const_kind_name = "class_const"
92 let get_symbol_id kind parent_class name =
93 let prefix = match kind with
94 | Function -> Some function_kind_name
95 | Class | Typedef | Enum | Interface | Trait -> Some type_id_kind_name
96 | Method -> Some method_kind_name
97 | Property -> Some property_kind_name
98 | Typeconst | Const -> Some class_const_kind_name
99 | LocalVar | Param -> None
101 match prefix, parent_class with
102 | Some prefix, Some parent_class ->
103 Some (Printf.sprintf "%s::%s::%s" prefix parent_class name)
104 | Some prefix, None ->
105 Some (Printf.sprintf "%s::%s" prefix name)
106 | None, _ -> None