forked from eclipse-theia/theia
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
preferences: fix duplicated resolution
The `WorkspacePreferenceProvider` can do one of two things: It may return preferences from the current workspace folder if the workspace is a single root workspace, or it may return preferences as defined in the workspace file if the workspace is a multi-root workspace. Whenever we want to resolve a preference, the following scopes are queried: `Default`, `User`, `Workspace`, and `Folder`. When a single root workspace is opened the following happens: - Query `Default` scope [...] - Query `User` scope [...] - Query `Workspace` scope: use `WorkspacePreferenceProvider` which delegates to `FolderPreferenceProvider`. - Query `Folder` scope: use `FoldersPreferenceProvider`. Conclusion: The `FoldersPreferenceProvider` is queried twice. In order to fix that issue this commit introduces a new component: `TogglePreferenceProvider` which extends `PreferenceProvider` and wraps another provider so that it can be enabled/disabled. Whenever we are running a single root workspace, we'll enable the `FoldersPreferenceProvider` used by `WorkspacePreferenceProvider` and disable the one bound to the `Folder` scope. Whenever we are running a multi root workspace, we'll disable the `FoldersPreferenceProvider` used by `WorkspacePreferenceProvider` and enable the one bound to the `Folder` scope.
- Loading branch information
1 parent
6959b6a
commit 120d1a3
Showing
5 changed files
with
185 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
packages/core/src/browser/preferences/toggle-preference-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2023 Ericsson 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 | ||
// ***************************************************************************** | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { URI } from '../../common'; | ||
import { PreferenceProvider, PreferenceProviderDataChanges, PreferenceResolveResult } from './preference-provider'; | ||
|
||
/** | ||
* Allows enabling/disabling a {@link PreferenceProvider} by wrapping it. | ||
*/ | ||
export class TogglePreferenceProvider extends PreferenceProvider { | ||
|
||
#enabled: boolean; | ||
|
||
constructor( | ||
enabled: boolean, | ||
protected provider: PreferenceProvider | ||
) { | ||
super(); | ||
this.#enabled = enabled; | ||
this.provider.ready.then(() => this._ready.resolve()); | ||
this.provider.onDidPreferencesChanged(this.handleDidPreferencesChanged, this, this.toDispose); | ||
} | ||
|
||
get enabled(): boolean { | ||
return this.#enabled; | ||
} | ||
|
||
set enabled(value: boolean) { | ||
if (this.#enabled !== value) { | ||
this.#enabled = value; | ||
this.onDidPreferencesChangedEmitter.fire({}); | ||
} | ||
} | ||
|
||
getPreferences(resourceUri?: string): { [p: string]: any; } { | ||
if (this.enabled) { | ||
return this.provider.getPreferences(resourceUri); | ||
} | ||
return {}; | ||
} | ||
|
||
async setPreference(key: string, value: any, resourceUri?: string): Promise<boolean> { | ||
if (this.enabled) { | ||
return this.provider.setPreference(key, value, resourceUri); | ||
} | ||
return false; | ||
} | ||
|
||
override get<T>(preferenceName: string, resourceUri?: string): T | undefined { | ||
if (this.enabled) { | ||
return this.provider.get<T>(preferenceName, resourceUri); | ||
} | ||
} | ||
|
||
override resolve<T>(preferenceName: string, resourceUri?: string): PreferenceResolveResult<T> { | ||
if (this.enabled) { | ||
return this.provider.resolve<T>(preferenceName, resourceUri); | ||
} | ||
return {}; | ||
} | ||
|
||
override getDomain(): string[] | undefined { | ||
if (this.enabled) { | ||
return this.provider.getDomain(); | ||
} | ||
} | ||
|
||
override getConfigUri(resourceUri?: string, sectionName?: string): URI | undefined { | ||
if (this.enabled) { | ||
return this.provider.getConfigUri(resourceUri, sectionName); | ||
} | ||
} | ||
|
||
override getContainingConfigUri?(resourceUri?: string, sectionName?: string): URI | undefined { | ||
if (this.enabled) { | ||
return this.provider.getContainingConfigUri?.(resourceUri, sectionName); | ||
} | ||
} | ||
|
||
override dispose(): void { | ||
super.dispose(); | ||
this.provider.dispose(); | ||
} | ||
|
||
protected handleDidPreferencesChanged(event: PreferenceProviderDataChanges): void { | ||
if (this.enabled) { | ||
this.onDidPreferencesChangedEmitter.fire(event); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters