All files / indexer workspace-indexer.ts

56.02% Statements 93/166
36.48% Branches 27/74
59.25% Functions 16/27
55.9% Lines 90/161

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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368                  9x 9x 9x     9x 9x                                                                       6x 6x     6x 6x 6x                                                                             1x     1x     1x         1x   1x                   1x 1x                                       2x 2x 2x               1x   1x       1x 1x 5x 5x     5x 5x   3x     3x     5x 4x 4x 1x   3x 4x 1x       2x     2x 1x 1x       1x     1x             3x 1x 1x       1x 1x       2x 2x           1x 1x 1x     1x 1x           1x                                                   1x                       1x 1x   1x   1x 1x     1x   1x 1x 1x 1x       1x       3x               3x 7x 2x       1x                       3x 1x 2x 1x 1x 1x           1x 2x 2x         1x             1x               1x 1x 1x       5x       2x       2x      
// src/indexer/workspace-indexer.ts
// Main indexer class
 
import * as vscode from 'vscode';
import * as path from 'path';
import { FileSystem } from '../filesystem/file-system';
import { ExtensionConfig } from '../types/config';
 
export class WorkspaceIndexer {
  private twigIndex: Map<string, vscode.Uri> = new Map();
  private routeIndex: Map<string, vscode.Location> = new Map();
  private controllerIndex: Map<string, vscode.Location> = new Map();
 
  constructor(
    private fileSystem: FileSystem,
    private config: ExtensionConfig
  ) {}
 
  async indexWorkspace(): Promise<void> {
    if (!this.config.indexing.enabled) return;
 
    const tasks: Promise<void>[] = [];
 
    if (this.config.indexing.eagerIndexTwig) {
      tasks.push(this.indexTwigTemplates());
    }
 
    if (this.config.indexing.indexRoutes) {
      tasks.push(this.indexRoutes());
    }
 
    if (this.config.indexing.indexControllers) {
      tasks.push(this.indexControllers());
    }
 
    await Promise.all(tasks);
  }
 
  async indexTwigTemplates(): Promise<void> {
    const patterns = this.config.twig.fileExtensions
      .map(ext => `**/*${ext}`);
 
    for (const pattern of patterns) {
      const files = await this.fileSystem.findFiles(pattern);
      for (const file of files) {
        await this.indexTwigFile(file);
      }
    }
  }
 
  private async indexTwigFile(uri: vscode.Uri): Promise<void> {
    const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath || '';
    const relativePath = path.relative(workspaceRoot, uri.fsPath);
 
    // Store multiple keys for the same template
    this.twigIndex.set(relativePath, uri);
    this.twigIndex.set(relativePath.replace(/^templates\//, ''), uri);
    this.twigIndex.set(path.basename(uri.fsPath), uri);
  }
 
  async indexRoutes(): Promise<void> {
    await this.indexRoutesFromPhp();
    await this.indexRoutesFromYaml();
  }
 
  private async indexRoutesFromPhp(): Promise<void> {
    const patterns = this.config.symfony.phpFilePatterns;
    for (const pattern of patterns) {
      const files = await this.fileSystem.findFiles(pattern);
      for (const file of files) {
        await this.indexPhpFile(file);
      }
    }
  }
 
  private async indexRoutesFromYaml(): Promise<void> {
    const patterns = this.config.symfony.routeFilePatterns;
    for (const pattern of patterns) {
      const files = await this.fileSystem.findFiles(pattern);
      for (const file of files) {
        await this.indexYamlFile(file);
      }
    }
  }
 
  async indexControllers(): Promise<void> {
    const patterns = this.config.symfony.controllerPatterns;
    for (const pattern of patterns) {
      const files = await this.fileSystem.findFiles(pattern);
      for (const file of files) {
        await this.indexControllerFile(file);
      }
    }
  }
 
  private async indexPhpFile(uri: vscode.Uri): Promise<void> {
    const content = await this.fileSystem.readFile(uri);
    
    // Extract routes from PHP file and add to routeIndex
    await this.extractRoutesFromPhpContent(content, uri);
    
    // Also extract controllers from PHP file and add to controllerIndex
    await this.extractControllerFromPhpContent(content, uri);
  }
 
  private async extractRoutesFromPhpContent(content: string, uri: vscode.Uri): Promise<void> {
    // #[Route('/path', name: 'route_name')] public function myMethod()
    const attrRegex = /#\[\s*Route\s*\(([\s\S]*?)\)\s*\]\s*(?:public|protected|private)?\s*function\s+(\w+)/g;
    let m: RegExpExecArray | null;
    while ((m = attrRegex.exec(content)) !== null) {
      const routeName = this.extractRouteName(m[1]);
      if (routeName) {
        const fnPos = content.indexOf(`function ${m[2]}`, m.index);
        const position = this.positionAt(content, fnPos >= 0 ? fnPos : m.index);
        this.routeIndex.set(routeName, new vscode.Location(uri, position));
      }
    }
 
    // /** @Route("/path", name="route_name") */ public function myMethod()
    const annoRegex = /\/\*\*([\s\S]*?)\*\/\s*(?:public|protected|private)?\s*function\s+(\w+)/g;
    while ((m = annoRegex.exec(content)) !== null) {
      const block = m[1];
      if (!block.includes('@Route')) {
        continue;
      }
      const routeName = this.extractRouteName(block);
      if (routeName) {
        const fnPos = content.indexOf(`function ${m[2]}`, m.index);
        const position = this.positionAt(content, fnPos >= 0 ? fnPos : m.index);
        this.routeIndex.set(routeName, new vscode.Location(uri, position));
      }
    }
  }
 
  private extractRouteName(source: string): string | null {
    const m = /name\s*[:=]\s*(['"])(.*?)\1/.exec(source);
    return m ? m[2] : null;
  }
 
  private positionAt(text: string, offset: number): vscode.Position {
    const before = text.slice(0, offset);
    const lines = before.split(/\r?\n/);
    return new vscode.Position(
      lines.length - 1,
      lines[lines.length - 1]?.length ?? 0
    );
  }
 
  private async indexYamlFile(uri: vscode.Uri): Promise<void> {
    // Extract routes from YAML file and add to routeIndex
    const content = await this.fileSystem.readFile(uri);
    // Parse YAML file for route definitions
    await this.extractRoutesFromYamlContent(content, uri);
  }
 
  private async extractRoutesFromYamlContent(content: string, uri: vscode.Uri): Promise<void> {
    const lines = content.split(/\r?\n/);
    for (let i = 0; i < lines.length; i++) {
      const line = lines[i];
      const trimmed = line.trim();
 
      // Check if this line is a route name (e.g., "route_name:")
      const routeMatch = trimmed.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\s*:/);
      if (!routeMatch) continue;
 
      const routeName = routeMatch[1];
 
      // Found the route key — look for controller in sub-keys
      const baseIndent = (line.match(/^(\s*)/)?.[1] ?? '').length;
      let controllerFqcn: string | undefined;
 
      for (let j = i + 1; j < lines.length; j++) {
        const sub = lines[j];
        if (sub.trim().length === 0) {
          continue;
        }
        const indent = (sub.match(/^(\s*)/)?.[1] ?? '').length;
        if (indent <= baseIndent) {
          break;
        }
 
        // controller: App\Controller\MyController
        const ctrlMatch = sub
          .trim()
          .match(/^controller:\s*['"]?(.+?)['"]?\s*$/);
        if (ctrlMatch) {
          controllerFqcn = ctrlMatch[1];
          break;
        }
 
        // defaults: { _controller: App\Controller\MyController }
        const defMatch = sub
          .trim()
          .match(/^defaults:\s*\{[^}]*_controller:\s*['"]?(.+?)['"]?\s*\}/);
        Iif (defMatch) {
          controllerFqcn = defMatch[1];
          break;
        }
      }
 
      // Resolve the controller FQCN to a PHP file + method
      if (controllerFqcn) {
        const loc = await this.resolvePhpController(controllerFqcn);
        Iif (loc) {
          this.routeIndex.set(routeName, loc);
        } else {
          // Fallback: point to the route key in the YAML file
          const position = new vscode.Position(i, line.indexOf(routeName));
          this.routeIndex.set(routeName, new vscode.Location(uri, position));
        }
      } else {
        // Fallback: point to the route key in the YAML file
        const position = new vscode.Position(i, line.indexOf(routeName));
        this.routeIndex.set(routeName, new vscode.Location(uri, position));
      }
    }
  }
 
  private async resolvePhpController(controller: string): Promise<vscode.Location | null> {
    const parts = controller.split('::');
    const classFqcn = parts[0].replace(/\\\\/g, '\\');
    const methodName = parts[1] ?? '__invoke';
 
    // Search for the PHP file by class short name
    const classShortName = classFqcn.split('\\').pop()!;
    const files = await this.fileSystem.findFiles(
      `**/${classShortName}.php`,
      this.config.watch.excludePatterns.join(','),
      10
    );
 
    for (const file of files) {
      const content = await this.fileSystem.readFile(file);
 
      // Verify namespace + class name match the FQCN
      const nsMatch = /namespace\s+([^;\s]+)/.exec(content);
      const clsMatch = /class\s+(\w+)/.exec(content);
      if (!clsMatch) {
        continue;
      }
 
      const fullName = nsMatch ? `${nsMatch[1]}\\${clsMatch[1]}` : clsMatch[1];
      if (fullName !== classFqcn) {
        continue;
      }
 
      // Find the method
      const methodRegex = new RegExp(`function\\s+${methodName}\\s*\\(`);
      const methodMatch = methodRegex.exec(content);
      if (methodMatch) {
        return new vscode.Location(file, this.positionAt(content, methodMatch.index));
      }
 
      // Method not found — fallback to class declaration
      return new vscode.Location(file, this.positionAt(content, clsMatch.index));
    }
 
    return null;
  }
 
  private async indexControllerFile(uri: vscode.Uri): Promise<void> {
    // Extract controller FQCN and methods from PHP file
    const content = await this.fileSystem.readFile(uri);
    // Parse PHP file for class and method definitions
    await this.extractControllerFromPhpContent(content, uri);
  }
 
  private async extractControllerFromPhpContent(content: string, uri: vscode.Uri): Promise<void> {
    // Extract namespace and class name
    const nsMatch = /namespace\s+([^;\s]+)/.exec(content);
    const clsMatch = /class\s+(\w+)/.exec(content);
    
    Iif (!clsMatch) return;
 
    const className = clsMatch[1];
    const fullName = nsMatch ? `${nsMatch[1]}\\${className}` : className;
 
    // Find all public methods (potential action methods)
    const methodRegex = /(?:public\s+)?function\s+(\w+)\s*\(/g;
    let m: RegExpExecArray | null;
    while ((m = methodRegex.exec(content)) !== null) {
      const methodName = m[1];
      const controllerKey = `${fullName}::${methodName}`;
      this.controllerIndex.set(controllerKey, new vscode.Location(uri, this.positionAt(content, m.index)));
    }
 
    // Also index the class itself (for invokable controllers)
    this.controllerIndex.set(fullName, new vscode.Location(uri, this.positionAt(content, clsMatch.index)));
  }
 
  async findTwigTemplate(templatePath: string): Promise<vscode.Uri | null> {
    const candidates = [
      templatePath,
      templatePath.replace(/^\//, ''),
      path.join('templates', templatePath),
      path.join('templates', templatePath.replace(/^\//, '')),
      path.basename(templatePath),
    ];
 
    for (const candidate of candidates) {
      if (this.twigIndex.has(candidate)) {
        return this.twigIndex.get(candidate)!;
      }
    }
 
    return null;
  }
 
  async findRoute(routeName: string): Promise<vscode.Location | null> {
    return this.routeIndex.get(routeName) || null;
  }
 
  async findController(controllerFqcn: string): Promise<vscode.Location | null> {
    return this.controllerIndex.get(controllerFqcn) || null;
  }
 
  async indexFile(uri: vscode.Uri): Promise<void> {
    if (uri.fsPath.endsWith('.twig')) {
      await this.indexTwigFile(uri);
    } else if (uri.fsPath.endsWith('.php')) {
      await this.indexPhpFile(uri);
    } else Eif (uri.fsPath.match(/\.(yaml|yml)$/)) {
      await this.indexYamlFile(uri);
    }
  }
 
  remove(uri: vscode.Uri): void {
    // Remove from twigIndex
    for (const [key, value] of this.twigIndex.entries()) {
      Eif (value.toString() === uri.toString()) {
        this.twigIndex.delete(key);
      }
    }
 
    // Remove from routeIndex
    for (const [key, value] of this.routeIndex.entries()) {
      if (value.uri.toString() === uri.toString()) {
        this.routeIndex.delete(key);
      }
    }
 
    // Remove from controllerIndex
    for (const [key, value] of this.controllerIndex.entries()) {
      if (value.uri.toString() === uri.toString()) {
        this.controllerIndex.delete(key);
      }
    }
  }
 
  clear(): void {
    this.twigIndex.clear();
    this.routeIndex.clear();
    this.controllerIndex.clear();
  }
 
  getTwigIndexSize(): number {
    return this.twigIndex.size;
  }
 
  getRouteIndexSize(): number {
    return this.routeIndex.size;
  }
 
  getControllerIndexSize(): number {
    return this.controllerIndex.size;
  }
}