-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Nicholas Stenbeck <[email protected]> Co-authored-by: Colin Grant <[email protected]> Co-authored-by: Kenneth Marut<[email protected]> Signed-off-by: Nicholas Stenbeck <[email protected]> Signed-off-by: Colin Grant <[email protected]> Signed-off-by: Kenneth Marut <[email protected]>
- Loading branch information
Nicholas Stenbeck
authored and
egocalr
committed
Apr 30, 2020
1 parent
fd0ace8
commit cf5f150
Showing
34 changed files
with
2,734 additions
and
1,053 deletions.
There are no files selected for viewing
185 changes: 185 additions & 0 deletions
185
packages/preferences/src/browser/preference-contribution.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,185 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2018 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 | ||
********************************************************************************/ | ||
|
||
import { injectable, inject, postConstruct, named } from 'inversify'; | ||
import { MenuModelRegistry, CommandRegistry } from '@theia/core'; | ||
import { | ||
CommonMenus, | ||
AbstractViewContribution, | ||
CommonCommands, | ||
KeybindingRegistry, | ||
Widget, | ||
PreferenceScope, | ||
PreferenceProvider, | ||
PreferenceService, | ||
PreferenceItem | ||
} from '@theia/core/lib/browser'; | ||
import { isFirefox } from '@theia/core/lib/browser'; | ||
import { isOSX } from '@theia/core/lib/common/os'; | ||
import { TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar'; | ||
import { FileSystem } from '@theia/filesystem/lib/common'; | ||
import { EditorManager, EditorWidget } from '@theia/editor/lib/browser'; | ||
import URI from '@theia/core/lib/common/uri'; | ||
import { PreferencesWidget } from './views/preference-widget'; | ||
import { PreferencesEventService } from './util/preference-event-service'; | ||
import { WorkspacePreferenceProvider } from './workspace-preference-provider'; | ||
import { USER_PREFERENCE_URI } from './user-preference-provider'; | ||
import { Preference, PreferencesCommands } from './util/preference-types'; | ||
|
||
@injectable() | ||
export class PreferencesContribution extends AbstractViewContribution<PreferencesWidget> { | ||
|
||
@inject(PreferencesEventService) protected readonly preferencesEventService: PreferencesEventService; | ||
@inject(FileSystem) protected readonly filesystem: FileSystem; | ||
@inject(PreferenceProvider) @named(PreferenceScope.Workspace) protected readonly workspacePreferenceProvider: WorkspacePreferenceProvider; | ||
@inject(EditorManager) protected readonly editorManager: EditorManager; | ||
@inject(PreferenceService) protected readonly preferenceValueRetrievalService: PreferenceService; | ||
|
||
protected preferencesScope = Preference.DEFAULT_SCOPE; | ||
|
||
constructor() { | ||
super({ | ||
widgetId: PreferencesWidget.ID, | ||
widgetName: PreferencesWidget.LABEL, | ||
defaultWidgetOptions: { | ||
area: 'main', | ||
}, | ||
}); | ||
} | ||
|
||
@postConstruct() | ||
init(): void { | ||
this.preferencesEventService.onTabScopeSelected.event(async e => { | ||
const widget: PreferencesWidget = await this.widget; | ||
this.preferencesScope = e; | ||
widget.preferenceScope = this.preferencesScope; | ||
}); | ||
} | ||
|
||
registerCommands(commands: CommandRegistry): void { | ||
commands.registerCommand(CommonCommands.OPEN_PREFERENCES, { | ||
execute: () => this.openView({ reveal: true }), | ||
}); | ||
commands.registerCommand(PreferencesCommands.OPEN_PREFERENCES_JSON_TOOLBAR, { | ||
isEnabled: () => true, | ||
isVisible: w => this.withWidget(w, () => true), | ||
execute: (preferenceNode: Preference.NodeWithValueInAllScopes) => { | ||
this.openPreferencesJSON(preferenceNode); | ||
} | ||
}); | ||
} | ||
|
||
registerMenus(menus: MenuModelRegistry): void { | ||
menus.registerMenuAction(CommonMenus.FILE_SETTINGS_SUBMENU_OPEN, { | ||
commandId: CommonCommands.OPEN_PREFERENCES.id, | ||
label: CommonCommands.OPEN_PREFERENCES.label, | ||
order: 'a10', | ||
}); | ||
} | ||
|
||
registerKeybindings(keybindings: KeybindingRegistry): void { | ||
if (isOSX && !isFirefox) { | ||
keybindings.registerKeybinding({ | ||
command: CommonCommands.OPEN_PREFERENCES.id, | ||
keybinding: 'cmd+,' | ||
}); | ||
} | ||
|
||
keybindings.registerKeybinding({ | ||
command: CommonCommands.OPEN_PREFERENCES.id, | ||
keybinding: 'ctrl+,', | ||
}); | ||
} | ||
|
||
registerToolbarItems(toolbar: TabBarToolbarRegistry): void { | ||
toolbar.registerItem({ | ||
id: PreferencesCommands.OPEN_PREFERENCES_JSON_TOOLBAR.id, | ||
command: PreferencesCommands.OPEN_PREFERENCES_JSON_TOOLBAR.id, | ||
tooltip: 'Open Preferences in JSON', | ||
priority: 0, | ||
}); | ||
} | ||
|
||
protected async openPreferencesJSON(preferenceNode: Preference.NodeWithValueInAllScopes): Promise<void> { | ||
const wasOpenedFromEditor = preferenceNode.constructor.name !== 'PreferencesWidget'; | ||
const { scope, activeScopeIsFolder, uri } = this.preferencesScope; | ||
const preferenceId = wasOpenedFromEditor ? preferenceNode.id : ''; | ||
// when opening from toolbar, widget is passed as arg by default (we don't need this info) | ||
if (wasOpenedFromEditor) { | ||
const currentPreferenceValue = preferenceNode.preference.values!; | ||
const key = Preference.LookupKeys[Number(scope)] as keyof Preference.ValuesInAllScopes; | ||
const valueInCurrentScope = currentPreferenceValue[key] === undefined ? currentPreferenceValue.defaultValue : currentPreferenceValue[key] as PreferenceItem; | ||
this.preferenceValueRetrievalService.set(preferenceId, valueInCurrentScope, Number(scope), uri); | ||
} | ||
|
||
let jsonEditorWidget: EditorWidget; | ||
const jsonUriToOpen = await this.getPreferencesJSONUri(scope, activeScopeIsFolder, uri); | ||
if (jsonUriToOpen) { | ||
jsonEditorWidget = await this.editorManager.open(jsonUriToOpen); | ||
|
||
if (wasOpenedFromEditor) { | ||
const text = jsonEditorWidget.editor.document.getText(); | ||
if (preferenceId) { | ||
const { index } = text.match(preferenceId)!; | ||
const numReturns = text.slice(0, index).match(new RegExp('\n', 'g'))!.length; | ||
jsonEditorWidget.editor.cursor = { line: numReturns, character: 4 + preferenceId.length + 4 }; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private async getPreferencesJSONUri(scope: string, activeScopeIsFolder: string, uri: string): Promise<URI | undefined> { | ||
const scopeNumber = Number(scope); | ||
if (PreferenceScope.User === scopeNumber) { | ||
return USER_PREFERENCE_URI; | ||
} else if (PreferenceScope.Workspace === scopeNumber) { | ||
if (activeScopeIsFolder === 'true') { | ||
return this.getOrCreateSettingsFile(uri); | ||
} else { | ||
const wsURI = this.workspacePreferenceProvider.getConfigUri(); | ||
if (wsURI) { | ||
const wsURIString = wsURI.toString(); | ||
if (!await this.filesystem.exists(wsURIString)) { | ||
await this.filesystem.createFile(wsURIString); | ||
} | ||
return new URI(wsURIString); | ||
} | ||
} | ||
|
||
} else if (PreferenceScope.Folder === scopeNumber) { | ||
return this.getOrCreateSettingsFile(uri); | ||
} | ||
return undefined; | ||
} | ||
|
||
protected async getOrCreateSettingsFile(folderURI: string): Promise<URI> { | ||
const folderSettingsURI = `${folderURI}/.theia/settings.json`; | ||
if (folderSettingsURI && !await this.filesystem.exists(folderSettingsURI)) { | ||
await this.filesystem.createFile(folderSettingsURI); | ||
} | ||
return new URI(folderSettingsURI); | ||
} | ||
|
||
/** | ||
* Determine if the current widget is the PreferencesWidget. | ||
*/ | ||
protected withWidget<T>(widget: Widget | undefined = this.tryGetWidget(), fn: (widget: PreferencesWidget) => T): T | false { | ||
if (widget instanceof PreferencesWidget && widget.id === PreferencesWidget.ID) { | ||
return fn(widget); | ||
} | ||
return false; | ||
} | ||
} |
145 changes: 0 additions & 145 deletions
145
packages/preferences/src/browser/preference-editor-widget.ts
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.