Skip to content

Commit

Permalink
Allow opening changes for files associated with custom editors (#13916)
Browse files Browse the repository at this point in the history
* Fix loading of webview resources that depend on query params

Some resource url (notably git) use the query part of the url to store
additional information. The query part of the url was incorrectly being
dropped while attempting to load these resources inside of webviews.

See also microsoft/vscode@48387df

* Fix `Error: Unknown Webview` messages in the log

* Fix design and implementation issues surrounding `CustomEditorOpener`

This commit ensures that the promise returned by `CustomEditorOpener.open`
will only resolve to a properly initialized and opened `CustomEditorWidget`.
In particular, it ensures that the widget is opened according to the specified
`WidgetOpenerOptions`, including `widgetOptions.ref` and `mode`.

Essentially, it revises the work done in #9671 and #10580
to fix #9670 and #10583.

* Restore custom editors as part of layout

Fixes an incorrect assumption that a custom editor cannot be restored
if no `WebviewPanelSerializer` is registered for its view type. (Actually,
custom editors are created and restored using a custom editor provider.)

Also, ensures that `CustomEditorWidget.modelRef` satisfies the shape for the
`CustomEditorWidget` defined in `editor.ts` and cannot return `undefined`.
(However, `CustomEditorWidget.modelRef.object` can be `undefined`
until the custom editor is resolved.)

Fixes #10787

* Fix a race condition when file system provider is activated

When file system provider is activated, wait until it is registered.

* git: add support for custom editors

* Uses `OpenerService` instead of `EditorManager` to open editors

* Contributes a `FileSystemProvider` for git-resources

* Fixes an issue with getting blob contents

* custom editor: open a diff-uri in a side-by-side editor

`CustomEditorOpener` is now able to open a diff-uri in a side-by-side editor,
which contains the corresponding `CustomEditor`s.

Fixes #9079
  • Loading branch information
pisv authored Aug 21, 2024
1 parent 5d12ee4 commit 6f36201
Show file tree
Hide file tree
Showing 31 changed files with 665 additions and 162 deletions.
66 changes: 65 additions & 1 deletion packages/core/src/browser/saveable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { MaybePromise } from '../common/types';
import { Key } from './keyboard/keys';
import { AbstractDialog } from './dialogs';
import { nls } from '../common/nls';
import { DisposableCollection, isObject } from '../common';
import { Disposable, DisposableCollection, isObject } from '../common';
import { BinaryBuffer } from '../common/buffer';

export type AutoSaveMode = 'off' | 'afterDelay' | 'onFocusChange' | 'onWindowChange';
Expand Down Expand Up @@ -112,6 +112,70 @@ export class DelegatingSaveable implements Saveable {

}

export class CompositeSaveable implements Saveable {
protected isDirty = false;
protected readonly onDirtyChangedEmitter = new Emitter<void>();
protected readonly onContentChangedEmitter = new Emitter<void>();
protected readonly toDispose = new DisposableCollection(this.onDirtyChangedEmitter, this.onContentChangedEmitter);
protected readonly saveablesMap = new Map<Saveable, Disposable>();

get dirty(): boolean {
return this.isDirty;
}

get onDirtyChanged(): Event<void> {
return this.onDirtyChangedEmitter.event;
}

get onContentChanged(): Event<void> {
return this.onContentChangedEmitter.event;
}

async save(options?: SaveOptions): Promise<void> {
await Promise.all(this.saveables.map(saveable => saveable.save(options)));
}

get saveables(): readonly Saveable[] {
return Array.from(this.saveablesMap.keys());
}

add(saveable: Saveable): void {
if (this.saveablesMap.has(saveable)) {
return;
}
const toDispose = new DisposableCollection();
this.toDispose.push(toDispose);
this.saveablesMap.set(saveable, toDispose);
toDispose.push(Disposable.create(() => {
this.saveablesMap.delete(saveable);
}));
toDispose.push(saveable.onDirtyChanged(() => {
const wasDirty = this.isDirty;
this.isDirty = this.saveables.some(s => s.dirty);
if (this.isDirty !== wasDirty) {
this.onDirtyChangedEmitter.fire();
}
}));
toDispose.push(saveable.onContentChanged(() => {
this.onContentChangedEmitter.fire();
}));
if (saveable.dirty && !this.isDirty) {
this.isDirty = true;
this.onDirtyChangedEmitter.fire();
}
}

remove(saveable: Saveable): boolean {
const toDispose = this.saveablesMap.get(saveable);
toDispose?.dispose();
return !!toDispose;
}

dispose(): void {
this.toDispose.dispose();
}
}

export namespace Saveable {
export interface RevertOptions {
/**
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/browser/style/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,4 @@ button.secondary[disabled],
@import "./progress-bar.css";
@import "./breadcrumbs.css";
@import "./tooltip.css";
@import "./split-widget.css";
38 changes: 38 additions & 0 deletions packages/core/src/browser/style/split-widget.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/********************************************************************************
* Copyright (C) 2024 1C-Soft LLC 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-only WITH Classpath-exception-2.0
********************************************************************************/

.theia-split-widget > .p-SplitPanel {
height: 100%;
width: 100%;
outline: none;
}

.theia-split-widget > .p-SplitPanel > .p-SplitPanel-child {
min-width: 50px;
min-height: var(--theia-content-line-height);
}

.theia-split-widget > .p-SplitPanel > .p-SplitPanel-handle {
box-sizing: border-box;
}

.theia-split-widget > .p-SplitPanel[data-orientation="horizontal"] > .p-SplitPanel-handle {
border-left: var(--theia-border-width) solid var(--theia-sideBarSectionHeader-border);
}

.theia-split-widget > .p-SplitPanel[data-orientation="vertical"] > .p-SplitPanel-handle {
border-top: var(--theia-border-width) solid var(--theia-sideBarSectionHeader-border);
}
5 changes: 4 additions & 1 deletion packages/core/src/browser/widget-open-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import { WidgetManager } from './widget-manager';

export type WidgetOpenMode = 'open' | 'reveal' | 'activate';
/**
* `WidgetOpenerOptions` define serializable generic options used by the {@link WidgetOpenHandler}.
* `WidgetOpenerOptions` define generic options used by the {@link WidgetOpenHandler}.
*
* _Note:_ This object may contain references to widgets (e.g. `widgetOptions.ref`);
* these need to be transformed before it can be serialized.
*/
export interface WidgetOpenerOptions extends OpenerOptions {
/**
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/browser/widgets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './widget';
export * from './react-renderer';
export * from './react-widget';
export * from './extractable-widget';
export * from './split-widget';
163 changes: 163 additions & 0 deletions packages/core/src/browser/widgets/split-widget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// *****************************************************************************
// Copyright (C) 2024 1C-Soft LLC 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-only WITH Classpath-exception-2.0
// *****************************************************************************

import { Emitter } from 'vscode-languageserver-protocol';
import { ApplicationShell, StatefulWidget } from '../shell';
import { BaseWidget, Message, PanelLayout, SplitPanel, Widget } from './widget';
import { CompositeSaveable, Saveable, SaveableSource } from '../saveable';
import { Navigatable } from '../navigatable-types';
import { URI } from '../../common';

/**
* A widget containing a number of panes in a split layout.
*/
export class SplitWidget extends BaseWidget implements ApplicationShell.TrackableWidgetProvider, SaveableSource, Navigatable, StatefulWidget {

protected readonly splitPanel: SplitPanel;

protected readonly onDidChangeTrackableWidgetsEmitter = new Emitter<Widget[]>();
readonly onDidChangeTrackableWidgets = this.onDidChangeTrackableWidgetsEmitter.event;

protected readonly compositeSaveable = new CompositeSaveable();

protected navigatable?: Navigatable;

constructor(options?: SplitPanel.IOptions & { navigatable?: Navigatable }) {
super();

this.toDispose.pushAll([this.onDidChangeTrackableWidgetsEmitter]);

this.addClass('theia-split-widget');

const layout = new PanelLayout();
this.layout = layout;
const that = this;
this.splitPanel = new class extends SplitPanel {

protected override onChildAdded(msg: Widget.ChildMessage): void {
super.onChildAdded(msg);
that.onPaneAdded(msg.child);
}

protected override onChildRemoved(msg: Widget.ChildMessage): void {
super.onChildRemoved(msg);
that.onPaneRemoved(msg.child);
}
}({
spacing: 1, // --theia-border-width
...options
});
this.splitPanel.node.tabIndex = -1;
layout.addWidget(this.splitPanel);

this.navigatable = options?.navigatable;
}

get orientation(): SplitPanel.Orientation {
return this.splitPanel.orientation;
}

set orientation(value: SplitPanel.Orientation) {
this.splitPanel.orientation = value;
}

relativeSizes(): number[] {
return this.splitPanel.relativeSizes();
}

setRelativeSizes(sizes: number[]): void {
this.splitPanel.setRelativeSizes(sizes);
}

get handles(): readonly HTMLDivElement[] {
return this.splitPanel.handles;
}

get saveable(): Saveable {
return this.compositeSaveable;
}

getResourceUri(): URI | undefined {
return this.navigatable?.getResourceUri();
}

createMoveToUri(resourceUri: URI): URI | undefined {
return this.navigatable?.createMoveToUri(resourceUri);
}

storeState(): SplitWidget.State {
return { orientation: this.orientation, widgets: this.panes, relativeSizes: this.relativeSizes() };
}

restoreState(oldState: SplitWidget.State): void {
const { orientation, widgets, relativeSizes } = oldState;
if (orientation) {
this.orientation = orientation;
}
for (const widget of widgets) {
this.addPane(widget);
}
if (relativeSizes) {
this.setRelativeSizes(relativeSizes);
}
}

get panes(): readonly Widget[] {
return this.splitPanel.widgets;
}

getTrackableWidgets(): Widget[] {
return [...this.panes];
}

protected fireDidChangeTrackableWidgets(): void {
this.onDidChangeTrackableWidgetsEmitter.fire(this.getTrackableWidgets());
}

addPane(pane: Widget): void {
this.splitPanel.addWidget(pane);
}

insertPane(index: number, pane: Widget): void {
this.splitPanel.insertWidget(index, pane);
}

protected onPaneAdded(pane: Widget): void {
if (Saveable.isSource(pane)) {
this.compositeSaveable.add(pane.saveable);
}
this.fireDidChangeTrackableWidgets();
}

protected onPaneRemoved(pane: Widget): void {
if (Saveable.isSource(pane)) {
this.compositeSaveable.remove(pane.saveable);
}
this.fireDidChangeTrackableWidgets();
}

protected override onActivateRequest(msg: Message): void {
this.splitPanel.node.focus();
}
}

export namespace SplitWidget {
export interface State {
orientation?: SplitPanel.Orientation;
widgets: readonly Widget[]; // note: don't rename this property; it has special meaning for `ShellLayoutRestorer`
relativeSizes?: number[];
}
}
6 changes: 6 additions & 0 deletions packages/core/src/common/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ export namespace Event {
return new Promise(resolve => once(event)(resolve));
}

export function filter<T>(event: Event<T>, predicate: (e: T) => unknown): Event<T>;
export function filter<T, S extends T>(event: Event<T>, predicate: (e: T) => e is S): Event<S>;
export function filter<T>(event: Event<T>, predicate: (e: T) => unknown): Event<T> {
return (listener, thisArg, disposables) => event(e => predicate(e) && listener.call(thisArg, e), undefined, disposables);
}

/**
* Given an event and a `map` function, returns another event which maps each element
* through the mapping function.
Expand Down
4 changes: 4 additions & 0 deletions packages/filesystem/src/browser/file-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ export class FileService {
return activation;
}

hasProvider(scheme: string): boolean {
return this.providers.has(scheme);
}

/**
* Tests if the service (i.e. any of its registered {@link FileSystemProvider}s) can handle the given resource.
* @param resource `URI` of the resource to test.
Expand Down
9 changes: 7 additions & 2 deletions packages/git/src/browser/git-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import {
MenuAction,
MenuContribution,
MenuModelRegistry,
MessageService,
Mutable
} from '@theia/core';
import { codicon, DiffUris, Widget } from '@theia/core/lib/browser';
import { codicon, DiffUris, Widget, open, OpenerService } from '@theia/core/lib/browser';
import {
TabBarToolbarContribution,
TabBarToolbarItem,
Expand Down Expand Up @@ -281,6 +282,8 @@ export class GitContribution implements CommandContribution, MenuContribution, T

protected toDispose = new DisposableCollection();

@inject(OpenerService) protected openerService: OpenerService;
@inject(MessageService) protected messageService: MessageService;
@inject(EditorManager) protected readonly editorManager: EditorManager;
@inject(GitQuickOpenService) protected readonly quickOpenService: GitQuickOpenService;
@inject(GitRepositoryTracker) protected readonly repositoryTracker: GitRepositoryTracker;
Expand Down Expand Up @@ -562,7 +565,9 @@ export class GitContribution implements CommandContribution, MenuContribution, T
registry.registerCommand(GIT_COMMANDS.OPEN_CHANGED_FILE, {
execute: (...arg: ScmResource[]) => {
for (const resource of arg) {
this.editorManager.open(resource.sourceUri, { mode: 'reveal' });
open(this.openerService, resource.sourceUri, { mode: 'reveal' }).catch(e => {
this.messageService.error(e.message);
});
}
}
});
Expand Down
33 changes: 33 additions & 0 deletions packages/git/src/browser/git-file-service-contribution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// *****************************************************************************
// Copyright (C) 2024 1C-Soft LLC 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-only WITH Classpath-exception-2.0
// *****************************************************************************

import { interfaces } from '@theia/core/shared/inversify';
import { FileService, FileServiceContribution } from '@theia/filesystem/lib/browser/file-service';
import { GitFileSystemProvider } from './git-file-system-provider';
import { GIT_RESOURCE_SCHEME } from './git-resource';

export class GitFileServiceContribution implements FileServiceContribution {

constructor(protected readonly container: interfaces.Container) { }

registerFileSystemProviders(service: FileService): void {
service.onWillActivateFileSystemProvider(event => {
if (event.scheme === GIT_RESOURCE_SCHEME) {
service.registerProvider(GIT_RESOURCE_SCHEME, this.container.get(GitFileSystemProvider));
}
});
}
}
Loading

0 comments on commit 6f36201

Please sign in to comment.