Skip to content

Commit ea99523

Browse files
committed
fix!: options in .editorconfig not work with shfmt
Must pass --filename option to shfmt. When shfmt read content from stdin, it doesn't know the filename and its extension. So it won't match the patterns "[*.sh]" and "[*.bash]" in .editorconfig. Do not pass any Parser and Printer options like -i/-p/-bn/-l. It will cause the .editorconfig not to be loaded. See https://github.com/mvdan/sh/blob/23633a432f903599a4ce46c30c4337e413a26ef1/cmd/shfmt/main.go#L186-L196 Breaking Change: Removed shfmtConfig options. Use the .editorconfig options instead of. The .editorconfig options of shfmt refer to https://github.com/mvdan/sh/blob/master/cmd/shfmt/shfmt.1.scd#examples
1 parent 04a2cb3 commit ea99523

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

server/src/shfmt/index.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { spawn } from 'child_process'
22
import * as LSP from 'vscode-languageserver/node'
33
import { TextDocument, TextEdit } from 'vscode-languageserver-textdocument'
4+
import { basename } from 'node:path'
45

56
import { logger } from '../util/logger'
67

@@ -41,9 +42,7 @@ export class Formatter {
4142
formatOptions?: LSP.FormattingOptions | null,
4243
shfmtConfig?: Record<string, string | boolean> | null,
4344
): Promise<TextEdit[]> {
44-
const documentText = document.getText()
45-
46-
const result = await this.runShfmt(documentText, formatOptions, shfmtConfig)
45+
const result = await this.runShfmt(document, formatOptions, shfmtConfig)
4746

4847
if (!this._canFormat) {
4948
return []
@@ -61,16 +60,20 @@ export class Formatter {
6160
}
6261

6362
private async runShfmt(
64-
documentText: string,
63+
document: TextDocument,
6564
formatOptions?: LSP.FormattingOptions | null,
6665
shfmtConfig?: Record<string, string | boolean> | null,
6766
): Promise<string> {
68-
const indentation: number = formatOptions?.insertSpaces ? formatOptions.tabSize : 0
69-
const args: string[] = [`-i=${indentation}`] // --indent
70-
if (shfmtConfig?.binaryNextLine) args.push('-bn') // --binary-next-line
71-
if (shfmtConfig?.caseIndent) args.push('-ci') // --case-indent
72-
if (shfmtConfig?.funcNextLine) args.push('-fn') // --func-next-line
73-
if (shfmtConfig?.spaceRedirects) args.push('-sr') // --space-redirects
67+
// documentText: string,
68+
const documentText = document.getText()
69+
const documentUri = document.uri
70+
const filename = basename(documentUri)
71+
72+
// Do not pass any Parser and Printer options like -i/-p/-bn/-l. It will cause the .editorconfig not to be loaded.
73+
// See https://github.com/mvdan/sh/blob/23633a432f903599a4ce46c30c4337e413a26ef1/cmd/shfmt/main.go#L186-L196
74+
const args: string[] = [
75+
`--filename=${filename}`, // Must set filename for matching the rules in .editorconfig.
76+
]
7477

7578
logger.debug(`Shfmt: running "${this.executablePath} ${args.join(' ')}"`)
7679

0 commit comments

Comments
 (0)