Skip to content
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

Sweep: Hovering on a macro should show more details like description … #1180

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ export type DocMetaMap = Map<string, DocMetaData>;
export type NodeMetaType = NodeMetaData;
export type SourceMetaType = SourceTable;

interface MacroMetaData {
export interface MacroMetaData {
path: string | undefined; // in dbt cloud, packages are not downloaded locally
line: number;
character: number;
uniqueId: string;
description?: string;
arguments?: { name: string; type: string; description: string }[];
name: string;
depends_on: DependsOn;
}

interface MetricMetaData {
Expand All @@ -34,6 +38,7 @@ export interface NodeMetaData {
columns: { [columnName: string]: ColumnMetaData };
config: Config;
resource_type: string;
depends_on: DependsOn;
}

export interface ColumnMetaData {
Expand Down
8 changes: 8 additions & 0 deletions src/hover_provider/index.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DBTPowerUserExtension } from "../dbtPowerUserExtension";
import { provideSingleton } from "../utils";
import { ModelHoverProvider } from "./modelHoverProvider";
import { SourceHoverProvider } from "./sourceHoverProvider";
import { MacroHoverProvider } from "./macroHoverProvider";

@provideSingleton(HoverProviders)
export class HoverProviders implements Disposable {
Expand All @@ -11,6 +12,7 @@ export class HoverProviders implements Disposable {
constructor(
private modelHoverProvider: ModelHoverProvider,
private sourceHoverProvider: SourceHoverProvider,
private macroHoverProvider: MacroHoverProvider,
) {
this.disposables.push(
languages.registerHoverProvider(
Expand All @@ -24,6 +26,12 @@ export class HoverProviders implements Disposable {
this.sourceHoverProvider,
),
);
this.disposables.push(
languages.registerHoverProvider(
DBTPowerUserExtension.DBT_SQL_SELECTOR,
this.macroHoverProvider,
),
);
}

dispose() {
Expand Down
94 changes: 94 additions & 0 deletions src/hover_provider/macroHoverProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import {
CancellationToken,
HoverProvider,
Hover,
Position,
ProviderResult,
TextDocument,
Disposable,
} from "vscode";
import { TelemetryService } from "../telemetry";
import { generateMacroHoverMarkdown } from "./utils";
import { DBTTerminal } from "../dbt_client/dbtTerminal";
import { QueryManifestService } from "../services/queryManifestService";
import { provideSingleton } from "../utils";
import {
MacroMetaData,
MacroMetaMap,
NodeMetaData,
NodeMetaMap,
SourceMetaMap,
} from "../domain";

@provideSingleton(MacroHoverProvider)
export class MacroHoverProvider implements HoverProvider, Disposable {
private disposables: Disposable[] = [];

constructor(
private telemetry: TelemetryService,
private dbtTerminal: DBTTerminal,
private queryManifestService: QueryManifestService,
) {}

dispose() {
while (this.disposables.length) {
const x = this.disposables.pop();
if (x) {
x.dispose();
}
}
}

provideHover(
document: TextDocument,
position: Position,
token: CancellationToken,
): ProviderResult<Hover> {
const hoverText = document.getText(
document.getWordRangeAtPosition(position),
);

this.dbtTerminal.debug("MacroHoverProvider", `checking: ${hoverText}`);
const eventResult = this.queryManifestService.getEventByDocument(
document.uri,
);
if (!eventResult) {
return;
}
const { macroMetaMap, nodeMetaMap } = eventResult;
const macroMeta = macroMetaMap.get(hoverText);
if (!macroMeta) {
return null;
}

const referencedBy = this.getNodesReferencingMacro(
macroMeta.uniqueId,
macroMetaMap,
nodeMetaMap,
);
const hoverContent = generateMacroHoverMarkdown(
macroMeta,
referencedBy,
eventResult,
);
this.telemetry.sendTelemetryEvent("provideMacroHover");
return new Hover(hoverContent);
}

private getNodesReferencingMacro(
macroMetaName: string,
macroMetaMap: MacroMetaMap,
nodeMetaMap: NodeMetaMap,
) {
const referencedBy: (MacroMetaData | NodeMetaData)[] = [];
const allNodes = [...macroMetaMap.values(), ...nodeMetaMap.values()];

allNodes.forEach((node) => {
if (node.depends_on.macros.includes(macroMetaName)) {
referencedBy.push(node);
}
});

return referencedBy;
}
}
97 changes: 90 additions & 7 deletions src/hover_provider/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { MarkdownString } from "vscode";
import { NodeMetaType, SourceMetaType } from "../domain";
import { MarkdownString, Uri } from "vscode";
import {
MacroMetaData,
NodeMetaData,
NodeMetaType,
SourceMetaType,
} from "../domain";
import { ManifestCacheProjectAddedEvent } from "../manifest/event/manifestCacheChangedEvent";

export function generateHoverMarkdownString(
node: NodeMetaType | SourceMetaType,
Expand All @@ -14,11 +20,7 @@ export function generateHoverMarkdownString(
if (node.description !== "") {
content.appendMarkdown(`</br><span>${node.description}</span>`);
}
content.appendText("\n");
content.appendText("\n");
content.appendMarkdown("---");
content.appendText("\n");
content.appendText("\n");
addSeparator(content);
for (const colKey in node.columns) {
const column = node.columns[colKey];
content.appendMarkdown(
Expand All @@ -38,3 +40,84 @@ export function generateHoverMarkdownString(
}
return content;
}

export const generateMacroHoverMarkdown = (
node: MacroMetaData,
referencedBy: (MacroMetaData | NodeMetaData)[],
event: ManifestCacheProjectAddedEvent,
) => {
const content = new MarkdownString();
content.supportHtml = true;
content.isTrusted = true;
content.appendMarkdown(
`<span style="color:#347890;">(Macro)&nbsp;</span><span><strong>${node.name}</strong></span>`,
);
if (node.description !== "") {
content.appendMarkdown(`</br><span>${node.description}</span>`);
}
addSeparator(content);
node.arguments?.forEach((macroArg) => {
content.appendMarkdown(
`<span style="color:#347890;">(argument)&nbsp;</span><span>${macroArg.name} &nbsp;</span>`,
);
if (macroArg.type !== null) {
content.appendMarkdown(
`<span>-&nbsp;${macroArg.type.toLowerCase()}</span>`,
);
}
if (macroArg.description !== "") {
content.appendMarkdown(
`<br/><span><em>${macroArg.description}</em></span>`,
);
}
content.appendMarkdown("</br>");
});

if (referencedBy.length) {
addSeparator(content);
content.appendMarkdown(
`<span style="color:#347890;">(Referenced by)&nbsp;</span><span>${referencedBy
.map((node) => buildLink(node))
.join(",&nbsp;")}</span>`,
);
content.appendMarkdown("</br>");
}

if (node.depends_on.macros?.length || node.depends_on.nodes?.length) {
const dependsOn = [
...(node.depends_on.macros?.map((m) =>
[...event.macroMetaMap.values()].find((macro) => macro.uniqueId === m),
) || []),
...(node.depends_on.nodes?.map((m) =>
[...event.nodeMetaMap.values()].find((macro) => macro.uniqueId === m),
) || []),
];
addSeparator(content);
content.appendMarkdown(
`<span style="color:#347890;">(Depends on)&nbsp;</span><span>${dependsOn
.map((node) => buildLink(node))
.join(",&nbsp;")}</span>`,
);
}

return content;
};

const addSeparator = (content: MarkdownString) => {
content.appendText("\n");
content.appendText("\n");
content.appendMarkdown("---");
content.appendText("\n");
content.appendText("\n");
};

const buildLink = (node: MacroMetaData | NodeMetaData | undefined) => {
if (!node) {
return;
}
if (!node.path) {
return node.name;
}

return `[${node.name}](${Uri.file(node.path)} "${node.uniqueId}")`;
};
6 changes: 5 additions & 1 deletion src/manifest/parsers/macroParser.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class MacroParser {
}
for (const key in macros) {
const macro = macros[key];
const { package_name, name, original_file_path } = macro;
const { package_name, name, original_file_path, depends_on } = macro;
const packageName = package_name;
const macroName =
packageName === projectName ? name : `${packageName}.${name}`;
Expand Down Expand Up @@ -66,6 +66,10 @@ export class MacroParser {
line: index,
character: currentLine.indexOf(name),
uniqueId: key,
description: macro.description,
arguments: macro.arguments,
name,
depends_on,
});
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/manifest/parsers/nodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class NodeParser {
patch_path,
config,
resource_type,
depends_on,
} = nodesMap;
const fullPath = createFullPathForNode(
projectName,
Expand All @@ -70,6 +71,7 @@ export class NodeParser {
patch_path,
config,
resource_type,
depends_on,
});
}
this.terminal.debug(
Expand Down
Loading