Skip to content

Commit

Permalink
Adjust function template snippet return (#985)
Browse files Browse the repository at this point in the history
* tweak function snippet template return to play better with incremental type checking

* tweak output

* changelog
  • Loading branch information
zth authored May 25, 2024
1 parent 34b4c23 commit 65d6f05
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Make sure doc strings are always on top in hovers. https://github.com/rescript-lang/rescript-vscode/pull/956
- Make JSX completion work for `make` functions of type `React.component<props>`, like what you get when using `React.lazy_`. https://github.com/rescript-lang/rescript-vscode/pull/966
- Hover: print signature above docstrings. https://github.com/rescript-lang/rescript-vscode/pull/969
- Adjust function template snippet return. https://github.com/rescript-lang/rescript-vscode/pull/985

#### :rocket: New Feature

Expand Down
15 changes: 13 additions & 2 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1739,11 +1739,22 @@ let rec completeTypedValue ?(typeArgContext : typeArgContext option) ~rawOpens
| _ -> false
in
let asyncPrefix = if isAsync then "async " else "" in
let functionBody, functionBodyInsertText =
match args with
| [(Nolabel, argTyp)] ->
let varName =
CompletionExpressions.prettyPrintFnTemplateArgName ~env ~full argTyp
in
( (" => " ^ if varName = "()" then "{}" else varName),
" => ${0:" ^ varName ^ "}" )
| _ -> (" => {}", " => {${0:()}}")
in
[
create
(asyncPrefix ^ mkFnArgs ~asSnippet:false ^ " => {}")
(asyncPrefix ^ mkFnArgs ~asSnippet:false ^ functionBody)
~includesSnippets:true
~insertText:(asyncPrefix ^ mkFnArgs ~asSnippet:true ^ " => " ^ "{$0}")
~insertText:
(asyncPrefix ^ mkFnArgs ~asSnippet:true ^ functionBodyInsertText)
~sortText:"A" ~kind:(Value typ) ~env;
]
| Tfunction _ ->
Expand Down
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 @@ -2230,13 +2230,13 @@ Resolved opens 3 pervasives Completion.res Completion.res
ContextPath Type[withUncurried]
Path withUncurried
[{
"label": "v => {}",
"label": "v => v",
"kind": 12,
"tags": [],
"detail": "int => unit",
"documentation": null,
"sortText": "A",
"insertText": "${1:v} => {$0}",
"insertText": "${1:v} => ${0:v}",
"insertTextFormat": 2
}]

Expand Down
46 changes: 23 additions & 23 deletions analysis/tests/src/expected/CompletionExpressions.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ Path fnTakingCallback
"detail": "unit => unit",
"documentation": null,
"sortText": "A",
"insertText": "() => {$0}",
"insertText": "() => ${0:()}",
"insertTextFormat": 2
}]

Expand All @@ -808,13 +808,13 @@ ContextPath CArgument Value[fnTakingCallback]($1)
ContextPath Value[fnTakingCallback]
Path fnTakingCallback
[{
"label": "v => {}",
"label": "v => v",
"kind": 12,
"tags": [],
"detail": "bool => unit",
"documentation": null,
"sortText": "A",
"insertText": "${1:v} => {$0}",
"insertText": "${1:v} => ${0:v}",
"insertTextFormat": 2
}]

Expand All @@ -828,13 +828,13 @@ ContextPath CArgument Value[fnTakingCallback]($2)
ContextPath Value[fnTakingCallback]
Path fnTakingCallback
[{
"label": "event => {}",
"label": "event => event",
"kind": 12,
"tags": [],
"detail": "ReactEvent.Mouse.t => unit",
"documentation": null,
"sortText": "A",
"insertText": "${1:event} => {$0}",
"insertText": "${1:event} => ${0:event}",
"insertTextFormat": 2
}]

Expand All @@ -854,7 +854,7 @@ Path fnTakingCallback
"detail": "(~on: bool, ~off: bool=?, variant) => int",
"documentation": null,
"sortText": "A",
"insertText": "(~on, ~off=?, ${1:variant}) => {$0}",
"insertText": "(~on, ~off=?, ${1:variant}) => {${0:()}}",
"insertTextFormat": 2
}]

Expand All @@ -874,7 +874,7 @@ Path fnTakingCallback
"detail": "(bool, option<bool>, bool) => unit",
"documentation": null,
"sortText": "A",
"insertText": "(${1:v1}, ${2:v2}, ${3:v3}) => {$0}",
"insertText": "(${1:v1}, ${2:v2}, ${3:v3}) => {${0:()}}",
"insertTextFormat": 2
}]

Expand All @@ -894,7 +894,7 @@ Path fnTakingCallback
"detail": "(~on: bool=?, ~off: bool=?, unit) => int",
"documentation": null,
"sortText": "A",
"insertText": "(~on=?, ~off=?, ()) => {$0}",
"insertText": "(~on=?, ~off=?, ()) => {${0:()}}",
"insertTextFormat": 2
}]

Expand Down Expand Up @@ -964,13 +964,13 @@ ContextPath CArgument Value[takesCb]($0)
ContextPath Value[takesCb]
Path takesCb
[{
"label": "someTyp => {}",
"label": "someTyp => someTyp",
"kind": 12,
"tags": [],
"detail": "someTyp => 'a",
"documentation": null,
"sortText": "A",
"insertText": "${1:someTyp} => {$0}",
"insertText": "${1:someTyp} => ${0:someTyp}",
"insertTextFormat": 2
}]

Expand All @@ -984,13 +984,13 @@ ContextPath CArgument Value[takesCb2]($0)
ContextPath Value[takesCb2]
Path takesCb2
[{
"label": "environment => {}",
"label": "environment => environment",
"kind": 12,
"tags": [],
"detail": "Environment.t => 'a",
"documentation": null,
"sortText": "A",
"insertText": "${1:environment} => {$0}",
"insertText": "${1:environment} => ${0:environment}",
"insertTextFormat": 2
}]

Expand All @@ -1004,13 +1004,13 @@ ContextPath CArgument Value[takesCb3]($0)
ContextPath Value[takesCb3]
Path takesCb3
[{
"label": "apiCallResult => {}",
"label": "apiCallResult => apiCallResult",
"kind": 12,
"tags": [],
"detail": "apiCallResult => 'a",
"documentation": null,
"sortText": "A",
"insertText": "${1:apiCallResult} => {$0}",
"insertText": "${1:apiCallResult} => ${0:apiCallResult}",
"insertTextFormat": 2
}]

Expand All @@ -1024,13 +1024,13 @@ ContextPath CArgument Value[takesCb4]($0)
ContextPath Value[takesCb4]
Path takesCb4
[{
"label": "apiCallResult => {}",
"label": "apiCallResult => apiCallResult",
"kind": 12,
"tags": [],
"detail": "option<apiCallResult> => 'a",
"documentation": null,
"sortText": "A",
"insertText": "${1:apiCallResult} => {$0}",
"insertText": "${1:apiCallResult} => ${0:apiCallResult}",
"insertTextFormat": 2
}]

Expand All @@ -1044,13 +1044,13 @@ ContextPath CArgument Value[takesCb5]($0)
ContextPath Value[takesCb5]
Path takesCb5
[{
"label": "apiCallResults => {}",
"label": "apiCallResults => apiCallResults",
"kind": 12,
"tags": [],
"detail": "array<option<apiCallResult>> => 'a",
"documentation": null,
"sortText": "A",
"insertText": "${1:apiCallResults} => {$0}",
"insertText": "${1:apiCallResults} => ${0:apiCallResults}",
"insertTextFormat": 2
}]

Expand All @@ -1064,13 +1064,13 @@ ContextPath CArgument Value[commitLocalUpdate](~updater)
ContextPath Value[commitLocalUpdate]
Path commitLocalUpdate
[{
"label": "recordSourceSelectorProxy => {}",
"label": "recordSourceSelectorProxy => recordSourceSelectorProxy",
"kind": 12,
"tags": [],
"detail": "RecordSourceSelectorProxy.t => unit",
"documentation": null,
"sortText": "A",
"insertText": "${1:recordSourceSelectorProxy} => {$0}",
"insertText": "${1:recordSourceSelectorProxy} => ${0:recordSourceSelectorProxy}",
"insertTextFormat": 2
}]

Expand All @@ -1090,7 +1090,7 @@ Path fnTakingAsyncCallback
"detail": "unit => promise<unit>",
"documentation": null,
"sortText": "A",
"insertText": "async () => {$0}",
"insertText": "async () => ${0:()}",
"insertTextFormat": 2
}]

Expand All @@ -1103,13 +1103,13 @@ ContextPath CArgument Value[Belt, Array, map]($1)
ContextPath Value[Belt, Array, map]
Path Belt.Array.map
[{
"label": "v => {}",
"label": "v => v",
"kind": 12,
"tags": [],
"detail": "'a => 'b",
"documentation": null,
"sortText": "A",
"insertText": "${1:v} => {$0}",
"insertText": "${1:v} => ${0:v}",
"insertTextFormat": 2
}]

Expand Down
4 changes: 2 additions & 2 deletions analysis/tests/src/expected/CompletionJsxProps.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ ContextPath CJsxPropValue [div] onMouseEnter
Path ReactDOM.domProps
Path PervasivesU.JsxDOM.domProps
[{
"label": "event => {}",
"label": "event => event",
"kind": 12,
"tags": [],
"detail": "JsxEventU.Mouse.t => unit",
"documentation": null,
"sortText": "A",
"insertText": "{${1:event} => {$0}}",
"insertText": "{${1:event} => ${0:event}}",
"insertTextFormat": 2
}]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Path someFunc
"detail": "(int, string) => bool",
"documentation": null,
"sortText": "A",
"insertText": "(${1:v1}, ${2:v2}) => {$0}",
"insertText": "(${1:v1}, ${2:v2}) => {${0:()}}",
"insertTextFormat": 2
}]

Expand Down

0 comments on commit 65d6f05

Please sign in to comment.