Skip to content

Commit

Permalink
fix php variable on path
Browse files Browse the repository at this point in the history
  • Loading branch information
absszero committed Mar 28, 2024
1 parent cdbeadb commit 1f57bde
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 73 deletions.
51 changes: 26 additions & 25 deletions src/Blade.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { Place } from './Place';

export class Blade {
static readonly patterns = [
/\b(?:view|markdown)\b\(\s*(['"])([^'"]*)\1/,
/[lL]ayout\(\s*(['"])([^'"]*)\1/,
/\$view\s*=\s*(['"])([^'"]*)\1/,
/View::exists\(\s*(['"])([^'"]*)\1/,
/View::composer[^'"]*(['"])([^'"]*)\1/,
/View::creator[^'"]*(['"])([^'"]*)\1/,
/\b(?:view|text|html|markdown)\b\s*:\s*(['"])([^'"]*)\1/,
/view\(\s*['"][^'"]*['"],\s*(['"])([^'"]*)\1/,
/['"]layout['"]\s*=>\s*(['"])([^'"]*)\1/,
/@include(If\b)?\(\s*(['"])([^'"]*)\2/,
/@extends\(\s*(['"])([^'"]*)\1/,
/@include(When|Unless\b)?\([^'"]+(['"])([^'"]+)/,
/(resources\/views[^\s'"-]+)/,
];

static readonly multiViewsPatterns = [
/View::first\(\[(\s*['"][^'"]+['"]\s*[,]?\s*){2,}\]/,
/View::composer\(\[(\s*['"][^'"]+['"]\s*[,]?\s*){2,}\]/,
/view\(\[(\s*['"][^'"]+['"]\s*[,]?\s*){2,}\]/,
/@includeFirst\(\[(\s*['"][^'"]+['"]\s*[,]?\s*){2,}\]/,
/@each\(['"][^'"]+['"]\s*,[^,]+,[^,]+,[^)]+/,
];

public getPlace(path: string, line: string, lines = ''): Place {
let place = new Place({ path: '', paths: new Map, location: '', uris: [] });

const patterns = [
/\b(?:view|markdown)\b\(\s*(['"])([^'"]*)\1/,
/[lL]ayout\(\s*(['"])([^'"]*)\1/,
/\$view\s*=\s*(['"])([^'"]*)\1/,
/View::exists\(\s*(['"])([^'"]*)\1/,
/View::composer[^'"]*(['"])([^'"]*)\1/,
/View::creator[^'"]*(['"])([^'"]*)\1/,
/\b(?:view|text|html|markdown)\b\s*:\s*(['"])([^'"]*)\1/,
/view\(\s*['"][^'"]*['"],\s*(['"])([^'"]*)\1/,
/['"]layout['"]\s*=>\s*(['"])([^'"]*)\1/,
/@include(If\b)?\(\s*(['"])([^'"]*)\2/,
/@extends\(\s*(['"])([^'"]*)\1/,
/@include(When|Unless\b)?\([^'"]+(['"])([^'"]+)/,
/(resources\/views[^\s'"-]+)/,
];

for (const pattern of patterns) {
for (const pattern of Blade.patterns) {
const match = pattern.exec(line) ?? pattern.exec(lines);
if (match && match[match.length - 1] === path) {
place = this.transformFilename(path, place);
Expand All @@ -29,15 +37,8 @@ export class Blade {
}
}

const multiViewsPatterns = [
/View::first\(\[(\s*['"][^'"]+['"]\s*[,]?\s*){2,}\]/,
/View::composer\(\[(\s*['"][^'"]+['"]\s*[,]?\s*){2,}\]/,
/view\(\[(\s*['"][^'"]+['"]\s*[,]?\s*){2,}\]/,
/@includeFirst\(\[(\s*['"][^'"]+['"]\s*[,]?\s*){2,}\]/,
/@each\(['"][^'"]+['"]\s*,[^,]+,[^,]+,[^)]+/,
];

for (const pattern of multiViewsPatterns) {
for (const pattern of Blade.multiViewsPatterns) {
if (pattern.exec(line) ?? pattern.exec(lines)) {
place = this.transformFilename(path, place);
return place;
Expand Down
28 changes: 28 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Place } from './Place';

export class Config {
static readonly patterns = [
/Config::[^'"]*(['"])([^'"]*)\1/,
/config\([^'"]*(['"])([^'"]*)\1/g
];

public getPlace(path: string, line: string, lines = ''): Place {
const place = new Place({ path: '', paths: new Map, location: '', uris: [] });

for (const pattern of Config.patterns) {
const match = pattern.exec(line) ?? pattern.exec(lines);
if (!match) {
continue;
}

const split = path.split('.');
place.path = 'config/' + split[0] + '.php';
if (2 <= split.length) {
place.location = "(['\"]{1})" + split[1] + "\\1\\s*=>";
}
return place;
}

return place;
}
}
27 changes: 6 additions & 21 deletions src/Finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Console } from './Console';
import { Router } from './Router';
import { Language } from './Language';
import { Blade } from './Blade';
import { Config } from './Config';


export class Finder {
Expand All @@ -21,6 +22,8 @@ export class Finder {
this.selection = selection;
this.path = document.getText(selection).trim();
this.path = this.path.replace(/^[\s{<!-]+|[-\s>}]+$/g, '');
this.path = this.path.replace(/{.*/g, ''); // remove the rest of string after {
this.path = this.path.replace(/\.$/, ''); // remove dot at the end
this.line = document.lineAt(selection.start).text;
this.lines = getLinesAfterDelimiter(document, selection.start.line);
}
Expand Down Expand Up @@ -169,28 +172,10 @@ export class Finder {
/**
* get config place
*/
configPlace(place: Place): Place {
const patterns = [
/Config::[^'"]*(['"])([^'"]*)\1/,
/config\([^'"]*(['"])([^'"]*)\1/g
];
configPlace(): Place {
const config = new Config;

for (const pattern of patterns) {
let match;
do {
match = pattern.exec(this.line) ?? pattern.exec(this.lines);
if (match && match[2] === this.path) {
const split = this.path.split('.');
place.path = 'config/' + split[0] + '.php';
if (2 <= split.length) {
place.location = "(['\"]{1})" + split[1] + "\\1\\s*=>";
}
return place;
}
} while (match);
}

return place;
return config.getPlace(this.path, this.line, this.lines);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let event: vscode.Disposable | null;
* @var {[type]}
*/
export async function locate(document: vscode.TextDocument, range: vscode.Range): Promise<Place | undefined> {
const selection = getSelection(document, range, "<(\"'[,)>");
const selection = getSelection(document, range, `<("'[,)>`);
if (!selection) {
return undefined;
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/suite/Config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as assert from 'assert';
import { Config } from '../../Config';

suite('Config Test Suite', () => {
const config = new Config;

test('facade config', () => {
let place = config.getPlace('app.timezone', `Config::get('app.timezone');`);
assert.strictEqual(place.path, "config/app.php");

place = config.getPlace('app.timezone', `Config::set( 'app.timezone', 'UTC');`);
assert.strictEqual(place.path, "config/app.php");
});

test('config helper', () => {
let place = config.getPlace('app', `config('app');`);
assert.strictEqual(place.path, "config/app.php");

place = config.getPlace('app', `config('app.{$var}');`);
assert.strictEqual(place.path, "config/app.php");

place = config.getPlace('app.timezone', `config('app.timezone');`);
assert.strictEqual(place.path, "config/app.php");
assert.ok(place.location.includes('timezone'));

place = config.getPlace('app.timezone', `config( ['app.timezone' => 'UTC']);`);
assert.strictEqual(place.path, "config/app.php");
});
});
25 changes: 0 additions & 25 deletions src/test/suite/Finder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,6 @@ suite('Finder Test Suite', () => {
await assertPath("hello.JS");
});

test('facade config get', async () => {
await replace(editor, `Config::get('app.t|imezone');`);
await assertPath("config/app.php");
});

test('facade config set', async () => {
await replace(editor, `Config::set( 'app.time|zone', 'UTC');`);
await assertPath("config/app.php");
});

test('config get only file', async () => {
await replace(editor, `config('a|pp');`);
await assertPath("config/app.php");
});

test('config get', async () => {
await replace(editor, `config('app.time|zone');`);
await assertPath("config/app.php");
});

test('config set', async () => {
await replace(editor, `config( ['app.time|zone' => 'UTC']);`);
await assertPath("config/app.php");
});

test('filesystem', async () => {
await replace(editor, `Storage::disk('lo|cal')->put('example.txt', 'Contents');`);
await assertPath("config/filesystems.php", "(['\"]{1})local\\1\\s*=>");
Expand Down
4 changes: 3 additions & 1 deletion src/test/test-fixtures/sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@

'hello.JS';

Config::get('app.timezone');
Config::get('app.timezone.{$var}');

Config::get("app.{$var}.hello");

Config::set( 'app.timezone', 'UTC');

Expand Down

0 comments on commit 1f57bde

Please sign in to comment.