-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Enhance dbt-power-user Side Panel to Show Model Info for Non-SQL Files #1475
base: master
Are you sure you want to change the base?
Changes from 3 commits
0e8750f
1e8243e
adc62fc
5984671
148516a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
|
@@ -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,15 +108,28 @@ abstract class ModelTreeviewProvider | |
if (element) { | ||
return Promise.resolve(this.getTreeItems(element.key, event)); | ||
} | ||
const fileName = path.basename( | ||
window.activeTextEditor!.document.fileName, | ||
".sql", | ||
|
||
const model_by_file_content = event.nodeMetaMap.lookupByBaseName( | ||
getModelNameInActiveEditor(), | ||
); | ||
const model = event.nodeMetaMap.lookupByBaseName(fileName); | ||
if (!model) { | ||
return Promise.resolve([]); | ||
|
||
if (model_by_file_content) { | ||
return Promise.resolve( | ||
this.getTreeItems(model_by_file_content.uniqueId, event), | ||
); | ||
} | ||
return Promise.resolve(this.getTreeItems(model.uniqueId, event)); | ||
|
||
const fileName = path.parse( | ||
window.activeTextEditor!.document.fileName, | ||
).name; | ||
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([]); | ||
} | ||
|
||
private getNodeTreeItem(node: Node): NodeTreeItem { | ||
|
@@ -180,6 +197,9 @@ class DocumentationTreeviewProvider implements TreeDataProvider<DocTreeItem> { | |
this.dbtProjectContainer.onManifestChanged((event) => | ||
this.onManifestCacheChanged(event), | ||
), | ||
window.onDidChangeTextEditorSelection(() => { | ||
this._onDidChangeTreeData.fire(); | ||
}), | ||
); | ||
} | ||
|
||
|
@@ -222,14 +242,21 @@ class DocumentationTreeviewProvider implements TreeDataProvider<DocTreeItem> { | |
const { nodeMetaMap } = event; | ||
|
||
if (!element) { | ||
const modelName = path.basename( | ||
const fileName = path.parse( | ||
window.activeTextEditor!.document.fileName, | ||
".sql", | ||
).name; | ||
const currentNodeByFileName = | ||
event.nodeMetaMap.lookupByBaseName(fileName); | ||
const currentNodeByFileContent = event.nodeMetaMap.lookupByBaseName( | ||
getModelNameInActiveEditor(), | ||
); | ||
const currentNode = nodeMetaMap.lookupByBaseName(modelName); | ||
|
||
const currentNode = currentNodeByFileContent || currentNodeByFileName; | ||
if (currentNode === undefined) { | ||
return Promise.resolve([]); | ||
} | ||
const modelName = currentNode.name; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Align implementation with ModelTreeviewProvider and add validation There are two concerns with the current implementation:
Consider this improvement: - const currentNodeByFileName =
- event.nodeMetaMap.lookupByBaseName(fileName);
- const currentNodeByFileContent = event.nodeMetaMap.lookupByBaseName(
- getModelNameInActiveEditor(),
- );
-
- const currentNode = currentNodeByFileContent || currentNodeByFileName;
+ // Try content-based lookup first
+ const currentNodeByFileContent = event.nodeMetaMap.lookupByBaseName(
+ getModelNameInActiveEditor().catch(error => {
+ console.warn('Failed to parse editor content:', error);
+ return undefined;
+ })
+ );
+
+ // Fall back to filename-based lookup
+ const currentNode = currentNodeByFileContent ??
+ event.nodeMetaMap.lookupByBaseName(fileName);
if (currentNode === undefined) {
return Promise.resolve([]);
}
- const modelName = currentNode.name;
+ const modelName = currentNode?.name;
|
||
const children = []; | ||
|
||
if (Object.keys(currentNode.columns).length !== 0) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Extract shared model lookup logic to reduce code duplication
The model lookup logic is duplicated between
ModelTreeviewProvider
andDocumentationTreeviewProvider
. Consider extracting it into a shared utility function.Create a new utility function:
Then use it in both providers: