-
Notifications
You must be signed in to change notification settings - Fork 23
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
Showing
1 changed file
with
7 additions
and
39 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 |
---|---|---|
@@ -1,85 +1,53 @@ | ||
import * as vscode from 'vscode'; | ||
import * as httpyac from 'httpyac'; | ||
import { DocumentStore } from '../documentStore'; | ||
|
||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
import * as httpyac from 'httpyac'; | ||
|
||
// thanks to https://github.com/abierbaum/vscode-file-peek | ||
|
||
export class HttpDefinitionProvider implements vscode.DefinitionProvider { | ||
|
||
protected fileSearchExtensions: string[] = ['json']; | ||
|
||
constructor(readonly documentStore: DocumentStore) { | ||
} | ||
|
||
public async provideDefinition( | ||
document: vscode.TextDocument, | ||
position: vscode.Position | ||
): Promise<vscode.Definition | vscode.LocationLink[]> { | ||
|
||
const result: Array<vscode.Location> = []; | ||
const httpFile = await this.documentStore.getHttpFile(document); | ||
const workingDir = path.dirname(document.fileName); | ||
const word = document.getText(document.getWordRangeAtPosition(position)); | ||
const line = document.lineAt(position); | ||
|
||
|
||
if(httpFile){ | ||
for (const httpRegion of httpFile.httpRegions) { | ||
// find if current position is with in requestBody | ||
const requestBody = | ||
httpRegion.symbol.children?.find(obj => | ||
// this could be with new new SymbolKind.File | ||
obj.kind === httpyac.HttpSymbolKind.requestBody && | ||
obj.startLine === line.lineNumber | ||
); | ||
if(requestBody){ | ||
// check if request body is link to file | ||
const source = requestBody.source; | ||
const reStr = `^<@?\\w*\\s+(.*?${word}.*?)\\s*$`; | ||
const match = source?.match(reStr); | ||
if (match) { | ||
const potentialFname = match[1] || match[2]; | ||
console.log("potentialFname", potentialFname) | ||
// resolve from relative path to absolute | ||
const fullPath = path.resolve(workingDir, potentialFname); | ||
const potentialFnames: string[] = this.getPotentialPaths(fullPath); | ||
const foundFname = potentialFnames.find(fnameFull => fs.existsSync(fnameFull)) || ""; | ||
result.push( | ||
new vscode.Location(vscode.Uri.file(foundFname), new vscode.Position(0, 1)) | ||
new vscode.Location(vscode.Uri.file(fullPath), new vscode.Position(0, 1)) | ||
); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return Promise.resolve(result); | ||
} | ||
|
||
|
||
|
||
getPotentialPaths(lookupPath: string): string[] { | ||
const potential_paths: string[] = [lookupPath]; | ||
|
||
// Add on list where we just add the file extension directly | ||
this.fileSearchExtensions.forEach(extStr => { | ||
potential_paths.push(lookupPath + extStr); | ||
}); | ||
|
||
// if we have an extension, then try replacing it. | ||
const parsed_path = path.parse(lookupPath); | ||
if (parsed_path.ext !== '') { | ||
this.fileSearchExtensions.forEach(extStr => { | ||
const new_path = path.format({ | ||
base: parsed_path.name + extStr, | ||
dir: parsed_path.dir, | ||
ext: extStr, | ||
name: parsed_path.name, | ||
root: parsed_path.root, | ||
}); | ||
potential_paths.push(new_path); | ||
}); | ||
} | ||
|
||
return potential_paths; | ||
} | ||
} |