-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileToLineAggregation.service.ts
More file actions
203 lines (175 loc) · 7.43 KB
/
Copy pathfileToLineAggregation.service.ts
File metadata and controls
203 lines (175 loc) · 7.43 KB
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
import * as path from 'path';
import * as fs from 'fs';
import { ActiveHistoryItem, FileToLineAggregation } from '../types';
import { CsvReaderService } from './csvReader.service';
import { PathResolverService } from './pathResolver.service';
/**
* Service for aggregating trace links from code files to documentation lines
* Includes directory heuristic: if a directory has links, all files in that directory inherit them
*/
export class FileToLineAggregationService {
/**
* Aggregates links from active history items by code file/directory
* Includes directory heuristic: files inherit links from their parent directories
* @param items Active history items with CSV files
* @param workspaceRoot Root path of the workspace for resolving relative paths
* @returns File-to-line aggregation map
*/
static aggregateFileToLines(items: ActiveHistoryItem[], workspaceRoot: string): FileToLineAggregation {
const agg: FileToLineAggregation = {};
// First pass: collect direct file/directory links
for (const item of items) {
const rows = CsvReaderService.readCsv(item.csvPath);
for (const row of rows) {
const codePath = this.normalizePath(row.codeID, workspaceRoot);
if (!codePath) {
continue;
}
if (!agg[codePath]) {
agg[codePath] = [];
}
agg[codePath].push({
sentenceID: row.sentenceID,
color: item.color,
sourceId: item.id
});
}
}
// Second pass: apply directory heuristic
// For each file in the workspace, check if any parent directory has links
this.applyDirectoryHeuristic(agg, workspaceRoot);
return agg;
}
/**
* Normalizes a code path to an absolute path
*/
private static normalizePath(codePath: string, workspaceRoot: string): string | undefined {
if (!codePath) {
return undefined;
}
// If already absolute, return as-is
if (path.isAbsolute(codePath)) {
return path.normalize(codePath);
}
// If relative, resolve against workspace root
return path.normalize(path.resolve(workspaceRoot, codePath));
}
/**
* Applies directory heuristic: files inherit links from their parent directories
*/
private static applyDirectoryHeuristic(agg: FileToLineAggregation, workspaceRoot: string): void {
// Get all directories that have links
const directoriesWithLinks = new Set<string>();
for (const codePath of Object.keys(agg)) {
// Check if path exists and is a directory
if (fs.existsSync(codePath)) {
try {
const stat = fs.statSync(codePath);
if (stat.isDirectory()) {
directoriesWithLinks.add(path.normalize(codePath));
}
} catch {
// Ignore errors, try heuristic
}
}
// Fallback to heuristic if path doesn't exist
if (!directoriesWithLinks.has(path.normalize(codePath))) {
const pathInfo = PathResolverService.resolvePathType(codePath);
if (pathInfo.isDir) {
directoriesWithLinks.add(path.normalize(codePath));
}
}
}
// For each directory with links, find all files in that directory
for (const dirPath of directoriesWithLinks) {
if (!fs.existsSync(dirPath)) {
continue;
}
const links = agg[dirPath];
if (!links || links.length === 0) {
continue;
}
// Recursively find all files in this directory
const filesInDir = this.getAllFilesInDirectory(dirPath);
for (const filePath of filesInDir) {
const normalizedFile = path.normalize(filePath);
// Don't overwrite existing direct links, but merge directory links
if (!agg[normalizedFile]) {
agg[normalizedFile] = [];
}
// Add directory links that aren't already present
for (const link of links) {
const exists = agg[normalizedFile].some(
existing => existing.sentenceID === link.sentenceID &&
existing.color === link.color &&
existing.sourceId === link.sourceId
);
if (!exists) {
agg[normalizedFile].push({ ...link });
}
}
}
}
}
/**
* Recursively gets all files in a directory
*/
private static getAllFilesInDirectory(dirPath: string): string[] {
const files: string[] = [];
try {
const entries = fs.readdirSync(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dirPath, entry.name);
if (entry.isFile()) {
files.push(fullPath);
} else if (entry.isDirectory()) {
// Recursively get files from subdirectories
files.push(...this.getAllFilesInDirectory(fullPath));
}
}
} catch {
// Ignore errors (permissions, etc.)
}
return files;
}
/**
* Gets trace links for a specific file, including directory inheritance
* Checks the file itself and all parent directories
* @param filePath Absolute path to the file
* @param aggregation File-to-line aggregation map
* @returns Array of linked documentation lines
*/
static getLinksForFile(filePath: string, aggregation: FileToLineAggregation): Array<{ sentenceID: number; color: string; sourceId: string }> {
const normalizedPath = path.normalize(filePath);
const links: Array<{ sentenceID: number; color: string; sourceId: string }> = [];
const seen = new Set<string>();
// First, get direct links for the file
const directLinks = aggregation[normalizedPath] || [];
for (const link of directLinks) {
const key = `${link.sentenceID}-${link.color}-${link.sourceId}`;
if (!seen.has(key)) {
seen.add(key);
links.push(link);
}
}
// Then, check all parent directories
let currentDir = path.dirname(normalizedPath);
const rootDir = path.parse(normalizedPath).root;
while (currentDir !== rootDir && currentDir !== normalizedPath) {
const dirLinks = aggregation[currentDir] || [];
for (const link of dirLinks) {
const key = `${link.sentenceID}-${link.color}-${link.sourceId}`;
if (!seen.has(key)) {
seen.add(key);
links.push(link);
}
}
const parentDir = path.dirname(currentDir);
if (parentDir === currentDir) {
break; // Reached root
}
currentDir = parentDir;
}
return links;
}
}