import * as vscode from 'vscode' ; import { DefaultClient , LocalizeDocumentSymbol , GetDocumentSymbolRequestParams , GetDocumentSymbolRequest , SymbolScope } from '../client' ; import * as util from '../../common' ; import { processDelayedDidOpen } from '../extension' ; export class DocumentSymbolProvider implements vscode . DocumentSymbolProvider { private client : DefaultClient ; constructor ( client ) { this . client = client ; } private getChildrenSymbols ( symbols ) { const documentSymbols = [ ] ; if ( symbols ) { symbols . forEach ( ( symbol ) => { let detail = util . getLocalizedString ( symbol . detail ) ; if ( symbol . scope === SymbolScope . Private ) { if ( detail . length === 0 ) { detail = "private" ; } else { detail = util . getLocalizedSymbolScope ( "private" , detail ) ; } } else if ( symbol . scope === SymbolScope . Protected ) { if ( detail . length === 0 ) { detail = "protected" ; } else { detail = util . getLocalizedSymbolScope ( "protected" , detail ) ; } } if ( detail . length === 0 ) { let offset_paren = symbol . name . indexOf ( "(" ) ; if ( offset_paren < 0 ) { offset_paren = symbol . name . length ; } const offset_scope = symbol . name . lastIndexOf ( "::" , offset_paren - 2 ) ; if ( offset_scope > 0 ) { detail = symbol . name . substr ( 0 , offset_scope ) ; symbol . name = symbol . name . substr ( offset_scope + 2 ) ; } } const r = new vscode . Range ( symbol . range . start . line , symbol . range . start . character , symbol . range . end . line , symbol . range . end . character ) ; const sr = new vscode . Range ( symbol . selectionRange . start . line , symbol . selectionRange . start . character , symbol . selectionRange . end . line , symbol . selectionRange . end . character ) ; const vscodeSymbol = new vscode . DocumentSymbol ( symbol . name , detail , symbol . kind , r , sr ) ; vscodeSymbol . children = this . getChildrenSymbols ( symbol . children ) ; documentSymbols . push ( vscodeSymbol ) ; } ) ; } return documentSymbols ; } public async provideDocumentSymbols ( document ) { if ( ! this . client . TrackedDocuments . has ( document ) ) { processDelayedDidOpen ( document ) ; } return this . client . requestWhenReady ( async ( ) => { const params = { uri : document . uri . toString ( ) } ; const symbols = await this . client . languageClient . sendRequest ( GetDocumentSymbolRequest , params ) ; const resultSymbols = this . getChildrenSymbols ( symbols ) ; return resultSymbols ; } ) ; } }