'use strict' ; import * as vscode from 'vscode' ; import { ReferenceType , ReferenceInfo , ReferencesResult } from './references' ; export class ReferencesModel { readonly nodes : TreeNode [ ] = [ ] ; private originalSymbol : string = "" ; public groupByFile : boolean ; constructor ( resultsInput , readonly isCanceled , groupByFile , readonly refreshCallback ) { this . originalSymbol = resultsInput . text ; this . groupByFile = groupByFile ; const results = resultsInput . referenceInfos . filter ( r => r . type !== ReferenceType . Confirmed ) ; for ( const r of results ) { const noReferenceLocation = ( r . position . line === 0 && r . position . character === 0 ) ; if ( noReferenceLocation ) { const node = new TreeNode ( this , NodeType . fileWithPendingRef ) ; node . fileUri = vscode . Uri . file ( r . file ) ; node . filename = r . file ; node . referenceType = r . type ; this . nodes . push ( node ) ; } else { const range = new vscode . Range ( r . position . line , r . position . character , r . position . line , r . position . character + this . originalSymbol . length ) ; const uri = vscode . Uri . file ( r . file ) ; const location = new vscode . Location ( uri , range ) ; const node = new TreeNode ( this , NodeType . reference ) ; node . fileUri = uri ; node . filename = r . file ; node . referencePosition = r . position ; node . referenceLocation = location ; node . referenceText = r . text ; node . referenceType = r . type ; this . nodes . push ( node ) ; } } } hasResults ( ) { return this . nodes . length > 0 ; } getReferenceTypeNodes ( ) { const result = [ ] ; for ( const n of this . nodes ) { const i = result . findIndex ( e => e . referenceType === n . referenceType ) ; if ( i < 0 ) { const node = new TreeNode ( this , NodeType . referenceType ) ; node . referenceType = n . referenceType ; result . push ( node ) ; } } return result ; } getFileNodes ( refType ? ) { const result = [ ] ; let filteredFiles = [ ] ; if ( refType !== undefined ) { filteredFiles = this . nodes . filter ( i => i . referenceType === refType ) ; } else { filteredFiles = this . nodes ; } for ( const n of filteredFiles ) { const i = result . findIndex ( item => item . filename === n . filename ) ; if ( i < 0 ) { const nodeType = ( n . node === NodeType . fileWithPendingRef ? NodeType . fileWithPendingRef : NodeType . file ) ; const node = new TreeNode ( this , nodeType ) ; node . filename = n . filename ; node . fileUri = n . fileUri ; node . referenceType = refType ; result . push ( node ) ; } } result . sort ( ( a , b ) => { if ( a . filename === undefined ) { if ( b . filename === undefined ) { return 0 ; } else { return - 1 ; } } else if ( b . filename === undefined ) { return 1 ; } else { return a . filename . localeCompare ( b . filename ) ; } } ) ; return result ; } getReferenceNodes ( filename ? , refType ? ) { if ( refType === undefined || refType === null ) { if ( filename === undefined || filename === null ) { return this . nodes ; } return this . nodes . filter ( i => i . filename === filename ) ; } if ( filename === undefined || filename === null ) { return this . nodes . filter ( i => i . referenceType === refType ) ; } return this . nodes . filter ( i => i . filename === filename && i . referenceType === refType ) ; } getAllReferenceNodes ( ) { return this . nodes . filter ( i => i . node === NodeType . reference ) ; } getAllFilesWithPendingReferenceNodes ( ) { const result = this . nodes . filter ( i => i . node === NodeType . fileWithPendingRef ) ; result . sort ( ( a , b ) => { if ( a . filename === undefined ) { if ( b . filename === undefined ) { return 0 ; } else { return - 1 ; } } else if ( b . filename === undefined ) { return 1 ; } else { return a . filename . localeCompare ( b . filename ) ; } } ) ; return result ; } } export enum NodeType { undefined , referenceType , file , fileWithPendingRef , reference } export class TreeNode { public filename ? : string ; public fileUri ? : vscode . Uri ; public referencePosition ? : vscode . Position ; public referenceLocation ? : vscode . Location ; public referenceText ? : string ; public referenceType ? : ReferenceType ; constructor ( readonly model , readonly node ) { } }