Skip to content

Commit 8b288aa

Browse files
committed
Merge remote-tracking branch 'origin' into format-without-res-project
2 parents 6ebcb9e + b022c94 commit 8b288aa

File tree

9 files changed

+272
-129
lines changed

9 files changed

+272
-129
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## master
2+
23
- Fix issue where using paths of the form `./something` would show multiple copies of the same file in vscode.
34
- When hovering on a field access, show the instantiated type of the field.
45
- Support autocomplete for objects from another module `M.x[...`.
@@ -9,6 +10,7 @@
910
- Fix issue in functions the form "~foo as name" where the location would only cover "ame".
1011
- Extend the command to create an interface file, to support components and ReScript decorators used in bindings.
1112
- Enable formatting files without needing the file to be in an actual ReScript project.
13+
- New feature: Show Outline which was previously disabled.
1214

1315
## 1.2.1
1416

analysis/src/Cli.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ let main () =
7676
| [_; "typeDefinition"; path; line; col] ->
7777
Commands.typeDefinition ~path ~line:(int_of_string line)
7878
~col:(int_of_string col)
79-
| [_; "documentSymbol"; path] -> Commands.documentSymbol ~path
79+
| [_; "documentSymbol"; path] -> DocumentSymbol.command ~path
8080
| [_; "hover"; path; line; col] ->
8181
Commands.hover ~path ~line:(int_of_string line) ~col:(int_of_string col)
8282
| [_; "references"; path; line; col] ->

analysis/src/Commands.ml

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -138,46 +138,6 @@ let references ~path ~line ~col =
138138
(if allLocs = [] then Protocol.null
139139
else "[\n" ^ (allLocs |> String.concat ",\n") ^ "\n]")
140140

141-
let documentSymbol ~path =
142-
let result =
143-
match Cmt.fromPath ~path with
144-
| None -> Protocol.null
145-
| Some {file} ->
146-
let open SharedTypes in
147-
let rec getItems topLevel =
148-
let rec getItem = function
149-
| Module.Value v -> (v |> variableKind, [])
150-
| Type (t, _) -> (t.decl |> declarationKind, [])
151-
| Module (Structure contents) -> (Module, getItems contents.items)
152-
| Module (Constraint (_, modTypeItem)) -> getItem (Module modTypeItem)
153-
| Module (Ident _) -> (Module, [])
154-
in
155-
let fn {Module.name; extentLoc; kind} =
156-
let item, siblings = getItem kind in
157-
if extentLoc.loc_ghost then siblings
158-
else (name, extentLoc, item) :: siblings
159-
in
160-
let x = topLevel |> List.map fn |> List.concat in
161-
x
162-
in
163-
let allSymbols =
164-
getItems file.structure.items
165-
|> List.map (fun (name, loc, kind) ->
166-
Protocol.stringifyDocumentSymbolItem
167-
{
168-
name;
169-
location =
170-
{
171-
uri = Uri2.toString (Uri2.fromPath path);
172-
range = Utils.cmtLocToRange loc;
173-
};
174-
kind = symbolKind kind;
175-
})
176-
in
177-
"[\n" ^ (allSymbols |> String.concat ",\n") ^ "\n]"
178-
in
179-
print_endline result
180-
181141
let rename ~path ~line ~col ~newName =
182142
let result =
183143
match Cmt.fromPath ~path with
@@ -304,7 +264,7 @@ let test ~path =
304264
references ~path ~line ~col
305265
| "doc" ->
306266
print_endline ("DocumentSymbol " ^ path);
307-
documentSymbol ~path
267+
DocumentSymbol.command ~path
308268
| "ren" ->
309269
let newName = String.sub rest 4 (len - mlen - 4) in
310270
let () =

analysis/src/DocumentSymbol.ml

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
(* https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol *)
2+
3+
type kind =
4+
| Module
5+
| Property
6+
| Constructor
7+
| Function
8+
| Variable
9+
| Constant
10+
| String
11+
| Number
12+
| EnumMember
13+
| TypeParameter
14+
15+
let kindNumber = function
16+
| Module -> 2
17+
| Property -> 7
18+
| Constructor -> 9
19+
| Function -> 12
20+
| Variable -> 13
21+
| Constant -> 14
22+
| String -> 15
23+
| Number -> 16
24+
| EnumMember -> 22
25+
| TypeParameter -> 26
26+
27+
let command ~path =
28+
let symbols = ref [] in
29+
let rec exprKind (exp : Parsetree.expression) =
30+
match exp.pexp_desc with
31+
| Pexp_fun _ -> Function
32+
| Pexp_function _ -> Function
33+
| Pexp_constraint (e, _) -> exprKind e
34+
| Pexp_constant (Pconst_string _) -> String
35+
| Pexp_constant (Pconst_float _ | Pconst_integer _) -> Number
36+
| Pexp_constant _ -> Constant
37+
| _ -> Variable
38+
in
39+
let processTypeKind (tk : Parsetree.type_kind) =
40+
match tk with
41+
| Ptype_variant constrDecls ->
42+
constrDecls
43+
|> List.iter (fun (cd : Parsetree.constructor_declaration) ->
44+
symbols := (cd.pcd_name.txt, cd.pcd_loc, EnumMember) :: !symbols)
45+
| Ptype_record labelDecls ->
46+
labelDecls
47+
|> List.iter (fun (ld : Parsetree.label_declaration) ->
48+
symbols := (ld.pld_name.txt, ld.pld_loc, Property) :: !symbols)
49+
| _ -> ()
50+
in
51+
let processTypeDeclaration (td : Parsetree.type_declaration) =
52+
symbols := (td.ptype_name.txt, td.ptype_loc, TypeParameter) :: !symbols;
53+
processTypeKind td.ptype_kind
54+
in
55+
let processValueDescription (vd : Parsetree.value_description) =
56+
symbols := (vd.pval_name.txt, vd.pval_loc, Variable) :: !symbols
57+
in
58+
let processModuleBinding (mb : Parsetree.module_binding) =
59+
symbols := (mb.pmb_name.txt, mb.pmb_loc, Module) :: !symbols
60+
in
61+
let processModuleDeclaration (md : Parsetree.module_declaration) =
62+
symbols := (md.pmd_name.txt, md.pmd_loc, Module) :: !symbols
63+
in
64+
let processExtensionConstructor (et : Parsetree.extension_constructor) =
65+
symbols := (et.pext_name.txt, et.pext_loc, Constructor) :: !symbols
66+
in
67+
let value_binding (iterator : Ast_iterator.iterator)
68+
(vb : Parsetree.value_binding) =
69+
(match vb.pvb_pat.ppat_desc with
70+
| Ppat_var {txt} | Ppat_constraint ({ppat_desc = Ppat_var {txt}}, _) ->
71+
symbols := (txt, vb.pvb_loc, exprKind vb.pvb_expr) :: !symbols
72+
| _ -> ());
73+
Ast_iterator.default_iterator.value_binding iterator vb
74+
in
75+
let expr (iterator : Ast_iterator.iterator) (e : Parsetree.expression) =
76+
(match e.pexp_desc with
77+
| Pexp_letmodule ({txt}, modExpr, _) ->
78+
symbols :=
79+
(txt, {e.pexp_loc with loc_end = modExpr.pmod_loc.loc_end}, Module)
80+
:: !symbols
81+
| Pexp_letexception (ec, _) -> processExtensionConstructor ec
82+
| _ -> ());
83+
Ast_iterator.default_iterator.expr iterator e
84+
in
85+
let structure_item (iterator : Ast_iterator.iterator)
86+
(item : Parsetree.structure_item) =
87+
(match item.pstr_desc with
88+
| Pstr_value _ -> ()
89+
| Pstr_primitive vd -> processValueDescription vd
90+
| Pstr_type (_, typDecls) -> typDecls |> List.iter processTypeDeclaration
91+
| Pstr_module mb -> processModuleBinding mb
92+
| Pstr_recmodule mbs -> mbs |> List.iter processModuleBinding
93+
| Pstr_exception ec -> processExtensionConstructor ec
94+
| _ -> Ast_iterator.default_iterator.structure_item iterator item);
95+
Ast_iterator.default_iterator.structure_item iterator item
96+
in
97+
let signature_item (iterator : Ast_iterator.iterator)
98+
(item : Parsetree.signature_item) =
99+
(match item.psig_desc with
100+
| Psig_value vd -> processValueDescription vd
101+
| Psig_type (_, typDecls) -> typDecls |> List.iter processTypeDeclaration
102+
| Psig_module md -> processModuleDeclaration md
103+
| Psig_recmodule mds -> mds |> List.iter processModuleDeclaration
104+
| Psig_exception ec -> processExtensionConstructor ec
105+
| _ -> ());
106+
Ast_iterator.default_iterator.signature_item iterator item
107+
in
108+
let module_expr (iterator : Ast_iterator.iterator)
109+
(me : Parsetree.module_expr) =
110+
match me.pmod_desc with
111+
| Pmod_constraint (modExpr, _modTyp) ->
112+
(* Don't double-list items in implementation and interface *)
113+
Ast_iterator.default_iterator.module_expr iterator modExpr
114+
| _ -> Ast_iterator.default_iterator.module_expr iterator me
115+
in
116+
let iterator =
117+
{
118+
Ast_iterator.default_iterator with
119+
expr;
120+
module_expr;
121+
signature_item;
122+
structure_item;
123+
value_binding;
124+
}
125+
in
126+
127+
(if Filename.check_suffix path ".res" then
128+
let parser =
129+
Res_driver.parsingEngine.parseImplementation ~forPrinter:false
130+
in
131+
let {Res_driver.parsetree = structure} = parser ~filename:path in
132+
iterator.structure iterator structure |> ignore
133+
else
134+
let parser = Res_driver.parsingEngine.parseInterface ~forPrinter:false in
135+
let {Res_driver.parsetree = signature} = parser ~filename:path in
136+
iterator.signature iterator signature |> ignore);
137+
let result =
138+
!symbols
139+
|> List.rev_map (fun (name, loc, kind) ->
140+
Protocol.stringifyDocumentSymbolItem
141+
{
142+
name;
143+
location =
144+
{
145+
uri = Uri2.toString (Uri2.fromPath path);
146+
range = Utils.cmtLocToRange loc;
147+
};
148+
kind = kindNumber kind;
149+
})
150+
|> String.concat ",\n"
151+
in
152+
print_endline ("[\n" ^ result ^ "\n]")

analysis/src/ProcessCmt.ml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ let rec forTypeSignatureItem ~env ~(exported : Exported.t)
5353
{
5454
Module.kind = Module.Value declared.item;
5555
name = declared.name.txt;
56-
extentLoc = declared.extentLoc;
5756
};
5857
]
5958
| Sig_type
@@ -132,7 +131,6 @@ let rec forTypeSignatureItem ~env ~(exported : Exported.t)
132131
{
133132
Module.kind = Type (declared.item, recStatus);
134133
name = declared.name.txt;
135-
extentLoc = declared.extentLoc;
136134
};
137135
]
138136
| Sig_module (ident, {md_type; md_attributes; md_loc}, _) ->
@@ -148,7 +146,6 @@ let rec forTypeSignatureItem ~env ~(exported : Exported.t)
148146
{
149147
Module.kind = Module declared.item;
150148
name = declared.name.txt;
151-
extentLoc = declared.extentLoc;
152149
};
153150
]
154151
| _ -> []
@@ -237,7 +234,6 @@ let forTypeDeclaration ~env ~(exported : Exported.t)
237234
{
238235
Module.kind = Module.Type (declared.item, recStatus);
239236
name = declared.name.txt;
240-
extentLoc = declared.extentLoc;
241237
}
242238

243239
let rec forSignatureItem ~env ~(exported : Exported.t)
@@ -255,7 +251,6 @@ let rec forSignatureItem ~env ~(exported : Exported.t)
255251
{
256252
Module.kind = Module.Value declared.item;
257253
name = declared.name.txt;
258-
extentLoc = declared.extentLoc;
259254
};
260255
]
261256
| Tsig_type (recFlag, decls) ->
@@ -281,7 +276,6 @@ let rec forSignatureItem ~env ~(exported : Exported.t)
281276
{
282277
Module.kind = Module declared.item;
283278
name = declared.name.txt;
284-
extentLoc = declared.extentLoc;
285279
};
286280
]
287281
| Tsig_recmodule modDecls ->
@@ -357,11 +351,7 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
357351
Stamps.addValue
358352
in
359353
items :=
360-
{
361-
Module.kind = Module.Value declared.item;
362-
name = declared.name.txt;
363-
extentLoc = declared.extentLoc;
364-
}
354+
{Module.kind = Module.Value declared.item; name = declared.name.txt}
365355
:: !items
366356
| Tpat_tuple pats | Tpat_array pats | Tpat_construct (_, _, pats) ->
367357
pats |> List.iter (fun p -> handlePattern [] p)
@@ -389,7 +379,6 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
389379
{
390380
Module.kind = Module declared.item;
391381
name = declared.name.txt;
392-
extentLoc = declared.extentLoc;
393382
};
394383
]
395384
| Tstr_recmodule modDecls ->
@@ -421,7 +410,6 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
421410
{
422411
Module.kind = Module modTypeItem;
423412
name = declared.name.txt;
424-
extentLoc = declared.extentLoc;
425413
};
426414
]
427415
| Tstr_include {incl_mod; incl_type} ->
@@ -451,7 +439,6 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
451439
{
452440
Module.kind = Value declared.item;
453441
name = declared.name.txt;
454-
extentLoc = declared.extentLoc;
455442
};
456443
]
457444
| Tstr_type (recFlag, decls) ->
@@ -1080,7 +1067,7 @@ struct
10801067
addScopeExtent expression.exp_loc
10811068
| Texp_function {cases} -> (
10821069
match cases with
1083-
| [{c_lhs = {pat_desc = Tpat_var _}; c_rhs}]->
1070+
| [{c_lhs = {pat_desc = Tpat_var _}; c_rhs}] ->
10841071
addScopeExtent c_rhs.exp_loc
10851072
| _ -> ())
10861073
| _ -> ()

analysis/src/SharedTypes.ml

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ module Module = struct
8383
| Type of Type.t * Types.rec_status
8484
| Module of t
8585

86-
and item = {kind : kind; name : string; extentLoc : Location.t}
86+
and item = {kind : kind; name : string}
8787

8888
and structure = {
8989
docstring : string list;
@@ -389,49 +389,3 @@ let locItemToString {loc = {Location.loc_start; loc_end}; locType} =
389389

390390
(* needed for debugging *)
391391
let _ = locItemToString
392-
393-
module SymbolKind = struct
394-
type t =
395-
| Module
396-
| Enum
397-
| Interface
398-
| Function
399-
| Variable
400-
| Array
401-
| Object
402-
| Null
403-
| EnumMember
404-
| TypeParameter
405-
end
406-
407-
let rec variableKind t =
408-
match t.Types.desc with
409-
| Tlink t -> variableKind t
410-
| Tsubst t -> variableKind t
411-
| Tarrow _ -> SymbolKind.Function
412-
| Ttuple _ -> Array
413-
| Tconstr _ -> Variable
414-
| Tobject _ -> Object
415-
| Tnil -> Null
416-
| Tvariant _ -> EnumMember
417-
| Tpoly _ -> EnumMember
418-
| Tpackage _ -> Module
419-
| _ -> Variable
420-
421-
let symbolKind = function
422-
| SymbolKind.Module -> 2
423-
| Enum -> 10
424-
| Interface -> 11
425-
| Function -> 12
426-
| Variable -> 13
427-
| Array -> 18
428-
| Object -> 19
429-
| Null -> 21
430-
| EnumMember -> 22
431-
| TypeParameter -> 26
432-
433-
let declarationKind t =
434-
match t.Types.type_kind with
435-
| Type_open | Type_abstract -> SymbolKind.TypeParameter
436-
| Type_record _ -> Interface
437-
| Type_variant _ -> Enum

0 commit comments

Comments
 (0)