Skip to content

Commit 81a7189

Browse files
authored
Formatter: Disable formatter in default config (marcoroth#944)
This pull request changes the default configuration to not have the formatter enabled by default. Since the formatter is still experimental people should be opting in to use it. Since the default configuration had the formatter enabled by default this meant that the language server started formatting on save by default as well, which is not ideal. This pull request addresses this, so that people have to opt-in and enable the formatter in teh `.herb.yml` configuration file. Additionally, it splits up the fix and format concerns in the language server, so that the `editor.formatOnSave` setting in VS Code is also properly respected. Resolves marcoroth#910 Resolves marcoroth#911
1 parent 0b9c9f0 commit 81a7189

File tree

8 files changed

+35
-14
lines changed

8 files changed

+35
-14
lines changed

javascript/packages/config/src/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ export class Config {
139139

140140
/**
141141
* Check if the formatter is enabled.
142-
* @returns true if formatter is enabled (default), false if explicitly disabled
142+
* @returns true if formatter is explicitly enabled, false otherwise (default)
143143
*/
144144
public get isFormatterEnabled(): boolean {
145-
return this.config.formatter?.enabled ?? Config.getDefaultConfig().formatter?.enabled ?? true
145+
return this.config.formatter?.enabled ?? Config.getDefaultConfig().formatter?.enabled ?? false
146146
}
147147

148148
/**
@@ -1096,7 +1096,7 @@ export class Config {
10961096
rules: {}
10971097
},
10981098
formatter: {
1099-
enabled: true,
1099+
enabled: false,
11001100
indentWidth: 2,
11011101
maxLineLength: 80
11021102
}

javascript/packages/config/test/config.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,10 @@ describe("@herb-tools/config", () => {
379379
expect(config.isFormatterEnabled).toBe(false)
380380
})
381381

382-
test("isFormatterEnabled returns true by default", () => {
382+
test("isFormatterEnabled returns false by default", () => {
383383
const config = Config.fromObject({}, { projectPath: testDir })
384384

385-
expect(config.isFormatterEnabled).toBe(true)
385+
expect(config.isFormatterEnabled).toBe(false)
386386
})
387387

388388
test("isRuleDisabled returns true when rule is disabled", () => {

javascript/packages/formatter/src/cli.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,18 @@ export class CLI {
171171
}
172172

173173
const config = await Config.loadForCLI(configFile || startPath, version)
174+
const hasConfigFile = Config.exists(config.projectPath)
174175
const formatterConfig = config.formatter || {}
175176

176-
if (formatterConfig.enabled === false && !isForceMode) {
177+
if (hasConfigFile && formatterConfig.enabled === false && !isForceMode) {
177178
console.log("Formatter is disabled in .herb.yml configuration.")
178179
console.log("To enable formatting, set formatter.enabled: true in .herb.yml")
179180
console.log("Or use --force to format anyway.")
180181

181182
process.exit(0)
182183
}
183184

184-
if (isForceMode && formatterConfig.enabled === false) {
185+
if (isForceMode && hasConfigFile && formatterConfig.enabled === false) {
185186
console.error("⚠️ Forcing formatter run (disabled in .herb.yml)")
186187
console.error()
187188
}

javascript/packages/language-server/src/document_save_service.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,31 @@ export class DocumentSaveService {
1818
this.formattingService = formattingService
1919
}
2020

21+
/**
22+
* Apply only autofix edits on save.
23+
* Called by willSaveWaitUntil - formatting is handled separately by editor.formatOnSave
24+
*/
25+
async applyFixes(document: TextDocument): Promise<TextEdit[]> {
26+
const settings = await this.settings.getDocumentSettings(document.uri)
27+
const fixOnSave = settings?.linter?.fixOnSave !== false
28+
29+
this.connection.console.log(`[DocumentSave] applyFixes fixOnSave=${fixOnSave}`)
30+
31+
if (!fixOnSave) return []
32+
33+
return this.autofixService.autofix(document)
34+
}
35+
36+
/**
37+
* Apply autofix and formatting.
38+
* Called by onDocumentFormatting (manual format or editor.formatOnSave)
39+
*/
2140
async applyFixesAndFormatting(document: TextDocument, reason: TextDocumentSaveReason): Promise<TextEdit[]> {
2241
const settings = await this.settings.getDocumentSettings(document.uri)
2342
const fixOnSave = settings?.linter?.fixOnSave !== false
2443
const formatterEnabled = settings?.formatter?.enabled ?? false
2544

26-
this.connection.console.log(`[DocumentSave] fixOnSave=${fixOnSave}, formatterEnabled=${formatterEnabled}`)
45+
this.connection.console.log(`[DocumentSave] applyFixesAndFormatting fixOnSave=${fixOnSave}, formatterEnabled=${formatterEnabled}`)
2746

2847
let autofixEdits: TextEdit[] = []
2948

javascript/packages/language-server/src/formatting_service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ export class FormattingService {
210210
if (filePath.endsWith('.herb.yml')) return false
211211
if (!this.config) return true
212212

213+
const hasConfigFile = Config.exists(this.config.projectPath)
214+
if (!hasConfigFile) return true
215+
213216
const relativePath = filePath.replace('file://', '').replace(this.project.projectPath + '/', '')
214217

215218
return this.config.isFormatterEnabledForPath(relativePath)

javascript/packages/language-server/src/server.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ import {
1212
DocumentRangeFormattingParams,
1313
CodeActionParams,
1414
CodeActionKind,
15-
TextEdit,
1615
} from "vscode-languageserver/node"
1716

1817
import { Service } from "./service"
1918
import { PersonalHerbSettings } from "./settings"
2019
import { Config } from "@herb-tools/config"
2120

22-
import type { TextDocument } from "vscode-languageserver-textdocument"
23-
2421
export class Server {
2522
private service!: Service
2623
private connection: Connection
@@ -37,7 +34,7 @@ export class Server {
3734
await this.service.init()
3835

3936
this.service.documentService.documents.onWillSaveWaitUntil(async (event) => {
40-
return this.service.documentSaveService.applyFixesAndFormatting(event.document, event.reason)
37+
return this.service.documentSaveService.applyFixes(event.document)
4138
})
4239

4340
const result: InitializeResult = {

javascript/packages/language-server/src/settings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ export class Settings {
8787
// TODO: ideally we can just use Config all the way through
8888
private mergeSettings(userSettings: PersonalHerbSettings | null, projectConfig?: Config): PersonalHerbSettings {
8989
const settings = userSettings || this.defaultSettings
90+
const hasConfigFile = projectConfig ? Config.exists(projectConfig.projectPath) : false
9091

91-
if (!projectConfig) {
92+
if (!projectConfig || !hasConfigFile) {
9293
return {
9394
trace: settings.trace,
9495
linter: {

javascript/packages/language-server/test/document_save_service.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ describe('DocumentSaveService', () => {
274274
)
275275

276276
expect(connection.console.log).toHaveBeenCalledWith(
277-
'[DocumentSave] fixOnSave=true, formatterEnabled=true'
277+
'[DocumentSave] applyFixesAndFormatting fixOnSave=true, formatterEnabled=true'
278278
)
279279
})
280280
})

0 commit comments

Comments
 (0)