Skip to content

Commit

Permalink
spawn uses workspace cwd (needed to find config)
Browse files Browse the repository at this point in the history
saving a `.ameba.yml` in a workspace will clear all diagnostics from all documents and re-run ameba on them

The latter allows for a very fluid experience of editing the config and seeing the errors in real time
  • Loading branch information
nobodywasishere committed Nov 17, 2024
1 parent c6a137b commit 5a2e88e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
17 changes: 11 additions & 6 deletions src/ameba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Ameba {
let stdoutArr: string[] = [];
let stderrArr: string[] = [];

const proc = spawn(args[0], args.slice(1));
const proc = spawn(args[0], args.slice(1), { cwd: space.uri.fsPath });

if (virtual) {
const documentText: string = document.getText();
Expand Down Expand Up @@ -174,11 +174,16 @@ export class Ameba {
}
}

public clear(document: TextDocument): void {
let uri = document.uri;
if (uri.scheme === 'file') {
this.taskQueue.cancel(uri);
this.diag.delete(uri);
public clear(document: TextDocument | null = null): void {
if (document) {
let uri = document.uri;
if (uri.scheme === 'file') {
this.taskQueue.cancel(uri);
this.diag.delete(uri);
}
} else {
this.taskQueue.clear();
this.diag.clear();
}
}
}
28 changes: 19 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ export function activate(context: ExtensionContext) {
context.subscriptions.push(
commands.registerCommand('crystal.ameba.restart', () => {
if (ameba) {
outputChannel.appendLine('[Restart] Clearing diagnostics')
const editor = window.activeTextEditor;
if (editor) ameba.clear(editor.document);
if (editor) {
outputChannel.appendLine(`[Restart] Clearing diagnostics for ${getRelativePath(editor.document)}`)
ameba.clear(editor.document);
}
} else {
outputChannel.appendLine('[Restart] Restarting ameba')
outputChannel.appendLine('[Restart] Starting ameba')
ameba = new Ameba(diag);
}
})
Expand All @@ -62,12 +64,7 @@ export function activate(context: ExtensionContext) {
ameba.config = getConfig();
});

workspace.textDocuments.forEach(doc => {
if (ameba && checkValidDocument(doc)) {
outputChannel.appendLine(`[Init] Running ameba on ${getRelativePath(doc)}`)
ameba.execute(doc);
}
});
executeAmebaOnWorkspace(ameba);

workspace.onDidOpenTextDocument(doc => {
if (ameba && checkValidDocument(doc)) {
Expand All @@ -88,6 +85,10 @@ export function activate(context: ExtensionContext) {
if (ameba && ameba.config.onSave && !ameba.config.onType && checkValidDocument(doc)) {
outputChannel.appendLine(`[Save] Running ameba on ${getRelativePath(doc)}`)
ameba.execute(doc);
} else if (ameba && path.basename(doc.fileName) == ".ameba.yml") {
outputChannel.appendLine(`[Config] Clearing all diagnostics after config file change`)
ameba.clear();
executeAmebaOnWorkspace(ameba);
}
});

Expand All @@ -99,6 +100,15 @@ export function activate(context: ExtensionContext) {
});
}

function executeAmebaOnWorkspace(ameba: Ameba | null) {
workspace.textDocuments.forEach(doc => {
if (ameba && checkValidDocument(doc)) {
outputChannel.appendLine(`[Init] Running ameba on ${getRelativePath(doc)}`);
ameba.execute(doc);
}
});
}

export function deactivate() { }

function checkValidDocument(document: TextDocument): boolean {
Expand Down
6 changes: 6 additions & 0 deletions src/taskQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export class TaskQueue {
});
}

public clear(): void {
this.tasks.forEach(task => {
task.cancel();
})
}

private async kick(): Promise<void> {
if (this.busy) return;
this.busy = true;
Expand Down

0 comments on commit 5a2e88e

Please sign in to comment.