Skip to content

Commit

Permalink
Add support for --keep-padding shfmt option
Browse files Browse the repository at this point in the history
This wasn't included initially as it has been deprecated:
mvdan/sh#658

However, it has been mentioned in #1165 and is a valid option in the
current version of `shfmt`, so support has been added (with a suitable
deprecation warning).
  • Loading branch information
chris-reeves committed May 31, 2024
1 parent 847c09d commit 09d80dd
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 2 deletions.
6 changes: 6 additions & 0 deletions server/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('ConfigSchema', () => {
"binaryNextLine": false,
"caseIndent": false,
"funcNextLine": false,
"keepPadding": false,
"path": "shfmt",
"spaceRedirects": false,
},
Expand All @@ -36,6 +37,7 @@ describe('ConfigSchema', () => {
binaryNextLine: true,
caseIndent: true,
funcNextLine: true,
keepPadding: true,
path: 'myshfmt',
spaceRedirects: true,
},
Expand All @@ -59,6 +61,7 @@ describe('ConfigSchema', () => {
"binaryNextLine": true,
"caseIndent": true,
"funcNextLine": true,
"keepPadding": true,
"path": "myshfmt",
"spaceRedirects": true,
},
Expand Down Expand Up @@ -92,6 +95,7 @@ describe('getConfigFromEnvironmentVariables', () => {
"binaryNextLine": false,
"caseIndent": false,
"funcNextLine": false,
"keepPadding": false,
"path": "shfmt",
"spaceRedirects": false,
},
Expand Down Expand Up @@ -119,6 +123,7 @@ describe('getConfigFromEnvironmentVariables', () => {
"binaryNextLine": false,
"caseIndent": false,
"funcNextLine": false,
"keepPadding": false,
"path": "",
"spaceRedirects": false,
},
Expand Down Expand Up @@ -155,6 +160,7 @@ describe('getConfigFromEnvironmentVariables', () => {
"binaryNextLine": false,
"caseIndent": true,
"funcNextLine": false,
"keepPadding": false,
"path": "/path/to/shfmt",
"spaceRedirects": false,
},
Expand Down
4 changes: 4 additions & 0 deletions server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand All @@ -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),
},
}
Expand Down
87 changes: 85 additions & 2 deletions server/src/shfmt/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -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() {
Expand Down Expand Up @@ -387,6 +465,7 @@ describe('formatter', () => {
binaryNextLine: true,
caseIndent: true,
funcNextLine: true,
keepPadding: true,
spaceRedirects: true,
},
})
Expand All @@ -401,18 +480,22 @@ describe('formatter', () => {
fi
echo binary \\
&& echo next line
&& 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
}
",
Expand Down
1 change: 1 addition & 0 deletions server/src/shfmt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ')}"`)
Expand Down
4 changes: 4 additions & 0 deletions testing/fixtures/shfmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
5 changes: 5 additions & 0 deletions vscode-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 09d80dd

Please sign in to comment.