A simpler way to view django projects
django dir finder doing the opposite of whati want but now i understand emitters
4 files changed, +59 -2
+3-0src/context.ts
| @@ -2,6 +2,7 @@ import * as vscode from "vscode"; | ||
| 2 | 2 | import * as path from "path"; |
| 3 | 3 | import * as fs from "fs"; |
| 4 | 4 | import DjangoProject from "./project"; |
| 5 | +import { onDjangoProject } from "./listeners"; | |
| 5 | 6 | |
| 6 | 7 | export const setDjangoContext = async () => { |
| 7 | 8 | const workspaceFolders = vscode.workspace.workspaceFolders; |
| @@ -36,6 +37,8 @@ export const setDjangoContext = async () => { | ||
| 36 | 37 | "django-overview.project", |
| 37 | 38 | project |
| 38 | 39 | ); |
| 40 | + onDjangoProject.fire(project); | |
| 41 | + console.log("Django context set"); | |
| 39 | 42 | } |
| 40 | 43 | }; |
| 41 | 44 |
+4-2src/extension.ts
| @@ -3,11 +3,13 @@ import ProjectDataProvider from "./providers/project"; | ||
| 3 | 3 | import providers from "./providers"; |
| 4 | 4 | import commands from "./commands"; |
| 5 | 5 | import { setDjangoContext } from "./context"; |
| 6 | +import listeners from "./listeners"; | |
| 6 | 7 | |
| 7 | -export function activate(context: vscode.ExtensionContext) { | |
| 8 | - setDjangoContext(); | |
| 8 | +export async function activate(context: vscode.ExtensionContext) { | |
| 9 | + await setDjangoContext(); | |
| 9 | 10 | context.subscriptions.concat(providers); |
| 10 | 11 | context.subscriptions.concat(commands); |
| 12 | + context.subscriptions.concat(listeners); | |
| 11 | 13 | } |
| 12 | 14 | |
| 13 | 15 | // This method is called when your extension is deactivated |
+10-0src/listeners.ts
| @@ -0,0 +1,10 @@ | ||
| 1 | +import * as vscode from "vscode"; | |
| 2 | +import DjangoProject from "./project"; | |
| 3 | + | |
| 4 | +export const onDjangoProject = new vscode.EventEmitter<DjangoProject>(); | |
| 5 | + | |
| 6 | +export default [ | |
| 7 | + onDjangoProject.event((e) => { | |
| 8 | + console.log("Django project updated", e); | |
| 9 | + }), | |
| 10 | +]; |
+42-0src/providers/project.ts
| @@ -1,16 +1,56 @@ | ||
| 1 | 1 | import * as vscode from "vscode"; |
| 2 | +import DjangoProject from "../project"; | |
| 2 | 3 | |
| 3 | 4 | export default class ProjectDataProvider |
| 4 | 5 | implements vscode.TreeDataProvider<vscode.TreeItem> |
| 5 | 6 | { |
| 7 | + private _onDidChangeTreeData: vscode.EventEmitter< | |
| 8 | + void | vscode.TreeItem | vscode.TreeItem[] | null | undefined | |
| 9 | + > = new vscode.EventEmitter< | |
| 10 | + void | vscode.TreeItem | vscode.TreeItem[] | null | undefined | |
| 11 | + >(); | |
| 12 | + | |
| 13 | + readonly onDidChangeTreeData: vscode.Event< | |
| 14 | + void | vscode.TreeItem | vscode.TreeItem[] | null | undefined | |
| 15 | + > = this._onDidChangeTreeData.event; | |
| 16 | + | |
| 17 | + project: DjangoProject; | |
| 18 | + | |
| 19 | + constructor() { | |
| 20 | + this.project = vscode.workspace | |
| 21 | + .getConfiguration("django-overview") | |
| 22 | + .get("project") as DjangoProject; | |
| 23 | + } | |
| 24 | + | |
| 25 | + setContext(context: DjangoProject) { | |
| 26 | + this.project = context; | |
| 27 | + console.log(`Setting project to ${context}`); | |
| 28 | + this._onDidChangeTreeData.fire(); | |
| 29 | + } | |
| 30 | + | |
| 6 | 31 | getTreeItem(element: vscode.TreeItem): vscode.TreeItem { |
| 7 | 32 | return element; |
| 8 | 33 | } |
| 9 | 34 | |
| 10 | 35 | getChildren(element?: vscode.TreeItem): Thenable<vscode.TreeItem[]> { |
| 36 | + console.log(this.project); | |
| 37 | + console.log(element?.contextValue); | |
| 11 | 38 | if (element === undefined) { |
| 12 | 39 | // Return the root items |
| 13 | 40 | return Promise.resolve([this.createModelsTreeItem()]); |
| 41 | + } else if (element.contextValue === "models") { | |
| 42 | + // const models = []; | |
| 43 | + // for (const app of this.project.apps) { | |
| 44 | + // for (const model of app.models) { | |
| 45 | + // models.push | |
| 46 | + // } | |
| 47 | + // } | |
| 48 | + const models = this.project.apps.flatMap((app) => app.models); | |
| 49 | + console.log(models); | |
| 50 | + console.log("Adding the models"); | |
| 51 | + return Promise.resolve( | |
| 52 | + models.map((model) => new vscode.TreeItem(model.name)) | |
| 53 | + ); | |
| 14 | 54 | } else { |
| 15 | 55 | // Return the child items for the given parent item |
| 16 | 56 | return Promise.resolve([ |
| @@ -22,7 +62,9 @@ export default class ProjectDataProvider | ||
| 22 | 62 | |
| 23 | 63 | private createModelsTreeItem(): vscode.TreeItem { |
| 24 | 64 | const treeItem = new vscode.TreeItem("Models"); |
| 65 | + treeItem.contextValue = "models"; | |
| 25 | 66 | treeItem.collapsibleState = vscode.TreeItemCollapsibleState.Collapsed; |
| 67 | + treeItem.iconPath = new vscode.ThemeIcon("database"); | |
| 26 | 68 | return treeItem; |
| 27 | 69 | } |
| 28 | 70 | } |