Skip to content

Commit

Permalink
feat: source.fixAll.swiftlint #81 (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
vknabel authored Nov 21, 2024
1 parent 0a6db73 commit 0b492ce
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ let package = Package(

## 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
5 changes: 5 additions & 0 deletions 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 Down
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 0b492ce

Please sign in to comment.