11import { SymbolKind , TextDocument } from 'vscode' ;
2- import { METHOD_KINDS , getClassesAndMethodsOfDocument , logger } from '../utils' ;
2+ import { METHOD_KINDS , getClassesAndMethodsContainedInRange , getClassesAndMethodsOfDocument , logger } from '../utils' ;
33import { Inspection } from './Inspection' ;
44import * as crypto from "crypto" ;
55import { SymbolNode } from './SymbolNode' ;
@@ -11,20 +11,37 @@ import { SymbolNode } from './SymbolNode';
1111const DOC_SYMBOL_VERSION_INSPECTIONS : Map < string , Map < string , [ string , Inspection [ ] ] > > = new Map ( ) ;
1212
1313export default class InspectionCache {
14- public static hasCache ( document : TextDocument , symbol ?: SymbolNode ) : boolean {
14+ /**
15+ * check if the document or the symbol is cached.
16+ * if the symbol is provided, check if the symbol or its contained symbols are cached.
17+ */
18+ public static async hasCache ( document : TextDocument , symbol ?: SymbolNode ) : Promise < boolean > {
1519 const documentKey = document . uri . fsPath ;
1620 if ( ! symbol ) {
1721 return DOC_SYMBOL_VERSION_INSPECTIONS . has ( documentKey ) ;
1822 }
1923 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 ;
24+ // check if the symbol or its contained symbols are cached
25+ const symbols = await getClassesAndMethodsContainedInRange ( symbol . range , document ) ;
26+ for ( const s of symbols ) {
27+ const versionInspections = symbolInspections ?. get ( s . qualifiedName ) ;
28+ const symbolVersionId = InspectionCache . calculateSymbolVersionId ( document , s ) ;
29+ if ( versionInspections ?. [ 0 ] === symbolVersionId ) {
30+ return true ;
31+ }
32+ }
33+ return false ;
2334 }
2435
36+ /**
37+ * Get cached inspections of a document, if the document is not cached, return an empty array.
38+ * Cached inspections of outdated symbols are filtered out.Symbols are considered outdated if
39+ * their content has changed.
40+ */
2541 public static async getCachedInspectionsOfDoc ( document : TextDocument ) : Promise < Inspection [ ] > {
2642 const symbols : SymbolNode [ ] = await getClassesAndMethodsOfDocument ( document ) ;
2743 const inspections : Inspection [ ] = [ ] ;
44+ // we don't get cached inspections directly from the cache, because we need to filter out outdated symbols
2845 for ( const symbol of symbols ) {
2946 const cachedInspections = InspectionCache . getCachedInspectionsOfSymbol ( document , symbol ) ;
3047 inspections . push ( ...cachedInspections ) ;
@@ -109,6 +126,10 @@ export default class InspectionCache {
109126 const documentKey = document . uri . fsPath ;
110127 const symbolVersionId = InspectionCache . calculateSymbolVersionId ( document , symbol ) ;
111128 const cachedSymbolInspections = DOC_SYMBOL_VERSION_INSPECTIONS . get ( documentKey ) ?? new Map ( ) ;
129+ inspections . forEach ( s => {
130+ s . document = document ;
131+ s . symbol = symbol ;
132+ } ) ;
112133 // use qualified name to prevent conflicts between symbols with the same signature in same document
113134 cachedSymbolInspections . set ( symbol . qualifiedName , [ symbolVersionId , inspections ] ) ;
114135 DOC_SYMBOL_VERSION_INSPECTIONS . set ( documentKey , cachedSymbolInspections ) ;
0 commit comments