Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: source.fixAll.swiftlint #95

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this fix, I linting could fail.

return new Map();
}

Expand Down