diff --git a/src/provider/httpDefinitionProvider.ts b/src/provider/httpDefinitionProvider.ts index 8927cfc..e341ed5 100644 --- a/src/provider/httpDefinitionProvider.ts +++ b/src/provider/httpDefinitionProvider.ts @@ -1,17 +1,12 @@ 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) { } @@ -19,67 +14,40 @@ export class HttpDefinitionProvider implements vscode.DefinitionProvider { document: vscode.TextDocument, position: vscode.Position ): Promise { + const result: Array = []; 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; - } }