Skip to content

Remove builtin formatter #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

- Add support for "dot completion everywhere". In addition to record fields, dots will now complete for object fields, and pipe completions applicable to the type the dot is on. You can also configure where the editor draws extra pipe completions from via the `@editor.completeFrom` attribute. https://github.com/rescript-lang/rescript-vscode/pull/1054
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
Loading