|
1 | 1 | import MapParser from '../parsers/MapParser.js'; |
| 2 | +import IncludeResolver from '../utils/IncludeResolver.js'; |
| 3 | +import fs from 'fs'; |
2 | 4 |
|
3 | 5 | /** |
4 | 6 | * Singleton service for tracking Map variables across workspace |
5 | 7 | */ |
6 | 8 | class MapTrackingService { |
7 | | - constructor() { |
| 9 | + constructor(workspaceRoot = '', autoitIncludePaths = [], maxIncludeDepth = 3) { |
8 | 10 | if (MapTrackingService.instance) { |
9 | 11 | return MapTrackingService.instance; |
10 | 12 | } |
11 | 13 |
|
12 | 14 | this.fileParsers = new Map(); // filePath -> MapParser |
| 15 | + this.includeResolver = new IncludeResolver(workspaceRoot, autoitIncludePaths, maxIncludeDepth); |
| 16 | + this.workspaceRoot = workspaceRoot; |
13 | 17 | MapTrackingService.instance = this; |
14 | 18 | } |
15 | 19 |
|
16 | 20 | /** |
17 | 21 | * Get singleton instance |
18 | 22 | * @returns {MapTrackingService} |
19 | 23 | */ |
20 | | - static getInstance() { |
| 24 | + static getInstance(workspaceRoot, autoitIncludePaths, maxIncludeDepth) { |
21 | 25 | if (!MapTrackingService.instance) { |
22 | | - MapTrackingService.instance = new MapTrackingService(); |
| 26 | + MapTrackingService.instance = new MapTrackingService( |
| 27 | + workspaceRoot, |
| 28 | + autoitIncludePaths, |
| 29 | + maxIncludeDepth, |
| 30 | + ); |
23 | 31 | } |
24 | 32 | return MapTrackingService.instance; |
25 | 33 | } |
@@ -64,6 +72,49 @@ class MapTrackingService { |
64 | 72 |
|
65 | 73 | return parser.getKeysForMapAtLine(mapName, line); |
66 | 74 | } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get keys for a Map including keys from #include files |
| 78 | + * @param {string} filePath - Absolute file path |
| 79 | + * @param {string} mapName - Map variable name |
| 80 | + * @param {number} line - Line number |
| 81 | + * @returns {object} Object with directKeys and functionKeys arrays |
| 82 | + */ |
| 83 | + getKeysForMapWithIncludes(filePath, mapName, line) { |
| 84 | + // Get keys from current file |
| 85 | + const currentKeys = this.getKeysForMap(filePath, mapName, line); |
| 86 | + |
| 87 | + // Get keys from included files |
| 88 | + const includedFiles = this.includeResolver.resolveAllIncludes(filePath); |
| 89 | + const allDirectKeys = new Set(currentKeys.directKeys); |
| 90 | + const allFunctionKeys = [...currentKeys.functionKeys]; |
| 91 | + |
| 92 | + for (const includedFile of includedFiles) { |
| 93 | + // Parse included file if not already cached |
| 94 | + if (!this.fileParsers.has(includedFile)) { |
| 95 | + try { |
| 96 | + if (fs.existsSync(includedFile)) { |
| 97 | + const source = fs.readFileSync(includedFile, 'utf8'); |
| 98 | + this.updateFile(includedFile, source); |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + // Gracefully handle read errors |
| 102 | + continue; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + const includedKeys = this.getKeysForMap(includedFile, mapName, Infinity); |
| 107 | + |
| 108 | + // Merge keys |
| 109 | + includedKeys.directKeys.forEach(key => allDirectKeys.add(key)); |
| 110 | + allFunctionKeys.push(...includedKeys.functionKeys); |
| 111 | + } |
| 112 | + |
| 113 | + return { |
| 114 | + directKeys: Array.from(allDirectKeys), |
| 115 | + functionKeys: allFunctionKeys, |
| 116 | + }; |
| 117 | + } |
67 | 118 | } |
68 | 119 |
|
69 | 120 | export default MapTrackingService; |
0 commit comments