Skip to content

Commit 52e9e14

Browse files
committed
specify prefixes for blade components
1 parent 3818c48 commit 52e9e14

File tree

4 files changed

+64
-28
lines changed

4 files changed

+64
-28
lines changed

php-templates/blade-components.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
$components = new class {
44
protected $autoloaded = [];
55

6+
protected $prefixes = [];
7+
68
public function __construct()
79
{
810
$this->autoloaded = require base_path("vendor/composer/autoload_psr4.php");
911
}
1012

1113
public function all()
1214
{
13-
return collect(array_merge(
15+
$components = collect(array_merge(
1416
$this->getStandardClasses(),
1517
$this->getStandardViews(),
1618
$this->getNamespaced(),
@@ -21,6 +23,11 @@ public function all()
2123
'isVendor' => $items->first()['isVendor'],
2224
'paths' => $items->pluck('path')->values(),
2325
]);
26+
27+
return [
28+
'components' => $components,
29+
'prefixes' => $this->prefixes,
30+
];
2431
}
2532

2633
protected function getStandardViews()
@@ -139,6 +146,10 @@ protected function getAnonymous()
139146
})
140147
)
141148
);
149+
150+
if (!in_array($item['prefix'], $this->prefixes)) {
151+
$this->prefixes[] = $item['prefix'];
152+
}
142153
}
143154

144155
return $components;

src/features/bladeComponent.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,47 @@ export const linkProvider: LinkProvider = (doc: vscode.TextDocument) => {
88
const links: vscode.DocumentLink[] = [];
99
const text = doc.getText();
1010
const lines = text.split("\n");
11-
const views = getBladeComponents().items;
11+
const components = getBladeComponents().items;
12+
const regexes = [new RegExp(/<\/?x-([^\s>]+)/)];
1213

13-
lines.forEach((line, index) => {
14-
const regexes = [
15-
new RegExp(/<\/?x-([^\s>]+)/),
16-
new RegExp(/<\/?((flux|what)\:[^\s>]+)/),
17-
];
14+
if (components.prefixes.length > 0) {
15+
regexes.push(
16+
new RegExp(`<\\/?((${components.prefixes.join("|")})\\:[^\\s>]+)`),
17+
);
18+
}
1819

20+
lines.forEach((line, index) => {
1921
for (const regex of regexes) {
2022
const match = line.match(regex);
2123
// get reflection properties for classes
2224
// auto complete them + hover?
2325

24-
if (match && match.index !== undefined) {
25-
const component = views[match[1]];
26+
if (!match || match.index === undefined) {
27+
continue;
28+
}
2629

27-
if (!component) {
28-
return;
29-
}
30+
const component = components.components[match[1]];
3031

31-
links.push(
32-
new vscode.DocumentLink(
33-
new vscode.Range(
34-
new vscode.Position(index, match.index + 1),
35-
new vscode.Position(
36-
index,
37-
match.index + match[0].length,
38-
),
32+
if (!component) {
33+
return;
34+
}
35+
36+
const path =
37+
component.paths.find((p) => p.endsWith(".blade.php")) ||
38+
component.paths[0];
39+
40+
links.push(
41+
new vscode.DocumentLink(
42+
new vscode.Range(
43+
new vscode.Position(index, match.index + 1),
44+
new vscode.Position(
45+
index,
46+
match.index + match[0].length,
3947
),
40-
vscode.Uri.parse(projectPath(component.paths[0])),
4148
),
42-
);
43-
}
49+
vscode.Uri.parse(projectPath(path)),
50+
),
51+
);
4452
}
4553
});
4654

src/repositories/bladeComponents.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { runInLaravel, template } from "@src/support/php";
22
import { repository } from ".";
33

44
export interface BladeComponents {
5-
[key: string]: {
6-
paths: string[];
7-
isVendor: boolean;
5+
components: {
6+
[key: string]: {
7+
paths: string[];
8+
isVendor: boolean;
9+
};
810
};
11+
prefixes: string[];
912
}
1013

1114
const load = () => {
@@ -15,6 +18,9 @@ const load = () => {
1518
export const getBladeComponents = repository<BladeComponents>(
1619
load,
1720
"{,**/}{view,views}/{*,**/*}",
18-
{},
21+
{
22+
components: {},
23+
prefixes: [],
24+
},
1925
["create", "delete"],
2026
);

src/templates/blade-components.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ export default `
33
$components = new class {
44
protected $autoloaded = [];
55
6+
protected $prefixes = [];
7+
68
public function __construct()
79
{
810
$this->autoloaded = require base_path("vendor/composer/autoload_psr4.php");
911
}
1012
1113
public function all()
1214
{
13-
return collect(array_merge(
15+
$components = collect(array_merge(
1416
$this->getStandardClasses(),
1517
$this->getStandardViews(),
1618
$this->getNamespaced(),
@@ -21,6 +23,11 @@ $components = new class {
2123
'isVendor' => $items->first()['isVendor'],
2224
'paths' => $items->pluck('path')->values(),
2325
]);
26+
27+
return [
28+
'components' => $components,
29+
'prefixes' => $this->prefixes,
30+
];
2431
}
2532
2633
protected function getStandardViews()
@@ -139,6 +146,10 @@ $components = new class {
139146
})
140147
)
141148
);
149+
150+
if (!in_array($item['prefix'], $this->prefixes)) {
151+
$this->prefixes[] = $item['prefix'];
152+
}
142153
}
143154
144155
return $components;

0 commit comments

Comments
 (0)