From 4e6159ab94469f982f1190137d1d2121ac90ce3e Mon Sep 17 00:00:00 2001 From: Mindaugas Greibus Date: Fri, 5 Jan 2024 09:24:05 +0200 Subject: [PATCH] Now search is based on httpyac regiond. --- src/provider/httpDefinitionProvider.ts | 82 +++++++++++--------------- 1 file changed, 33 insertions(+), 49 deletions(-) diff --git a/src/provider/httpDefinitionProvider.ts b/src/provider/httpDefinitionProvider.ts index 0ef858a..8927cfc 100644 --- a/src/provider/httpDefinitionProvider.ts +++ b/src/provider/httpDefinitionProvider.ts @@ -4,74 +4,58 @@ 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 { - private isDebug: boolean; + protected fileSearchExtensions: string[] = ['json']; constructor(readonly documentStore: DocumentStore) { - this.isDebug = false; } - provideDefinition( + public async provideDefinition( document: vscode.TextDocument, position: vscode.Position - ): vscode.ProviderResult { - const working_dir = path.dirname(document.fileName); + ): 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); - this.log_debug('====== peek-file definition lookup ===========', ''); - this.log_debug('word: ', word); - this.log_debug('line: ', line.text); - - // We are looking for strings with filenames - // - simple hack for now we look for the string with our current word in it on our line - // and where our cursor position is inside the string - const re_str = `^<@?\\w*\\s+(.*?${word}.*?)\\s*$`; - const match = line.text.match(re_str); - - this.log_debug('re_str: ', re_str); - this.log_debug(' Match: ', match); - - if (match) { - const potential_fname = match[1] || match[2]; - const match_index: number = match.index || 0; - const match_start: number = match_index; - const match_end: number = match_index + potential_fname.length; - - // Verify the match string is at same location as cursor - if (position.character >= match_start && position.character <= match_end) { - const full_path = path.resolve(working_dir, potential_fname); - this.log_debug(' Match: ', match); - this.log_debug(' Fname: ', potential_fname); - this.log_debug(' Full: ', full_path); - - // Find all potential paths to check and return the first one found - const potential_fnames: string[] = this.getPotentialPaths(full_path); - this.log_debug(' potential fnames: ', potential_fnames); - - const found_fname = potential_fnames.find(fname_full => { - this.log_debug(' checking: ', fname_full); - return fs.existsSync(fname_full); - }); - if (found_fname !== null) { - const exact_found_fname: string = found_fname || ''; - this.log_debug('found: ', exact_found_fname); - return new vscode.Location(vscode.Uri.file(exact_found_fname), new vscode.Position(0, 1)); + + if(httpFile){ + for (const httpRegion of httpFile.httpRegions) { + const requestBody = + httpRegion.symbol.children?.find(obj => + obj.kind === httpyac.HttpSymbolKind.requestBody && + obj.startLine === line.lineNumber + ); + if(requestBody){ + 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) + 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)) + ); + } + break; } } } - return null; + return Promise.resolve(result); } - private log_debug(message: string, obj: string[] | string | number | RegExpExecArray | null): void { - if (this.isDebug) { - console.debug(message, obj); - } - } + getPotentialPaths(lookupPath: string): string[] { const potential_paths: string[] = [lookupPath];