Skip to content

Commit

Permalink
Remove builtin formatter (#1073)
Browse files Browse the repository at this point in the history
* remove builtin formatter

* changelog
  • Loading branch information
zth authored Feb 16, 2025
1 parent 2019372 commit cb160f5
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 70 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
## master

#### :nail_care: Polish

- Remove the built-in formatter since it has been causing more harm than done good. https://github.com/rescript-lang/rescript-vscode/pull/1073

#### :rocket: New Feature

- Port [7292](https://github.com/rescript-lang/rescript/pull/7292): Fix dot completion issue with React primitives. https://github.com/rescript-lang/rescript-vscode/pull/1074
Expand Down
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,6 @@
"type": "object",
"title": "ReScript",
"properties": {
"rescript.settings.allowBuiltInFormatter": {
"scope": "language-overridable",
"type": "boolean",
"default": false,
"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."
},
"rescript.settings.askToStartBuild": {
"scope": "language-overridable",
"type": "boolean",
Expand Down
6 changes: 0 additions & 6 deletions server/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ These configurations are sent to the server on [initialization](https://microsof

```typescript
interface config {
/**
* Format code using builtin formatter from server
* @default false
*/
allowBuiltInFormatter: boolean;

/**
* Whether you want the extension to prompt for autostarting a ReScript build if a project is opened with no build running
* @default true
Expand Down
2 changes: 0 additions & 2 deletions server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Message } from "vscode-languageserver-protocol";
export type send = (msg: Message) => void;

export interface extensionConfiguration {
allowBuiltInFormatter?: boolean;
askToStartBuild?: boolean;
inlayHints?: {
enable?: boolean;
Expand Down Expand Up @@ -32,7 +31,6 @@ export interface extensionConfiguration {
// initialized, and the current config is received from the client.
let config: { extensionConfiguration: extensionConfiguration } = {
extensionConfiguration: {
allowBuiltInFormatter: false,
askToStartBuild: true,
inlayHints: {
enable: false,
Expand Down
27 changes: 1 addition & 26 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export interface extensionClientCapabilities {
let extensionClientCapabilities: extensionClientCapabilities = {};

// Below here is some state that's not important exactly how long it lives.
let hasPromptedAboutBuiltInFormatter = false;
let pullConfigurationPeriodically: NodeJS.Timeout | null = null;

// https://microsoft.github.io/language-server-protocol/specification#initialize
Expand Down Expand Up @@ -806,12 +805,7 @@ function format(msg: p.RequestMessage): Array<p.Message> {
projectRootPath != null ? projectsFiles.get(projectRootPath) : null;
let bscExeBinaryPath = project?.bscBinaryLocation ?? null;

let formattedResult = utils.formatCode(
bscExeBinaryPath,
filePath,
code,
Boolean(config.extensionConfiguration.allowBuiltInFormatter)
);
let formattedResult = utils.formatCode(bscExeBinaryPath, filePath, code);
if (formattedResult.kind === "success") {
let max = code.length;
let result: p.TextEdit[] = [
Expand All @@ -829,25 +823,6 @@ function format(msg: p.RequestMessage): Array<p.Message> {
result: result,
};
return [response];
} else if (formattedResult.kind === "blocked-using-built-in-formatter") {
// Let's only prompt the user once about this, or things might become annoying.
if (hasPromptedAboutBuiltInFormatter) {
return [fakeSuccessResponse];
}
hasPromptedAboutBuiltInFormatter = true;

let params: p.ShowMessageParams = {
type: p.MessageType.Warning,
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(
JSON.stringify(["rescript.settings.allowBuiltInFormatter"])
)})`,
};
let response: p.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "window/showMessage",
params: params,
};
return [fakeSuccessResponse, response];
} else {
// let the diagnostics logic display the updated syntax errors,
// from the build.
Expand Down
33 changes: 3 additions & 30 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,12 @@ type execResult =
error: string;
};

type formatCodeResult =
| execResult
| {
kind: "blocked-using-built-in-formatter";
};
type formatCodeResult = execResult;

export let formatCode = (
bscPath: p.DocumentUri | null,
filePath: string,
code: string,
allowBuiltInFormatter: boolean
code: string
): formatCodeResult => {
let extension = path.extname(filePath);
let formatTempFileFullPath = createFileInTempDir(extension);
Expand All @@ -132,29 +127,7 @@ export let formatCode = (
result: result.toString(),
};
} else {
if (!allowBuiltInFormatter) {
return {
kind: "blocked-using-built-in-formatter",
};
}

let result = runAnalysisAfterSanityCheck(
formatTempFileFullPath,
["format", formatTempFileFullPath],
false
);

// The formatter returning an empty string means it couldn't format the
// sources, probably because of errors. In that case, we bail from
// formatting by returning the unformatted content.
if (result === "") {
result = code;
}

return {
kind: "success",
result,
};
throw new Error("Could not find ReScript compiler for project.");
}
} catch (e) {
return {
Expand Down

0 comments on commit cb160f5

Please sign in to comment.