diff --git a/CHANGELOG.md b/CHANGELOG.md index 8000c2bc9..252e754ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,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`, 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 diff --git a/analysis/src/CompletionBackEnd.ml b/analysis/src/CompletionBackEnd.ml index 60199b716..8550c07c5 100644 --- a/analysis/src/CompletionBackEnd.ml +++ b/analysis/src/CompletionBackEnd.ml @@ -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 _ -> diff --git a/analysis/tests/src/expected/Completion.res.txt b/analysis/tests/src/expected/Completion.res.txt index 9b345c299..ccadf462f 100644 --- a/analysis/tests/src/expected/Completion.res.txt +++ b/analysis/tests/src/expected/Completion.res.txt @@ -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 }] diff --git a/analysis/tests/src/expected/CompletionExpressions.res.txt b/analysis/tests/src/expected/CompletionExpressions.res.txt index 0ab4a56f8..1cddba3b7 100644 --- a/analysis/tests/src/expected/CompletionExpressions.res.txt +++ b/analysis/tests/src/expected/CompletionExpressions.res.txt @@ -783,7 +783,7 @@ Path fnTakingCallback "detail": "unit => unit", "documentation": null, "sortText": "A", - "insertText": "() => {$0}", + "insertText": "() => ${0:()}", "insertTextFormat": 2 }] @@ -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 }] @@ -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 }] @@ -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 }] @@ -874,7 +874,7 @@ Path fnTakingCallback "detail": "(bool, option, bool) => unit", "documentation": null, "sortText": "A", - "insertText": "(${1:v1}, ${2:v2}, ${3:v3}) => {$0}", + "insertText": "(${1:v1}, ${2:v2}, ${3:v3}) => {${0:()}}", "insertTextFormat": 2 }] @@ -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 }] @@ -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 }] @@ -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 }] @@ -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 }] @@ -1024,13 +1024,13 @@ ContextPath CArgument Value[takesCb4]($0) ContextPath Value[takesCb4] Path takesCb4 [{ - "label": "apiCallResult => {}", + "label": "apiCallResult => apiCallResult", "kind": 12, "tags": [], "detail": "option => 'a", "documentation": null, "sortText": "A", - "insertText": "${1:apiCallResult} => {$0}", + "insertText": "${1:apiCallResult} => ${0:apiCallResult}", "insertTextFormat": 2 }] @@ -1044,13 +1044,13 @@ ContextPath CArgument Value[takesCb5]($0) ContextPath Value[takesCb5] Path takesCb5 [{ - "label": "apiCallResults => {}", + "label": "apiCallResults => apiCallResults", "kind": 12, "tags": [], "detail": "array> => 'a", "documentation": null, "sortText": "A", - "insertText": "${1:apiCallResults} => {$0}", + "insertText": "${1:apiCallResults} => ${0:apiCallResults}", "insertTextFormat": 2 }] @@ -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 }] @@ -1090,7 +1090,7 @@ Path fnTakingAsyncCallback "detail": "unit => promise", "documentation": null, "sortText": "A", - "insertText": "async () => {$0}", + "insertText": "async () => ${0:()}", "insertTextFormat": 2 }] @@ -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 }] diff --git a/analysis/tests/src/expected/CompletionJsxProps.res.txt b/analysis/tests/src/expected/CompletionJsxProps.res.txt index 1eb10f825..7175c70e4 100644 --- a/analysis/tests/src/expected/CompletionJsxProps.res.txt +++ b/analysis/tests/src/expected/CompletionJsxProps.res.txt @@ -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 }] diff --git a/analysis/tests/src/expected/CompletionTypeAnnotation.res.txt b/analysis/tests/src/expected/CompletionTypeAnnotation.res.txt index 7f8f2ea47..b00084e7f 100644 --- a/analysis/tests/src/expected/CompletionTypeAnnotation.res.txt +++ b/analysis/tests/src/expected/CompletionTypeAnnotation.res.txt @@ -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 }]