Skip to content

Commit

Permalink
Add support for --simplify shfmt option
Browse files Browse the repository at this point in the history
This wasn't included initially as it does more than just format code.
However, its absence has been noted in a comment on the last PR:
#1136 (comment)
and it is a valid `shfmt` option, so support has been added.

There's a similar option (`-mn` or `--minify`), but this will not be
implemented as its output is not intended for human consumption and it
will essentially ignore all other options presented anyway.
  • Loading branch information
chris-reeves committed May 31, 2024
1 parent 351b194 commit 2d81228
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 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 @@ -19,6 +19,7 @@ describe('ConfigSchema', () => {
"funcNextLine": false,
"keepPadding": false,
"path": "shfmt",
"simplifyCode": false,
"spaceRedirects": false,
},
}
Expand All @@ -39,6 +40,7 @@ describe('ConfigSchema', () => {
funcNextLine: true,
keepPadding: true,
path: 'myshfmt',
simplifyCode: true,
spaceRedirects: true,
},
}),
Expand All @@ -63,6 +65,7 @@ describe('ConfigSchema', () => {
"funcNextLine": true,
"keepPadding": true,
"path": "myshfmt",
"simplifyCode": true,
"spaceRedirects": true,
},
}
Expand Down Expand Up @@ -97,6 +100,7 @@ describe('getConfigFromEnvironmentVariables', () => {
"funcNextLine": false,
"keepPadding": false,
"path": "shfmt",
"simplifyCode": false,
"spaceRedirects": false,
},
}
Expand Down Expand Up @@ -125,6 +129,7 @@ describe('getConfigFromEnvironmentVariables', () => {
"funcNextLine": false,
"keepPadding": false,
"path": "",
"simplifyCode": false,
"spaceRedirects": false,
},
}
Expand Down Expand Up @@ -162,6 +167,7 @@ describe('getConfigFromEnvironmentVariables', () => {
"funcNextLine": false,
"keepPadding": false,
"path": "/path/to/shfmt",
"simplifyCode": false,
"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 @@ -58,6 +58,9 @@ export const ConfigSchema = z.object({
// (Deprecated) Keep column alignment padding.
keepPadding: z.boolean().default(false),

// Simplify code before formatting.
simplifyCode: z.boolean().default(false),

// Follow redirection operators with a space.
spaceRedirects: z.boolean().default(false),
})
Expand Down Expand Up @@ -85,6 +88,7 @@ export function getConfigFromEnvironmentVariables(): {
caseIndent: toBoolean(process.env.SHFMT_CASE_INDENT),
funcNextLine: toBoolean(process.env.SHFMT_FUNC_NEXT_LINE),
keepPadding: toBoolean(process.env.SHFMT_KEEP_PADDING),
simplifyCode: toBoolean(process.env.SHFMT_SIMPLIFY_CODE),
spaceRedirects: toBoolean(process.env.SHFMT_SPACE_REDIRECTS),
},
}
Expand Down
71 changes: 71 additions & 0 deletions server/src/shfmt/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ describe('formatter', () => {
echo four five six
echo seven eight nine
[[ "$simplify" == "simplify" ]]
echo space redirects >/dev/null
function next() {
Expand Down Expand Up @@ -136,6 +138,8 @@ describe('formatter', () => {
echo four five six
echo seven eight nine
[[ "$simplify" == "simplify" ]]
echo space redirects >/dev/null
function next() {
Expand Down Expand Up @@ -185,6 +189,8 @@ describe('formatter', () => {
echo four five six
echo seven eight nine
[[ "$simplify" == "simplify" ]]
echo space redirects >/dev/null
function next() {
Expand Down Expand Up @@ -235,6 +241,8 @@ describe('formatter', () => {
echo four five six
echo seven eight nine
[[ "$simplify" == "simplify" ]]
echo space redirects >/dev/null
function next() {
Expand Down Expand Up @@ -285,6 +293,8 @@ describe('formatter', () => {
echo four five six
echo seven eight nine
[[ "$simplify" == "simplify" ]]
echo space redirects >/dev/null
function next() {
Expand Down Expand Up @@ -335,6 +345,8 @@ describe('formatter', () => {
echo four five six
echo seven eight nine
[[ "$simplify" == "simplify" ]]
echo space redirects >/dev/null
function next()
Expand Down Expand Up @@ -386,6 +398,60 @@ describe('formatter', () => {
echo four five six
echo seven eight nine
[[ "$simplify" == "simplify" ]]
echo space redirects >/dev/null
function next() {
echo line
}
",
"range": {
"end": {
"character": 2147483647,
"line": 2147483647,
},
"start": {
"character": 0,
"line": 0,
},
},
},
]
`)
})

it('should format after simplifying the code when simplifyCode is true', async () => {
const [result] = await getFormattingResult({
document: FIXTURE_DOCUMENT.SHFMT,
formatOptions: { tabSize: 2, insertSpaces: true },
shfmtConfig: { simplifyCode: 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
[[ $simplify == "simplify" ]]
echo space redirects >/dev/null
function next() {
Expand Down Expand Up @@ -436,6 +502,8 @@ describe('formatter', () => {
echo four five six
echo seven eight nine
[[ "$simplify" == "simplify" ]]
echo space redirects > /dev/null
function next() {
Expand Down Expand Up @@ -466,6 +534,7 @@ describe('formatter', () => {
caseIndent: true,
funcNextLine: true,
keepPadding: true,
simplifyCode: true,
spaceRedirects: true,
},
})
Expand All @@ -492,6 +561,8 @@ describe('formatter', () => {
echo four five six
echo seven eight nine
[[ $simplify == "simplify" ]]
echo space redirects > /dev/null
function next()
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 @@ -71,6 +71,7 @@ export class Formatter {
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?.simplifyCode) args.push('-s') // --simplify
if (shfmtConfig?.spaceRedirects) args.push('-sr') // --space-redirects

logger.debug(`Shfmt: running "${this.executablePath} ${args.join(' ')}"`)
Expand Down
2 changes: 2 additions & 0 deletions testing/fixtures/shfmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ echo one two three
echo four five six
echo seven eight nine

[[ "$simplify" == "simplify" ]]

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 @@ -104,6 +104,11 @@
"description": "(Deprecated) Keep column alignment padding.",
"markdownDescription": "**([Deprecated](https://github.com/mvdan/sh/issues/658))** Keep column alignment padding."
},
"bashIde.shfmt.simplifyCode": {
"type": "boolean",
"default": false,
"description": "Simplify code before formatting."
},
"bashIde.shfmt.spaceRedirects": {
"type": "boolean",
"default": false,
Expand Down

0 comments on commit 2d81228

Please sign in to comment.