Skip to content

Commit

Permalink
feat: smarter path default for vknabel/vscode-apple-swift-format#17
Browse files Browse the repository at this point in the history
  • Loading branch information
vknabel committed Dec 12, 2022
1 parent 4812ab7 commit 8e38f76
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased
# 1.6.0

- docs: clarified `swiftformat.options` #17
- Added: `swiftformat.path` can now be an array of strings and defaults to `[/usr/bin/env, swiftformat]` #17

# 1.5.1

Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ let package = Package(

## Configuration

| Config | Type | Default | Description |
| ----------------------------------------- | ---------- | ---------------------------- | ------------------------------------------------------------------------------------------ |
| `swiftformat.enable` | `Bool` | `true` | Whether SwiftFormat should actually do something. |
| `swiftformat.onlyEnableOnSwiftPMProjects` | `Bool` | `false` | Requires and uses a SwiftFormat as SwiftPM dependency. |
| `swiftformat.onlyEnableWithConfig` | `Bool` | `false` | Only format if config present. |
| `swiftformat.path` | `String` | `/usr/local/bin/swiftformat` | The location of the globally installed SwiftFormat. |
| `swiftformat.options` | `[String]` | `[]` | Additional [options for SwiftFormat](https://github.com/nicklockwood/SwiftFormat#options). |
| `swiftformat.configSearchPaths` | `[String]` | `[".swiftformat"]` | Possible paths for SwiftFormat config. |
| Config | Type | Default | Description |
| ----------------------------------------- | ------------------- | ----------------------------- | ------------------------------------------------------------------------------------------ |
| `swiftformat.enable` | `Bool` | `true` | Whether SwiftFormat should actually do something. |
| `swiftformat.onlyEnableOnSwiftPMProjects` | `Bool` | `false` | Requires and uses a SwiftFormat as SwiftPM dependency. |
| `swiftformat.onlyEnableWithConfig` | `Bool` | `false` | Only format if config present. |
| `swiftformat.path` | `[String] | String` | `[/usr/bin/env, swiftformat]` | The location of the globally installed SwiftFormat. |
| `swiftformat.options` | `[String]` | `[]` | Additional [options for SwiftFormat](https://github.com/nicklockwood/SwiftFormat#options). |
| `swiftformat.configSearchPaths` | `[String]` | `[".swiftformat"]` | Possible paths for SwiftFormat 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-swiftformat"
},
"version": "1.5.1",
"version": "1.6.0",
"license": "MIT",
"author": {
"name": "Valentin Knabel",
Expand Down Expand Up @@ -69,10 +69,29 @@
"description": "Only use SwiftFormat when a config exists."
},
"swiftformat.path": {
"type": "string",
"default": "/usr/local/bin/swiftformat",
"description": "The location of your globally installed SwiftFormat.",
"scope": "machine"
"scope": "machine",
"default": [
"/usr/bin/env",
"swiftformat"
],
"oneOf": [
{
"type": "string",
"default": "/usr/local/bin/swiftformat"
},
{
"type": "array",
"minItems": 1,
"default": [
"/usr/bin/env",
"swiftformat"
],
"items": {
"type": "string"
}
}
]
},
"swiftformat.options": {
"type": "array",
Expand Down
23 changes: 15 additions & 8 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;
formatOptions(): string[];
Expand Down Expand Up @@ -87,7 +87,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,19 @@ export function prodEnvironment(): Current {
};
}

const fallbackGlobalSwiftFormatPath = () =>
absolutePath(
vscode.workspace
.getConfiguration()
.get("swiftformat.path", "/usr/local/bin/swiftformat")
);
const fallbackGlobalSwiftFormatPath = (): string[] => {
const defaultPath = ["/usr/bin/env", "swiftformat"];
const path = vscode.workspace
.getConfiguration()
.get("swiftformat.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;
3 changes: 2 additions & 1 deletion src/SwiftFormatEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ function format(request: {
: "tabs"
];
const newContents = childProcess.execFileSync(
swiftFormatPath,
swiftFormatPath[0],
[
...swiftFormatPath.slice(1),
"stdin",
"--stdinpath",
request.document.fileName,
Expand Down

0 comments on commit 8e38f76

Please sign in to comment.