diff --git a/README.md b/README.md index 27b560b..4b746d3 100755 --- a/README.md +++ b/README.md @@ -41,23 +41,24 @@ let package = Package( ## Configuration -| Config | Type | Default | Description | -| --------------------------------------- | ---------- | -------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | -| `swiftlint.enable` | `Bool` | `true` | Whether SwiftLint should actually do something. | -| `swiftlint.onlyEnableOnSwiftPMProjects` | `Bool` | `false` | Requires and uses a SwiftLint as SwiftPM dependency. | -| `swiftlint.onlyEnableWithConfig` | `Bool` | `false` | Only lint if config present. | -| `swiftlint.path` | `String` | `swiftlint` | The location of the globally installed SwiftLint (resolved with the current path if only a filename). | -| `swiftlint.additionalParameters` | `[String]` | `[]` | Additional parameters to pass to SwiftLint. | -| `swiftlint.configSearchPaths` | `[String]` | `[]` | Possible paths for SwiftLint config. _This disables [nested configurations](https://github.com/realm/SwiftLint#nested-configurations)!_ | -| `swiftlint.autoLintWorkspace` | `Bool` | `true` | Automatically lint the whole project right after start. | +| Config | Type | Default | Description | +| --------------------------------------- | ---------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------- | +| `swiftlint.enable` | `Bool` | `true` | Whether SwiftLint should actually do something. | +| `swiftlint.onlyEnableOnSwiftPMProjects` | `Bool` | `false` | Requires and uses a SwiftLint as SwiftPM dependency. | +| `swiftlint.onlyEnableWithConfig` | `Bool` | `false` | Only lint if config present. | +| `swiftlint.path` | `String` | `swiftlint` | The location of the globally installed SwiftLint (resolved with the current path if only a filename). | +| `swiftlint.additionalParameters` | `[String]` | `[]` | Additional parameters to pass to SwiftLint. | +| `swiftlint.configSearchPaths` | `[String]` | `[]` | Possible paths for SwiftLint config. _This disables [nested configurations](https://github.com/realm/SwiftLint#nested-configurations)!_ | +| `swiftlint.autoLintWorkspace` | `Bool` | `true` | Automatically lint the whole project right after start. | ## Commands -| Short Title | Command | -| ------------------------- | ------------------------- | -| SwiftLint: Lint workspace | `swiftlint.lintWorkspace` | -| SwiftLint: Fix workspace | `swiftlint.fixWorkspace` | -| SwiftLint: Fix document | `swiftlint.fixDocument` | +| Short Title | Command | +| ------------------------------- | ------------------------- | +| SwiftLint: Lint workspace | `swiftlint.lintWorkspace` | +| SwiftLint: Fix workspace | `swiftlint.fixWorkspace` | +| SwiftLint: Fix document | `swiftlint.fixDocument` | +| SwiftLint: Fix all known issues | `source.fixAll.swiftlint` | To automatically fix all issues within a document on save, add the following to your `.vscode/settings.json`: diff --git a/package.json b/package.json index 4343b45..443d112 100644 --- a/package.json +++ b/package.json @@ -120,6 +120,11 @@ "title": "SwiftLint: Fix all autocorrect issues in document", "command": "swiftlint.fixDocument", "shortTitle": "SwiftLint: Fix document" + }, + { + "title": "SwiftLint: Fix all known autocorrect issues", + "command": "source.fixAll.swiftlint", + "shortTitle": "SwiftLint: Fix all known issues" } ] }, @@ -139,4 +144,4 @@ "glob": "^10.0.0", "yaml": "^2.3.2" } -} +} \ No newline at end of file diff --git a/src/Current.ts b/src/Current.ts index 62e4933..077bf5d 100644 --- a/src/Current.ts +++ b/src/Current.ts @@ -20,6 +20,7 @@ export interface Current { lintWorkspace: string; fixWorkspace: string; fixDocument: string; + fixAll: string; }; config: { isEnabled(): boolean; @@ -80,6 +81,7 @@ export function prodEnvironment(): Current { lintWorkspace: "swiftlint.lintWorkspace", fixWorkspace: "swiftlint.fixWorkspace", fixDocument: "swiftlint.fixDocument", + fixAll: "source.fixAll.swiftlint", }, config: { affectsConfiguration: (changeEvent: vscode.ConfigurationChangeEvent) => diff --git a/src/SwiftLintProvider.ts b/src/SwiftLintProvider.ts index 6751177..0da847e 100644 --- a/src/SwiftLintProvider.ts +++ b/src/SwiftLintProvider.ts @@ -65,32 +65,11 @@ export class SwiftLint { this.fixWorkspace().then(() => this.lintWorkspace()); }); commands.registerCommand(Current.commands.fixDocument, (...args) => - setTimeout(() => performfixDocument(...args), 1) + setTimeout(() => this.performFixDocument(...args), 1) + ); + commands.registerCommand(Current.commands.fixAll, () => + setTimeout(() => this.fixAllKnownDiagnostics(), 1) ); - const performfixDocument = async (...args: any[]) => { - let docs: TextDocument[]; - if (args.length === 0 && window.activeTextEditor) { - docs = [window.activeTextEditor.document]; - } else { - docs = []; - for (let arg of args) { - const textDocument = await workspace.openTextDocument(arg); - docs.push(textDocument); - } - } - for (const doc of docs) { - if (doc.isDirty) { - await doc.save(); - } - await this.fixDocument(doc); - setTimeout(() => { - const updatedDoc = workspace.textDocuments.find( - (textDoc) => textDoc.uri === doc.uri - ); - this.lintDocument(updatedDoc ?? doc); - }, 100); - } - }; workspace.onDidChangeConfiguration((configChange) => { if (Current.config.affectsConfiguration(configChange)) { @@ -125,6 +104,32 @@ export class SwiftLint { } } + private async performFixDocument(...args: any[]) { + let docs: TextDocument[]; + if (args.length === 0 && window.activeTextEditor) { + docs = [window.activeTextEditor.document]; + } else { + docs = []; + for (let arg of args) { + const textDocument = await workspace.openTextDocument(arg); + docs.push(textDocument); + } + } + const updates = docs.map(async (doc) => { + if (doc.isDirty) { + await doc.save(); + } + await this.fixDocument(doc); + setTimeout(() => { + const updatedDoc = workspace.textDocuments.find( + (textDoc) => textDoc.uri === doc.uri + ); + this.lintDocument(updatedDoc ?? doc); + }, 100); + }); + return Promise.all(updates); + } + public async fixDocument(document: TextDocument) { if (document.languageId !== "swift" || document.uri.scheme === "git") { return; @@ -213,4 +218,15 @@ export class SwiftLint { await Promise.all(lintWorkspaces); } + + public async fixAllKnownDiagnostics() { + const docs = new Set(); + this.diagnosticCollection.forEach((uri) => { + if (docs.has(uri)) { + return; + } + docs.add(uri); + }); + return this.performFixDocument(...docs.values()); + } } diff --git a/src/lint.ts b/src/lint.ts index ee9fe91..6cb47fe 100644 --- a/src/lint.ts +++ b/src/lint.ts @@ -53,7 +53,10 @@ export async function diagnosticsForDocument(request: { return []; } - if (!request.document.uri.fsPath || request.document.uri.fsPath.includes(".swiftinterface")) { + if ( + !request.document.uri.fsPath || + request.document.uri.fsPath.includes(".swiftinterface") + ) { return []; } @@ -180,7 +183,7 @@ export async function diagnosticsForFolder(request: { ? [] : await filterAsync(allFiles, (path) => config.includes(path)) : request.parameters || []; - if (includedFiles.length === 0) { + if (includedFiles.length === 0 && config != null) { return new Map(); }