diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dbfbb2..fa9839a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.4.0 + +- Added: `apple-swift-format.path` can now be an array of strings and defaults to `[/usr/bin/env, swift-format]` [vknabel/vscode-apple-swift-format#17](https://github.com/vknabel/vscode-apple-swift-format/issues/17) + ## 1.3.1 - Fixed: correctly display syntax errors #16 diff --git a/README.md b/README.md index 476fe95..3ba7d55 100755 --- a/README.md +++ b/README.md @@ -47,13 +47,13 @@ let package = Package( ## Configuration -| Config | Type | Default | Description | -| ------------------------------------------------ | ---------- | ----------------------------- | ------------------------------------------------------------- | -| `apple-swift-format.enable` | `Bool` | `true` | Whether apple/swift-format should actually do something. | -| `apple-swift-format.onlyEnableOnSwiftPMProjects` | `Bool` | `false` | Requires and uses a apple/swift-format as SwiftPM dependency. | -| `apple-swift-format.onlyEnableWithConfig` | `Bool` | `false` | Only format if config present. | -| `apple-swift-format.path` | `String` | `/usr/local/bin/swift-format` | The location of the globally installed apple/swift-format. | -| `apple-swift-format.configSearchPaths` | `[String]` | `[".swift-format"]` | Possible paths for apple/swift-format config. | +| Config | Type | Default | Description | +| ------------------------------------------------ | ---------- | ------------------- | ------------------------------------------------------------- | ---------------------------------------------------------- | +| `apple-swift-format.enable` | `Bool` | `true` | Whether apple/swift-format should actually do something. | +| `apple-swift-format.onlyEnableOnSwiftPMProjects` | `Bool` | `false` | Requires and uses a apple/swift-format as SwiftPM dependency. | +| `apple-swift-format.onlyEnableWithConfig` | `Bool` | `false` | Only format if config present. | +| `apple-swift-format.path` | `[String] | String` | `/usr/local/bin/swift-format` | The location of the globally installed apple/swift-format. | +| `apple-swift-format.configSearchPaths` | `[String]` | `[".swift-format"]` | Possible paths for apple/swift-format config. | ## Contributors diff --git a/package.json b/package.json index e1b3af9..d2f5045 100755 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "type": "git", "url": "https://github.com/vknabel/vscode-apple-swift-format" }, - "version": "1.3.1", + "version": "1.4.0", "license": "MIT", "author": { "name": "Valentin Knabel", @@ -69,10 +69,29 @@ "description": "Only use apple/swift-format when a config exists." }, "apple-swift-format.path": { - "type": "string", - "default": "/usr/local/bin/swift-format", "description": "The location of your globally installed apple/swift-format.", - "scope": "machine" + "scope": "machine", + "default": [ + "/usr/bin/env", + "swift-format" + ], + "oneOf": [ + { + "type": "string", + "default": "/usr/local/bin/swift-format" + }, + { + "type": "array", + "minItems": 1, + "default": [ + "/usr/bin/env", + "swift-format" + ], + "items": { + "type": "string" + } + } + ] }, "apple-swift-format.configSearchPaths": { "type": "array", diff --git a/src/Current.ts b/src/Current.ts index f91c565..8193deb 100644 --- a/src/Current.ts +++ b/src/Current.ts @@ -17,7 +17,7 @@ export interface Current { isEnabled(): boolean; onlyEnableOnSwiftPMProjects(): boolean; onlyEnableWithConfig(): boolean; - swiftFormatPath(document: vscode.TextDocument): string | null; + swiftFormatPath(document: vscode.TextDocument): string[] | null; resetSwiftFormatPath(): void; configureSwiftFormatPath(): void; formatConfigSearchPaths(): string[]; @@ -89,7 +89,7 @@ export function prodEnvironment(): Current { const fullPath = join(workspace.uri.fsPath, path); if (existsSync(fullPath)) { - return absolutePath(fullPath); + return [absolutePath(fullPath)]; } } if ( @@ -117,12 +117,18 @@ export function prodEnvironment(): Current { }; } -const fallbackGlobalSwiftFormatPath = () => - absolutePath( - vscode.workspace - .getConfiguration() - .get("apple-swift-format.path", "/usr/local/bin/swift-format") - ); - +const fallbackGlobalSwiftFormatPath = (): string[] => { + const defaultPath = ["/usr/bin/env", "swift-format"]; + const path = vscode.workspace + .getConfiguration() + .get("apple-swift-format.path", defaultPath); + if (typeof path === "string") { + return [absolutePath(path)]; + } else if (Array.isArray(path) && path.length > 0) { + return [absolutePath(path[0]), ...path.slice(1)]; + } else { + return defaultPath; + } +}; const Current = prodEnvironment(); export default Current as Current; diff --git a/src/SwiftFormatEditProvider.ts b/src/SwiftFormatEditProvider.ts index 45e6973..6ceaf2a 100644 --- a/src/SwiftFormatEditProvider.ts +++ b/src/SwiftFormatEditProvider.ts @@ -53,8 +53,12 @@ function format(request: { return []; } const newContents = childProcess.execFileSync( - swiftFormatPath, - [...userDefinedParams.options, ...(request.parameters || [])], + swiftFormatPath[0], + [ + ...swiftFormatPath.slice(1), + ...userDefinedParams.options, + ...(request.parameters || []), + ], { encoding: "utf8", input,