Skip to content

Commit

Permalink
VS Code: Add task provider.
Browse files Browse the repository at this point in the history
Part of #64.
  • Loading branch information
alexrp committed Apr 17, 2023
1 parent b5ac650 commit 4a3ccad
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@
{
"title": "Celerity",
"properties": {
"celerity.autoDetectTasks": {
"title": "Auto-Detect Tasks",
"scope": "machine",
"type": "boolean",
"default": true,
"markdownDescription": "Controls whether `celerity.json` tasks are automatically detected."
},
"celerity.autoStartServer": {
"title": "Auto-Start Language Server",
"scope": "machine",
Expand Down Expand Up @@ -201,6 +208,11 @@
"language": "celerity",
"path": "snippets/celerity.json"
}
],
"taskDefinitions": [
{
"type": "celerity"
}
]
},
"devDependencies": {
Expand Down
69 changes: 67 additions & 2 deletions src/extensions/vscode/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import {
type CancellationToken,
Disposable,
type ExtensionContext,
MarkdownString,
ShellExecution,
StatusBarAlignment,
Task,
TaskGroup,
type TaskProvider,
type Uri,
commands,
tasks,
window,
workspace,
} from "vscode";
Expand All @@ -20,6 +27,8 @@ export async function activate(context : ExtensionContext) : Promise<void> {

let client : LanguageClient | null = null;

pushDisposable(new Disposable(async () => client?.dispose()));

function setStatus(icon : string, tooltip ?: string | undefined) : void {
status.text = `$(${icon}) Celerity`;

Expand Down Expand Up @@ -98,12 +107,68 @@ export async function activate(context : ExtensionContext) : Promise<void> {
await commands.executeCommand("celerity.startServer");
});

status.show();
if (cfg.get<boolean>("autoDetectTasks", true))
pushDisposable(tasks.registerTaskProvider("celerity", new CelerityTaskProvider()));

pushDisposable(new Disposable(async () => client?.dispose()));
status.show();

if (cfg.get<boolean>("autoStartServer", true))
await commands.executeCommand("celerity.startServer");
else
setStatus("circle-slash", "Language server not started due to `celerity.autoStartServer` setting.");
}

class CelerityTaskProvider implements TaskProvider {
private readonly _tasks : Map<string, Task[]> = new Map<string, Task[]>();

public constructor() {
const glob = "**/celerity.json";

void workspace.findFiles(glob).then(uris => {
for (const uri of uris)
this.addTasks(uri);
});

const watcher = workspace.createFileSystemWatcher(glob);

watcher.onDidCreate(uri => this.addTasks(uri));
watcher.onDidDelete(uri => this._tasks.delete(uri.fsPath));
}

public provideTasks(_token : CancellationToken) : Task[] {
return [... this._tasks.values()].flat();
}

// eslint-disable-next-line class-methods-use-this
public resolveTask(_task : Task, _token : CancellationToken) : undefined {
return undefined;
}

private addTasks(uri : Uri) : void {
const folder = workspace.getWorkspaceFolder(uri);

if (folder !== undefined)
this._tasks.set(
uri.fsPath,
["Check", "Format", "Test"].map(kind => {
const task = new Task(
{ type : "celerity" },
folder,
`${kind} ${workspace.asRelativePath(uri)}`,
"Celerity",
new ShellExecution(`celerity ${kind.toLowerCase()}`, kind === "Format" ? ["-f"] : []),
"$celerity");

switch (kind) {
case "Check":
task.group = TaskGroup.Build;
break;
case "Test":
task.group = TaskGroup.Test;
break;
}

return task;
}));
}
}

0 comments on commit 4a3ccad

Please sign in to comment.