|
| 1 | +import type Parser from "tree-sitter"; |
| 2 | +import type { DependencyManifest } from "../../../manifest/dependencyManifest/types.ts"; |
| 3 | +import { JavaInvocationResolver } from "../invocationResolver/index.ts"; |
| 4 | +import { javaParser } from "../../../helpers/treeSitter/parsers.ts"; |
| 5 | +import { JavaPackageMapper } from "../packageMapper/index.ts"; |
| 6 | +import { JavaImportResolver } from "../importResolver/index.ts"; |
| 7 | +import { ExportedFile } from "./types.ts"; |
| 8 | + |
| 9 | +export class JavaExtractor { |
| 10 | + manifest: DependencyManifest; |
| 11 | + resolver: JavaInvocationResolver; |
| 12 | + constructor( |
| 13 | + files: Map<string, { path: string; content: string }>, |
| 14 | + manifest: DependencyManifest, |
| 15 | + ) { |
| 16 | + this.manifest = manifest; |
| 17 | + const parsedFiles = new Map< |
| 18 | + string, |
| 19 | + { path: string; rootNode: Parser.SyntaxNode } |
| 20 | + >(); |
| 21 | + for (const [filepath, file] of files) { |
| 22 | + parsedFiles.set(filepath, { |
| 23 | + path: file.path, |
| 24 | + rootNode: javaParser.parse(file.content).rootNode, |
| 25 | + }); |
| 26 | + } |
| 27 | + const mapper = new JavaPackageMapper(parsedFiles); |
| 28 | + const importresolver = new JavaImportResolver(mapper); |
| 29 | + this.resolver = new JavaInvocationResolver(importresolver); |
| 30 | + } |
| 31 | + |
| 32 | + extractSymbols( |
| 33 | + symbolsMap: Map< |
| 34 | + string, |
| 35 | + { |
| 36 | + filePath: string; |
| 37 | + symbols: Set<string>; |
| 38 | + } |
| 39 | + >, |
| 40 | + ): Map<string, { path: string; content: string }> { |
| 41 | + const exportedfiles = Array.from( |
| 42 | + symbolsMap.keys().map((k) => new ExportedFile(k, this.resolver)), |
| 43 | + ); |
| 44 | + const filesToKeep: Map<string, { path: string; content: string }> = |
| 45 | + new Map(); |
| 46 | + for (const f of exportedfiles) { |
| 47 | + if (!filesToKeep.has(f.file.path)) { |
| 48 | + filesToKeep.set(f.file.path, { |
| 49 | + path: f.file.path, |
| 50 | + content: f.file.rootNode.text, |
| 51 | + }); |
| 52 | + } |
| 53 | + for (const [k, v] of f.dependencies) { |
| 54 | + if (!filesToKeep.has(k)) { |
| 55 | + filesToKeep.set(k, { |
| 56 | + path: v.path, |
| 57 | + content: v.rootNode.text, |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return filesToKeep; |
| 63 | + } |
| 64 | +} |
0 commit comments