Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add infra for adding docs for attributes. #424

Merged
merged 8 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## master

- Fix issue with autocompletion for constructors in switch statements.
- Fix issue with autocompletion inside template expressions.
- Fix handling of local opens.
- Fix extension crash when renaming a file.
- Add hover information with links to documentation for decorators.

## 1.3.0

Expand Down
8 changes: 6 additions & 2 deletions analysis/src/Commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ let hover ~path ~line ~col ~currentFile ~debug =
~forHover:true
in
match completions with
| {kind = Label typString} :: _ ->
Protocol.stringifyHover (Hover.codeBlock typString)
| {kind = Label typString; docstring} :: _ ->
let parts =
(if typString = "" then [] else [Hover.codeBlock typString])
@ docstring
in
Protocol.stringifyHover (String.concat "\n\n" parts)
| _ -> (
match CompletionBackEnd.completionsGetTypeEnv completions with
| Some (typ, _env) ->
Expand Down
236 changes: 205 additions & 31 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1402,46 +1402,220 @@ let processCompletable ~debug ~package ~scope ~env ~pos ~forHover
|> List.map mkLabel)
@ keyLabels
| Cdecorator prefix ->
let mkDecorator name = Completion.create ~name ~kind:(Label "") ~env in
let mkDecorator (name, docstring) =
{(Completion.create ~name ~kind:(Label "") ~env) with docstring}
in
[
"as";
"deriving";
"genType";
"genType.as";
"genType.import";
"genType.opaque";
"get";
"get_index";
"inline";
"int";
"meth";
"module";
"new";
"obj";
"react.component";
"return";
"scope";
"send";
"set";
"set_index";
"string";
"this";
"unboxed";
"uncurry";
"unwrap";
"val";
"variadic";
( "as",
[
{|The `@as` decorator is commonly used on record types to alias record field names to a different JavaScript attribute name.

This is useful to map to JavaScript attribute names that cannot be expressed in ReScript (such as keywords).

It is also possible to map a ReScript record to a JavaScript array by passing indices to the `@as` decorator.

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

`@dead` suppresses reporting on the value/type, but can also be used to force the analysis to consider a value as dead. Typically used to acknowledge cases of dead code you are not planning to address right now, but can be searched easily later.

[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#dead-decorator).

> Hint: Did you know you can run an interactive dead code analysis in your project by running the command `> ReScript: Start dead code analysis.`? Try it!|};
] );
( "deriving",
[
{|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.

[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#deriving-decorator).|};
] );
( "deprecated",
[
{|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.

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

[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#expression-deprecated-decorator).|};
] );
( "genType",
[
{|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.

[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#gentype-decorator).|};
] );
( "genType.as",
[
{|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.

[Read more and see examples in the documentation](https://rescript-lang.org/docs/gentype/latest/usage).|};
] );
( "genType.import",
[
{|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.

[Read more and see examples in the documentation](https://rescript-lang.org/docs/gentype/latest/usage).|};
] );
( "genType.opaque",
[
{|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.

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

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

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

[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#inline-decorator).|};
] );
( "int",
[
{|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.

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

`@live` tells the dead code analysis that the value should be considered live, even though it might appear to be dead. This is typically used in case of FFI where there are indirect ways to access values. It can be added to everything that could otherwise be considered unused by the dead code analysis - values, functions, arguments, records, individual record fields, and so on.

[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#live-decorator).

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

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

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

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

[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#obj-decorator).|};
] );
( "react.component",
[
{|The `@react.component` decorator is used to annotate functions that are RescriptReact components.

You will need this decorator whenever you want to use a ReScript / React component in ReScript JSX expressions.

Note: The `@react.component` decorator requires the react-jsx config to be set in your `bsconfig.json` to enable the required React transformations.

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

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

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

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

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

[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#set-index-decorator).|};
] );
( "string",
[
{|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.

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

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

[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#unboxed-decorator).|};
] );
( "uncurry",
[
{|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 (`(.) => { ... }`).

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

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

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

[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#variadic-decorator).|};
] );
]
|> List.filter (fun decorator -> Utils.startsWith decorator prefix)
|> List.map (fun decorator ->
|> List.filter (fun (decorator, _) -> Utils.startsWith decorator prefix)
|> List.map (fun (decorator, doc) ->
let parts = String.split_on_char '.' prefix in
let len = String.length prefix in
let dec2 =
if List.length parts > 1 then
String.sub decorator len (String.length decorator - len)
else decorator
in
dec2)
(dec2, doc))
|> List.map mkDecorator
| CnamedArg (cp, prefix, identsSeen) ->
let labels =
Expand Down
3 changes: 3 additions & 0 deletions analysis/tests/src/Hover.res
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,6 @@ let typeOk = funAlias

let typeDuplicate = AA.fnnxx
// ^hov

@live let dd = 34
// ^hov
4 changes: 2 additions & 2 deletions analysis/tests/src/expected/Completion.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ Completable: Cdecorator(reac)
"kind": 4,
"tags": [],
"detail": "",
"documentation": null
"documentation": {"kind": "markdown", "value": "The `@react.component` decorator is used to annotate functions that are RescriptReact components.\n\nYou will need this decorator whenever you want to use a ReScript / React component in ReScript JSX expressions.\n\nNote: The `@react.component` decorator requires the react-jsx config to be set in your `bsconfig.json` to enable the required React transformations.\n\n[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#react-component-decorator)."}
}]

Complete tests/src/Completion.res 68:10
Expand All @@ -507,7 +507,7 @@ Completable: Cdecorator(react.)
"kind": 4,
"tags": [],
"detail": "",
"documentation": null
"documentation": {"kind": "markdown", "value": "The `@react.component` decorator is used to annotate functions that are RescriptReact components.\n\nYou will need this decorator whenever you want to use a ReScript / React component in ReScript JSX expressions.\n\nNote: The `@react.component` decorator requires the react-jsx config to be set in your `bsconfig.json` to enable the required React transformations.\n\n[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#react-component-decorator)."}
}]

Complete tests/src/Completion.res 71:27
Expand Down
6 changes: 6 additions & 0 deletions analysis/tests/src/expected/Hover.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ Hover tests/src/Hover.res 106:16
Hover tests/src/Hover.res 109:25
{"contents": "```rescript\nAA.cond<[< #str(string)]> => AA.cond<[< #str(string)]>\n```"}

Hover tests/src/Hover.res 112:3
Nothing at that position. Now trying to use completion.
Attribute id:live:[112:0->112:5] label:live
Completable: Cdecorator(live)
{"contents": "The `@live` decorator is for reanalyze, a static analysis tool for ReScript that can do dead code analysis.\n\n`@live` tells the dead code analysis that the value should be considered live, even though it might appear to be dead. This is typically used in case of FFI where there are indirect ways to access values. It can be added to everything that could otherwise be considered unused by the dead code analysis - values, functions, arguments, records, individual record fields, and so on.\n\n[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#live-decorator).\n\nHint: Did you know you can run an interactive dead code analysis in your project by running the command `> ReScript: Start dead code analysis.`? Try it!"}