Skip to content

Commit f16fcc8

Browse files
Merge pull request #284 from laravel/component-index-files
Better support for Blade component index files
2 parents b1efadb + 698af70 commit f16fcc8

File tree

3 files changed

+106
-95
lines changed

3 files changed

+106
-95
lines changed

php-templates/views.php

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,57 @@
11
<?php
22

3-
function vsCodeFindBladeFiles($path)
4-
{
5-
$paths = [];
3+
$blade = new class {
4+
public function findFiles($path)
5+
{
6+
$paths = [];
67

7-
if (!is_dir($path)) {
8-
return $paths;
9-
}
8+
if (!is_dir($path)) {
9+
return $paths;
10+
}
1011

11-
foreach (
12-
\Symfony\Component\Finder\Finder::create()
12+
$files = \Symfony\Component\Finder\Finder::create()
1313
->files()
1414
->name("*.blade.php")
15-
->in($path)
16-
as $file
17-
) {
18-
$paths[] = [
19-
"path" => str_replace(base_path(DIRECTORY_SEPARATOR), '', $file->getRealPath()),
20-
"isVendor" => str_contains($file->getRealPath(), base_path("vendor")),
21-
"key" => \Illuminate\Support\Str::of($file->getRealPath())
22-
->replace(realpath($path), "")
23-
->replace(".blade.php", "")
24-
->ltrim(DIRECTORY_SEPARATOR)
25-
->replace(DIRECTORY_SEPARATOR, ".")
26-
];
15+
->in($path);
16+
17+
foreach ($files as $file) {
18+
$paths[] = [
19+
"path" => str_replace(base_path(DIRECTORY_SEPARATOR), '', $file->getRealPath()),
20+
"isVendor" => str_contains($file->getRealPath(), base_path("vendor")),
21+
"key" => \Illuminate\Support\Str::of($file->getRealPath())
22+
->replace(realpath($path), "")
23+
->replace(".blade.php", "")
24+
->ltrim(DIRECTORY_SEPARATOR)
25+
->replace(DIRECTORY_SEPARATOR, ".")
26+
];
27+
}
28+
29+
return $paths;
2730
}
31+
};
2832

29-
return $paths;
30-
}
3133
$paths = collect(
3234
app("view")
3335
->getFinder()
3436
->getPaths()
35-
)->flatMap(function ($path) {
36-
return vsCodeFindBladeFiles($path);
37-
});
37+
)->flatMap(fn($path) => $blade->findFiles($path));
3838

3939
$hints = collect(
4040
app("view")
4141
->getFinder()
4242
->getHints()
43-
)->flatMap(function ($paths, $key) {
44-
return collect($paths)->flatMap(function ($path) use ($key) {
45-
return collect(vsCodeFindBladeFiles($path))->map(function ($value) use (
46-
$key
47-
) {
48-
return array_merge($value, ["key" => "{$key}::{$value["key"]}"]);
49-
});
50-
});
51-
});
43+
)->flatMap(
44+
fn($paths, $key) => collect($paths)->flatMap(
45+
fn($path) => collect($blade->findFiles($path))->map(
46+
fn($value) => array_merge($value, ["key" => "{$key}::{$value["key"]}"])
47+
)
48+
)
49+
);
5250

5351
[$local, $vendor] = $paths
5452
->merge($hints)
5553
->values()
56-
->partition(function ($v) {
57-
return !$v["isVendor"];
58-
});
54+
->partition(fn($v) => !$v["isVendor"]);
5955

6056
echo $local
6157
->sortBy("key", SORT_NATURAL)

src/features/bladeComponent.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,30 @@ export const linkProvider: LinkProvider = (doc: vscode.TextDocument) => {
1818
// Standard component
1919
const viewName = "components." + componentName;
2020
// Index component
21-
const altName = `${viewName}.${componentName}`;
22-
const view = views.find((v) => [viewName, altName].includes(v.key));
23-
24-
if (view) {
25-
links.push(
26-
new vscode.DocumentLink(
27-
new vscode.Range(
28-
new vscode.Position(index, match.index + 1),
29-
new vscode.Position(
30-
index,
31-
match.index + match[0].length,
32-
),
21+
const indexName = `${viewName}.index`;
22+
// Index component (via same name)
23+
const sameIndexName = `${viewName}.${componentName.split(".").pop()}`;
24+
25+
const view = views.find((v) =>
26+
[viewName, indexName, sameIndexName].includes(v.key),
27+
);
28+
29+
if (!view) {
30+
return;
31+
}
32+
33+
links.push(
34+
new vscode.DocumentLink(
35+
new vscode.Range(
36+
new vscode.Position(index, match.index + 1),
37+
new vscode.Position(
38+
index,
39+
match.index + match[0].length,
3340
),
34-
vscode.Uri.parse(projectPath(view.path)),
3541
),
36-
);
37-
}
42+
vscode.Uri.parse(projectPath(view.path)),
43+
),
44+
);
3845
}
3946
});
4047

@@ -69,11 +76,23 @@ export const completionProvider: vscode.CompletionItemProvider = {
6976

7077
return getViews()
7178
.items.filter((view) => view.key.startsWith(pathPrefix))
72-
.map(
73-
(view) =>
74-
new vscode.CompletionItem(
75-
"x-" + view.key.replace(pathPrefix, ""),
76-
),
77-
);
79+
.map((view) => {
80+
const parts = view.key.split(".");
81+
82+
if (parts[parts.length - 1] === "index") {
83+
parts.pop();
84+
}
85+
86+
while (
87+
parts.length > 1 &&
88+
parts[parts.length - 1] === parts[parts.length - 2]
89+
) {
90+
parts.pop();
91+
}
92+
93+
return new vscode.CompletionItem(
94+
"x-" + parts.join(".").replace(pathPrefix, ""),
95+
);
96+
});
7897
},
7998
};

src/templates/views.ts

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,57 @@
11
// This file was generated from php-templates/views.php, do not edit directly
22
export default `
3-
function vsCodeFindBladeFiles($path)
4-
{
5-
$paths = [];
3+
$blade = new class {
4+
public function findFiles($path)
5+
{
6+
$paths = [];
67
7-
if (!is_dir($path)) {
8-
return $paths;
9-
}
8+
if (!is_dir($path)) {
9+
return $paths;
10+
}
1011
11-
foreach (
12-
\\Symfony\\Component\\Finder\\Finder::create()
12+
$files = \\Symfony\\Component\\Finder\\Finder::create()
1313
->files()
1414
->name("*.blade.php")
15-
->in($path)
16-
as $file
17-
) {
18-
$paths[] = [
19-
"path" => str_replace(base_path(DIRECTORY_SEPARATOR), '', $file->getRealPath()),
20-
"isVendor" => str_contains($file->getRealPath(), base_path("vendor")),
21-
"key" => \\Illuminate\\Support\\Str::of($file->getRealPath())
22-
->replace(realpath($path), "")
23-
->replace(".blade.php", "")
24-
->ltrim(DIRECTORY_SEPARATOR)
25-
->replace(DIRECTORY_SEPARATOR, ".")
26-
];
15+
->in($path);
16+
17+
foreach ($files as $file) {
18+
$paths[] = [
19+
"path" => str_replace(base_path(DIRECTORY_SEPARATOR), '', $file->getRealPath()),
20+
"isVendor" => str_contains($file->getRealPath(), base_path("vendor")),
21+
"key" => \\Illuminate\\Support\\Str::of($file->getRealPath())
22+
->replace(realpath($path), "")
23+
->replace(".blade.php", "")
24+
->ltrim(DIRECTORY_SEPARATOR)
25+
->replace(DIRECTORY_SEPARATOR, ".")
26+
];
27+
}
28+
29+
return $paths;
2730
}
31+
};
2832
29-
return $paths;
30-
}
3133
$paths = collect(
3234
app("view")
3335
->getFinder()
3436
->getPaths()
35-
)->flatMap(function ($path) {
36-
return vsCodeFindBladeFiles($path);
37-
});
37+
)->flatMap(fn($path) => $blade->findFiles($path));
3838
3939
$hints = collect(
4040
app("view")
4141
->getFinder()
4242
->getHints()
43-
)->flatMap(function ($paths, $key) {
44-
return collect($paths)->flatMap(function ($path) use ($key) {
45-
return collect(vsCodeFindBladeFiles($path))->map(function ($value) use (
46-
$key
47-
) {
48-
return array_merge($value, ["key" => "{$key}::{$value["key"]}"]);
49-
});
50-
});
51-
});
43+
)->flatMap(
44+
fn($paths, $key) => collect($paths)->flatMap(
45+
fn($path) => collect($blade->findFiles($path))->map(
46+
fn($value) => array_merge($value, ["key" => "{$key}::{$value["key"]}"])
47+
)
48+
)
49+
);
5250
5351
[$local, $vendor] = $paths
5452
->merge($hints)
5553
->values()
56-
->partition(function ($v) {
57-
return !$v["isVendor"];
58-
});
54+
->partition(fn($v) => !$v["isVendor"]);
5955
6056
echo $local
6157
->sortBy("key", SORT_NATURAL)

0 commit comments

Comments
 (0)