Skip to content

Commit

Permalink
Rerender.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Dec 31, 2023
1 parent af82eff commit 82f909a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
4 changes: 4 additions & 0 deletions packages/vscode-extension/DEV.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions packages/vscode-extension/src/graphVisualizerView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class GraphVisualizerView {
localResourceRoots: [context.extensionUri],
},
);

workspace.onDidChangeWorkspace(() => {
void this.renderPanel();
});
}

renderHtml(content: string) {
Expand Down
4 changes: 4 additions & 0 deletions packages/vscode-extension/src/lastRunView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> | void {
Expand Down
17 changes: 0 additions & 17 deletions packages/vscode-extension/src/moon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,6 @@ export function findMoonBin(workspaceRoot: string): string | null {
return null;
}

export async function findWorkspaceRoot(
workingDir: vscode.WorkspaceFolder,
): Promise<string | null> {
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);

Expand Down
6 changes: 5 additions & 1 deletion packages/vscode-extension/src/projectsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ export class ProjectsProvider implements vscode.TreeDataProvider<TreeItem> {
watcher1,
watcher2,
);

workspace.onDidChangeWorkspace(() => {
this.refresh();
});
}

getParent(element: TreeItem): vscode.ProviderResult<TreeItem> {
Expand All @@ -232,7 +236,7 @@ export class ProjectsProvider implements vscode.TreeDataProvider<TreeItem> {
}

async getChildren(element?: TreeItem | undefined): Promise<TreeItem[]> {
if (element instanceof TaskItem) {
if (element instanceof TaskItem || !this.workspace.root) {
return [];
}

Expand Down
43 changes: 28 additions & 15 deletions packages/vscode-extension/src/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import execa from 'execa';
import vscode from 'vscode';
import { findMoonBin, isRealBin } from './moon';
Expand All @@ -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;
}

Expand All @@ -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');
Expand Down

0 comments on commit 82f909a

Please sign in to comment.