Skip to content

Commit

Permalink
Removed redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindaugas Greibus authored and AnWeber committed Jan 14, 2024
1 parent 4e6159a commit 645a324
Showing 1 changed file with 7 additions and 39 deletions.
46 changes: 7 additions & 39 deletions src/provider/httpDefinitionProvider.ts
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;
}
}

0 comments on commit 645a324

Please sign in to comment.