|
| 1 | +import { SymbolKind, TextDocument } from 'vscode'; |
| 2 | +import { METHOD_KINDS, getClassesAndMethodsOfDocument, logger } from '../utils'; |
| 3 | +import { Inspection } from './Inspection'; |
| 4 | +import * as crypto from "crypto"; |
| 5 | +import { SymbolNode } from './SymbolNode'; |
| 6 | + |
| 7 | +/** |
| 8 | + * A map based cache for inspections of a document. |
| 9 | + * format: `Map<documentKey, Map<symbolQualifiedName, [symbolVersionId, Inspection[]]` |
| 10 | + */ |
| 11 | +const DOC_SYMBOL_VERSION_INSPECTIONS: Map<string, Map<string, [string, Inspection[]]>> = new Map(); |
| 12 | + |
| 13 | +export default class InspectionCache { |
| 14 | + public static hasCache(document: TextDocument, symbol?: SymbolNode): boolean { |
| 15 | + const documentKey = document.uri.fsPath; |
| 16 | + if (!symbol) { |
| 17 | + return DOC_SYMBOL_VERSION_INSPECTIONS.has(documentKey); |
| 18 | + } |
| 19 | + const symbolInspections = DOC_SYMBOL_VERSION_INSPECTIONS.get(documentKey); |
| 20 | + const versionInspections = symbolInspections?.get(symbol.qualifiedName); |
| 21 | + const symbolVersionId = InspectionCache.calculateSymbolVersionId(document, symbol); |
| 22 | + return versionInspections?.[0] === symbolVersionId; |
| 23 | + } |
| 24 | + |
| 25 | + public static async getCachedInspectionsOfDoc(document: TextDocument): Promise<Inspection[]> { |
| 26 | + const symbols: SymbolNode[] = await getClassesAndMethodsOfDocument(document); |
| 27 | + const inspections: Inspection[] = []; |
| 28 | + for (const symbol of symbols) { |
| 29 | + const cachedInspections = InspectionCache.getCachedInspectionsOfSymbol(document, symbol); |
| 30 | + inspections.push(...cachedInspections); |
| 31 | + } |
| 32 | + return inspections; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @returns the cached inspections, or undefined if not found |
| 37 | + */ |
| 38 | + public static getCachedInspectionsOfSymbol(document: TextDocument, symbol: SymbolNode): Inspection[] { |
| 39 | + const documentKey = document.uri.fsPath; |
| 40 | + const symbolInspections = DOC_SYMBOL_VERSION_INSPECTIONS.get(documentKey); |
| 41 | + const versionInspections = symbolInspections?.get(symbol.qualifiedName); |
| 42 | + const symbolVersionId = InspectionCache.calculateSymbolVersionId(document, symbol); |
| 43 | + if (versionInspections?.[0] === symbolVersionId) { |
| 44 | + logger.debug(`cache hit for ${SymbolKind[symbol.kind]} ${symbol.qualifiedName} of ${document.uri.fsPath}`); |
| 45 | + const inspections = versionInspections[1]; |
| 46 | + inspections.forEach(s => { |
| 47 | + s.document = document; |
| 48 | + s.problem.position.line = s.problem.position.relativeLine + symbol.range.start.line; |
| 49 | + }); |
| 50 | + return inspections; |
| 51 | + } |
| 52 | + logger.debug(`cache miss for ${SymbolKind[symbol.kind]} ${symbol.qualifiedName} of ${document.uri.fsPath}`); |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + public static cache(document: TextDocument, symbols: SymbolNode[], inspections: Inspection[]): void { |
| 57 | + for (const symbol of symbols) { |
| 58 | + const isMethod = METHOD_KINDS.includes(symbol.kind); |
| 59 | + const symbolInspections: Inspection[] = inspections.filter(inspection => { |
| 60 | + const inspectionLine = inspection.problem.position.line; |
| 61 | + return isMethod ? |
| 62 | + // NOTE: method inspections are inspections whose `position.line` is within the method's range |
| 63 | + inspectionLine >= symbol.range.start.line && inspectionLine <= symbol.range.end.line : |
| 64 | + // NOTE: class inspections are inspections whose `position.line` is exactly the first line number of the class |
| 65 | + inspectionLine === symbol.range.start.line; |
| 66 | + }); |
| 67 | + // re-calculate `relativeLine` of method inspections, `relativeLine` is the relative line number to the start of the method |
| 68 | + symbolInspections.forEach(inspection => inspection.problem.position.relativeLine = inspection.problem.position.line - symbol.range.start.line); |
| 69 | + InspectionCache.cacheSymbolInspections(document, symbol, symbolInspections); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * invalidate the cache of a document, a symbol, or an inspection. |
| 75 | + * NOTE: the cached inspections of the symbol and its contained symbols will be removed when invalidating a symbol. |
| 76 | + */ |
| 77 | + public static invalidateInspectionCache(document?: TextDocument, symbol?: SymbolNode, inspeciton?: Inspection): void { |
| 78 | + if (!document) { |
| 79 | + DOC_SYMBOL_VERSION_INSPECTIONS.clear(); |
| 80 | + } else if (!symbol) { |
| 81 | + const documentKey = document.uri.fsPath; |
| 82 | + DOC_SYMBOL_VERSION_INSPECTIONS.delete(documentKey); |
| 83 | + } else if (!inspeciton) { |
| 84 | + const documentKey = document.uri.fsPath; |
| 85 | + const symbolInspections = DOC_SYMBOL_VERSION_INSPECTIONS.get(documentKey); |
| 86 | + // remove the cached inspections of the symbol |
| 87 | + symbolInspections?.delete(symbol.qualifiedName); |
| 88 | + // remove the cached inspections of contained symbols |
| 89 | + symbolInspections?.forEach((_, key) => { |
| 90 | + if (key.startsWith(symbol.qualifiedName)) { |
| 91 | + symbolInspections.delete(key); |
| 92 | + } |
| 93 | + }); |
| 94 | + } else { |
| 95 | + const documentKey = document.uri.fsPath; |
| 96 | + const symbolInspections = DOC_SYMBOL_VERSION_INSPECTIONS.get(documentKey); |
| 97 | + const versionInspections = symbolInspections?.get(symbol.qualifiedName); |
| 98 | + const symbolVersionId = InspectionCache.calculateSymbolVersionId(document, symbol); |
| 99 | + if (versionInspections?.[0] === symbolVersionId) { |
| 100 | + const inspections = versionInspections[1]; |
| 101 | + // remove the inspection |
| 102 | + inspections.splice(inspections.indexOf(inspeciton), 1); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static cacheSymbolInspections(document: TextDocument, symbol: SymbolNode, inspections: Inspection[]): void { |
| 108 | + logger.debug(`cache ${inspections.length} inspections for ${SymbolKind[symbol.kind]} ${symbol.qualifiedName} of ${document.uri.fsPath}`); |
| 109 | + const documentKey = document.uri.fsPath; |
| 110 | + const symbolVersionId = InspectionCache.calculateSymbolVersionId(document, symbol); |
| 111 | + const cachedSymbolInspections = DOC_SYMBOL_VERSION_INSPECTIONS.get(documentKey) ?? new Map(); |
| 112 | + // use qualified name to prevent conflicts between symbols with the same signature in same document |
| 113 | + cachedSymbolInspections.set(symbol.qualifiedName, [symbolVersionId, inspections]); |
| 114 | + DOC_SYMBOL_VERSION_INSPECTIONS.set(documentKey, cachedSymbolInspections); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * generate a unique id for the symbol based on its content, so that we can detect if the symbol has changed |
| 119 | + */ |
| 120 | + private static calculateSymbolVersionId(document: TextDocument, symbol: SymbolNode): string { |
| 121 | + const body = document.getText(symbol.range); |
| 122 | + return crypto.createHash('md5').update(body).digest("hex") |
| 123 | + } |
| 124 | +} |
0 commit comments