Skip to content

Commit 29a8ca3

Browse files
committed
Open migration file
1 parent d0e7f69 commit 29a8ca3

File tree

5 files changed

+76
-7
lines changed

5 files changed

+76
-7
lines changed

src/actions/IAction.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export interface IAction {
2-
run(): Promise<string>;
2+
run(): Promise<string | void>;
33
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as vscode from 'vscode';
2+
3+
import type { Migration } from '../types/Migration';
4+
import type { IAction } from './IAction';
5+
6+
export class OpenMigrationFileAction implements IAction {
7+
constructor(
8+
private readonly workspaceRoot: string,
9+
private readonly migration: Migration,
10+
) {}
11+
12+
public async run() {
13+
// The ef cli does not give us any information about migration file paths,
14+
// which is why we have to find it ourselves.
15+
const [migrationFile] = await vscode.workspace.findFiles(
16+
new vscode.RelativePattern(
17+
this.workspaceRoot,
18+
`**/${this.migration.id}.cs`,
19+
),
20+
undefined,
21+
1,
22+
);
23+
if (migrationFile) {
24+
await vscode.commands.executeCommand('vscode.open', migrationFile);
25+
}
26+
}
27+
}

src/commands/CommandProvider.ts

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Disposable } from '../util/Disposable';
88
import { AddMigrationCommand } from './AddMigrationCommand';
99
import type { Command } from './Command';
1010
import { GenerateScriptCommand } from './GenerateScriptCommand';
11+
import { OpenMigrationFileCommand } from './OpenMigrationFileCommand';
1112
import { RefreshTreeCommand } from './RefreshTreeCommand';
1213
import { RemoveMigrationCommand } from './RemoveMigrationCommand';
1314
import { RunMigrationCommand } from './RunMigrationCommand';
@@ -50,6 +51,10 @@ export class CommandProvider extends Disposable {
5051
(clearCache: boolean) =>
5152
new RefreshTreeCommand(treeDataProvider, clearCache),
5253
);
54+
this.registerCommand(
55+
OpenMigrationFileCommand.commandName,
56+
(item?: MigrationTreeItem) => new OpenMigrationFileCommand(item),
57+
);
5358
}
5459

5560
public static getCommandName(name: string) {
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { OpenMigrationFileAction } from '../actions/OpenMigrationFileAction';
2+
import type { MigrationTreeItem } from '../treeView/MigrationTreeItem';
3+
import { Command } from './Command';
4+
5+
export class OpenMigrationFileCommand extends Command {
6+
public static commandName = 'openMigrationFile';
7+
8+
constructor(private readonly item?: MigrationTreeItem) {
9+
super();
10+
}
11+
12+
public async run() {
13+
if (!this.item) {
14+
return;
15+
}
16+
return new OpenMigrationFileAction(
17+
this.item.workspaceRoot,
18+
this.item.migration,
19+
).run();
20+
}
21+
}

src/treeView/TreeDataProvider.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { EXTENSION_NAMESPACE } from '../constants/constants';
55
import type { ProjectFile } from '../types/ProjectFile';
66
import { ProjectTreeItem } from './ProjectTreeItem';
77
import type { CLI } from '../cli/CLI';
8+
import { MigrationTreeItem } from './MigrationTreeItem';
9+
import { CommandProvider } from '../commands/CommandProvider';
10+
import { OpenMigrationFileCommand } from '../commands/OpenMigrationFileCommand';
811

912
export class TreeDataProvider
1013
extends Disposable
@@ -23,12 +26,25 @@ export class TreeDataProvider
2326
private readonly cli: CLI,
2427
) {
2528
super();
26-
this.subscriptions.push(
27-
vscode.window.registerTreeDataProvider(
28-
`${EXTENSION_NAMESPACE}Tree`,
29-
this,
30-
),
31-
);
29+
const view = vscode.window.createTreeView(`${EXTENSION_NAMESPACE}Tree`, {
30+
treeDataProvider: this,
31+
});
32+
view.onDidChangeSelection(this.handleTreeItemSelection.bind(this));
33+
this.subscriptions.push(view);
34+
}
35+
36+
private async handleTreeItemSelection(
37+
e: vscode.TreeViewSelectionChangeEvent<vscode.TreeItem>,
38+
) {
39+
if (
40+
e.selection.length === 1 &&
41+
e.selection[0] instanceof MigrationTreeItem
42+
) {
43+
await vscode.commands.executeCommand(
44+
CommandProvider.getCommandName(OpenMigrationFileCommand.commandName),
45+
e.selection[0],
46+
);
47+
}
3248
}
3349

3450
public refresh(): void {

0 commit comments

Comments
 (0)