Skip to content

Commit ecb2c4d

Browse files
committed
feat: smarter path default for #17
1 parent 27dfee3 commit ecb2c4d

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.4.0
2+
3+
- 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)
4+
15
## 1.3.1
26

37
- Fixed: correctly display syntax errors #16

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ let package = Package(
4747
4848
## Configuration
4949

50-
| Config | Type | Default | Description |
51-
| ------------------------------------------------ | ---------- | ----------------------------- | ------------------------------------------------------------- |
52-
| `apple-swift-format.enable` | `Bool` | `true` | Whether apple/swift-format should actually do something. |
53-
| `apple-swift-format.onlyEnableOnSwiftPMProjects` | `Bool` | `false` | Requires and uses a apple/swift-format as SwiftPM dependency. |
54-
| `apple-swift-format.onlyEnableWithConfig` | `Bool` | `false` | Only format if config present. |
55-
| `apple-swift-format.path` | `String` | `/usr/local/bin/swift-format` | The location of the globally installed apple/swift-format. |
56-
| `apple-swift-format.configSearchPaths` | `[String]` | `[".swift-format"]` | Possible paths for apple/swift-format config. |
50+
| Config | Type | Default | Description |
51+
| ------------------------------------------------ | ---------- | ------------------- | ------------------------------------------------------------- | ---------------------------------------------------------- |
52+
| `apple-swift-format.enable` | `Bool` | `true` | Whether apple/swift-format should actually do something. |
53+
| `apple-swift-format.onlyEnableOnSwiftPMProjects` | `Bool` | `false` | Requires and uses a apple/swift-format as SwiftPM dependency. |
54+
| `apple-swift-format.onlyEnableWithConfig` | `Bool` | `false` | Only format if config present. |
55+
| `apple-swift-format.path` | `[String] | String` | `/usr/local/bin/swift-format` | The location of the globally installed apple/swift-format. |
56+
| `apple-swift-format.configSearchPaths` | `[String]` | `[".swift-format"]` | Possible paths for apple/swift-format config. |
5757

5858
## Contributors
5959

package.json

+23-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "git",
77
"url": "https://github.com/vknabel/vscode-apple-swift-format"
88
},
9-
"version": "1.3.1",
9+
"version": "1.4.0",
1010
"license": "MIT",
1111
"author": {
1212
"name": "Valentin Knabel",
@@ -69,10 +69,29 @@
6969
"description": "Only use apple/swift-format when a config exists."
7070
},
7171
"apple-swift-format.path": {
72-
"type": "string",
73-
"default": "/usr/local/bin/swift-format",
7472
"description": "The location of your globally installed apple/swift-format.",
75-
"scope": "machine"
73+
"scope": "machine",
74+
"default": [
75+
"/usr/bin/env",
76+
"swift-format"
77+
],
78+
"oneOf": [
79+
{
80+
"type": "string",
81+
"default": "/usr/local/bin/swift-format"
82+
},
83+
{
84+
"type": "array",
85+
"minItems": 1,
86+
"default": [
87+
"/usr/bin/env",
88+
"swift-format"
89+
],
90+
"items": {
91+
"type": "string"
92+
}
93+
}
94+
]
7695
},
7796
"apple-swift-format.configSearchPaths": {
7897
"type": "array",

src/Current.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface Current {
1717
isEnabled(): boolean;
1818
onlyEnableOnSwiftPMProjects(): boolean;
1919
onlyEnableWithConfig(): boolean;
20-
swiftFormatPath(document: vscode.TextDocument): string | null;
20+
swiftFormatPath(document: vscode.TextDocument): string[] | null;
2121
resetSwiftFormatPath(): void;
2222
configureSwiftFormatPath(): void;
2323
formatConfigSearchPaths(): string[];
@@ -89,7 +89,7 @@ export function prodEnvironment(): Current {
8989
const fullPath = join(workspace.uri.fsPath, path);
9090

9191
if (existsSync(fullPath)) {
92-
return absolutePath(fullPath);
92+
return [absolutePath(fullPath)];
9393
}
9494
}
9595
if (
@@ -117,12 +117,18 @@ export function prodEnvironment(): Current {
117117
};
118118
}
119119

120-
const fallbackGlobalSwiftFormatPath = () =>
121-
absolutePath(
122-
vscode.workspace
123-
.getConfiguration()
124-
.get("apple-swift-format.path", "/usr/local/bin/swift-format")
125-
);
126-
120+
const fallbackGlobalSwiftFormatPath = (): string[] => {
121+
const defaultPath = ["/usr/bin/env", "swift-format"];
122+
const path = vscode.workspace
123+
.getConfiguration()
124+
.get("apple-swift-format.path", defaultPath);
125+
if (typeof path === "string") {
126+
return [absolutePath(path)];
127+
} else if (Array.isArray(path) && path.length > 0) {
128+
return [absolutePath(path[0]), ...path.slice(1)];
129+
} else {
130+
return defaultPath;
131+
}
132+
};
127133
const Current = prodEnvironment();
128134
export default Current as Current;

src/SwiftFormatEditProvider.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ function format(request: {
5353
return [];
5454
}
5555
const newContents = childProcess.execFileSync(
56-
swiftFormatPath,
57-
[...userDefinedParams.options, ...(request.parameters || [])],
56+
swiftFormatPath[0],
57+
[
58+
...swiftFormatPath.slice(1),
59+
...userDefinedParams.options,
60+
...(request.parameters || []),
61+
],
5862
{
5963
encoding: "utf8",
6064
input,

0 commit comments

Comments
 (0)