Skip to content

Commit 65d6f05

Browse files
authoredMay 25, 2024··
Adjust function template snippet return (#985)
* tweak function snippet template return to play better with incremental type checking * tweak output * changelog
1 parent 34b4c23 commit 65d6f05

6 files changed

+42
-30
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Make sure doc strings are always on top in hovers. https://github.com/rescript-lang/rescript-vscode/pull/956
2424
- 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
2525
- Hover: print signature above docstrings. https://github.com/rescript-lang/rescript-vscode/pull/969
26+
- Adjust function template snippet return. https://github.com/rescript-lang/rescript-vscode/pull/985
2627

2728
#### :rocket: New Feature
2829

‎analysis/src/CompletionBackEnd.ml

+13-2
Original file line numberDiff line numberDiff line change
@@ -1739,11 +1739,22 @@ let rec completeTypedValue ?(typeArgContext : typeArgContext option) ~rawOpens
17391739
| _ -> false
17401740
in
17411741
let asyncPrefix = if isAsync then "async " else "" in
1742+
let functionBody, functionBodyInsertText =
1743+
match args with
1744+
| [(Nolabel, argTyp)] ->
1745+
let varName =
1746+
CompletionExpressions.prettyPrintFnTemplateArgName ~env ~full argTyp
1747+
in
1748+
( (" => " ^ if varName = "()" then "{}" else varName),
1749+
" => ${0:" ^ varName ^ "}" )
1750+
| _ -> (" => {}", " => {${0:()}}")
1751+
in
17421752
[
17431753
create
1744-
(asyncPrefix ^ mkFnArgs ~asSnippet:false ^ " => {}")
1754+
(asyncPrefix ^ mkFnArgs ~asSnippet:false ^ functionBody)
17451755
~includesSnippets:true
1746-
~insertText:(asyncPrefix ^ mkFnArgs ~asSnippet:true ^ " => " ^ "{$0}")
1756+
~insertText:
1757+
(asyncPrefix ^ mkFnArgs ~asSnippet:true ^ functionBodyInsertText)
17471758
~sortText:"A" ~kind:(Value typ) ~env;
17481759
]
17491760
| Tfunction _ ->

‎analysis/tests/src/expected/Completion.res.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2230,13 +2230,13 @@ Resolved opens 3 pervasives Completion.res Completion.res
22302230
ContextPath Type[withUncurried]
22312231
Path withUncurried
22322232
[{
2233-
"label": "v => {}",
2233+
"label": "v => v",
22342234
"kind": 12,
22352235
"tags": [],
22362236
"detail": "int => unit",
22372237
"documentation": null,
22382238
"sortText": "A",
2239-
"insertText": "${1:v} => {$0}",
2239+
"insertText": "${1:v} => ${0:v}",
22402240
"insertTextFormat": 2
22412241
}]
22422242

‎analysis/tests/src/expected/CompletionExpressions.res.txt

+23-23
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ Path fnTakingCallback
783783
"detail": "unit => unit",
784784
"documentation": null,
785785
"sortText": "A",
786-
"insertText": "() => {$0}",
786+
"insertText": "() => ${0:()}",
787787
"insertTextFormat": 2
788788
}]
789789

@@ -808,13 +808,13 @@ ContextPath CArgument Value[fnTakingCallback]($1)
808808
ContextPath Value[fnTakingCallback]
809809
Path fnTakingCallback
810810
[{
811-
"label": "v => {}",
811+
"label": "v => v",
812812
"kind": 12,
813813
"tags": [],
814814
"detail": "bool => unit",
815815
"documentation": null,
816816
"sortText": "A",
817-
"insertText": "${1:v} => {$0}",
817+
"insertText": "${1:v} => ${0:v}",
818818
"insertTextFormat": 2
819819
}]
820820

@@ -828,13 +828,13 @@ ContextPath CArgument Value[fnTakingCallback]($2)
828828
ContextPath Value[fnTakingCallback]
829829
Path fnTakingCallback
830830
[{
831-
"label": "event => {}",
831+
"label": "event => event",
832832
"kind": 12,
833833
"tags": [],
834834
"detail": "ReactEvent.Mouse.t => unit",
835835
"documentation": null,
836836
"sortText": "A",
837-
"insertText": "${1:event} => {$0}",
837+
"insertText": "${1:event} => ${0:event}",
838838
"insertTextFormat": 2
839839
}]
840840

@@ -854,7 +854,7 @@ Path fnTakingCallback
854854
"detail": "(~on: bool, ~off: bool=?, variant) => int",
855855
"documentation": null,
856856
"sortText": "A",
857-
"insertText": "(~on, ~off=?, ${1:variant}) => {$0}",
857+
"insertText": "(~on, ~off=?, ${1:variant}) => {${0:()}}",
858858
"insertTextFormat": 2
859859
}]
860860

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

@@ -894,7 +894,7 @@ Path fnTakingCallback
894894
"detail": "(~on: bool=?, ~off: bool=?, unit) => int",
895895
"documentation": null,
896896
"sortText": "A",
897-
"insertText": "(~on=?, ~off=?, ()) => {$0}",
897+
"insertText": "(~on=?, ~off=?, ()) => {${0:()}}",
898898
"insertTextFormat": 2
899899
}]
900900

@@ -964,13 +964,13 @@ ContextPath CArgument Value[takesCb]($0)
964964
ContextPath Value[takesCb]
965965
Path takesCb
966966
[{
967-
"label": "someTyp => {}",
967+
"label": "someTyp => someTyp",
968968
"kind": 12,
969969
"tags": [],
970970
"detail": "someTyp => 'a",
971971
"documentation": null,
972972
"sortText": "A",
973-
"insertText": "${1:someTyp} => {$0}",
973+
"insertText": "${1:someTyp} => ${0:someTyp}",
974974
"insertTextFormat": 2
975975
}]
976976

@@ -984,13 +984,13 @@ ContextPath CArgument Value[takesCb2]($0)
984984
ContextPath Value[takesCb2]
985985
Path takesCb2
986986
[{
987-
"label": "environment => {}",
987+
"label": "environment => environment",
988988
"kind": 12,
989989
"tags": [],
990990
"detail": "Environment.t => 'a",
991991
"documentation": null,
992992
"sortText": "A",
993-
"insertText": "${1:environment} => {$0}",
993+
"insertText": "${1:environment} => ${0:environment}",
994994
"insertTextFormat": 2
995995
}]
996996

@@ -1004,13 +1004,13 @@ ContextPath CArgument Value[takesCb3]($0)
10041004
ContextPath Value[takesCb3]
10051005
Path takesCb3
10061006
[{
1007-
"label": "apiCallResult => {}",
1007+
"label": "apiCallResult => apiCallResult",
10081008
"kind": 12,
10091009
"tags": [],
10101010
"detail": "apiCallResult => 'a",
10111011
"documentation": null,
10121012
"sortText": "A",
1013-
"insertText": "${1:apiCallResult} => {$0}",
1013+
"insertText": "${1:apiCallResult} => ${0:apiCallResult}",
10141014
"insertTextFormat": 2
10151015
}]
10161016

@@ -1024,13 +1024,13 @@ ContextPath CArgument Value[takesCb4]($0)
10241024
ContextPath Value[takesCb4]
10251025
Path takesCb4
10261026
[{
1027-
"label": "apiCallResult => {}",
1027+
"label": "apiCallResult => apiCallResult",
10281028
"kind": 12,
10291029
"tags": [],
10301030
"detail": "option<apiCallResult> => 'a",
10311031
"documentation": null,
10321032
"sortText": "A",
1033-
"insertText": "${1:apiCallResult} => {$0}",
1033+
"insertText": "${1:apiCallResult} => ${0:apiCallResult}",
10341034
"insertTextFormat": 2
10351035
}]
10361036

@@ -1044,13 +1044,13 @@ ContextPath CArgument Value[takesCb5]($0)
10441044
ContextPath Value[takesCb5]
10451045
Path takesCb5
10461046
[{
1047-
"label": "apiCallResults => {}",
1047+
"label": "apiCallResults => apiCallResults",
10481048
"kind": 12,
10491049
"tags": [],
10501050
"detail": "array<option<apiCallResult>> => 'a",
10511051
"documentation": null,
10521052
"sortText": "A",
1053-
"insertText": "${1:apiCallResults} => {$0}",
1053+
"insertText": "${1:apiCallResults} => ${0:apiCallResults}",
10541054
"insertTextFormat": 2
10551055
}]
10561056

@@ -1064,13 +1064,13 @@ ContextPath CArgument Value[commitLocalUpdate](~updater)
10641064
ContextPath Value[commitLocalUpdate]
10651065
Path commitLocalUpdate
10661066
[{
1067-
"label": "recordSourceSelectorProxy => {}",
1067+
"label": "recordSourceSelectorProxy => recordSourceSelectorProxy",
10681068
"kind": 12,
10691069
"tags": [],
10701070
"detail": "RecordSourceSelectorProxy.t => unit",
10711071
"documentation": null,
10721072
"sortText": "A",
1073-
"insertText": "${1:recordSourceSelectorProxy} => {$0}",
1073+
"insertText": "${1:recordSourceSelectorProxy} => ${0:recordSourceSelectorProxy}",
10741074
"insertTextFormat": 2
10751075
}]
10761076

@@ -1090,7 +1090,7 @@ Path fnTakingAsyncCallback
10901090
"detail": "unit => promise<unit>",
10911091
"documentation": null,
10921092
"sortText": "A",
1093-
"insertText": "async () => {$0}",
1093+
"insertText": "async () => ${0:()}",
10941094
"insertTextFormat": 2
10951095
}]
10961096

@@ -1103,13 +1103,13 @@ ContextPath CArgument Value[Belt, Array, map]($1)
11031103
ContextPath Value[Belt, Array, map]
11041104
Path Belt.Array.map
11051105
[{
1106-
"label": "v => {}",
1106+
"label": "v => v",
11071107
"kind": 12,
11081108
"tags": [],
11091109
"detail": "'a => 'b",
11101110
"documentation": null,
11111111
"sortText": "A",
1112-
"insertText": "${1:v} => {$0}",
1112+
"insertText": "${1:v} => ${0:v}",
11131113
"insertTextFormat": 2
11141114
}]
11151115

‎analysis/tests/src/expected/CompletionJsxProps.res.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ ContextPath CJsxPropValue [div] onMouseEnter
203203
Path ReactDOM.domProps
204204
Path PervasivesU.JsxDOM.domProps
205205
[{
206-
"label": "event => {}",
206+
"label": "event => event",
207207
"kind": 12,
208208
"tags": [],
209209
"detail": "JsxEventU.Mouse.t => unit",
210210
"documentation": null,
211211
"sortText": "A",
212-
"insertText": "{${1:event} => {$0}}",
212+
"insertText": "{${1:event} => ${0:event}}",
213213
"insertTextFormat": 2
214214
}]
215215

‎analysis/tests/src/expected/CompletionTypeAnnotation.res.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Path someFunc
156156
"detail": "(int, string) => bool",
157157
"documentation": null,
158158
"sortText": "A",
159-
"insertText": "(${1:v1}, ${2:v2}) => {$0}",
159+
"insertText": "(${1:v1}, ${2:v2}) => {${0:()}}",
160160
"insertTextFormat": 2
161161
}]
162162

0 commit comments

Comments
 (0)
Please sign in to comment.