Skip to content

Commit

Permalink
feat: source.fixAll.swiftlint #81
Browse files Browse the repository at this point in the history
  • Loading branch information
vknabel committed Jun 22, 2024
1 parent dc1a963 commit dbcc1db
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 42 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
},
Expand All @@ -139,4 +144,4 @@
"glob": "^10.0.0",
"yaml": "^2.3.2"
}
}
}
2 changes: 2 additions & 0 deletions src/Current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface Current {
lintWorkspace: string;
fixWorkspace: string;
fixDocument: string;
fixAll: string;
};
config: {
isEnabled(): boolean;
Expand Down Expand Up @@ -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) =>
Expand Down
66 changes: 41 additions & 25 deletions src/SwiftLintProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -213,4 +218,15 @@ export class SwiftLint {

await Promise.all(lintWorkspaces);
}

public async fixAllKnownDiagnostics() {
const docs = new Set<Uri>();
this.diagnosticCollection.forEach((uri) => {
if (docs.has(uri)) {
return;
}
docs.add(uri);
});
return this.performFixDocument(...docs.values());
}
}
7 changes: 5 additions & 2 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}

Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit dbcc1db

Please sign in to comment.