From 0e8750f34d50fe8e33c966873550aba2f2499ab6 Mon Sep 17 00:00:00 2001 From: Makoto Kimura Date: Mon, 21 Oct 2024 22:34:50 +0900 Subject: [PATCH 1/4] Open Panel other than SQL files --- src/treeview_provider/modelTreeviewProvider.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/treeview_provider/modelTreeviewProvider.ts b/src/treeview_provider/modelTreeviewProvider.ts index 663fc8abe..e12fd9848 100755 --- a/src/treeview_provider/modelTreeviewProvider.ts +++ b/src/treeview_provider/modelTreeviewProvider.ts @@ -104,10 +104,9 @@ abstract class ModelTreeviewProvider if (element) { return Promise.resolve(this.getTreeItems(element.key, event)); } - const fileName = path.basename( + const fileName = path.parse( window.activeTextEditor!.document.fileName, - ".sql", - ); + ).name; const model = event.nodeMetaMap.lookupByBaseName(fileName); if (!model) { return Promise.resolve([]); @@ -222,10 +221,9 @@ class DocumentationTreeviewProvider implements TreeDataProvider { const { nodeMetaMap } = event; if (!element) { - const modelName = path.basename( + const modelName = path.parse( window.activeTextEditor!.document.fileName, - ".sql", - ); + ).name; const currentNode = nodeMetaMap.lookupByBaseName(modelName); if (currentNode === undefined) { return Promise.resolve([]); From 1e8243ea3205e91a035edab0d9c0c01fce5b6586 Mon Sep 17 00:00:00 2001 From: Makoto Kimura Date: Sun, 27 Oct 2024 09:07:18 +0900 Subject: [PATCH 2/4] Open the appropriate model in a yaml file with multiple models --- .../modelTreeviewProvider.ts | 43 ++++++++++++++++--- src/utils.ts | 40 ++++++++++++++++- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/src/treeview_provider/modelTreeviewProvider.ts b/src/treeview_provider/modelTreeviewProvider.ts index e12fd9848..6a047f83f 100755 --- a/src/treeview_provider/modelTreeviewProvider.ts +++ b/src/treeview_provider/modelTreeviewProvider.ts @@ -1,6 +1,7 @@ import { unmanaged } from "inversify"; import { provide } from "inversify-binding-decorators"; import * as path from "path"; + import { Command, Disposable, @@ -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 @@ -54,6 +55,9 @@ abstract class ModelTreeviewProvider this.dbtProjectContainer.onManifestChanged((event) => this.onManifestCacheChanged(event), ), + window.onDidChangeTextEditorSelection(() => { + this._onDidChangeTreeData.fire(); + }), ); } @@ -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 { @@ -179,6 +197,9 @@ class DocumentationTreeviewProvider implements TreeDataProvider { this.dbtProjectContainer.onManifestChanged((event) => this.onManifestCacheChanged(event), ), + window.onDidChangeTextEditorSelection(() => { + this._onDidChangeTreeData.fire(); + }), ); } @@ -221,13 +242,21 @@ class DocumentationTreeviewProvider implements TreeDataProvider { 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) { diff --git a/src/utils.ts b/src/utils.ts index b6a0633e7..f859f4e7e 100755 --- a/src/utils.ts +++ b/src/utils.ts @@ -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, @@ -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 ""; +} From adc62fc429a324e196bf3059e8597e6c11e473cd Mon Sep 17 00:00:00 2001 From: Makoto Kimura Date: Sun, 10 Nov 2024 18:43:23 +0900 Subject: [PATCH 3/4] fix YAML error check (CodeRabbit) --- src/utils.ts | 64 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index f859f4e7e..204005e76 100755 --- a/src/utils.ts +++ b/src/utils.ts @@ -369,31 +369,47 @@ export function getModelNameInActiveEditor(): string { 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; + try { + 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); + + const contents = parsedYaml.contents as { items?: Array }; + if (!contents.items) { + return ""; + } + + const modelsNode = contents.items.find( + (item) => item?.key?.value === "models", + ); + if (!modelsNode?.value?.items) { + return ""; } - } + // Find a model at the current position + for (const model of modelsNode.value.items) { + if (!model?.items) { + continue; + } + + const nameNode = model.items.find( + (item: any) => item?.key?.value === "name", + ); + if (!nameNode?.value) { + continue; + } + + if (model.range?.[0] < offset && offset < model.range?.[1]) { + return nameNode.value.toString(); + } + } + } catch (error) { + console.error("Error parsing YAML document:", error); + } return ""; } From 59846718449d4743651a0e0b045ac4c2cd75404a Mon Sep 17 00:00:00 2001 From: Makoto Kimura Date: Sun, 10 Nov 2024 18:56:57 +0900 Subject: [PATCH 4/4] fix format(Code Rabbit) --- src/treeview_provider/modelTreeviewProvider.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/treeview_provider/modelTreeviewProvider.ts b/src/treeview_provider/modelTreeviewProvider.ts index 6a047f83f..80d7be7f3 100755 --- a/src/treeview_provider/modelTreeviewProvider.ts +++ b/src/treeview_provider/modelTreeviewProvider.ts @@ -245,13 +245,15 @@ class DocumentationTreeviewProvider implements TreeDataProvider { const fileName = path.parse( window.activeTextEditor!.document.fileName, ).name; - const currentNodeByFileName = - event.nodeMetaMap.lookupByBaseName(fileName); + // Try content-based lookup first const currentNodeByFileContent = event.nodeMetaMap.lookupByBaseName( getModelNameInActiveEditor(), ); + // Fall back to filename-based lookup + const currentNode = + currentNodeByFileContent ?? + event.nodeMetaMap.lookupByBaseName(fileName); - const currentNode = currentNodeByFileContent || currentNodeByFileName; if (currentNode === undefined) { return Promise.resolve([]); }