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 | 9x 1x 6x 6x 6x 6x 4x 4x 4x 3x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 20x 3x 1x 3x 2x 3x 1x 1x 1x 3x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 3x 1x 1x 1x 1x 1x | // src/providers/HoverProvider.ts
import * as vscode from 'vscode';
import type { CacheManager } from '../cache/CacheManager';
import type { ModelDefinition, PhpClass } from '../types';
// ─── Laravel Hover Provider ───────────────────────────────────────────────
export class LaravelHoverProvider implements vscode.HoverProvider {
constructor(private readonly cache: CacheManager) {}
provideHover(
document: vscode.TextDocument,
position: vscode.Position
): vscode.Hover | undefined {
const index = this.cache.getIndex();
// ── 1. Hover over a class name ─────────────────────────────────────
const classRange = document.getWordRangeAtPosition(position, /\b[A-Z][a-zA-Z0-9_\\]+\b/);
if (classRange) {
const word = document.getText(classRange);
// Check if it's a model
const model = this.findModel(word, index.models);
if (model) {
return this.buildModelHover(model, classRange);
}
// Check if it's a known PHP class
const cls = index.classes.get(word) ?? this.findClassByName(word, index.classes);
Eif (cls) {
return this.buildClassHover(cls, classRange);
}
}
// ── 2. Hover over a property/method name ──────────────────────────
const memberRange = document.getWordRangeAtPosition(position, /[a-z_][a-zA-Z0-9_]*/);
Eif (memberRange) {
const lineText = document.lineAt(position).text;
const textBefore = lineText.substring(0, document.offsetAt(memberRange.start) - document.offsetAt(new vscode.Position(position.line, 0)));
if (textBefore.endsWith('->') || textBefore.endsWith('::')) {
const memberName = document.getText(memberRange);
const methodHover = this.buildMethodHover(memberName, index.models, index.classes);
Eif (methodHover) { return new vscode.Hover(methodHover, memberRange); }
}
}
return undefined;
}
// ─── Model Hover ──────────────────────────────────────────────────────
private buildModelHover(model: ModelDefinition, range: vscode.Range): vscode.Hover {
const md = new vscode.MarkdownString();
md.isTrusted = true;
md.appendMarkdown(`### 🗄 Model: \`${model.className}\`\n\n`);
md.appendMarkdown(`**Table:** \`${model.tableName}\` | **Namespace:** \`${model.namespace}\`\n\n`);
Eif (model.columns.length > 0) {
md.appendMarkdown('**Columns:**\n\n');
md.appendMarkdown('| Column | Type | Nullable |\n|---|---|---|\n');
for (const col of model.columns.slice(0, 15)) {
md.appendMarkdown(`| \`${col.name}\` | \`${col.type}\` | ${col.nullable ? '✓' : '—'} |\n`);
}
if (model.columns.length > 15) {
md.appendMarkdown(`\n*…and ${model.columns.length - 15} more columns*\n`);
}
}
if (model.fillable.length > 0) {
md.appendMarkdown(`\n**Fillable:** ${model.fillable.map((f) => `\`${f}\``).join(', ')}\n`);
}
if (model.relationships.length > 0) {
md.appendMarkdown('\n**Relationships:**\n');
for (const rel of model.relationships) {
md.appendMarkdown(`- \`${rel.name}\` → ${rel.type}(\`${rel.relatedModel}\`)\n`);
}
}
if (model.softDeletes) {
md.appendMarkdown('\n_Uses SoftDeletes_\n');
}
return new vscode.Hover(md, range);
}
// ─── Class Hover ──────────────────────────────────────────────────────
private buildClassHover(cls: PhpClass, range: vscode.Range): vscode.Hover {
const md = new vscode.MarkdownString();
md.isTrusted = true;
md.appendMarkdown(`### 📦 \`${cls.fullyQualifiedName}\`\n\n`);
Eif (cls.docComment) {
const doc = cls.docComment.replace(/^\s*\*\s?/gm, '').replace(/^\/\*\*/, '').replace(/\*\/$/, '').trim();
md.appendMarkdown(`${doc}\n\n`);
}
Eif (cls.extends) {
md.appendMarkdown(`**Extends:** \`${cls.extends}\`\n\n`);
}
Eif (cls.implements.length > 0) {
md.appendMarkdown(`**Implements:** ${cls.implements.map((i) => `\`${i}\``).join(', ')}\n\n`);
}
const publicMethods = cls.methods.filter((m) => m.visibility === 'public').slice(0, 8);
Eif (publicMethods.length > 0) {
md.appendMarkdown('**Public Methods:**\n');
for (const m of publicMethods) {
const params = m.parameters.map((p) => `${p.type ? p.type + ' ' : ''}$${p.name}`).join(', ');
const ret = m.returnType ? `: ${m.returnType}` : '';
md.appendMarkdown(`- \`${m.isStatic ? 'static ' : ''}${m.name}(${params})${ret}\`\n`);
}
}
return new vscode.Hover(md, range);
}
// ─── Method Hover ─────────────────────────────────────────────────────
private buildMethodHover(
name: string,
_models: Map<string, ModelDefinition>,
classes: Map<string, PhpClass>
): vscode.MarkdownString | undefined {
// Search in classes for the method
for (const [, cls] of classes) {
const method = cls.methods.find((m) => m.name === name);
Iif (!method) { continue; }
const md = new vscode.MarkdownString();
const params = method.parameters
.map((p) => `${p.isNullable ? '?' : ''}${p.type ? p.type + ' ' : ''}$${p.name}${p.defaultValue ? ` = ${p.defaultValue}` : ''}`)
.join(', ');
const ret = method.returnType ? `: ${method.returnType}` : '';
md.appendCodeblock(
`${method.visibility}${method.isStatic ? ' static' : ''} function ${method.name}(${params})${ret}`,
'php'
);
Eif (method.docComment) {
const doc = method.docComment.replace(/^\s*\*\s?/gm, '').replace(/^\/\*\*/, '').replace(/\*\/$/, '').trim();
md.appendMarkdown(`\n${doc}`);
}
md.appendMarkdown(`\n\n_Defined in \`${cls.fullyQualifiedName}\`_`);
return md;
}
return undefined;
}
// ─── Helpers ──────────────────────────────────────────────────────────
private findModel(
name: string,
models: Map<string, ModelDefinition>
): ModelDefinition | undefined {
for (const [, m] of models) {
Eif (m.className === name || m.namespace + '\\' + m.className === name) { return m; }
}
return undefined;
}
private findClassByName(
name: string,
classes: Map<string, PhpClass>
): PhpClass | undefined {
const direct = classes.get(name);
Iif (direct) { return direct; }
for (const [, cls] of classes) {
Eif (cls.name === name) { return cls; }
}
return undefined;
}
}
|