Skip to content

Commit fa7403d

Browse files
committed
Add config params for shfmt printer flags
1 parent 3f31ed7 commit fa7403d

File tree

5 files changed

+104
-13
lines changed

5 files changed

+104
-13
lines changed

server/src/__tests__/config.test.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ describe('ConfigSchema', () => {
1313
"logLevel": "info",
1414
"shellcheckArguments": [],
1515
"shellcheckPath": "shellcheck",
16-
"shfmtPath": "shfmt",
16+
"shfmt": {
17+
"binaryNextLine": false,
18+
"caseIndent": false,
19+
"funcNextLine": false,
20+
"path": "shfmt",
21+
"spaceRedirects": false,
22+
},
1723
}
1824
`)
1925
})
@@ -26,7 +32,13 @@ describe('ConfigSchema', () => {
2632
includeAllWorkspaceSymbols: true,
2733
shellcheckArguments: ' -e SC2001 -e SC2002 ',
2834
shellcheckPath: '',
29-
shfmtPath: 'myshfmt',
35+
shfmt: {
36+
binaryNextLine: true,
37+
caseIndent: true,
38+
funcNextLine: true,
39+
path: 'myshfmt',
40+
spaceRedirects: true,
41+
},
3042
}),
3143
).toMatchInlineSnapshot(`
3244
{
@@ -43,7 +55,13 @@ describe('ConfigSchema', () => {
4355
"SC2002",
4456
],
4557
"shellcheckPath": "",
46-
"shfmtPath": "myshfmt",
58+
"shfmt": {
59+
"binaryNextLine": true,
60+
"caseIndent": true,
61+
"funcNextLine": true,
62+
"path": "myshfmt",
63+
"spaceRedirects": true,
64+
},
4765
}
4866
`)
4967
})
@@ -70,7 +88,13 @@ describe('getConfigFromEnvironmentVariables', () => {
7088
"logLevel": "info",
7189
"shellcheckArguments": [],
7290
"shellcheckPath": "shellcheck",
73-
"shfmtPath": "shfmt",
91+
"shfmt": {
92+
"binaryNextLine": false,
93+
"caseIndent": false,
94+
"funcNextLine": false,
95+
"path": "shfmt",
96+
"spaceRedirects": false,
97+
},
7498
}
7599
`)
76100
})
@@ -91,7 +115,13 @@ describe('getConfigFromEnvironmentVariables', () => {
91115
"logLevel": "info",
92116
"shellcheckArguments": [],
93117
"shellcheckPath": "",
94-
"shfmtPath": "",
118+
"shfmt": {
119+
"binaryNextLine": false,
120+
"caseIndent": false,
121+
"funcNextLine": false,
122+
"path": "",
123+
"spaceRedirects": false,
124+
},
95125
}
96126
`)
97127
})
@@ -101,6 +131,7 @@ describe('getConfigFromEnvironmentVariables', () => {
101131
SHELLCHECK_PATH: '/path/to/shellcheck',
102132
SHELLCHECK_ARGUMENTS: '-e SC2001',
103133
SHFMT_PATH: '/path/to/shfmt',
134+
SHFMT_CASE_INDENT: 'true',
104135
EXPLAINSHELL_ENDPOINT: 'localhost:8080',
105136
GLOB_PATTERN: '*.*',
106137
BACKGROUND_ANALYSIS_MAX_FILES: '1',
@@ -120,7 +151,13 @@ describe('getConfigFromEnvironmentVariables', () => {
120151
"SC2001",
121152
],
122153
"shellcheckPath": "/path/to/shellcheck",
123-
"shfmtPath": "/path/to/shfmt",
154+
"shfmt": {
155+
"binaryNextLine": false,
156+
"caseIndent": true,
157+
"funcNextLine": false,
158+
"path": "/path/to/shfmt",
159+
"spaceRedirects": false,
160+
},
124161
}
125162
`)
126163
})

server/src/config.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,24 @@ export const ConfigSchema = z.object({
4141
// Controls the executable used for ShellCheck linting information. An empty string will disable linting.
4242
shellcheckPath: z.string().trim().default('shellcheck'),
4343

44-
// Controls the executable used for Shfmt formatting. An empty string will disable formatting
45-
shfmtPath: z.string().trim().default('shfmt'),
44+
shfmt: z
45+
.object({
46+
// Controls the executable used for Shfmt formatting. An empty string will disable formatting
47+
path: z.string().trim().default('shfmt'),
48+
49+
// Allow boolean operators (like && and ||) to start a line.
50+
binaryNextLine: z.boolean().default(false),
51+
52+
// Indent patterns in case statements.
53+
caseIndent: z.boolean().default(false),
54+
55+
// Place function opening braces on a separate line.
56+
funcNextLine: z.boolean().default(false),
57+
58+
// Follow redirection operators with a space.
59+
spaceRedirects: z.boolean().default(false),
60+
})
61+
.default({}),
4662
})
4763

4864
export type Config = z.infer<typeof ConfigSchema>
@@ -60,7 +76,13 @@ export function getConfigFromEnvironmentVariables(): {
6076
logLevel: process.env[LOG_LEVEL_ENV_VAR],
6177
shellcheckArguments: process.env.SHELLCHECK_ARGUMENTS,
6278
shellcheckPath: process.env.SHELLCHECK_PATH,
63-
shfmtPath: process.env.SHFMT_PATH,
79+
shfmt: {
80+
path: process.env.SHFMT_PATH,
81+
binaryNextLine: toBoolean(process.env.SHFMT_BINARY_NEXT_LINE),
82+
caseIndent: toBoolean(process.env.SHFMT_CASE_INDENT),
83+
funcNextLine: toBoolean(process.env.SHFMT_FUNC_NEXT_LINE),
84+
spaceRedirects: toBoolean(process.env.SHFMT_SPACE_REDIRECTS),
85+
},
6486
}
6587

6688
const environmentVariablesUsed = Object.entries(rawConfig)

server/src/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,9 @@ export default class BashServer {
279279
this.linter = new Linter({ executablePath: shellcheckPath })
280280
}
281281

282-
const { shfmtPath } = this.config
282+
const shfmtPath = this.config.shfmt?.path
283283
if (!shfmtPath) {
284-
logger.info('Shfmt formatting is disabled as "shfmtPath" was not set')
284+
logger.info('Shfmt formatting is disabled as "shfmt.path" was not set')
285285
this.formatter = undefined
286286
} else {
287287
this.formatter = new Formatter({ executablePath: shfmtPath })

vscode-client/__tests__/config.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ import { LOG_LEVELS } from '../../server/src/util/logger'
44

55
const defaultConfig = getDefaultConfiguration()
66

7+
function flattenObjectKeys(obj: Record<string, any>, prefix = '') {
8+
return Object.keys(obj).reduce((flattenedKeys: string[], key) => {
9+
const pre = prefix.length ? `${prefix}.` : ''
10+
if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
11+
flattenedKeys.push(...flattenObjectKeys(obj[key], pre + key))
12+
} else {
13+
flattenedKeys.push(pre + key)
14+
}
15+
return flattenedKeys
16+
}, [])
17+
}
18+
719
describe('config', () => {
820
const configProperties = packageJson.contributes.configuration.properties
921

@@ -18,7 +30,7 @@ describe('config', () => {
1830
.map((key) => key.replace(/^bashIde\./, ''))
1931
.sort()
2032

21-
const defaultConfigKeys = Object.keys(defaultConfig).sort()
33+
const defaultConfigKeys = flattenObjectKeys(defaultConfig).sort()
2234
expect(configKeys).toEqual(defaultConfigKeys)
2335
})
2436

vscode-client/package.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,30 @@
7878
"default": "",
7979
"description": "Additional ShellCheck arguments. Note that we already add the following arguments: --shell, --format, --external-sources."
8080
},
81-
"bashIde.shfmtPath": {
81+
"bashIde.shfmt.path": {
8282
"type": "string",
8383
"default": "shfmt",
8484
"description": "Controls the executable used for Shfmt formatting. An empty string will disable formatting."
85+
},
86+
"bashIde.shfmt.binaryNextLine": {
87+
"type": "boolean",
88+
"default": "false",
89+
"description": "Allow boolean operators (like && and ||) to start a line."
90+
},
91+
"bashIde.shfmt.caseIndent": {
92+
"type": "boolean",
93+
"default": "false",
94+
"description": "Indent patterns in case statements."
95+
},
96+
"bashIde.shfmt.funcNextLine": {
97+
"type": "boolean",
98+
"default": "false",
99+
"description": "Place function opening braces on a separate line."
100+
},
101+
"bashIde.shfmt.spaceRedirects": {
102+
"type": "boolean",
103+
"default": "false",
104+
"description": "Follow redirection operators with a space."
85105
}
86106
}
87107
}

0 commit comments

Comments
 (0)