Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support contextual attributes #45

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Attribute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Place } from './Place';

export class Attribute {
static readonly patterns = [
/#\[([^(]+)\('([^"']+)/g,
];

static readonly files = new Map([
['Auth', 'config/auth.php'],
['Cache', 'config/cache.php'],
['DB', 'config/database.php'],
['Log', 'config/logging.php'],
['Storage', 'config/filesystems.php'],
]);

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

for (const pattern of Attribute.patterns) {
const matches = [...line.matchAll(pattern), ...lines.matchAll(pattern)];
for (const match of matches) {
if (match[2] !== path) {
continue;
}

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

if (!Attribute.files.has(match[1])) {
continue;
}
place.path = Attribute.files.get(match[1]) ?? '';
place.location = `['"]${path}['"]\\s*=>`;
return place;
}
}

return place;
}
}
11 changes: 11 additions & 0 deletions src/Finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Language } from './Language';
import { Blade } from './Blade';
import { Config } from './Config';
import { ClassName } from './ClassName';
import { Attribute } from './Attribute';

export class Finder {
document: vscode.TextDocument;
Expand Down Expand Up @@ -45,6 +46,7 @@ export class Finder {
this.commandPlace.bind(this),
this.filesystemPlace.bind(this),
this.routePlace.bind(this),
this.attributePlace.bind(this),
this.bladePlace.bind(this),
];

Expand Down Expand Up @@ -176,6 +178,15 @@ export class Finder {
return config.getPlace(this.path, this.line, this.lines);
}

/**
* get attribute place
*/
attributePlace(): Place {
const attribute = new Attribute;

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

/**
* get file system place
*/
Expand Down
32 changes: 32 additions & 0 deletions src/test/suite/Attribute.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as assert from 'assert';
import { Attribute } from '../../Attribute';

suite('Attribute Test Suite', () => {
const attribute = new Attribute;

test('contextual attributes', () => {
let place = attribute.getPlace('web', `#[Auth('web')]`);
assert.strictEqual(place.path, "config/auth.php");
assert.strictEqual(place.location, `['"]web['"]\\s*=>`);

place = attribute.getPlace('redis', `#[Cache('redis')]`);
assert.strictEqual(place.path, "config/cache.php");
assert.strictEqual(place.location, `['"]redis['"]\\s*=>`);

place = attribute.getPlace('app.timezone', `#[Config('app.timezone')]`);
assert.strictEqual(place.path, "config/app.php");
assert.strictEqual(place.location, `['"]timezone['"]\\s*=>`);

place = attribute.getPlace('mysql', `#[DB('mysql')]`);
assert.strictEqual(place.path, "config/database.php");
assert.strictEqual(place.location, `['"]mysql['"]\\s*=>`);

place = attribute.getPlace('daily', `#[Log('daily')]`);
assert.strictEqual(place.path, "config/logging.php");
assert.strictEqual(place.location, `['"]daily['"]\\s*=>`);

place = attribute.getPlace('s3', `#[Storage('s3')]`);
assert.strictEqual(place.path, "config/filesystems.php");
assert.strictEqual(place.location, `['"]s3['"]\\s*=>`);
});
});