Skip to content

Commit 09d80dd

Browse files
committed
Add support for --keep-padding shfmt option
This wasn't included initially as it has been deprecated: mvdan/sh#658 However, it has been mentioned in bash-lsp#1165 and is a valid option in the current version of `shfmt`, so support has been added (with a suitable deprecation warning).
1 parent 847c09d commit 09d80dd

File tree

6 files changed

+105
-2
lines changed

6 files changed

+105
-2
lines changed

server/src/__tests__/config.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('ConfigSchema', () => {
1717
"binaryNextLine": false,
1818
"caseIndent": false,
1919
"funcNextLine": false,
20+
"keepPadding": false,
2021
"path": "shfmt",
2122
"spaceRedirects": false,
2223
},
@@ -36,6 +37,7 @@ describe('ConfigSchema', () => {
3637
binaryNextLine: true,
3738
caseIndent: true,
3839
funcNextLine: true,
40+
keepPadding: true,
3941
path: 'myshfmt',
4042
spaceRedirects: true,
4143
},
@@ -59,6 +61,7 @@ describe('ConfigSchema', () => {
5961
"binaryNextLine": true,
6062
"caseIndent": true,
6163
"funcNextLine": true,
64+
"keepPadding": true,
6265
"path": "myshfmt",
6366
"spaceRedirects": true,
6467
},
@@ -92,6 +95,7 @@ describe('getConfigFromEnvironmentVariables', () => {
9295
"binaryNextLine": false,
9396
"caseIndent": false,
9497
"funcNextLine": false,
98+
"keepPadding": false,
9599
"path": "shfmt",
96100
"spaceRedirects": false,
97101
},
@@ -119,6 +123,7 @@ describe('getConfigFromEnvironmentVariables', () => {
119123
"binaryNextLine": false,
120124
"caseIndent": false,
121125
"funcNextLine": false,
126+
"keepPadding": false,
122127
"path": "",
123128
"spaceRedirects": false,
124129
},
@@ -155,6 +160,7 @@ describe('getConfigFromEnvironmentVariables', () => {
155160
"binaryNextLine": false,
156161
"caseIndent": true,
157162
"funcNextLine": false,
163+
"keepPadding": false,
158164
"path": "/path/to/shfmt",
159165
"spaceRedirects": false,
160166
},

server/src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ export const ConfigSchema = z.object({
5555
// Place function opening braces on a separate line.
5656
funcNextLine: z.boolean().default(false),
5757

58+
// (Deprecated) Keep column alignment padding.
59+
keepPadding: z.boolean().default(false),
60+
5861
// Follow redirection operators with a space.
5962
spaceRedirects: z.boolean().default(false),
6063
})
@@ -81,6 +84,7 @@ export function getConfigFromEnvironmentVariables(): {
8184
binaryNextLine: toBoolean(process.env.SHFMT_BINARY_NEXT_LINE),
8285
caseIndent: toBoolean(process.env.SHFMT_CASE_INDENT),
8386
funcNextLine: toBoolean(process.env.SHFMT_FUNC_NEXT_LINE),
87+
keepPadding: toBoolean(process.env.SHFMT_KEEP_PADDING),
8488
spaceRedirects: toBoolean(process.env.SHFMT_SPACE_REDIRECTS),
8589
},
8690
}

server/src/shfmt/__tests__/index.test.ts

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ describe('formatter', () => {
8383
;;
8484
esac
8585
86+
echo one two three
87+
echo four five six
88+
echo seven eight nine
89+
8690
echo space redirects >/dev/null
8791
8892
function next() {
@@ -128,6 +132,10 @@ describe('formatter', () => {
128132
;;
129133
esac
130134
135+
echo one two three
136+
echo four five six
137+
echo seven eight nine
138+
131139
echo space redirects >/dev/null
132140
133141
function next() {
@@ -173,6 +181,10 @@ describe('formatter', () => {
173181
;;
174182
esac
175183
184+
echo one two three
185+
echo four five six
186+
echo seven eight nine
187+
176188
echo space redirects >/dev/null
177189
178190
function next() {
@@ -219,6 +231,10 @@ describe('formatter', () => {
219231
;;
220232
esac
221233
234+
echo one two three
235+
echo four five six
236+
echo seven eight nine
237+
222238
echo space redirects >/dev/null
223239
224240
function next() {
@@ -265,6 +281,10 @@ describe('formatter', () => {
265281
;;
266282
esac
267283
284+
echo one two three
285+
echo four five six
286+
echo seven eight nine
287+
268288
echo space redirects >/dev/null
269289
270290
function next() {
@@ -311,6 +331,10 @@ describe('formatter', () => {
311331
;;
312332
esac
313333
334+
echo one two three
335+
echo four five six
336+
echo seven eight nine
337+
314338
echo space redirects >/dev/null
315339
316340
function next()
@@ -333,6 +357,56 @@ describe('formatter', () => {
333357
`)
334358
})
335359

360+
it('should format with padding kept as-is when keepPadding is true', async () => {
361+
const [result] = await getFormattingResult({
362+
document: FIXTURE_DOCUMENT.SHFMT,
363+
formatOptions: { tabSize: 2, insertSpaces: true },
364+
shfmtConfig: { keepPadding: true },
365+
})
366+
expect(result).toMatchInlineSnapshot(`
367+
[
368+
{
369+
"newText": "#!/bin/bash
370+
set -ueo pipefail
371+
372+
if [ -z "$arg" ]; then
373+
echo indent
374+
fi
375+
376+
echo binary &&
377+
echo next line
378+
379+
case "$arg" in
380+
a)
381+
echo case indent
382+
;;
383+
esac
384+
385+
echo one two three
386+
echo four five six
387+
echo seven eight nine
388+
389+
echo space redirects >/dev/null
390+
391+
function next() {
392+
echo line
393+
}
394+
",
395+
"range": {
396+
"end": {
397+
"character": 2147483647,
398+
"line": 2147483647,
399+
},
400+
"start": {
401+
"character": 0,
402+
"line": 0,
403+
},
404+
},
405+
},
406+
]
407+
`)
408+
})
409+
336410
it('should format with redirect operators followed by a space when spaceRedirects is true', async () => {
337411
const [result] = await getFormattingResult({
338412
document: FIXTURE_DOCUMENT.SHFMT,
@@ -358,6 +432,10 @@ describe('formatter', () => {
358432
;;
359433
esac
360434
435+
echo one two three
436+
echo four five six
437+
echo seven eight nine
438+
361439
echo space redirects > /dev/null
362440
363441
function next() {
@@ -387,6 +465,7 @@ describe('formatter', () => {
387465
binaryNextLine: true,
388466
caseIndent: true,
389467
funcNextLine: true,
468+
keepPadding: true,
390469
spaceRedirects: true,
391470
},
392471
})
@@ -401,18 +480,22 @@ describe('formatter', () => {
401480
fi
402481
403482
echo binary \\
404-
&& echo next line
483+
&& echo next line
405484
406485
case "$arg" in
407486
a)
408487
echo case indent
409488
;;
410489
esac
411490
491+
echo one two three
492+
echo four five six
493+
echo seven eight nine
494+
412495
echo space redirects > /dev/null
413496
414497
function next()
415-
{
498+
{
416499
echo line
417500
}
418501
",

server/src/shfmt/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export class Formatter {
7070
if (shfmtConfig?.binaryNextLine) args.push('-bn') // --binary-next-line
7171
if (shfmtConfig?.caseIndent) args.push('-ci') // --case-indent
7272
if (shfmtConfig?.funcNextLine) args.push('-fn') // --func-next-line
73+
if (shfmtConfig?.keepPadding) args.push('-kp') // --keep-padding
7374
if (shfmtConfig?.spaceRedirects) args.push('-sr') // --space-redirects
7475

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

testing/fixtures/shfmt.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ echo case indent
1414
;;
1515
esac
1616

17+
echo one two three
18+
echo four five six
19+
echo seven eight nine
20+
1721
echo space redirects> /dev/null
1822

1923
function next(){

vscode-client/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@
9898
"default": false,
9999
"description": "Place function opening braces on a separate line."
100100
},
101+
"bashIde.shfmt.keepPadding": {
102+
"type": "boolean",
103+
"default": false,
104+
"description": "(Deprecated) Keep column alignment padding."
105+
},
101106
"bashIde.shfmt.spaceRedirects": {
102107
"type": "boolean",
103108
"default": false,

0 commit comments

Comments
 (0)