All files / src/providers RouteCompletionProvider.ts

97.01% Statements 65/67
87.5% Branches 28/32
100% Functions 8/8
98.38% Lines 61/62

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 14722x 1x         1x               14x               14x 2x     12x 12x   12x 48x 36x 36x     12x           36x     36x     36x 36x 36x   36x 36x   36x 36x   36x 24x 24x 24x     36x     36x     36x 24x             36x 36x   36x           14x 14x     14x   30x 30x 12x         2x       2x             5x           5x 5x   4x 4x 4x   4x   3x 3x 3x 3x 3x 3x   3x 2x     3x 1x 1x 1x       3x      
// src/providers/RouteCompletionProvider.ts
import * as vscode from 'vscode';
import type { CacheManager } from '../cache/CacheManager';
import type { RouteDefinition } from '../types';
 
// Functions that accept a route name as first argument
const ROUTE_FUNCTIONS = new Set([
  'route', 'to_route', 'redirect', 'signedRoute',
  'temporarySignedRoute', 'action',
]);
 
// ─── Route Completion Provider ────────────────────────────────────────────
 
export class RouteCompletionProvider implements vscode.CompletionItemProvider {
  constructor(private readonly cache: CacheManager) {}
 
  provideCompletionItems(
    document: vscode.TextDocument,
    position: vscode.Position,
    _token: vscode.CancellationToken,
    _context: vscode.CompletionContext
  ): vscode.CompletionItem[] {
    if (!this.isInsideRouteCall(document, position)) {
      return [];
    }
 
    const index = this.cache.getIndex();
    const items: vscode.CompletionItem[] = [];
 
    for (const [, route] of index.routes) {
      if (!route.name) { continue; } // skip anonymous routes
      Iif (!route.name) { continue; }
      items.push(this.buildCompletionItem(route));
    }
 
    return items;
  }
 
  // ─── Build Item ───────────────────────────────────────────────────────
 
  private buildCompletionItem(route: RouteDefinition): vscode.CompletionItem {
    const item = new vscode.CompletionItem(route.name, vscode.CompletionItemKind.Value);
 
    // Label shows method badges: [GET|POST]
    item.detail = `[${route.methods.join('|')}] /${route.uri}`;
 
    // Documentation
    const md = new vscode.MarkdownString();
    md.appendMarkdown(`**Route:** \`${route.name}\`\n\n`);
    md.appendMarkdown(`**URI:** \`${route.methods.join('|')} /${route.uri}\`\n\n`);
 
    Eif (route.action && route.action !== 'Closure') {
      md.appendMarkdown(`**Action:** \`${route.action}\`\n\n`);
    }
    if (route.middleware.length > 0) {
      md.appendMarkdown(`**Middleware:** ${route.middleware.map((m) => `\`${m}\``).join(', ')}\n\n`);
    }
    if (route.parameters.length > 0) {
      md.appendMarkdown(`**Parameters:**\n`);
      for (const p of route.parameters) {
        md.appendMarkdown(`- \`${p.name}\`${p.required ? '' : ' *(optional)*'}\n`);
      }
    }
    item.documentation = md;
 
    // Insert the route name (quoted context already provided by the function call)
    item.insertText = route.name;
 
    // Trigger parameter completion if route has params
    if (route.parameters.length > 0) {
      item.command = {
        command: 'editor.action.triggerSuggest',
        title: 'Trigger parameter suggestion',
      };
    }
 
    // Sort: group by prefix (admin., api., etc.)
    const prefix = route.name.split('.')[0];
    item.sortText = `0_${prefix}_${route.name}`;
 
    return item;
  }
 
  // ─── Context Detection ────────────────────────────────────────────────
 
  private isInsideRouteCall(document: vscode.TextDocument, position: vscode.Position): boolean {
    const lineText = document.lineAt(position).text;
    const textBefore = lineText.substring(0, position.character);
 
    // Check for: route('...', to_route("..."), redirect('...
    for (const fn of ROUTE_FUNCTIONS) {
      // Match function name followed by opening paren and optional quote
      const pattern = new RegExp(`\\b${fn}\\s*\\(\\s*['"]([^'"]*)$`);
      if (pattern.test(textBefore)) {
        return true;
      }
    }
 
    // Also handle Blade directives: @route('...
    Iif (/\broute\s*\(\s*['"][^'"]*$/.test(textBefore)) {
      return true;
    }
 
    return false;
  }
}
 
// ─── Route Hover Provider ─────────────────────────────────────────────────
 
export class RouteHoverProvider implements vscode.HoverProvider {
  constructor(private readonly cache: CacheManager) {}
 
  provideHover(
    document: vscode.TextDocument,
    position: vscode.Position
  ): vscode.Hover | undefined {
    const wordRange = document.getWordRangeAtPosition(position, /['"][^'"]+['"]/);
    if (!wordRange) { return undefined; }
 
    const word = document.getText(wordRange).replace(/['"]/g, '');
    const index = this.cache.getIndex();
    const route = index.routes.get(word);
 
    if (!route) { return undefined; }
 
    const md = new vscode.MarkdownString();
    md.isTrusted = true;
    md.appendMarkdown(`### 🛣 Route: \`${route.name}\`\n\n`);
    md.appendMarkdown(`| Property | Value |\n|---|---|\n`);
    md.appendMarkdown(`| URI | \`${route.methods.join('|')} /${route.uri}\` |\n`);
    md.appendMarkdown(`| Action | \`${route.action}\` |\n`);
 
    if (route.middleware.length > 0) {
      md.appendMarkdown(`| Middleware | ${route.middleware.join(', ')} |\n`);
    }
 
    if (route.parameters.length > 0) {
      md.appendMarkdown(`\n**Parameters:**\n`);
      for (const p of route.parameters) {
        md.appendMarkdown(`- \`{${p.name}}\` — ${p.required ? 'required' : 'optional'}\n`);
      }
    }
 
    return new vscode.Hover(md, wordRange);
  }
}