From e11e039860cbc834fc0bad7dc73540e6944e24fb Mon Sep 17 00:00:00 2001 From: Valentin Knabel Date: Sat, 23 Apr 2022 11:58:33 +0200 Subject: [PATCH] added swiftformat.onlyEnableWithConfig #20 --- .nvmrc | 1 - CHANGELOG.md | 5 +++++ README.md | 14 ++++++++------ package.json | 7 ++++++- src/Current.ts | 21 +++++++++++++++++++-- src/SwiftFormatEditProvider.ts | 28 +++++++++++++++++++--------- 6 files changed, 57 insertions(+), 19 deletions(-) delete mode 100644 .nvmrc diff --git a/.nvmrc b/.nvmrc deleted file mode 100644 index 2f9417a..0000000 --- a/.nvmrc +++ /dev/null @@ -1 +0,0 @@ -8.11.4 diff --git a/CHANGELOG.md b/CHANGELOG.md index 24742c9..df641c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 3e5391e..e67ff53 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 148ae5a..fdaf9c5 100755 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/Current.ts b/src/Current.ts index 161bbeb..df14185 100644 --- a/src/Current.ts +++ b/src/Current.ts @@ -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[]; @@ -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 = [ @@ -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(); }, diff --git a/src/SwiftFormatEditProvider.ts b/src/SwiftFormatEditProvider.ts index ddad39c..6a3a6a8 100644 --- a/src/SwiftFormatEditProvider.ts +++ b/src/SwiftFormatEditProvider.ts @@ -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) || @@ -26,9 +27,11 @@ 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: { @@ -36,15 +39,22 @@ function format(request: { 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", @@ -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 ],