Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesmet committed Aug 26, 2020
1 parent cafffe8 commit 9b99aed
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions src/definition_provider/sourceDefinitionProvider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { SourceMetaMap, DBTManifestCacheChangedEvent } from "../dbtManifest";
import { DefinitionProvider, TextDocument, Position, CancellationToken, ProviderResult, Definition, DefinitionLink, Location, Uri } from "vscode";
import {
DefinitionProvider,
TextDocument,
Position,
CancellationToken,
ProviderResult,
Definition,
DefinitionLink,
Location,
Uri,
} from "vscode";
import { readFileSync } from "fs";
import path = require("path");
import { isEnclosedWithinCodeBlock } from "../utils";

export class SourceDefinitionProvider implements DefinitionProvider {
private sourceMetaMap: SourceMetaMap = new Map();
private static readonly IS_SOURCE = /(source)[^}]*/;
private static readonly HAS_SOURCE_NAME = /(?!['"])(\w+)(?=['"])/;
private static readonly GET_SOURCE_INFO = /(?!['"])(\w+)(?=['"])/g;

provideDefinition(
Expand All @@ -17,57 +26,52 @@ export class SourceDefinitionProvider implements DefinitionProvider {
): ProviderResult<Definition | DefinitionLink[]> {
return new Promise((resolve, reject) => {
const hover = document.getText(document.getWordRangeAtPosition(position));
const range = document.getWordRangeAtPosition(position, SourceDefinitionProvider.IS_SOURCE);
const range = document.getWordRangeAtPosition(
position,
SourceDefinitionProvider.IS_SOURCE
);
const word = document.getText(range);

const linePrefix = document
.lineAt(position)
.text.substr(0, position.character);

if (!isEnclosedWithinCodeBlock(document, position) ||
!linePrefix.includes('source') ||
hover === 'source') { return undefined; }
if (
!isEnclosedWithinCodeBlock(document, position) ||
!linePrefix.includes("source") ||
hover === "source"
) {
reject();
return;
}

const source = word.match(SourceDefinitionProvider.GET_SOURCE_INFO);
if (source === null || source === undefined) {
return undefined;
}

const sourceInfo = linePrefix.match(SourceDefinitionProvider.HAS_SOURCE_NAME) ? this.getTableName(source) : this.getSourceName(source);

if (sourceInfo) {
const definition = this.getSourceDefinition(sourceInfo.sourceName, sourceInfo.lookupItem);
resolve(definition);
reject();
return;
}

reject();
console.log(source.length > 1 && hover === source[1] ? source[1] : undefined)
const definition = this.getSourceDefinition(
source[0],
source.length > 1 && hover === source[1] ? source[1] : undefined
);
resolve(definition);
});
}

onDBTManifestCacheChanged(event: DBTManifestCacheChangedEvent): void {
this.sourceMetaMap = event.sourceMetaMap;
}

private getSourceName(source: RegExpMatchArray) {
return {
sourceName: source[0],
lookupItem: source[0]
};
}

private getTableName(source: RegExpMatchArray) {
return {
sourceName: source[0],
lookupItem: source[1]
};
}

private getSourceDefinition(sourceName: string, lookupItem: string): Definition | undefined {
private getSourceDefinition(
sourceName: string,
tableName?: string
): Definition | undefined {
const location = this.sourceMetaMap.get(sourceName);
if (location) {
const sourceFile: string = readFileSync(location.path).toString("utf8");
const sourceFileLines = sourceFile.split("\n");
const lookupItem = tableName || sourceName;

for (let index = 0; index < sourceFileLines.length; index++) {
const currentLine = sourceFileLines[index];
Expand All @@ -81,4 +85,4 @@ export class SourceDefinitionProvider implements DefinitionProvider {
}
return undefined;
}
}
}

0 comments on commit 9b99aed

Please sign in to comment.