Skip to content

Commit

Permalink
Open the appropriate model in a yaml file with multiple models
Browse files Browse the repository at this point in the history
  • Loading branch information
toukoudo committed Nov 10, 2024
1 parent 0e8750f commit 1e8243e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
43 changes: 36 additions & 7 deletions src/treeview_provider/modelTreeviewProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { unmanaged } from "inversify";
import { provide } from "inversify-binding-decorators";
import * as path from "path";

import {
Command,
Disposable,
Expand Down Expand Up @@ -29,7 +30,7 @@ import {
ManifestCacheChangedEvent,
ManifestCacheProjectAddedEvent,
} from "../manifest/event/manifestCacheChangedEvent";
import { provideSingleton } from "../utils";
import { getModelNameInActiveEditor, provideSingleton } from "../utils";

@provide(ModelTreeviewProvider)
abstract class ModelTreeviewProvider
Expand All @@ -54,6 +55,9 @@ abstract class ModelTreeviewProvider
this.dbtProjectContainer.onManifestChanged((event) =>
this.onManifestCacheChanged(event),
),
window.onDidChangeTextEditorSelection(() => {
this._onDidChangeTreeData.fire();
}),
);
}

Expand Down Expand Up @@ -104,14 +108,28 @@ abstract class ModelTreeviewProvider
if (element) {
return Promise.resolve(this.getTreeItems(element.key, event));
}

const model_by_file_content = event.nodeMetaMap.lookupByBaseName(
getModelNameInActiveEditor(),
);

if (model_by_file_content) {
return Promise.resolve(
this.getTreeItems(model_by_file_content.uniqueId, event),
);
}

const fileName = path.parse(
window.activeTextEditor!.document.fileName,
).name;
const model = event.nodeMetaMap.lookupByBaseName(fileName);
if (!model) {
return Promise.resolve([]);
const model_by_file_name = event.nodeMetaMap.lookupByBaseName(fileName);
if (model_by_file_name) {
return Promise.resolve(
this.getTreeItems(model_by_file_name.uniqueId, event),
);
}
return Promise.resolve(this.getTreeItems(model.uniqueId, event));

return Promise.resolve([]);
}

private getNodeTreeItem(node: Node): NodeTreeItem {
Expand Down Expand Up @@ -179,6 +197,9 @@ class DocumentationTreeviewProvider implements TreeDataProvider<DocTreeItem> {
this.dbtProjectContainer.onManifestChanged((event) =>
this.onManifestCacheChanged(event),
),
window.onDidChangeTextEditorSelection(() => {
this._onDidChangeTreeData.fire();
}),
);
}

Expand Down Expand Up @@ -221,13 +242,21 @@ class DocumentationTreeviewProvider implements TreeDataProvider<DocTreeItem> {
const { nodeMetaMap } = event;

if (!element) {
const modelName = path.parse(
const fileName = path.parse(
window.activeTextEditor!.document.fileName,
).name;
const currentNode = nodeMetaMap.lookupByBaseName(modelName);
const currentNodeByFileName =
event.nodeMetaMap.lookupByBaseName(fileName);
const currentNodeByFileContent = event.nodeMetaMap.lookupByBaseName(
getModelNameInActiveEditor(),
);

const currentNode = currentNodeByFileContent || currentNodeByFileName;
if (currentNode === undefined) {
return Promise.resolve([]);
}
const modelName = currentNode.name;

const children = [];

if (Object.keys(currentNode.columns).length !== 0) {
Expand Down
40 changes: 39 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
TextDocument,
Uri,
workspace,
window,
} from "vscode";
import { readFileSync } from "fs";
import { parse } from "yaml";
import { parse, parseDocument } from "yaml";
import {
TestMetadataAcceptedValues,
TestMetadataRelationships,
Expand Down Expand Up @@ -359,3 +360,40 @@ export const getStringSizeInMb = (str: string): number => {
const sizeInMB = sizeInBytes / (1024 * 1024);
return sizeInMB;
};

export function getModelNameInActiveEditor(): string {
if (
window.activeTextEditor === undefined ||
window.activeTextEditor.document.languageId !== "yaml"
) {
return "";
}

const parsedYaml = parseDocument(window.activeTextEditor.document.getText());
if (parsedYaml.contents === null) {
return "";
}
const cursorPosition = window.activeTextEditor.selection.active;
const offset = window.activeTextEditor.document.offsetAt(cursorPosition);

// parseDocument returns Pair of Key-Value
// So, we need to find the node with key "models", and extract items of it
const models = (parsedYaml.contents as any).items.find(
(y: any) => y.key.value === "models",
).value.items;

// Find a model at the current position
for (const model of models) {
// each element of models is a Pair of Key-Value, and Value is Scalar Type.
// So, we need to find the node with key "name", and extract value of it by toString
const name = model.items
.find((x: any) => x.key.value === "name")
.value.toString();

if (model.range[0] < offset && offset < model.range[1]) {
return name;
}
}

return "";
}

0 comments on commit 1e8243e

Please sign in to comment.