Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | // src/providers/BladeCompletionProvider.ts import * as vscode from 'vscode'; import type { CacheManager } from '../cache/CacheManager'; // ─── All built-in Blade directives ─────────────────────────────────────── const BLADE_DIRECTIVES: Array<{ name: string; snippet: string; doc: string }> = [ { name: 'if', snippet: '@if ($1)\n\t$0\n@endif', doc: 'Conditional block' }, { name: 'elseif', snippet: '@elseif ($1)', doc: 'Else-if branch' }, { name: 'else', snippet: '@else', doc: 'Else branch' }, { name: 'unless', snippet: '@unless ($1)\n\t$0\n@endunless', doc: 'Unless block' }, { name: 'isset', snippet: '@isset($$$1)\n\t$0\n@endisset', doc: 'Isset check' }, { name: 'empty', snippet: '@empty($$$1)\n\t$0\n@endempty', doc: 'Empty check' }, { name: 'auth', snippet: '@auth\n\t$0\n@endauth', doc: 'Auth check' }, { name: 'guest', snippet: '@guest\n\t$0\n@endguest', doc: 'Guest check' }, { name: 'can', snippet: "@can('$1')\n\t$0\n@endcan", doc: 'Authorization check' }, { name: 'cannot', snippet: "@cannot('$1')\n\t$0\n@endcannot", doc: 'Unauthorized check' }, { name: 'switch', snippet: '@switch($$$1)\n\t@case($2)\n\t\t$0\n\t\t@break\n@endswitch', doc: 'Switch block' }, { name: 'case', snippet: '@case($1)', doc: 'Switch case' }, { name: 'break', snippet: '@break', doc: 'Break out of switch' }, { name: 'default', snippet: '@default', doc: 'Switch default' }, { name: 'for', snippet: '@for ($i = 0; $i < $1; $i++)\n\t$0\n@endfor', doc: 'For loop' }, { name: 'foreach', snippet: '@foreach ($$$1 as $$$2)\n\t$0\n@endforeach', doc: 'Foreach loop' }, { name: 'forelse', snippet: '@forelse ($$$1 as $$$2)\n\t$0\n@empty\n\t\n@endforelse', doc: 'Foreach with empty fallback' }, { name: 'while', snippet: '@while ($1)\n\t$0\n@endwhile', doc: 'While loop' }, { name: 'loop', snippet: '$loop', doc: 'Loop variable inside foreach' }, { name: 'section', snippet: "@section('$1')\n\t$0\n@endsection", doc: 'Define a template section' }, { name: 'yield', snippet: "@yield('$1')", doc: 'Render a section' }, { name: 'extends', snippet: "@extends('$1')", doc: 'Extend a parent template' }, { name: 'include', snippet: "@include('$1')", doc: 'Include a sub-view' }, { name: 'includeIf', snippet: "@includeIf('$1')", doc: 'Include if view exists' }, { name: 'includeWhen', snippet: "@includeWhen($1, '$2')", doc: 'Include if condition true' }, { name: 'includeUnless', snippet: "@includeUnless($1, '$2')", doc: 'Include unless condition true' }, { name: 'each', snippet: "@each('$1', $$$2, '$3')", doc: 'Render a view for each item' }, { name: 'push', snippet: "@push('$1')\n\t$0\n@endpush", doc: 'Push to a stack' }, { name: 'stack', snippet: "@stack('$1')", doc: 'Render a stack' }, { name: 'prepend', snippet: "@prepend('$1')\n\t$0\n@endprepend", doc: 'Prepend to a stack' }, { name: 'once', snippet: '@once\n\t$0\n@endonce', doc: 'Render content only once' }, { name: 'component', snippet: "@component('$1')\n\t$0\n@endcomponent", doc: 'Render a component' }, { name: 'slot', snippet: "@slot('$1')\n\t$0\n@endslot", doc: 'Define a slot in component' }, { name: 'php', snippet: "@php\n\t$0\n@endphp", doc: 'Inline PHP block' }, { name: 'verbatim', snippet: '@verbatim\n\t$0\n@endverbatim', doc: 'Verbatim block (no parsing)' }, { name: 'lang', snippet: "@lang('$1')", doc: 'Translation string' }, { name: 'json', snippet: '@json($$$1)', doc: 'JSON-encode a variable' }, { name: 'class', snippet: "@class(['$1' => $2])", doc: 'Conditional CSS classes' }, { name: 'style', snippet: "@style(['$1' => $2])", doc: 'Conditional inline styles' }, { name: 'checked', snippet: '@checked($1)', doc: 'Checked attribute shorthand' }, { name: 'selected', snippet: '@selected($1)', doc: 'Selected attribute shorthand' }, { name: 'disabled', snippet: '@disabled($1)', doc: 'Disabled attribute shorthand' }, { name: 'readonly', snippet: '@readonly($1)', doc: 'Readonly attribute shorthand' }, { name: 'required', snippet: '@required($1)', doc: 'Required attribute shorthand' }, { name: 'csrf', snippet: '@csrf', doc: 'CSRF token field' }, { name: 'method', snippet: "@method('$1')", doc: 'HTTP method spoofing' }, { name: 'error', snippet: "@error('$1')\n\t{{ \$message }}\n@enderror", doc: 'Validation error' }, { name: 'vite', snippet: "@vite(['$1'])", doc: 'Vite asset helper' }, { name: 'production', snippet: '@production\n\t$0\n@endproduction', doc: 'Production-only block' }, { name: 'env', snippet: "@env('$1')\n\t$0\n@endenv", doc: 'Env-specific block' }, { name: 'aware', snippet: "@aware(['$1'])", doc: 'Component data awareness' }, { name: 'props', snippet: "@props(['$1'])", doc: 'Declare component props' }, ]; // ─── Blade Completion Provider ──────────────────────────────────────────── export class BladeCompletionProvider implements vscode.CompletionItemProvider { constructor(private readonly cache: CacheManager) {} provideCompletionItems( document: vscode.TextDocument, position: vscode.Position, _token: vscode.CancellationToken ): vscode.CompletionItem[] { const lineText = document.lineAt(position).text; const textBefore = lineText.substring(0, position.character); // ── 1. Blade Directive: @| ──────────────────────────────────────── if (/@\w*$/.test(textBefore)) { return this.buildDirectiveCompletions(); } // ── 2. x-component tag: <x-| ───────────────────────────────────── if (/<x-[\w.-]*$/.test(textBefore)) { return this.buildComponentTagCompletions(); } // ── 3. Component attribute: <x-alert | ─────────────────────────── const componentAttrMatch = /<x-([\w.-]+)\s[^>]*$/.exec(textBefore); if (componentAttrMatch?.[1]) { return this.buildComponentAttributeCompletions(componentAttrMatch[1]); } // ── 4. @include / @extends / @component view names ─────────────── if (/@(?:include|extends|component|view)\s*\(\s*['"][^'"]*$/.test(textBefore)) { return this.buildViewNameCompletions(); } // ── 5. @section names ──────────────────────────────────────────── if (/@(?:section|yield|push|stack)\s*\(\s*['"][^'"]*$/.test(textBefore)) { return this.buildSectionNameCompletions(document); } return []; } // ─── Directive Completions ──────────────────────────────────────────── private buildDirectiveCompletions(): vscode.CompletionItem[] { return BLADE_DIRECTIVES.map(({ name, snippet, doc }) => { const item = new vscode.CompletionItem(`@${name}`, vscode.CompletionItemKind.Keyword); item.insertText = new vscode.SnippetString(snippet); item.documentation = new vscode.MarkdownString(`**Blade Directive**\n\n${doc}`); item.filterText = `@${name}`; item.sortText = `0_${name}`; return item; }); } // ─── Component Tag Completions ──────────────────────────────────────── private buildComponentTagCompletions(): vscode.CompletionItem[] { const index = this.cache.getIndex(); const items: vscode.CompletionItem[] = []; for (const [, comp] of index.bladeComponents) { const item = new vscode.CompletionItem(comp.tag, vscode.CompletionItemKind.Class); item.detail = comp.isAnonymous ? `Anonymous Blade Component — ${comp.filePath}` : `Component Class: ${comp.className ?? ''}`; const md = new vscode.MarkdownString(); md.appendMarkdown(`### 🧩 \`${comp.tag}\`\n\n`); if (comp.props.length > 0) { md.appendMarkdown('**Props:**\n'); for (const prop of comp.props) { md.appendMarkdown( `- \`${prop.name}\`${prop.required ? ' *(required)*' : ''}: \`${prop.type ?? 'mixed'}\`\n` ); } } item.documentation = md; // Auto-insert required props as snippet const requiredProps = comp.props.filter((p) => p.required); let snippet = comp.tag; if (requiredProps.length > 0) { snippet += ' ' + requiredProps .map((p, i) => `${p.name}="$\{${i + 1}\}"`) .join(' '); } snippet += ' />'; item.insertText = new vscode.SnippetString(snippet); item.sortText = `0_${comp.tag}`; items.push(item); } return items; } // ─── Component Attribute Completions ────────────────────────────────── private buildComponentAttributeCompletions(componentName: string): vscode.CompletionItem[] { const index = this.cache.getIndex(); const comp = index.bladeComponents.get(componentName); if (!comp) { return []; } return comp.props.map((prop) => { const item = new vscode.CompletionItem(prop.name, vscode.CompletionItemKind.Property); item.detail = `${prop.type ?? 'mixed'}${prop.required ? ' (required)' : ''}`; item.documentation = new vscode.MarkdownString( `Prop for \`${comp.tag}\`\n\nType: \`${prop.type ?? 'mixed'}\`` ); item.insertText = new vscode.SnippetString(`${prop.name}="$1"`); item.sortText = prop.required ? `0_${prop.name}` : `1_${prop.name}`; return item; }); } // ─── View Name Completions ──────────────────────────────────────────── private buildViewNameCompletions(): vscode.CompletionItem[] { const index = this.cache.getIndex(); return Array.from(index.views.keys()).map((viewName) => { const item = new vscode.CompletionItem(viewName, vscode.CompletionItemKind.File); item.detail = 'Blade view'; item.insertText = viewName; return item; }); } // ─── Section Name Completions ───────────────────────────────────────── private buildSectionNameCompletions(document: vscode.TextDocument): vscode.CompletionItem[] { const text = document.getText(); const sections = new Set<string>(); // Find all @section('name') in the current file const pattern = /@(?:section|yield|push|stack)\s*\(\s*['"]([^'"]+)['"]/g; let m: RegExpExecArray | null; while ((m = pattern.exec(text)) !== null) { sections.add(m[1]); } return Array.from(sections).map((name) => { const item = new vscode.CompletionItem(name, vscode.CompletionItemKind.Module); item.detail = 'Section name'; return item; }); } } // ─── Blade Hover Provider ───────────────────────────────────────────────── export class BladeHoverProvider implements vscode.HoverProvider { constructor(private readonly cache: CacheManager) {} provideHover( document: vscode.TextDocument, position: vscode.Position ): vscode.Hover | undefined { const wordRange = document.getWordRangeAtPosition(position, /x-[\w.-]+/); if (!wordRange) { return undefined; } const tag = 'x-' + document.getText(wordRange).replace(/^x-/, ''); const index = this.cache.getIndex(); const comp = index.bladeComponents.get(tag.replace(/^x-/, '')); if (!comp) { return undefined; } const md = new vscode.MarkdownString(); md.isTrusted = true; md.appendMarkdown(`### 🧩 \`${comp.tag}\`\n\n`); if (!comp.isAnonymous && comp.className) { md.appendMarkdown(`**Class:** \`${comp.className}\`\n\n`); } md.appendMarkdown(`**File:** \`${comp.filePath}\`\n\n`); if (comp.props.length > 0) { md.appendMarkdown('**Props:**\n\n'); for (const p of comp.props) { md.appendMarkdown( `| \`${p.name}\` | \`${p.type ?? 'mixed'}\` | ${p.required ? '**required**' : 'optional'} |\n` ); } } return new vscode.Hover(md, wordRange); } } |