Skip to content

Commit f9e1902

Browse files
aspeddrozth
authored andcommitted
add completion to toplevel decorators
1 parent 1ff514b commit f9e1902

File tree

2 files changed

+68
-33
lines changed

2 files changed

+68
-33
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1475,9 +1475,29 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
14751475
let mkDecorator (name, docstring) =
14761476
{(Completion.create name ~kind:(Label "") ~env) with docstring}
14771477
in
1478+
let isTopLevel = String.starts_with ~prefix:"@" prefix in
1479+
let prefix =
1480+
if isTopLevel then String.sub prefix 1 (String.length prefix - 1)
1481+
else prefix
1482+
in
14781483
CompletionDecorators.decorators
1479-
|> List.filter (fun (decorator, _) -> Utils.startsWith decorator prefix)
1484+
|> List.filter (fun (decorator, _) ->
1485+
match isTopLevel with
1486+
| true -> (
1487+
match decorator with
1488+
| CompletionDecorators.TopLevel s | TopLevelOrLocal s ->
1489+
Utils.startsWith s prefix
1490+
| _ -> false)
1491+
| false -> (
1492+
match decorator with
1493+
| CompletionDecorators.Local s -> Utils.startsWith s prefix
1494+
| _ -> false))
14801495
|> List.map (fun (decorator, doc) ->
1496+
let decorator =
1497+
match decorator with
1498+
| CompletionDecorators.Local s | TopLevel s | TopLevelOrLocal s ->
1499+
s
1500+
in
14811501
let parts = String.split_on_char '.' prefix in
14821502
let len = String.length prefix in
14831503
let dec2 =

analysis/src/CompletionDecorators.ml

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
type kind = TopLevel of string | Local of string | TopLevelOrLocal of string
2+
13
let decorators =
24
[
3-
( "as",
5+
( Local "as",
46
[
57
{|The `@as` decorator is commonly used on record types to alias record field names to a different JavaScript attribute name.
68

@@ -10,7 +12,7 @@ It is also possible to map a ReScript record to a JavaScript array by passing in
1012

1113
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#as-decorator).|};
1214
] );
13-
( "dead",
15+
( Local "dead",
1416
[
1517
{|The `@dead` decorator is for reanalyze, a static analysis tool for ReScript that can do dead code analysis.
1618

@@ -20,21 +22,21 @@ It is also possible to map a ReScript record to a JavaScript array by passing in
2022

2123
> Hint: Did you know you can run an interactive code analysis in your project by running the command `> ReScript: Start Code Analyzer`? Try it!|};
2224
] );
23-
( "deriving",
25+
( Local "deriving",
2426
[
2527
{|When the `@deriving` decorator is applied to a record type, it expands the type into a factory function plus a set of getter/setter functions for its fields.
2628

2729
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#deriving-decorator).|};
2830
] );
29-
( "deprecated",
31+
( TopLevelOrLocal "deprecated",
3032
[
3133
{|The `@deprecated` decorator is used to add deprecation notes to types, values and submodules. The compiler and editor tooling will yield a warning whenever a deprecated entity is being used.
3234

3335
Alternatively, use the `@@deprecated` decorator to add a deprecation warning to the file level.
3436

3537
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#expression-deprecated-decorator).|};
3638
] );
37-
( "doesNotRaise",
39+
( Local "doesNotRaise",
3840
[
3941
{|The `@doesNotRaise` decorator is for reanalyze, a static analysis tool for ReScript that can perform exception analysis.
4042

@@ -45,55 +47,55 @@ could potentially raise.
4547
[Read more and see examples in the documentation](https://github.com/rescript-association/reanalyze/blob/master/EXCEPTION.md).
4648
> Hint: Did you know you can run an interactive code analysis in your project by running the command `> ReScript: Start Code Analyzer`? Try it!|};
4749
] );
48-
( "genType",
50+
( Local "genType",
4951
[
5052
{|The @genType decorator may be used to export ReScript values and types to JavaScript, and import JavaScript values and types into ReScript. It allows seamless integration of compiled ReScript modules in existing TypeScript, Flow, or plain JavaScript codebases, without loosing type information across different type systems.
5153

5254
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#gentype-decorator).|};
5355
] );
54-
( "genType.as",
56+
( Local "genType.as",
5557
[
5658
{|The @genType decorator may be used to export ReScript values and types to JavaScript, and import JavaScript values and types into ReScript. It allows seamless integration of compiled ReScript modules in existing TypeScript, Flow, or plain JavaScript codebases, without loosing type information across different type systems.
5759

5860
[Read more and see examples in the documentation](https://rescript-lang.org/docs/gentype/latest/usage).|};
5961
] );
60-
( "genType.import",
62+
( Local "genType.import",
6163
[
6264
{|The @genType decorator may be used to export ReScript values and types to JavaScript, and import JavaScript values and types into ReScript. It allows seamless integration of compiled ReScript modules in existing TypeScript, Flow, or plain JavaScript codebases, without loosing type information across different type systems.
6365

6466
[Read more and see examples in the documentation](https://rescript-lang.org/docs/gentype/latest/usage).|};
6567
] );
66-
( "genType.opaque",
68+
( Local "genType.opaque",
6769
[
6870
{|The @genType decorator may be used to export ReScript values and types to JavaScript, and import JavaScript values and types into ReScript. It allows seamless integration of compiled ReScript modules in existing TypeScript, Flow, or plain JavaScript codebases, without loosing type information across different type systems.
6971

7072
[Read more and see examples in the documentation](https://rescript-lang.org/docs/gentype/latest/usage).|};
7173
] );
72-
( "get",
74+
( Local "get",
7375
[
7476
{|The `@get` decorator is used to bind to a property of an object.
7577

7678
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#get-decorator).|};
7779
] );
78-
( "get_index",
80+
( Local "get_index",
7981
[
8082
{|The `@get_index` decorator is used to access a dynamic property on an object, or an index of an array.
8183

8284
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#get-index-decorator).|};
8385
] );
84-
( "inline",
86+
( Local "inline",
8587
[
8688
{|The `@inline` decorator tells the compiler to inline its value in every place the binding is being used, rather than use a variable.
8789

8890
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#inline-decorator).|};
8991
] );
90-
( "int",
92+
( Local "int",
9193
[
9294
{|The `@int` decorator can be used with polymorphic variants and the @as decorator on externals to modify the compiled JavaScript to use integers for the values instead of strings.
9395

9496
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#int-decorator).|};
9597
] );
96-
( "live",
98+
( Local "live",
9799
[
98100
{|The `@live` decorator is for reanalyze, a static analysis tool for ReScript that can do dead code analysis.
99101

@@ -103,32 +105,32 @@ could potentially raise.
103105

104106
Hint: Did you know you can run an interactive code analysis in your project by running the command `> ReScript: Start Code Analyzer`? Try it!|};
105107
] );
106-
( "meth",
108+
( Local "meth",
107109
[
108110
{|The `@meth` decorator is used to call a function on a JavaScript object, and avoid issues with currying.
109111

110112
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#meth-decorator).|};
111113
] );
112-
( "module",
114+
( Local "module",
113115
[
114116
{|The `@module` decorator is used to bind to a JavaScript module.
115117

116118
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#module-decorator).|};
117119
] );
118-
( "new",
120+
( Local "new",
119121
[
120122
{|
121123
The `@new` decorator is used whenever you need to bind to a JavaScript class constructor that requires the new keword for instantiation.|
122124

123125
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#new-decorator).|};
124126
] );
125-
( "obj",
127+
( Local "obj",
126128
[
127129
{|The `@obj` decorator is used to create functions that return JavaScript objects with properties that match the function's parameter labels.
128130

129131
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#obj-decorator).|};
130132
] );
131-
( "raises",
133+
( Local "raises",
132134
[
133135
{|The `@raises` decorator is for reanalyze, a static analysis tool for ReScript that can perform exception analysis.
134136

@@ -138,7 +140,7 @@ Example `@raises(Exn)` or `@raises([E1, E2, E3])` for multiple exceptions.
138140
[Read more and see examples in the documentation](https://github.com/rescript-association/reanalyze/blob/master/EXCEPTION.md).
139141
> Hint: Did you know you can run an interactive code analysis in your project by running the command `> ReScript: Start Code Analyzer`? Try it!|};
140142
] );
141-
( "react.component",
143+
( Local "react.component",
142144
[
143145
{|The `@react.component` decorator is used to annotate functions that are RescriptReact components.
144146

@@ -148,76 +150,89 @@ Note: The `@react.component` decorator requires the react-jsx config to be set i
148150

149151
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#react-component-decorator).|};
150152
] );
151-
( "return",
153+
( Local "return",
152154
[
153155
{|The `@return` decorator is used to control how `null` and `undefined` values are converted to option types in ReScript.
154156

155157
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#return-decorator).|};
156158
] );
157-
( "scope",
159+
( Local "scope",
158160
[
159161
{|The `@scope` decorator is used with other decorators such as `@val` and `@module` to declare a parent scope for the binding.
160162

161163
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#scope-decorator).|};
162164
] );
163-
( "send",
165+
( Local "send",
164166
[
165167
{|The `@send` decorator is used to bind to a method on an object or array.
166168

167169
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#send-decorator).|};
168170
] );
169-
( "set",
171+
( Local "set",
170172
[
171173
{|The `@set` decorator is used to set a property of an object.
172174

173175
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#set-decorator).|};
174176
] );
175-
( "set_index",
177+
( Local "set_index",
176178
[
177179
{|The `@set_index` decorator is used to set a dynamic property on an object, or an index of an array.
178180

179181
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#set-index-decorator).|};
180182
] );
181-
( "string",
183+
( Local "string",
182184
[
183185
{|The `@string` decorator can be used with polymorphic variants and the `@as` decorator on externals to modify the string values used for the variants in the compiled JavaScript.
184186

185187
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#string-decorator).|};
186188
] );
187-
( "this",
189+
( Local "this",
188190
[
189191
{|The `@this` decorator may be used to bind to an external callback function that require access to a this context.
190192

191193
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#this-decorator).|};
192194
] );
193-
( "unboxed",
195+
( Local "unboxed",
194196
[
195197
{|The `@unboxed` decorator provides a way to unwrap variant constructors that have a single argument, or record objects that have a single field.
196198

197199
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#unboxed-decorator).|};
198200
] );
199-
( "uncurry",
201+
( Local "uncurry",
200202
[
201203
{|The `@uncurry` decorator can be used to mark any callback argument within an external function as an uncurried function without the need for any explicit uncurried function syntax (`(.) => { ... }`).
202204

203205
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#uncurry-decorator).|};
204206
] );
205-
( "unwrap",
207+
( Local "unwrap",
206208
[
207209
{|The `@unwrap` decorator may be used when binding to external functions that accept multiple types for an argument.
208210

209211
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#unwrap-decorator).|};
210212
] );
211-
( "val",
213+
( Local "val",
212214
[
213215
{|The `@val` decorator allows you to bind to JavaScript values that are on the global scope.
214216

215217
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#val-decorator).|};
216218
] );
217-
( "variadic",
219+
( Local "variadic",
218220
[
219221
{|The `@variadic` decorator is used to model JavaScript functions that take a variable number of arguments, where all arguments are of the same type.
220222

221223
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#variadic-decorator).|};
222224
] );
225+
( TopLevel "directive",
226+
[
227+
{|The `@@directive` will output that string verbatim at the very top of the generated JavaScript file, before any imports.
228+
229+
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#directive-decorator).|};
230+
] );
231+
( TopLevel "warning",
232+
[
233+
{|The @@warning decorator is used to modify the enabled compiler warnings for the current module. See here for all available warning numbers.
234+
235+
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#module-warning-decorator).
236+
|};
237+
] );
223238
]

0 commit comments

Comments
 (0)