Skip to content

Commit 91eef72

Browse files
committed
prompt user to explicitly allow formatting using the built in formatter
1 parent 01074fe commit 91eef72

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@
121121
"type": "object",
122122
"title": "ReScript",
123123
"properties": {
124+
"rescript.settings.allowBuiltInFormatter": {
125+
"scope": "language-overridable",
126+
"type": "boolean",
127+
"default": false,
128+
"description": "Whether you want to allow the extension to format your code using its built in formatter when it cannot find a ReScript compiler version in your current project to use for formatting."
129+
},
124130
"rescript.settings.askToStartBuild": {
125131
"scope": "language-overridable",
126132
"type": "boolean",

server/src/server.ts

+26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { WorkspaceEdit } from "vscode-languageserver";
2525
import { filesDiagnostics } from "./utils";
2626

2727
interface extensionConfiguration {
28+
allowBuiltInFormatter: boolean;
2829
askToStartBuild: boolean;
2930
inlayHints: {
3031
enable: boolean;
@@ -37,6 +38,7 @@ interface extensionConfiguration {
3738
// All values here are temporary, and will be overridden as the server is
3839
// initialized, and the current config is received from the client.
3940
let extensionConfiguration: extensionConfiguration = {
41+
allowBuiltInFormatter: false,
4042
askToStartBuild: true,
4143
inlayHints: {
4244
enable: false,
@@ -45,6 +47,8 @@ let extensionConfiguration: extensionConfiguration = {
4547
codeLens: false,
4648
binaryPath: null,
4749
};
50+
// Below here is some state that's not important exactly how long it lives.
51+
let hasPromptedAboutBuiltInFormatter = false;
4852
let pullConfigurationPeriodically: NodeJS.Timeout | null = null;
4953

5054
// https://microsoft.github.io/language-server-protocol/specification#initialize
@@ -736,6 +740,28 @@ function format(msg: p.RequestMessage): Array<p.Message> {
736740
let projectRootPath = utils.findProjectRootOfFile(filePath);
737741
let bscBinaryPath =
738742
projectRootPath === null ? null : findBscBinary(projectRootPath);
743+
744+
if (
745+
bscBinaryPath == null &&
746+
!extensionConfiguration.allowBuiltInFormatter &&
747+
!hasPromptedAboutBuiltInFormatter
748+
) {
749+
// Let's only prompt the user once about this, or things might become annoying.
750+
hasPromptedAboutBuiltInFormatter = true;
751+
let params: p.ShowMessageParams = {
752+
type: p.MessageType.Warning,
753+
message: `Formatting not applied! Could not find the ReScript compiler in the current project, and you haven't configured the extension to allow formatting using the built in formatter. To allow formatting files not strictly part of a ReScript project using the built in formatter, [please configure the extension to allow that.](command:workbench.action.openSettings?${encodeURIComponent(
754+
"rescript.settings.allowBuiltInFormatter"
755+
)})`,
756+
};
757+
let response: p.NotificationMessage = {
758+
jsonrpc: c.jsonrpcVersion,
759+
method: "window/showMessage",
760+
params: params,
761+
};
762+
return [fakeSuccessResponse, response];
763+
}
764+
739765
let formattedResult = utils.formatCode(bscBinaryPath, filePath, code);
740766
if (formattedResult.kind === "success") {
741767
let max = code.length;

0 commit comments

Comments
 (0)