/********************************************************************************
 * Copyright (C) 2018 Red Hat, Inc. and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the Eclipse
 * Public License v. 2.0 are satisfied: GNU General Public License, version 2
 * with the GNU Classpath Exception which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 ********************************************************************************/ import { URI } from '@theia/core/shared/vscode-uri' ; import * as theia from '@theia/plugin' ; import { DocumentsExtImpl } from '../documents' ; import * as Converter from '../type-converters' ; import { Position } from '../../common/plugin-api-rpc' ; import { SignatureHelp , SignatureHelpContext } from '../../common/plugin-api-rpc-model' ; export class SignatureHelpAdapter { private idSequence = 1 ; private readonly cache = new Map < number , theia . SignatureHelp > ( ) ; constructor ( private readonly delegate , private readonly documents ) { } async provideSignatureHelp ( resource , position , token , context ) { const documentData = this . documents . getDocumentData ( resource ) ; if ( ! documentData ) { return Promise . reject ( new Error ( `There are no document for  ${ resource }` ) ) ; } const document = documentData . document ; const zeroBasedPosition = Converter . toPosition ( position ) ; const pluginHelpContext = this . reviveContext ( context ) ; const value = await this . delegate . provideSignatureHelp ( document , zeroBasedPosition , token , pluginHelpContext ) ; if ( ! value ) { return undefined ; } const id = this . idSequence ++ ; this . cache . set ( id , value ) ; return Converter . SignatureHelp . from ( id , value ) ; } private reviveContext ( context ) { let activeSignatureHelp = undefined ; if ( context . activeSignatureHelp ) { const revivedSignatureHelp = Converter . SignatureHelp . to ( context . activeSignatureHelp ) ; const saved = typeof context . activeSignatureHelp . id === 'number' && this . cache . get ( context . activeSignatureHelp . id ) ; if ( saved ) { activeSignatureHelp = saved ; activeSignatureHelp . activeSignature = revivedSignatureHelp . activeSignature ; activeSignatureHelp . activeParameter = revivedSignatureHelp . activeParameter ; } else { activeSignatureHelp = revivedSignatureHelp ; } } return { ... context , activeSignatureHelp } ; } releaseSignatureHelp ( id ) { this . cache . delete ( id ) ; } }