Skip to content

Commit

Permalink
feat: smarter path default for #17
Browse files Browse the repository at this point in the history
  • Loading branch information
vknabel committed Dec 12, 2022
1 parent 27dfee3 commit ecb2c4d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 23 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
24 changes: 15 additions & 9 deletions src/Current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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;
8 changes: 6 additions & 2 deletions src/SwiftFormatEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ecb2c4d

Please sign in to comment.