All files / src/providers ConfigCompletionProvider.ts

100% Statements 75/75
92.3% Branches 24/26
100% Functions 12/12
100% Lines 71/71

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 18328x 1x           6x           6x 6x     6x   1x     5x 5x   5x 20x 20x 20x     20x 20x     5x       20x 15x 10x 10x             5x           5x 5x     5x 1x     4x 4x 4x   4x 12x 8x   8x 8x 8x     8x 8x     4x             8x           8x 8x     8x 1x     7x 7x 14x 14x 14x     14x                 1x                                           6x           6x 6x     6x   1x     5x 5x     5x 95x 95x 95x 95x       95x 5x 5x 10x 5x 5x 5x 5x 5x         5x      
// src/providers/ConfigCompletionProvider.ts
import * as vscode from 'vscode';
import type { CacheManager } from '../cache/CacheManager';
 
// ─── Config Completion Provider ───────────────────────────────────────────
 
export class ConfigCompletionProvider 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);
 
    // config('...) or Config::get('...)
    if (!/\bconfig\s*\(\s*['"][^'"]*$/.test(textBefore) &&
        !/Config::get\s*\(\s*['"][^'"]*$/.test(textBefore)) {
      return [];
    }
 
    const index = this.cache.getIndex();
    const items: vscode.CompletionItem[] = [];
 
    for (const [key, cfg] of index.configs) {
      const item = new vscode.CompletionItem(key, vscode.CompletionItemKind.Variable);
      item.detail = this.formatValue(cfg.value);
      item.documentation = new vscode.MarkdownString(
        `**Config key:** \`${key}\`\n\nValue: \`${JSON.stringify(cfg.value)}\``
      );
      item.sortText = key;
      items.push(item);
    }
 
    return items;
  }
 
  private formatValue(value: unknown): string {
    if (value === null) { return 'null'; }
    if (typeof value === 'boolean') { return value ? 'true' : 'false'; }
    const str = String(value);
    return str.length > 60 ? str.slice(0, 57) + '...' : str;
  }
}
 
// ─── Translation Completion Provider ──────────────────────────────────────
 
export class TranslationCompletionProvider 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);
 
    // __('...'), trans('...'), @lang('...')
    if (!/(?:__|\btrans\b|@lang)\s*\(\s*['"][^'"]*$/.test(textBefore)) {
      return [];
    }
 
    const index = this.cache.getIndex();
    const items: vscode.CompletionItem[] = [];
    const seen = new Set<string>();
 
    for (const [, t] of index.translations) {
      if (seen.has(t.key)) { continue; }
      seen.add(t.key);
 
      const item = new vscode.CompletionItem(t.key, vscode.CompletionItemKind.Text);
      item.detail = `"${t.value.slice(0, 60)}"`;
      item.documentation = new vscode.MarkdownString(
        `**Translation:** \`${t.key}\`\n\n> ${t.value}\n\nLocale: \`${t.locale}\``
      );
      item.insertText = t.key;
      items.push(item);
    }
 
    return items;
  }
}
 
// ─── View Completion Provider ──────────────────────────────────────────────
 
export class ViewCompletionProvider 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);
 
    // view('...'), View::make('...'), @include('...), @extends('...')
    if (!/(?:\bview\b|View::make|@include|@extends|@component|Inertia::render)\s*\(\s*['"][^'"]*$/.test(textBefore)) {
      return [];
    }
 
    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.documentation = new vscode.MarkdownString(
        `**View:** \`${viewName}\`\n\nFile: \`${index.views.get(viewName) ?? 'unknown'}\``
      );
      return item;
    });
  }
}
 
// ─── Middleware Completion Provider ────────────────────────────────────────
 
export class MiddlewareCompletionProvider implements vscode.CompletionItemProvider {
  // Well-known Laravel built-in middleware
  private static readonly BUILTIN_MIDDLEWARE = [
    { name: 'auth', doc: 'Authenticate the user' },
    { name: 'auth:api', doc: 'Authenticate via API guard' },
    { name: 'auth:sanctum', doc: 'Authenticate via Sanctum' },
    { name: 'can', doc: 'Authorize via policy' },
    { name: 'guest', doc: 'Allow only unauthenticated users' },
    { name: 'verified', doc: 'Require email verification' },
    { name: 'throttle', doc: 'Rate limiting' },
    { name: 'throttle:api', doc: 'API rate limiting' },
    { name: 'signed', doc: 'Require signed URL' },
    { name: 'cache.headers', doc: 'Set cache headers' },
    { name: 'bindings', doc: 'Enable route model binding' },
    { name: 'cors', doc: 'Cross-Origin Resource Sharing' },
    { name: 'web', doc: 'Web middleware group' },
    { name: 'api', doc: 'API middleware group' },
    { name: 'encrypt.cookies', doc: 'Encrypt cookies' },
    { name: 'prevent-requests-during-maintenance', doc: 'Block during maintenance mode' },
    { name: 'redirect-if-authenticated', doc: 'Redirect if already authenticated' },
    { name: 'trim.strings', doc: 'Trim string inputs' },
    { name: 'convert-empty-strings-to-null', doc: 'Convert empty strings to null' },
  ];
 
  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);
 
    // ->middleware('...'), middleware(['...')
    if (!/->middleware\s*\(\s*(?:\[\s*)?['"][^'"]*$/.test(textBefore) &&
        !/['"]middleware['"]\s*=>\s*['"][^'"]*$/.test(textBefore)) {
      return [];
    }
 
    const index = this.cache.getIndex();
    const items: vscode.CompletionItem[] = [];
 
    // Built-in middleware
    for (const mw of MiddlewareCompletionProvider.BUILTIN_MIDDLEWARE) {
      const item = new vscode.CompletionItem(mw.name, vscode.CompletionItemKind.Keyword);
      item.detail = mw.doc;
      item.sortText = `0_${mw.name}`;
      items.push(item);
    }
 
    // Project-specific middleware from route index
    const seen = new Set(MiddlewareCompletionProvider.BUILTIN_MIDDLEWARE.map((m) => m.name));
    for (const route of index.routes.values()) {
      for (const mw of route.middleware) {
        if (!seen.has(mw)) {
          seen.add(mw);
          const item = new vscode.CompletionItem(mw, vscode.CompletionItemKind.Keyword);
          item.detail = 'Project middleware';
          item.sortText = `1_${mw}`;
          items.push(item);
        }
      }
    }
 
    return items;
  }
}