Skip to content

Commit

Permalink
Merge pull request #1171 from chris-reeves/shfmt-editorconfig-support
Browse files Browse the repository at this point in the history
Add .editorconfig support for shfmt
  • Loading branch information
skovhus authored Jun 10, 2024
2 parents fe93389 + 986548d commit 23fdaa9
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 22 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ there is no `shfmt`-specific configuration variable for this. If your editor is
two-space indents then that's what it will use. If you're using tabs for indentation then `shfmt`
will use that.

The `shfmt` integration also supports configuration via `.editorconfig`. If any `shfmt`-specific
configuration properties are found in `.editorconfig` then the config in `.editorconfig` will be
used and the language server config will be ignored. This follows `shfmt`'s approach of using either
`.editorconfig` or command line flags, but not both. Note that only `shfmt`-specific configuration
properties are read from `.editorconfig` - indentation preferences are still provided by the editor,
so to format using the indentation specified in `.editorconfig` make sure your editor is also
configured to read `.editorconfig`. It is possible to disable `.editorconfig` support and always use
the language server config by setting the "Ignore Editorconfig" configuration variable.

## Logging

The minimum logging level for the server can be adjusted using the `BASH_IDE_LOG_LEVEL` environment variable
Expand Down
35 changes: 30 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Bash Language Server

## 5.4.0

- Add .editorconfig support for shfmt https://github.com/bash-lsp/bash-language-server/pull/1171

## 5.3.4

- Add additonal shfmt formatting config options https://github.com/bash-lsp/bash-language-server/pull/1168
Expand Down
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A language server for Bash",
"author": "Mads Hartmann",
"license": "MIT",
"version": "5.3.4",
"version": "5.4.0",
"main": "./out/server.js",
"typings": "./out/server.d.ts",
"bin": {
Expand All @@ -17,6 +17,7 @@
"node": ">=16"
},
"dependencies": {
"editorconfig": "2.0.0",
"fast-glob": "3.3.2",
"fuzzy-search": "3.2.1",
"node-fetch": "2.7.0",
Expand Down
12 changes: 12 additions & 0 deletions server/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ describe('ConfigSchema', () => {
"binaryNextLine": false,
"caseIndent": false,
"funcNextLine": false,
"ignoreEditorconfig": false,
"keepPadding": false,
"languageDialect": "auto",
"path": "shfmt",
"simplifyCode": false,
"spaceRedirects": false,
Expand All @@ -38,7 +40,9 @@ describe('ConfigSchema', () => {
binaryNextLine: true,
caseIndent: true,
funcNextLine: true,
ignoreEditorconfig: true,
keepPadding: true,
languageDialect: 'posix',
path: 'myshfmt',
simplifyCode: true,
spaceRedirects: true,
Expand All @@ -63,7 +67,9 @@ describe('ConfigSchema', () => {
"binaryNextLine": true,
"caseIndent": true,
"funcNextLine": true,
"ignoreEditorconfig": true,
"keepPadding": true,
"languageDialect": "posix",
"path": "myshfmt",
"simplifyCode": true,
"spaceRedirects": true,
Expand Down Expand Up @@ -98,7 +104,9 @@ describe('getConfigFromEnvironmentVariables', () => {
"binaryNextLine": false,
"caseIndent": false,
"funcNextLine": false,
"ignoreEditorconfig": false,
"keepPadding": false,
"languageDialect": "auto",
"path": "shfmt",
"simplifyCode": false,
"spaceRedirects": false,
Expand Down Expand Up @@ -127,7 +135,9 @@ describe('getConfigFromEnvironmentVariables', () => {
"binaryNextLine": false,
"caseIndent": false,
"funcNextLine": false,
"ignoreEditorconfig": false,
"keepPadding": false,
"languageDialect": "auto",
"path": "",
"simplifyCode": false,
"spaceRedirects": false,
Expand Down Expand Up @@ -165,7 +175,9 @@ describe('getConfigFromEnvironmentVariables', () => {
"binaryNextLine": false,
"caseIndent": true,
"funcNextLine": false,
"ignoreEditorconfig": false,
"keepPadding": false,
"languageDialect": "auto",
"path": "/path/to/shfmt",
"simplifyCode": false,
"spaceRedirects": false,
Expand Down
8 changes: 8 additions & 0 deletions server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export const ConfigSchema = z.object({
// Controls the executable used for Shfmt formatting. An empty string will disable formatting
path: z.string().trim().default('shfmt'),

// Ignore shfmt config options in .editorconfig (always use language server config)
ignoreEditorconfig: z.boolean().default(false),

// Language dialect to use when parsing (bash/posix/mksh/bats).
languageDialect: z.enum(['auto', 'bash', 'posix', 'mksh', 'bats']).default('auto'),

// Allow boolean operators (like && and ||) to start a line.
binaryNextLine: z.boolean().default(false),

Expand Down Expand Up @@ -84,6 +90,8 @@ export function getConfigFromEnvironmentVariables(): {
shellcheckPath: process.env.SHELLCHECK_PATH,
shfmt: {
path: process.env.SHFMT_PATH,
ignoreEditorconfig: toBoolean(process.env.SHFMT_IGNORE_EDITORCONFIG),
languageDialect: process.env.SHFMT_LANGUAGE_DIALECT,
binaryNextLine: toBoolean(process.env.SHFMT_BINARY_NEXT_LINE),
caseIndent: toBoolean(process.env.SHFMT_CASE_INDENT),
funcNextLine: toBoolean(process.env.SHFMT_FUNC_NEXT_LINE),
Expand Down
Loading

0 comments on commit 23fdaa9

Please sign in to comment.