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 | // src/providers/filament/FilamentProvider.ts import * as vscode from 'vscode'; import type { CacheManager } from '../../cache/CacheManager'; import type { FilamentResource } from '../../types'; // ─── Filament Indexer ───────────────────────────────────────────────────── export class FilamentIndexer { static extract(source: string, filePath: string): FilamentResource | null { const isResource = /extends\s+(?:[\w\\]*Resource)\b/.test(source) || source.includes('Filament\\Resources'); if (!isResource) { return null; } const classMatch = /class\s+(\w+)\s+extends/.exec(source); if (!classMatch) { return null; } const nsMatch = /^namespace\s+([\w\\]+)/m.exec(source); const namespace = nsMatch?.[1] ?? 'App\\Filament\\Resources'; const className = classMatch[1]; const modelMatch = /protected\s+static\s+(?:string|\?string)\s+\$model\s*=\s*([^;]+)/m.exec(source); const modelClass = modelMatch?.[1]?.replace(/['"\\]/g, '').trim(); return { name: className, className, namespace, filePath, modelClass, hasTable: source.includes('public static function table('), hasForm: source.includes('public static function form('), pages: [], }; } } // ─── Filament Form Schema Completion Provider ───────────────────────────── const FILAMENT_FORM_FIELDS: Array<{ name: string; namespace: string; doc: string; snippet: string }> = [ { name: 'TextInput', namespace: 'Filament\\Forms\\Components', doc: 'Text input field', snippet: "TextInput::make('${1:name}')" }, { name: 'Textarea', namespace: 'Filament\\Forms\\Components', doc: 'Textarea field', snippet: "Textarea::make('${1:name}')" }, { name: 'Select', namespace: 'Filament\\Forms\\Components', doc: 'Select dropdown', snippet: "Select::make('${1:name}')" }, { name: 'Toggle', namespace: 'Filament\\Forms\\Components', doc: 'Toggle/switch input', snippet: "Toggle::make('${1:name}')" }, { name: 'Checkbox', namespace: 'Filament\\Forms\\Components', doc: 'Checkbox input', snippet: "Checkbox::make('${1:name}')" }, { name: 'CheckboxList', namespace: 'Filament\\Forms\\Components', doc: 'Checkbox list', snippet: "CheckboxList::make('${1:name}')" }, { name: 'Radio', namespace: 'Filament\\Forms\\Components', doc: 'Radio buttons', snippet: "Radio::make('${1:name}')" }, { name: 'DatePicker', namespace: 'Filament\\Forms\\Components', doc: 'Date picker', snippet: "DatePicker::make('${1:name}')" }, { name: 'DateTimePicker', namespace: 'Filament\\Forms\\Components', doc: 'Date/time picker', snippet: "DateTimePicker::make('${1:name}')" }, { name: 'TimePicker', namespace: 'Filament\\Forms\\Components', doc: 'Time picker', snippet: "TimePicker::make('${1:name}')" }, { name: 'FileUpload', namespace: 'Filament\\Forms\\Components', doc: 'File upload', snippet: "FileUpload::make('${1:name}')" }, { name: 'RichEditor', namespace: 'Filament\\Forms\\Components', doc: 'Rich text editor', snippet: "RichEditor::make('${1:name}')" }, { name: 'MarkdownEditor', namespace: 'Filament\\Forms\\Components', doc: 'Markdown editor', snippet: "MarkdownEditor::make('${1:name}')" }, { name: 'ColorPicker', namespace: 'Filament\\Forms\\Components', doc: 'Color picker', snippet: "ColorPicker::make('${1:name}')" }, { name: 'TagsInput', namespace: 'Filament\\Forms\\Components', doc: 'Tags input', snippet: "TagsInput::make('${1:name}')" }, { name: 'KeyValue', namespace: 'Filament\\Forms\\Components', doc: 'Key-value pairs input', snippet: "KeyValue::make('${1:name}')" }, { name: 'Repeater', namespace: 'Filament\\Forms\\Components', doc: 'Repeatable fields group', snippet: "Repeater::make('${1:name}')" }, { name: 'Builder', namespace: 'Filament\\Forms\\Components', doc: 'Block-based builder', snippet: "Builder::make('${1:name}')" }, { name: 'Section', namespace: 'Filament\\Forms\\Components', doc: 'Section grouping', snippet: "Section::make('${1:label}')\n ->schema([\n $1\n ])" }, { name: 'Fieldset', namespace: 'Filament\\Forms\\Components', doc: 'Fieldset grouping', snippet: "Fieldset::make('${1:label}')\n ->schema([\n $1\n ])" }, { name: 'Grid', namespace: 'Filament\\Forms\\Components', doc: 'Grid layout', snippet: "Grid::make(${1:2})\n ->schema([\n $2\n ])" }, { name: 'Tabs', namespace: 'Filament\\Forms\\Components', doc: 'Tabbed sections', snippet: "Tabs::make('${1:tabs}')\n ->tabs([\n $1\n ])" }, { name: 'Hidden', namespace: 'Filament\\Forms\\Components', doc: 'Hidden input', snippet: "Hidden::make('${1:name}')" }, { name: 'Placeholder', namespace: 'Filament\\Forms\\Components', doc: 'Read-only placeholder', snippet: "Placeholder::make('${1:label}')" }, { name: 'BelongsToSelect', namespace: 'Filament\\Forms\\Components', doc: 'BelongsTo relationship select', snippet: "Select::make('${1:field}_id')\n ->relationship('${1:relationship}', '${2:name}')" }, ]; const FILAMENT_TABLE_COLUMNS: Array<{ name: string; doc: string; snippet: string }> = [ { name: 'TextColumn', doc: 'Text column', snippet: "TextColumn::make('${1:name}')" }, { name: 'IconColumn', doc: 'Boolean/icon column', snippet: "IconColumn::make('${1:name}')" }, { name: 'BadgeColumn', doc: 'Badge column', snippet: "BadgeColumn::make('${1:name}')" }, { name: 'ImageColumn', doc: 'Image column', snippet: "ImageColumn::make('${1:name}')" }, { name: 'ColorColumn', doc: 'Color column', snippet: "ColorColumn::make('${1:name}')" }, { name: 'TagsColumn', doc: 'Tags column', snippet: "TagsColumn::make('${1:name}')" }, { name: 'CheckboxColumn', doc: 'Inline editable checkbox', snippet: "CheckboxColumn::make('${1:name}')" }, { name: 'ToggleColumn', doc: 'Inline editable toggle', snippet: "ToggleColumn::make('${1:name}')" }, { name: 'SelectColumn', doc: 'Inline editable select', snippet: "SelectColumn::make('${1:name}')" }, { name: 'TextInputColumn', doc: 'Inline editable text', snippet: "TextInputColumn::make('${1:name}')" }, ]; const FILAMENT_TABLE_FILTERS: Array<{ name: string; doc: string; snippet: string }> = [ { name: 'Filter', doc: 'Custom filter', snippet: "Filter::make('${1:name}')" }, { name: 'SelectFilter', doc: 'Select dropdown filter', snippet: "SelectFilter::make('${1:name}')" }, { name: 'TernaryFilter', doc: 'True/false/null filter', snippet: "TernaryFilter::make('${1:name}')" }, { name: 'TrashedFilter', doc: 'Soft delete filter', snippet: 'TrashedFilter::make()' }, { name: 'DateRangeFilter', doc: 'Date range filter', snippet: "DateRangeFilter::make('${1:name}')" }, ]; const FILAMENT_TABLE_ACTIONS: Array<{ name: string; doc: string; snippet: string }> = [ { name: 'EditAction', doc: 'Edit record action', snippet: 'EditAction::make()' }, { name: 'ViewAction', doc: 'View record action', snippet: 'ViewAction::make()' }, { name: 'DeleteAction', doc: 'Delete record action', snippet: 'DeleteAction::make()' }, { name: 'ForceDeleteAction', doc: 'Force delete action', snippet: 'ForceDeleteAction::make()' }, { name: 'RestoreAction', doc: 'Restore soft-deleted action', snippet: 'RestoreAction::make()' }, { name: 'ReplicateAction', doc: 'Replicate record action', snippet: 'ReplicateAction::make()' }, { name: 'Action', doc: 'Custom action', snippet: "Action::make('${1:name}')" }, { name: 'BulkAction', doc: 'Bulk action', snippet: "BulkAction::make('${1:name}')" }, { name: 'DeleteBulkAction', doc: 'Bulk delete action', snippet: 'DeleteBulkAction::make()' }, { name: 'ExportBulkAction', doc: 'Bulk export action', snippet: 'ExportBulkAction::make()' }, ]; export class FilamentCompletionProvider implements vscode.CompletionItemProvider { constructor(private readonly cache: CacheManager) {} provideCompletionItems( document: vscode.TextDocument, position: vscode.Position ): vscode.CompletionItem[] { const index = this.cache.getIndex(); // Determine context: form schema, table columns, filters, or actions const fullText = document.getText(); const offset = document.offsetAt(position); const textUntilHere = fullText.slice(0, offset); const items: vscode.CompletionItem[] = []; // ── form() schema completion ─────────────────────────────────────── if (this.isInsideFormSchema(textUntilHere)) { // Model field completion from model columns const resource = this.findCurrentResource(document, index.filamentResources); if (resource?.modelClass) { const model = this.findModelByClass(resource.modelClass, index); if (model) { for (const col of model.columns) { const field = this.columnToFormField(col.name, col.type); const item = new vscode.CompletionItem(field.component, vscode.CompletionItemKind.Function); item.insertText = new vscode.SnippetString(field.snippet); item.detail = `${col.type} — ${col.name}`; item.sortText = `0_${col.name}`; items.push(item); } } } // Form field components for (const field of FILAMENT_FORM_FIELDS) { const item = new vscode.CompletionItem(field.name, vscode.CompletionItemKind.Class); item.detail = field.doc; item.insertText = new vscode.SnippetString(field.snippet); item.documentation = new vscode.MarkdownString(`**Filament Form Field**\n\n${field.doc}`); item.sortText = `1_${field.name}`; items.push(item); } return items; } // ── table() columns ─────────────────────────────────────────────── if (this.isInsideTableColumns(textUntilHere)) { const resource = this.findCurrentResource(document, index.filamentResources); if (resource?.modelClass) { const model = this.findModelByClass(resource.modelClass, index); if (model) { for (const col of model.columns) { const item = new vscode.CompletionItem(`TextColumn::${col.name}`, vscode.CompletionItemKind.Function); item.insertText = new vscode.SnippetString(`TextColumn::make('${col.name}')`); item.detail = col.type; items.push(item); } } } for (const col of FILAMENT_TABLE_COLUMNS) { const item = new vscode.CompletionItem(col.name, vscode.CompletionItemKind.Class); item.insertText = new vscode.SnippetString(col.snippet); item.detail = col.doc; items.push(item); } return items; } // ── table() filters ─────────────────────────────────────────────── if (this.isInsideTableFilters(textUntilHere)) { for (const f of FILAMENT_TABLE_FILTERS) { const item = new vscode.CompletionItem(f.name, vscode.CompletionItemKind.Class); item.insertText = new vscode.SnippetString(f.snippet); item.detail = f.doc; items.push(item); } return items; } // ── table() actions ─────────────────────────────────────────────── if (this.isInsideTableActions(textUntilHere)) { for (const a of FILAMENT_TABLE_ACTIONS) { const item = new vscode.CompletionItem(a.name, vscode.CompletionItemKind.Class); item.insertText = new vscode.SnippetString(a.snippet); item.detail = a.doc; items.push(item); } return items; } return items; } private isInsideFormSchema(textBefore: string): boolean { const lastForm = textBefore.lastIndexOf('public static function form('); const lastTable = textBefore.lastIndexOf('public static function table('); return lastForm !== -1 && (lastTable === -1 || lastForm > lastTable); } private isInsideTableColumns(textBefore: string): boolean { return textBefore.lastIndexOf('->columns([') !== -1 || textBefore.lastIndexOf('public static function table(') !== -1; } private isInsideTableFilters(textBefore: string): boolean { return textBefore.lastIndexOf('->filters([') !== -1; } private isInsideTableActions(textBefore: string): boolean { return textBefore.lastIndexOf('->actions([') !== -1 || textBefore.lastIndexOf('->bulkActions([') !== -1; } private findCurrentResource( document: vscode.TextDocument, resources: Map<string, FilamentResource> ): FilamentResource | undefined { for (const [, r] of resources) { if (r.filePath === document.uri.fsPath) { return r; } } return undefined; } private findModelByClass( modelClass: string, index: Readonly<import('../../types').WorkspaceIndex> ): import('../../types').ModelDefinition | undefined { for (const [, m] of index.models) { if (m.className === modelClass || m.namespace + '\\' + m.className === modelClass) { return m; } } return undefined; } private columnToFormField(colName: string, colType: string): { component: string; snippet: string } { const t = colType.toLowerCase(); if (t.startsWith('bool')) { return { component: 'Toggle', snippet: `Toggle::make('${colName}')` }; } if (t.includes('text') || t.includes('longtext')) { return { component: 'Textarea', snippet: `Textarea::make('${colName}')` }; } if (t.includes('date') || t.includes('time')) { return { component: 'DateTimePicker', snippet: `DateTimePicker::make('${colName}')` }; } return { component: 'TextInput', snippet: `TextInput::make('${colName}')` }; } } |