|
| 1 | +import { |
| 2 | + DecorationOptions, |
| 3 | + Disposable, |
| 4 | + Hover, |
| 5 | + HoverProvider, |
| 6 | + MarkdownString, |
| 7 | + Position, |
| 8 | + Range, |
| 9 | + TextDocument, |
| 10 | + TextEditor, |
| 11 | + TextEditorDecorationType, |
| 12 | + window, |
| 13 | + workspace, |
| 14 | +} from "vscode"; |
| 15 | +import { DBTProjectContainer } from "../manifest/dbtProjectContainer"; |
| 16 | +import { provideSingleton } from "../utils"; |
| 17 | +import { getDepthColor } from "../utils"; |
| 18 | + |
| 19 | +@provideSingleton(DepthDecorationProvider) |
| 20 | +export class DepthDecorationProvider implements HoverProvider, Disposable { |
| 21 | + private disposables: Disposable[] = []; |
| 22 | + private readonly REF_PATTERN = |
| 23 | + /\{\{\s*ref\s*\(\s*['"]([^'"]+)['"]\s*\)\s*\}\}/; |
| 24 | + private readonly decorationType: TextEditorDecorationType; |
| 25 | + private projectToDepthMap: Map<string, Map<string, number>> = new Map(); |
| 26 | + |
| 27 | + constructor(private dbtProjectContainer: DBTProjectContainer) { |
| 28 | + this.decorationType = window.createTextEditorDecorationType({ |
| 29 | + after: { |
| 30 | + margin: "0 0 0 5px", |
| 31 | + textDecoration: "none", |
| 32 | + }, |
| 33 | + }); |
| 34 | + |
| 35 | + this.dbtProjectContainer.onManifestChanged((event) => { |
| 36 | + event.added?.forEach((added) => { |
| 37 | + this.projectToDepthMap.set( |
| 38 | + added.project.projectRoot.fsPath, |
| 39 | + added.modelDepthMap, |
| 40 | + ); |
| 41 | + }); |
| 42 | + event.removed?.forEach((removed) => { |
| 43 | + this.projectToDepthMap.delete(removed.projectRoot.fsPath); |
| 44 | + }); |
| 45 | + |
| 46 | + window.visibleTextEditors.forEach((editor) => { |
| 47 | + this.updateDecorations(editor); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + this.disposables.push( |
| 52 | + window.onDidChangeActiveTextEditor((editor) => { |
| 53 | + if (editor) { |
| 54 | + this.updateDecorations(editor); |
| 55 | + } |
| 56 | + }), |
| 57 | + workspace.onDidChangeTextDocument((event) => { |
| 58 | + if ( |
| 59 | + window.activeTextEditor && |
| 60 | + event.document === window.activeTextEditor.document |
| 61 | + ) { |
| 62 | + this.updateDecorations(window.activeTextEditor); |
| 63 | + } |
| 64 | + }), |
| 65 | + workspace.onDidChangeConfiguration((event) => { |
| 66 | + if (event.affectsConfiguration("dbt")) { |
| 67 | + window.visibleTextEditors.forEach((editor) => { |
| 68 | + this.updateDecorations(editor); |
| 69 | + }); |
| 70 | + } |
| 71 | + }), |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + dispose() { |
| 76 | + this.decorationType.dispose(); |
| 77 | + while (this.disposables.length) { |
| 78 | + const x = this.disposables.pop(); |
| 79 | + if (x) { |
| 80 | + x.dispose(); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private updateDecorations(editor: TextEditor): void { |
| 86 | + const project = this.dbtProjectContainer.findDBTProject( |
| 87 | + editor.document.uri, |
| 88 | + ); |
| 89 | + if (!project) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + const projectRoot = project.projectRoot.fsPath; |
| 94 | + const depthMapForProject = this.projectToDepthMap.get(projectRoot); |
| 95 | + if (!depthMapForProject) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + const text = editor.document.getText(); |
| 100 | + const decorations: DecorationOptions[] = []; |
| 101 | + const pattern = new RegExp(this.REF_PATTERN, "g"); |
| 102 | + let match; |
| 103 | + |
| 104 | + while ((match = pattern.exec(text)) !== null) { |
| 105 | + const startPos = editor.document.positionAt(match.index); |
| 106 | + const endPos = editor.document.positionAt(match.index + match[0].length); |
| 107 | + const refRange = new Range(startPos, endPos); |
| 108 | + |
| 109 | + const modelName = match[1]; |
| 110 | + const depth = depthMapForProject.get(modelName); |
| 111 | + |
| 112 | + if (depth !== undefined) { |
| 113 | + decorations.push(this.createDepthDecoration(refRange, depth)); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + editor.setDecorations(this.decorationType, decorations); |
| 118 | + } |
| 119 | + |
| 120 | + private createDepthDecoration( |
| 121 | + refRange: Range, |
| 122 | + depth: number, |
| 123 | + ): DecorationOptions { |
| 124 | + const color = getDepthColor(depth); |
| 125 | + const depthText = `(${depth})`; |
| 126 | + |
| 127 | + return { |
| 128 | + range: refRange, |
| 129 | + renderOptions: { |
| 130 | + after: { |
| 131 | + contentText: depthText, |
| 132 | + backgroundColor: color, |
| 133 | + color: "white", |
| 134 | + fontWeight: "bold", |
| 135 | + margin: "0 0 0 5px", |
| 136 | + }, |
| 137 | + }, |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + public provideHover( |
| 142 | + document: TextDocument, |
| 143 | + position: Position, |
| 144 | + ): Hover | undefined { |
| 145 | + const project = this.dbtProjectContainer.findDBTProject(document.uri); |
| 146 | + if (!project) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + const projectRoot = project.projectRoot.fsPath; |
| 151 | + const depthMapForProject = this.projectToDepthMap.get(projectRoot); |
| 152 | + if (!depthMapForProject) { |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + const text = document.getText(); |
| 157 | + let matches: RegExpMatchArray[] = []; |
| 158 | + try { |
| 159 | + const pattern = new RegExp(this.REF_PATTERN, "g"); |
| 160 | + matches = Array.from(text.matchAll(pattern)); |
| 161 | + } catch (error) { |
| 162 | + console.error("Error matching ref pattern in provideHover:", error); |
| 163 | + return undefined; |
| 164 | + } |
| 165 | + |
| 166 | + for (const match of matches) { |
| 167 | + if (match.index === undefined) { |
| 168 | + continue; |
| 169 | + } |
| 170 | + const startPos = document.positionAt(match.index); |
| 171 | + const endPos = document.positionAt(match.index + match[0].length); |
| 172 | + const refRange = new Range(startPos, endPos); |
| 173 | + |
| 174 | + if (refRange.contains(position)) { |
| 175 | + const modelName = match[1]; |
| 176 | + const depth = depthMapForProject.get(modelName); |
| 177 | + |
| 178 | + if (depth !== undefined) { |
| 179 | + const color = getDepthColor(depth); |
| 180 | + const markdown = new MarkdownString( |
| 181 | + `**DAG Depth:** <span style="color:${color}">${depth}</span>\n\n` + |
| 182 | + `The longest path of models between a source and this model is ${depth} nodes long.`, |
| 183 | + ); |
| 184 | + markdown.isTrusted = true; |
| 185 | + markdown.supportHtml = true; |
| 186 | + return new Hover(markdown, refRange); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return undefined; |
| 192 | + } |
| 193 | +} |
0 commit comments