Skip to content

Commit d378743

Browse files
committed
blade component autocomplete
1 parent ea92d92 commit d378743

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

generatable.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"type": "bladeComponent",
4949
"label": "Blade components",
5050
"features": [
51-
"link"
51+
"link",
52+
"completion"
5253
]
5354
},
5455
{

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@
188188
"generated": true,
189189
"description": "Enable linking for Blade components."
190190
},
191+
"Laravel.bladeComponent.completion": {
192+
"type": "boolean",
193+
"default": true,
194+
"generated": true,
195+
"description": "Enable completion for Blade components."
196+
},
191197
"Laravel.config.diagnostics": {
192198
"type": "boolean",
193199
"default": true,

src/extension.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import EloquentCompletion from "./completion/Eloquent";
1313
import Registry from "./completion/Registry";
1414
import ValidationCompletion from "./completion/Validation";
1515
import { updateDiagnostics } from "./diagnostic/diagnostic";
16+
import { completionProvider as bladeComponentCompletion } from "./features/bladeComponent";
17+
import { completionProvider as livewireComponentCompletion } from "./features/livewireComponent";
1618
import { hoverProviders } from "./hover/HoverProvider";
1719
import { linkProviders } from "./link/LinkProvider";
1820
import { configAffected } from "./support/config";
@@ -113,7 +115,11 @@ export function activate(context: vscode.ExtensionContext) {
113115
...TRIGGER_CHARACTERS.concat(["|"]),
114116
),
115117
vscode.languages.registerCompletionItemProvider(
116-
LANGUAGES,
118+
BLADE_LANGUAGES,
119+
bladeComponentCompletion,
120+
"x",
121+
"-",
122+
),
117123
vscode.languages.registerCompletionItemProvider(
118124
BLADE_LANGUAGES,
119125
livewireComponentCompletion,

src/features/bladeComponent.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getViews } from "@src/repositories/views";
2+
import { config } from "@src/support/config";
23
import { projectPath } from "@src/support/project";
34
import * as vscode from "vscode";
45
import { LinkProvider } from "..";
@@ -39,3 +40,40 @@ export const linkProvider: LinkProvider = (doc: vscode.TextDocument) => {
3940

4041
return Promise.resolve(links);
4142
};
43+
44+
export const completionProvider: vscode.CompletionItemProvider = {
45+
provideCompletionItems(
46+
doc: vscode.TextDocument,
47+
pos: vscode.Position,
48+
): vscode.ProviderResult<vscode.CompletionItem[]> {
49+
if (!config("bladeComponent.completion", true)) {
50+
return undefined;
51+
}
52+
53+
const componentPrefixes = ["x", "x-"];
54+
const pathPrefix = "components.";
55+
const line = doc.lineAt(pos.line).text;
56+
57+
const match = componentPrefixes.find((prefix) => {
58+
const linePrefix = line.substring(
59+
pos.character - prefix.length,
60+
pos.character,
61+
);
62+
63+
return linePrefix !== prefix;
64+
});
65+
66+
if (!match) {
67+
return undefined;
68+
}
69+
70+
return getViews()
71+
.items.filter((view) => view.key.startsWith(pathPrefix))
72+
.map(
73+
(view) =>
74+
new vscode.CompletionItem(
75+
"x-" + view.key.replace(pathPrefix, ""),
76+
),
77+
);
78+
},
79+
};

src/support/generated-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export type GeneratedConfigKey = 'appBinding.diagnostics' | 'appBinding.hover' | 'appBinding.link' | 'appBinding.completion' | 'asset.diagnostics' | 'asset.hover' | 'asset.link' | 'asset.completion' | 'auth.diagnostics' | 'auth.hover' | 'auth.link' | 'auth.completion' | 'bladeComponent.link' | 'config.diagnostics' | 'config.hover' | 'config.link' | 'config.completion' | 'controllerAction.diagnostics' | 'controllerAction.hover' | 'controllerAction.link' | 'controllerAction.completion' | 'env.diagnostics' | 'env.hover' | 'env.link' | 'env.completion' | 'inertia.diagnostics' | 'inertia.hover' | 'inertia.link' | 'inertia.completion' | 'livewireComponent.link' | 'livewireComponent.completion' | 'middleware.diagnostics' | 'middleware.hover' | 'middleware.link' | 'middleware.completion' | 'mix.diagnostics' | 'mix.hover' | 'mix.link' | 'mix.completion' | 'paths.link' | 'route.diagnostics' | 'route.hover' | 'route.link' | 'route.completion' | 'translation.diagnostics' | 'translation.hover' | 'translation.link' | 'translation.completion' | 'view.diagnostics' | 'view.hover' | 'view.link' | 'view.completion';
1+
export type GeneratedConfigKey = 'appBinding.diagnostics' | 'appBinding.hover' | 'appBinding.link' | 'appBinding.completion' | 'asset.diagnostics' | 'asset.hover' | 'asset.link' | 'asset.completion' | 'auth.diagnostics' | 'auth.hover' | 'auth.link' | 'auth.completion' | 'bladeComponent.link' | 'bladeComponent.completion' | 'config.diagnostics' | 'config.hover' | 'config.link' | 'config.completion' | 'controllerAction.diagnostics' | 'controllerAction.hover' | 'controllerAction.link' | 'controllerAction.completion' | 'env.diagnostics' | 'env.hover' | 'env.link' | 'env.completion' | 'inertia.diagnostics' | 'inertia.hover' | 'inertia.link' | 'inertia.completion' | 'livewireComponent.link' | 'livewireComponent.completion' | 'middleware.diagnostics' | 'middleware.hover' | 'middleware.link' | 'middleware.completion' | 'mix.diagnostics' | 'mix.hover' | 'mix.link' | 'mix.completion' | 'paths.link' | 'route.diagnostics' | 'route.hover' | 'route.link' | 'route.completion' | 'translation.diagnostics' | 'translation.hover' | 'translation.link' | 'translation.completion' | 'view.diagnostics' | 'view.hover' | 'view.link' | 'view.completion';

0 commit comments

Comments
 (0)