diff --git a/server/src/__tests__/config.test.ts b/server/src/__tests__/config.test.ts index 1d02dcf0..be2f797a 100644 --- a/server/src/__tests__/config.test.ts +++ b/server/src/__tests__/config.test.ts @@ -17,6 +17,7 @@ describe('ConfigSchema', () => { "binaryNextLine": false, "caseIndent": false, "funcNextLine": false, + "keepPadding": false, "path": "shfmt", "spaceRedirects": false, }, @@ -36,6 +37,7 @@ describe('ConfigSchema', () => { binaryNextLine: true, caseIndent: true, funcNextLine: true, + keepPadding: true, path: 'myshfmt', spaceRedirects: true, }, @@ -59,6 +61,7 @@ describe('ConfigSchema', () => { "binaryNextLine": true, "caseIndent": true, "funcNextLine": true, + "keepPadding": true, "path": "myshfmt", "spaceRedirects": true, }, @@ -92,6 +95,7 @@ describe('getConfigFromEnvironmentVariables', () => { "binaryNextLine": false, "caseIndent": false, "funcNextLine": false, + "keepPadding": false, "path": "shfmt", "spaceRedirects": false, }, @@ -119,6 +123,7 @@ describe('getConfigFromEnvironmentVariables', () => { "binaryNextLine": false, "caseIndent": false, "funcNextLine": false, + "keepPadding": false, "path": "", "spaceRedirects": false, }, @@ -155,6 +160,7 @@ describe('getConfigFromEnvironmentVariables', () => { "binaryNextLine": false, "caseIndent": true, "funcNextLine": false, + "keepPadding": false, "path": "/path/to/shfmt", "spaceRedirects": false, }, diff --git a/server/src/config.ts b/server/src/config.ts index 1e39903b..2526ff96 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -55,6 +55,9 @@ export const ConfigSchema = z.object({ // Place function opening braces on a separate line. funcNextLine: z.boolean().default(false), + // (Deprecated) Keep column alignment padding. + keepPadding: z.boolean().default(false), + // Follow redirection operators with a space. spaceRedirects: z.boolean().default(false), }) @@ -81,6 +84,7 @@ export function getConfigFromEnvironmentVariables(): { binaryNextLine: toBoolean(process.env.SHFMT_BINARY_NEXT_LINE), caseIndent: toBoolean(process.env.SHFMT_CASE_INDENT), funcNextLine: toBoolean(process.env.SHFMT_FUNC_NEXT_LINE), + keepPadding: toBoolean(process.env.SHFMT_KEEP_PADDING), spaceRedirects: toBoolean(process.env.SHFMT_SPACE_REDIRECTS), }, } diff --git a/server/src/shfmt/__tests__/index.test.ts b/server/src/shfmt/__tests__/index.test.ts index d29f98d0..f6f685fd 100644 --- a/server/src/shfmt/__tests__/index.test.ts +++ b/server/src/shfmt/__tests__/index.test.ts @@ -83,6 +83,10 @@ describe('formatter', () => { ;; esac + echo one two three + echo four five six + echo seven eight nine + echo space redirects >/dev/null function next() { @@ -128,6 +132,10 @@ describe('formatter', () => { ;; esac + echo one two three + echo four five six + echo seven eight nine + echo space redirects >/dev/null function next() { @@ -173,6 +181,10 @@ describe('formatter', () => { ;; esac + echo one two three + echo four five six + echo seven eight nine + echo space redirects >/dev/null function next() { @@ -219,6 +231,10 @@ describe('formatter', () => { ;; esac + echo one two three + echo four five six + echo seven eight nine + echo space redirects >/dev/null function next() { @@ -265,6 +281,10 @@ describe('formatter', () => { ;; esac + echo one two three + echo four five six + echo seven eight nine + echo space redirects >/dev/null function next() { @@ -311,6 +331,10 @@ describe('formatter', () => { ;; esac + echo one two three + echo four five six + echo seven eight nine + echo space redirects >/dev/null function next() @@ -333,6 +357,56 @@ describe('formatter', () => { `) }) + it('should format with padding kept as-is when keepPadding is true', async () => { + const [result] = await getFormattingResult({ + document: FIXTURE_DOCUMENT.SHFMT, + formatOptions: { tabSize: 2, insertSpaces: true }, + shfmtConfig: { keepPadding: true }, + }) + expect(result).toMatchInlineSnapshot(` + [ + { + "newText": "#!/bin/bash + set -ueo pipefail + + if [ -z "$arg" ]; then + echo indent + fi + + echo binary && + echo next line + + case "$arg" in + a) + echo case indent + ;; + esac + + echo one two three + echo four five six + echo seven eight nine + + echo space redirects >/dev/null + + function next() { + echo line + } + ", + "range": { + "end": { + "character": 2147483647, + "line": 2147483647, + }, + "start": { + "character": 0, + "line": 0, + }, + }, + }, + ] + `) + }) + it('should format with redirect operators followed by a space when spaceRedirects is true', async () => { const [result] = await getFormattingResult({ document: FIXTURE_DOCUMENT.SHFMT, @@ -358,6 +432,10 @@ describe('formatter', () => { ;; esac + echo one two three + echo four five six + echo seven eight nine + echo space redirects > /dev/null function next() { @@ -387,6 +465,7 @@ describe('formatter', () => { binaryNextLine: true, caseIndent: true, funcNextLine: true, + keepPadding: true, spaceRedirects: true, }, }) @@ -401,7 +480,7 @@ describe('formatter', () => { fi echo binary \\ - && echo next line + && echo next line case "$arg" in a) @@ -409,10 +488,14 @@ describe('formatter', () => { ;; esac + echo one two three + echo four five six + echo seven eight nine + echo space redirects > /dev/null function next() - { + { echo line } ", diff --git a/server/src/shfmt/index.ts b/server/src/shfmt/index.ts index e039a04d..6ef66372 100644 --- a/server/src/shfmt/index.ts +++ b/server/src/shfmt/index.ts @@ -70,6 +70,7 @@ export class Formatter { if (shfmtConfig?.binaryNextLine) args.push('-bn') // --binary-next-line if (shfmtConfig?.caseIndent) args.push('-ci') // --case-indent if (shfmtConfig?.funcNextLine) args.push('-fn') // --func-next-line + if (shfmtConfig?.keepPadding) args.push('-kp') // --keep-padding if (shfmtConfig?.spaceRedirects) args.push('-sr') // --space-redirects logger.debug(`Shfmt: running "${this.executablePath} ${args.join(' ')}"`) diff --git a/testing/fixtures/shfmt.sh b/testing/fixtures/shfmt.sh index ff14200f..61344b52 100644 --- a/testing/fixtures/shfmt.sh +++ b/testing/fixtures/shfmt.sh @@ -14,6 +14,10 @@ echo case indent ;; esac +echo one two three +echo four five six +echo seven eight nine + echo space redirects> /dev/null function next(){ diff --git a/vscode-client/package.json b/vscode-client/package.json index 7c164ccf..93d8020e 100644 --- a/vscode-client/package.json +++ b/vscode-client/package.json @@ -98,6 +98,11 @@ "default": false, "description": "Place function opening braces on a separate line." }, + "bashIde.shfmt.keepPadding": { + "type": "boolean", + "default": false, + "description": "(Deprecated) Keep column alignment padding." + }, "bashIde.shfmt.spaceRedirects": { "type": "boolean", "default": false,