Skip to content

Commit

Permalink
added swiftformat.onlyEnableWithConfig #20
Browse files Browse the repository at this point in the history
  • Loading branch information
vknabel committed Apr 23, 2022
1 parent a290cb0 commit e11e039
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
1 change: 0 additions & 1 deletion .nvmrc

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.4.0

- Added: `swiftformat.onlyEnableWithConfig` to only enable SwiftFormat with a config #20
- Fixed: `swiftformat.onlyEnableOnSwiftPMProjects` didn't work correctly

# 1.3.10

- Fixed: `vsce publish` didn't rebuild the extension #19
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ let package = Package(

## Configuration

| Config | Type | Default | Description |
| ------------------------------- | ---------- | ---------------------------- | --------------------------------------------------- |
| `swiftformat.enable` | `Bool` | `true` | Whether SwiftFormat should actually do something. |
| `swiftformat.path` | `String` | `/usr/local/bin/swiftformat` | The location of the globally installed SwiftFormat. |
| `swiftformat.options` | `[String]` | `[]` | Additional parameters for SwiftFormat. |
| `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` | `/usr/local/bin/swiftformat` | The location of the globally installed SwiftFormat. |
| `swiftformat.options` | `[String]` | `[]` | Additional parameters for SwiftFormat. |
| `swiftformat.configSearchPaths` | `[String]` | `[".swiftformat"]` | Possible paths for SwiftFormat config. |

## Contributors

Expand Down
7 changes: 6 additions & 1 deletion 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.3.10",
"version": "1.4.0",
"license": "MIT",
"author": {
"name": "Valentin Knabel",
Expand Down Expand Up @@ -63,6 +63,11 @@
"default": false,
"description": "Only allows the extension to load up when SwiftFormat is available via Swift PM."
},
"swiftformat.onlyEnableWithConfig": {
"type": "boolean",
"default": false,
"description": "Only use SwiftFormat when a config exists."
},
"swiftformat.path": {
"type": "string",
"default": "/usr/local/bin/swiftformat",
Expand Down
21 changes: 19 additions & 2 deletions src/Current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export interface Current {
};
config: {
isEnabled(): boolean;

swiftFormatPath(document: vscode.TextDocument): string;
onlyEnableOnSwiftPMProjects(): boolean;
onlyEnableWithConfig(): boolean;
swiftFormatPath(document: vscode.TextDocument): string | null;
resetSwiftFormatPath(): void;
configureSwiftFormatPath(): void;
formatOptions(): string[];
Expand Down Expand Up @@ -62,6 +63,15 @@ export function prodEnvironment(): Current {
config: {
isEnabled: () =>
vscode.workspace.getConfiguration().get("swiftformat.enable", true),
onlyEnableOnSwiftPMProjects: () =>
vscode.workspace
.getConfiguration()
.get("swiftformat.onlyEnableOnSwiftPMProjects", false),
onlyEnableWithConfig: () =>
vscode.workspace
.getConfiguration()
.get("swiftformat.onlyEnableWithConfig", false),

swiftFormatPath: (document: vscode.TextDocument) => {
// Support running from Swift PM projects
const possibleLocalPaths = [
Expand All @@ -80,6 +90,13 @@ export function prodEnvironment(): Current {
return absolutePath(fullPath);
}
}
if (
vscode.workspace
.getConfiguration()
.get("swiftformat.onlyEnableOnSwiftPMProjects", false)
) {
return null;
}
// Fall back to global defaults found in settings
return fallbackGlobalSwiftFormatPath();
},
Expand Down
28 changes: 19 additions & 9 deletions src/SwiftFormatEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const wholeDocumentRange = new vscode.Range(

function userDefinedFormatOptionsForDocument(
document: vscode.TextDocument
): string[] {
): { options: string[]; hasConfig: boolean } {
const formatOptions = Current.config.formatOptions();
if (formatOptions.indexOf("--config") != -1) return formatOptions;
if (formatOptions.indexOf("--config") != -1)
return { options: formatOptions, hasConfig: true };
const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri);
const rootPath =
(workspaceFolder && workspaceFolder.uri.fsPath) ||
Expand All @@ -26,25 +27,34 @@ function userDefinedFormatOptionsForDocument(
.formatConfigSearchPaths()
.map(current => resolve(rootPath, current));
const existingConfig = searchPaths.find(existsSync);
return existingConfig != null
? ["--config", existingConfig, ...formatOptions]
: formatOptions;
const options =
existingConfig != null
? ["--config", existingConfig, ...formatOptions]
: formatOptions;
return { options, hasConfig: existingConfig != null };
}

function format(request: {
document: vscode.TextDocument;
parameters?: string[];
range?: vscode.Range;
formatting: vscode.FormattingOptions;
}) {
}): vscode.TextEdit[] {
try {
const swiftFormatPath = Current.config.swiftFormatPath(request.document);
if (swiftFormatPath == null) {
return [];
}
const input = request.document.getText(request.range);
if (input.trim() === "") return [];
const userDefinedParams = userDefinedFormatOptionsForDocument(
request.document
);
if (!userDefinedParams.hasConfig && Current.config.onlyEnableWithConfig()) {
return [];
}
const formattingParameters =
userDefinedParams.indexOf("--indent") !== -1
userDefinedParams.options.indexOf("--indent") !== -1
? []
: [
"--indent",
Expand All @@ -53,12 +63,12 @@ function format(request: {
: "tabs"
];
const newContents = childProcess.execFileSync(
Current.config.swiftFormatPath(request.document),
swiftFormatPath,
[
"stdin",
"--stdinpath",
request.document.fileName,
...userDefinedParams,
...userDefinedParams.options,
...(request.parameters || []),
...formattingParameters
],
Expand Down

0 comments on commit e11e039

Please sign in to comment.