diff --git a/packages/vscode-extension/DEV.md b/packages/vscode-extension/DEV.md new file mode 100644 index 0000000..a7db151 --- /dev/null +++ b/packages/vscode-extension/DEV.md @@ -0,0 +1,4 @@ +# Development commands + +- https://code.visualstudio.com/api/get-started/your-first-extension#developing-the-extension +- `F5` to start debugging the extension diff --git a/packages/vscode-extension/src/graphVisualizerView.ts b/packages/vscode-extension/src/graphVisualizerView.ts index 4e68537..92d5dca 100644 --- a/packages/vscode-extension/src/graphVisualizerView.ts +++ b/packages/vscode-extension/src/graphVisualizerView.ts @@ -26,6 +26,10 @@ export class GraphVisualizerView { localResourceRoots: [context.extensionUri], }, ); + + workspace.onDidChangeWorkspace(() => { + void this.renderPanel(); + }); } renderHtml(content: string) { diff --git a/packages/vscode-extension/src/lastRunView.ts b/packages/vscode-extension/src/lastRunView.ts index ae2ee67..ab6a01e 100644 --- a/packages/vscode-extension/src/lastRunView.ts +++ b/packages/vscode-extension/src/lastRunView.ts @@ -26,6 +26,10 @@ export class LastRunProvider implements vscode.WebviewViewProvider { watcher.onDidDelete(this.renderView, this); context.subscriptions.push(watcher); + + workspace.onDidChangeWorkspace(() => { + this.renderView(); + }); } resolveWebviewView(webviewView: vscode.WebviewView): Thenable | void { diff --git a/packages/vscode-extension/src/moon.ts b/packages/vscode-extension/src/moon.ts index dd37faf..fcf9329 100644 --- a/packages/vscode-extension/src/moon.ts +++ b/packages/vscode-extension/src/moon.ts @@ -31,23 +31,6 @@ export function findMoonBin(workspaceRoot: string): string | null { return null; } -export async function findWorkspaceRoot( - workingDir: vscode.WorkspaceFolder, -): Promise { - const baseRoot = vscode.workspace.getConfiguration('moon').get('workspaceRoot', '.'); - - const files = await vscode.workspace.findFiles( - new vscode.RelativePattern(workingDir, path.join(baseRoot, '.moon/*.yml')), - ); - - // Return folder containing the `.moon` folder - if (files.length > 0 && files[0].scheme === 'file') { - return path.dirname(path.dirname(files[0].fsPath)); - } - - return null; -} - export function isRealBin(binPath: string): boolean { const stats = fs.statSync(binPath); diff --git a/packages/vscode-extension/src/projectsView.ts b/packages/vscode-extension/src/projectsView.ts index f96d612..83ea979 100644 --- a/packages/vscode-extension/src/projectsView.ts +++ b/packages/vscode-extension/src/projectsView.ts @@ -221,6 +221,10 @@ export class ProjectsProvider implements vscode.TreeDataProvider { watcher1, watcher2, ); + + workspace.onDidChangeWorkspace(() => { + this.refresh(); + }); } getParent(element: TreeItem): vscode.ProviderResult { @@ -232,7 +236,7 @@ export class ProjectsProvider implements vscode.TreeDataProvider { } async getChildren(element?: TreeItem | undefined): Promise { - if (element instanceof TaskItem) { + if (element instanceof TaskItem || !this.workspace.root) { return []; } diff --git a/packages/vscode-extension/src/workspace.ts b/packages/vscode-extension/src/workspace.ts index 9b6d8d1..9115b6e 100644 --- a/packages/vscode-extension/src/workspace.ts +++ b/packages/vscode-extension/src/workspace.ts @@ -1,3 +1,4 @@ +import path from 'path'; import execa from 'execa'; import vscode from 'vscode'; import { findMoonBin, isRealBin } from './moon'; @@ -15,23 +16,32 @@ export class Workspace { // Current moon workspace root root: string | null = null; + private listeners: (() => void)[] = []; + constructor() { this.logger = vscode.window.createOutputChannel('moon', { log: true }); - // When a file is opened, attempt to find the moon workspace - vscode.workspace.onDidOpenTextDocument((text) => { - this.logger.appendLine('Opened a file, checking for workspace changes'); - void this.findRoot(text.uri); - }); + // Find moon workspace from default editor + if (vscode.window.activeTextEditor) { + void this.findRoot(vscode.window.activeTextEditor.document.uri); + } - vscode.workspace.onDidCloseTextDocument((text) => { - this.logger.appendLine('Closed a file, checking for workspace changes'); - void this.findRoot(text.uri); + // When an editor is changed, attempt to find the moon workspace + vscode.window.onDidChangeActiveTextEditor((editor) => { + if (editor) { + this.logger.appendLine('Opened a file, checking for workspace changes'); + void this.findRoot(editor.document.uri); + } }); } + onDidChangeWorkspace(listener: () => void) { + this.listeners.push(listener); + } + async findRoot(openUri: vscode.Uri) { if (this.root && openUri.fsPath.startsWith(this.root)) { + this.logger.appendLine('Already in a workspace, skipping'); return; } @@ -44,27 +54,30 @@ export class Workspace { const workspaceFolder = vscode.workspace.getWorkspaceFolder(openUri); if (workspaceFolder) { - this.logger.appendLine( - `Found workspace folder ${workspaceFolder.uri.fsPath} (${workspaceFolder.name})`, - ); + this.folder = workspaceFolder; + this.logger.appendLine(`Found workspace folder ${workspaceFolder.uri.fsPath}`); this.logger.appendLine('Attempting to find a moon installation'); + const rootPrefix = vscode.workspace.getConfiguration('moon').get('workspaceRoot', '.'); const files = await vscode.workspace.findFiles( - new vscode.RelativePattern(workspaceFolder.uri, '.moon/*.yml'), + new vscode.RelativePattern(workspaceFolder.uri, path.join(rootPrefix, '.moon/*.yml')), ); - this.folder = workspaceFolder; - if (files.length > 0) { this.root = workspaceFolder.uri.fsPath; this.binPath = findMoonBin(this.root); - this.logger.appendLine(`Found workspace root at ${this.root}`); + this.logger.appendLine(`Found moon workspace root at ${this.root}`); if (this.binPath) { this.logger.appendLine(`Found moon binary at ${this.binPath}`); } + + // Trigger re-renders + this.listeners.forEach((listener) => void listener()); + } else { + this.logger.appendLine('Did not find a moon installation, disabling'); } } else { this.logger.appendLine('Did not find a workspace folder, disabling moon');