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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | // src/providers/livewire/LivewireProvider.ts import * as vscode from 'vscode'; import * as fs from 'fs'; import type { CacheManager } from '../../cache/CacheManager'; import type { LivewireComponent, LivewireProperty, LivewireMethod } from '../../types'; // ─── Livewire Indexer ───────────────────────────────────────────────────── export class LivewireIndexer { static readonly LIVEWIRE_BASE_CLASSES = ['Component', 'Livewire\\Component', 'Illuminate\\Foundation\\Http\\FormRequest']; static readonly LIFECYCLE_HOOKS = [ 'mount', 'hydrate', 'dehydrate', 'updating', 'updated', 'updatingFoo', 'updatedFoo', 'rendering', 'rendered', 'boot', 'booted', 'placeholder', 'exception', ]; static extract(source: string, filePath: string): LivewireComponent | null { const classMatch = /class\s+(\w+)\s+extends\s+(?:[\w\\]*Component)\b/.exec(source); if (!classMatch) { return null; } const nsMatch = /^namespace\s+([\w\\]+)/m.exec(source); const namespace = nsMatch?.[1] ?? 'App\\Livewire'; const className = classMatch[1]; const fqn = `${namespace}\\${className}`; // Public properties const properties: LivewireProperty[] = []; const propRe = /public\s+(?:([\w|?\\]+)\s+)?\$(\w+)(?:\s*=\s*[^;]+)?;/g; let m: RegExpExecArray | null; while ((m = propRe.exec(source)) !== null) { properties.push({ name: m[2], type: m[1], isPublic: true, hasWire: true, }); } // Computed properties (#[Computed] or getXxxProperty) const computedProps: string[] = []; const computedAttrRe = /#\[Computed\][^\n]*\npublic\s+function\s+(\w+)/g; while ((m = computedAttrRe.exec(source)) !== null) { computedProps.push(m[1]); } const getterRe = /public\s+function\s+get(\w+)Property\s*\(/g; while ((m = getterRe.exec(source)) !== null) { computedProps.push(m[1].charAt(0).toLowerCase() + m[1].slice(1)); } // Public methods const methods: LivewireMethod[] = []; const methodRe = /public\s+function\s+(\w+)\s*\(([^)]*)\)/g; while ((m = methodRe.exec(source)) !== null) { const name = m[1]; if (['__construct', 'render', 'boot', 'booted'].includes(name)) { continue; } methods.push({ name, parameters: m[2].split(',').map(p => p.trim()).filter(Boolean), isAction: !LivewireIndexer.LIFECYCLE_HOOKS.includes(name), isLifecycle: LivewireIndexer.LIFECYCLE_HOOKS.includes(name), }); } // Determine view path from render() method const renderMatch = /function\s+render\s*\(\s*\)\s*\{[^}]*view\s*\(\s*['"]([^'"]+)['"]/s.exec(source); const viewPath = renderMatch?.[1]; // Convert className to livewire tag name: MyCounter -> my-counter const tagName = className .replace(/([A-Z])/g, '-$1').toLowerCase().replace(/^-/, '') .replace(/\//g, '.'); const isVolt = source.includes('use function Livewire\\Volt\\') || source.includes('Volt::component'); return { name: tagName, tag: `livewire:${tagName}`, className, namespace, fullyQualifiedName: fqn, filePath, viewPath, properties, methods, computedProperties: computedProps, isVolt, }; } } // ─── Livewire Completion Provider ───────────────────────────────────────── export class LivewireCompletionProvider implements vscode.CompletionItemProvider { constructor(private readonly cache: CacheManager) {} provideCompletionItems( document: vscode.TextDocument, position: vscode.Position ): vscode.CompletionItem[] { const lineText = document.lineAt(position).text; const textBefore = lineText.substring(0, position.character); const index = this.cache.getIndex(); const items: vscode.CompletionItem[] = []; // ── 1. <livewire:component-name ────────────────────────────────── if (/<livewire:[\w.-]*$/.test(textBefore)) { for (const [, comp] of index.livewireComponents) { const item = new vscode.CompletionItem(comp.name, vscode.CompletionItemKind.Class); item.detail = `Livewire Component — ${comp.fullyQualifiedName}`; item.insertText = new vscode.SnippetString(`${comp.name} />`); item.documentation = this.buildCompDoc(comp); items.push(item); } return items; } // ── 2. @livewire('component-name') ─────────────────────────────── if (/@livewire\s*\(\s*['"][^'"]*$/.test(textBefore)) { for (const [, comp] of index.livewireComponents) { const item = new vscode.CompletionItem(comp.name, vscode.CompletionItemKind.Class); item.detail = `Livewire Component`; items.push(item); } return items; } // ── 3. wire:model="property" ───────────────────────────────────── const wireModelMatch = /wire:(?:model|model\.live|model\.lazy|model\.debounce|target|click|submit|keydown|keyup|loading)(?:\.[\w]+)*\s*=\s*["'][^"']*$/.test(textBefore); if (wireModelMatch || /wire:model[.=]["']?[\w.]*$/.test(textBefore)) { // Find the nearest Livewire component context const comp = this.findContextComponent(document); if (comp) { for (const prop of comp.properties) { const item = new vscode.CompletionItem(prop.name, vscode.CompletionItemKind.Property); item.detail = prop.type ?? 'mixed'; item.documentation = new vscode.MarkdownString(`Livewire public property \`$${prop.name}\``); items.push(item); } for (const computed of comp.computedProperties) { const item = new vscode.CompletionItem(computed, vscode.CompletionItemKind.Property); item.detail = 'computed'; items.push(item); } } return items; } // ── 4. wire:click="method" ─────────────────────────────────────── if (/wire:(?:click|submit|keydown|keyup|change|input|focus|blur)\s*=\s*["'][^"']*$/.test(textBefore)) { const comp = this.findContextComponent(document); if (comp) { for (const method of comp.methods) { if (!method.isAction) { continue; } const item = new vscode.CompletionItem(method.name, vscode.CompletionItemKind.Method); item.detail = `Livewire action`; item.insertText = method.parameters.length > 0 ? new vscode.SnippetString(`${method.name}($1)`) : method.name; items.push(item); } } return items; } // ── 5. wire: attribute name completion ─────────────────────────── if (/\bwire:[\w.-]*$/.test(textBefore)) { const wireAttrs = [ { attr: 'wire:model', doc: 'Two-way data binding' }, { attr: 'wire:model.live', doc: 'Real-time binding (every keystroke)' }, { attr: 'wire:model.lazy', doc: 'Bind on blur' }, { attr: 'wire:model.debounce.500ms', doc: 'Debounced binding' }, { attr: 'wire:click', doc: 'Click action' }, { attr: 'wire:click.prevent', doc: 'Click action (prevent default)' }, { attr: 'wire:submit', doc: 'Form submit action' }, { attr: 'wire:submit.prevent', doc: 'Form submit (prevent default)' }, { attr: 'wire:keydown', doc: 'Keydown event' }, { attr: 'wire:keydown.enter', doc: 'Enter key event' }, { attr: 'wire:keydown.escape', doc: 'Escape key event' }, { attr: 'wire:loading', doc: 'Show/hide during loading' }, { attr: 'wire:loading.attr', doc: 'Add attribute during loading' }, { attr: 'wire:loading.class', doc: 'Add class during loading' }, { attr: 'wire:loading.remove', doc: 'Remove during loading' }, { attr: 'wire:loading.delay', doc: 'Delay loading indicator' }, { attr: 'wire:target', doc: 'Target specific action for loading state' }, { attr: 'wire:navigate', doc: 'Livewire navigate link' }, { attr: 'wire:navigate.hover', doc: 'Prefetch on hover' }, { attr: 'wire:current', doc: 'Active link styling' }, { attr: 'wire:ignore', doc: 'Ignore DOM updates' }, { attr: 'wire:ignore.self', doc: 'Ignore self but not children' }, { attr: 'wire:transition', doc: 'Animate element transition' }, { attr: 'wire:confirm', doc: 'Show confirm dialog before action' }, { attr: 'wire:offline', doc: 'Show when offline' }, { attr: 'wire:online', doc: 'Show when online' }, { attr: 'wire:init', doc: 'Run action on mount' }, { attr: 'wire:poll', doc: 'Poll server at interval' }, { attr: 'wire:key', doc: 'Unique key for loop items' }, { attr: 'wire:sortable', doc: 'Enable drag-to-sort' }, { attr: 'wire:stream', doc: 'Stream content from server' }, ]; for (const { attr, doc } of wireAttrs) { const item = new vscode.CompletionItem(attr, vscode.CompletionItemKind.Property); item.detail = doc; items.push(item); } return items; } return items; } private buildCompDoc(comp: LivewireComponent): vscode.MarkdownString { const md = new vscode.MarkdownString(); md.appendMarkdown(`### ⚡ Livewire: \`${comp.tag}\`\n\n`); md.appendMarkdown(`**Class:** \`${comp.fullyQualifiedName}\`\n\n`); if (comp.properties.length > 0) { md.appendMarkdown('**Properties:**\n'); for (const p of comp.properties) { md.appendMarkdown(`- \`$${p.name}\`: \`${p.type ?? 'mixed'}\`\n`); } } if (comp.methods.filter(m => m.isAction).length > 0) { md.appendMarkdown('\n**Actions:**\n'); for (const m of comp.methods.filter(a => a.isAction)) { md.appendMarkdown(`- \`${m.name}()\`\n`); } } return md; } private findContextComponent(document: vscode.TextDocument): LivewireComponent | undefined { const index = this.cache.getIndex(); // Match by view path for (const [, comp] of index.livewireComponents) { if (comp.viewPath && document.fileName.includes(comp.viewPath.replace('.', '/'))) { return comp; } } return undefined; } } // ─── Livewire Hover Provider ────────────────────────────────────────────── export class LivewireHoverProvider implements vscode.HoverProvider { constructor(private readonly cache: CacheManager) {} provideHover(document: vscode.TextDocument, position: vscode.Position): vscode.Hover | undefined { const wordRange = document.getWordRangeAtPosition(position, /livewire:[\w.-]+/); if (!wordRange) { return undefined; } const tag = document.getText(wordRange); const name = tag.replace('livewire:', ''); const index = this.cache.getIndex(); const comp = index.livewireComponents.get(name); if (!comp) { return undefined; } const md = new vscode.MarkdownString(); md.isTrusted = true; md.appendMarkdown(`### ⚡ \`${comp.tag}\`\n\n`); md.appendMarkdown(`**Class:** \`${comp.fullyQualifiedName}\`\n\n`); if (comp.properties.length > 0) { md.appendMarkdown('**Properties:**\n'); for (const p of comp.properties) { md.appendMarkdown(`- \`$${p.name}\`: \`${p.type ?? 'mixed'}\`\n`); } } return new vscode.Hover(md, wordRange); } } // ─── Livewire Definition Provider ──────────────────────────────────────── export class LivewireDefinitionProvider implements vscode.DefinitionProvider { constructor(private readonly cache: CacheManager) {} provideDefinition(document: vscode.TextDocument, position: vscode.Position): vscode.Location | undefined { const wordRange = document.getWordRangeAtPosition(position, /livewire:[\w.-]+/); if (!wordRange) { return undefined; } const tag = document.getText(wordRange); const name = tag.replace('livewire:', ''); const index = this.cache.getIndex(); const comp = index.livewireComponents.get(name); if (!comp || !fs.existsSync(comp.filePath)) { return undefined; } return new vscode.Location( vscode.Uri.file(comp.filePath), new vscode.Position(comp.methods[0]?.lineNumber ? comp.methods[0].lineNumber - 1 : 0, 0) ); } } |