-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
293182c
commit 5a66bfa
Showing
1 changed file
with
40 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ import { isEnclosedWithinCodeBlock } from "../utils"; | |
export class SourceDefinitionProvider implements DefinitionProvider { | ||
private sourceMetaMap: SourceMetaMap = new Map(); | ||
private static readonly IS_SOURCE = /(source)[^}]*/; | ||
private static readonly GET_SOURCE_NAME = /(?!['"])(\w+)(?=['"])/ig; | ||
private static readonly HAS_SOURCE_NAME = /(?!['"])(\w+)(?=['"])/; | ||
private static readonly GET_SOURCE_INFO = /(?!['"])(\w+)(?=['"])/g; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
provideDefinition( | ||
document: TextDocument, | ||
|
@@ -19,15 +20,27 @@ export class SourceDefinitionProvider implements DefinitionProvider { | |
const range = document.getWordRangeAtPosition(position, SourceDefinitionProvider.IS_SOURCE); | ||
const word = document.getText(range); | ||
|
||
if (range && word !== undefined && hover !== "source" | ||
&& isEnclosedWithinCodeBlock(document, range)) { | ||
const source = word.match(SourceDefinitionProvider.GET_SOURCE_NAME); | ||
if (source) { | ||
const definition = this.getSourceDefinition(source[0]); | ||
resolve(definition); | ||
return; | ||
} | ||
const linePrefix = document | ||
.lineAt(position) | ||
.text.substr(0, position.character); | ||
|
||
if (!isEnclosedWithinCodeBlock(document, position) || | ||
!linePrefix.includes('source') || | ||
hover === 'source') { return undefined; } | ||
|
||
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); | ||
return; | ||
} | ||
|
||
reject(); | ||
}); | ||
} | ||
|
@@ -36,18 +49,32 @@ export class SourceDefinitionProvider implements DefinitionProvider { | |
this.sourceMetaMap = event.sourceMetaMap; | ||
} | ||
|
||
private getSourceDefinition(name: string): Definition | undefined { | ||
const location = this.sourceMetaMap.get(name); | ||
private getSourceName(source: RegExpMatchArray) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
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 { | ||
const location = this.sourceMetaMap.get(sourceName); | ||
if (location) { | ||
const sourceFile: string = readFileSync(location.path).toString("utf8"); | ||
const sourceFileLines = sourceFile.split("\n"); | ||
|
||
for (let index = 0; index < sourceFileLines.length; index++) { | ||
const currentLine = sourceFileLines[index]; | ||
if (currentLine.includes(name)) { | ||
if (currentLine.includes(lookupItem)) { | ||
This comment has been minimized.
Sorry, something went wrong.
mdesmet
Contributor
|
||
return new Location( | ||
Uri.file(location.path), | ||
new Position(index, currentLine.indexOf(name)) | ||
new Position(index, currentLine.indexOf(lookupItem)) | ||
); | ||
} | ||
} | ||
|
These regexes are also used in the autocompletes. maybe we need to put them somewhere where we can reuse them. Is there any other logic that is used in both cases?