From 2d8122855f7dddbfb73f58da0b9c9326ccbc6e63 Mon Sep 17 00:00:00 2001 From: Chris Reeves Date: Fri, 31 May 2024 23:40:06 +0100 Subject: [PATCH] Add support for --simplify shfmt option 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: https://github.com/bash-lsp/bash-language-server/pull/1136#issuecomment-2090804329 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. --- server/src/__tests__/config.test.ts | 6 ++ server/src/config.ts | 4 ++ server/src/shfmt/__tests__/index.test.ts | 71 ++++++++++++++++++++++++ server/src/shfmt/index.ts | 1 + testing/fixtures/shfmt.sh | 2 + vscode-client/package.json | 5 ++ 6 files changed, 89 insertions(+) diff --git a/server/src/__tests__/config.test.ts b/server/src/__tests__/config.test.ts index be2f797a..ba927820 100644 --- a/server/src/__tests__/config.test.ts +++ b/server/src/__tests__/config.test.ts @@ -19,6 +19,7 @@ describe('ConfigSchema', () => { "funcNextLine": false, "keepPadding": false, "path": "shfmt", + "simplifyCode": false, "spaceRedirects": false, }, } @@ -39,6 +40,7 @@ describe('ConfigSchema', () => { funcNextLine: true, keepPadding: true, path: 'myshfmt', + simplifyCode: true, spaceRedirects: true, }, }), @@ -63,6 +65,7 @@ describe('ConfigSchema', () => { "funcNextLine": true, "keepPadding": true, "path": "myshfmt", + "simplifyCode": true, "spaceRedirects": true, }, } @@ -97,6 +100,7 @@ describe('getConfigFromEnvironmentVariables', () => { "funcNextLine": false, "keepPadding": false, "path": "shfmt", + "simplifyCode": false, "spaceRedirects": false, }, } @@ -125,6 +129,7 @@ describe('getConfigFromEnvironmentVariables', () => { "funcNextLine": false, "keepPadding": false, "path": "", + "simplifyCode": false, "spaceRedirects": false, }, } @@ -162,6 +167,7 @@ describe('getConfigFromEnvironmentVariables', () => { "funcNextLine": false, "keepPadding": false, "path": "/path/to/shfmt", + "simplifyCode": false, "spaceRedirects": false, }, } diff --git a/server/src/config.ts b/server/src/config.ts index 2526ff96..26584cfa 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -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), }) @@ -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), }, } diff --git a/server/src/shfmt/__tests__/index.test.ts b/server/src/shfmt/__tests__/index.test.ts index f6f685fd..0c05e5d1 100644 --- a/server/src/shfmt/__tests__/index.test.ts +++ b/server/src/shfmt/__tests__/index.test.ts @@ -87,6 +87,8 @@ describe('formatter', () => { echo four five six echo seven eight nine + [[ "$simplify" == "simplify" ]] + echo space redirects >/dev/null function next() { @@ -136,6 +138,8 @@ describe('formatter', () => { echo four five six echo seven eight nine + [[ "$simplify" == "simplify" ]] + echo space redirects >/dev/null function next() { @@ -185,6 +189,8 @@ describe('formatter', () => { echo four five six echo seven eight nine + [[ "$simplify" == "simplify" ]] + echo space redirects >/dev/null function next() { @@ -235,6 +241,8 @@ describe('formatter', () => { echo four five six echo seven eight nine + [[ "$simplify" == "simplify" ]] + echo space redirects >/dev/null function next() { @@ -285,6 +293,8 @@ describe('formatter', () => { echo four five six echo seven eight nine + [[ "$simplify" == "simplify" ]] + echo space redirects >/dev/null function next() { @@ -335,6 +345,8 @@ describe('formatter', () => { echo four five six echo seven eight nine + [[ "$simplify" == "simplify" ]] + echo space redirects >/dev/null function next() @@ -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() { @@ -436,6 +502,8 @@ describe('formatter', () => { echo four five six echo seven eight nine + [[ "$simplify" == "simplify" ]] + echo space redirects > /dev/null function next() { @@ -466,6 +534,7 @@ describe('formatter', () => { caseIndent: true, funcNextLine: true, keepPadding: true, + simplifyCode: true, spaceRedirects: true, }, }) @@ -492,6 +561,8 @@ describe('formatter', () => { echo four five six echo seven eight nine + [[ $simplify == "simplify" ]] + echo space redirects > /dev/null function next() diff --git a/server/src/shfmt/index.ts b/server/src/shfmt/index.ts index 6ef66372..00304d18 100644 --- a/server/src/shfmt/index.ts +++ b/server/src/shfmt/index.ts @@ -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(' ')}"`) diff --git a/testing/fixtures/shfmt.sh b/testing/fixtures/shfmt.sh index 61344b52..0ab419a7 100644 --- a/testing/fixtures/shfmt.sh +++ b/testing/fixtures/shfmt.sh @@ -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(){ diff --git a/vscode-client/package.json b/vscode-client/package.json index 9744bea4..457653cc 100644 --- a/vscode-client/package.json +++ b/vscode-client/package.json @@ -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,