/********************************************************************************
 * Copyright (C) 2019 TypeFox 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 { inject , injectable } from '@theia/core/shared/inversify' ; import URI from '@theia/core/lib/common/uri' ; import { PreferenceScope } from '@theia/core/lib/browser/preferences' ; import { WorkspaceService , WorkspaceData } from '@theia/workspace/lib/browser/workspace-service' ; import { AbstractResourcePreferenceProvider } from './abstract-resource-preference-provider' ; @ injectable ( ) export class WorkspaceFilePreferenceProviderOptions { workspaceUri : URI ; } export const WorkspaceFilePreferenceProviderFactory = Symbol ( 'WorkspaceFilePreferenceProviderFactory' ) ; export type WorkspaceFilePreferenceProviderFactory = ( options : WorkspaceFilePreferenceProviderOptions ) => WorkspaceFilePreferenceProvider ; @ injectable ( ) export class WorkspaceFilePreferenceProvider extends AbstractResourcePreferenceProvider { @ inject ( WorkspaceService ) protected readonly workspaceService : WorkspaceService ; @ inject ( WorkspaceFilePreferenceProviderOptions ) protected readonly options : WorkspaceFilePreferenceProviderOptions ; protected sectionsInsideSettings = new Set < string > ( ) ; protected getUri ( ) { return this . options . workspaceUri ; } protected parse ( content ) { const data = super . parse ( content ) ; if ( WorkspaceData . is ( data ) ) { const settings = { ... data . settings } ; for ( const key of this . configurations . getSectionNames ( ) . filter ( name => name !== 'settings' ) ) { if ( settings [ key ] ) { this . sectionsInsideSettings . add ( key ) ; } if ( data [ key ] ) { settings [ key ] = data [ key ] ; this . sectionsInsideSettings . delete ( key ) ; } } return settings ; } return { } ; } protected getPath ( preferenceName ) { const firstSegment = preferenceName . split ( '.' , 1 ) [ 0 ] ; const remainder = preferenceName . slice ( firstSegment . length + 1 ) ; if ( this . belongsInSection ( firstSegment , remainder ) ) { const path = [ firstSegment ] ; if ( remainder ) { path . push ( remainder ) ; } if ( this . sectionsInsideSettings . has ( firstSegment ) ) { path . unshift ( 'settings' ) ; } return path ; } return [ 'settings' , preferenceName ] ; } protected belongsInSection ( firstSegment , remainder ) { return this . configurations . isSectionName ( firstSegment ) ; } protected getScope ( ) { return PreferenceScope . Workspace ; } getDomain ( ) { return this . workspaceService . tryGetRoots ( ) . map ( r => r . resource . toString ( ) ) . concat ( [ this . options . workspaceUri . toString ( ) ] ) ; } }