Skip to content

Commit

Permalink
Extracts hot reload utilities, makes placeholderText contribution hot…
Browse files Browse the repository at this point in the history
… reloadable
  • Loading branch information
hediet committed Jul 1, 2024
1 parent af5cf64 commit b213c86
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 94 deletions.
30 changes: 30 additions & 0 deletions src/vs/base/common/hotReloadHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { isHotReloadEnabled, registerHotReloadHandler } from 'vs/base/common/hotReload';
import { IReader, observableSignalFromEvent } from 'vs/base/common/observable';

export function readHotReloadableExport<T>(value: T, reader: IReader | undefined): T {
observeHotReloadableExports([value], reader);
return value;
}

export function observeHotReloadableExports(values: any[], reader: IReader | undefined): void {
if (isHotReloadEnabled()) {
const o = observableSignalFromEvent(
'reload',
event => registerHotReloadHandler(({ oldExports }) => {
if (![...Object.values(oldExports)].some(v => values.includes(v))) {
return undefined;
}
return (_newExports) => {
event(undefined);
return true;
};
})
);
o.read(reader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
import { IObservable, IReader, ISettableObservable, ITransaction, autorun, autorunWithStore, derived, observableSignal, observableSignalFromEvent, observableValue, transaction, waitForState } from 'vs/base/common/observable';
import { IDiffProviderFactoryService } from 'vs/editor/browser/widget/diffEditor/diffProviderFactoryService';
import { filterWithPrevious, readHotReloadableExport } from 'vs/editor/browser/widget/diffEditor/utils';
import { filterWithPrevious } from 'vs/editor/browser/widget/diffEditor/utils';
import { readHotReloadableExport } from 'vs/base/common/hotReloadHelpers';
import { ISerializedLineRange, LineRange, LineRangeSet } from 'vs/editor/common/core/lineRange';
import { DefaultLinesDiffComputer } from 'vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer';
import { IDocumentDiff } from 'vs/editor/common/diff/documentDiffProvider';
Expand Down
3 changes: 2 additions & 1 deletion src/vs/editor/browser/widget/diffEditor/diffEditorWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import { HideUnchangedRegionsFeature } from 'vs/editor/browser/widget/diffEditor
import { MovedBlocksLinesFeature } from 'vs/editor/browser/widget/diffEditor/features/movedBlocksLinesFeature';
import { OverviewRulerFeature } from 'vs/editor/browser/widget/diffEditor/features/overviewRulerFeature';
import { RevertButtonsFeature } from 'vs/editor/browser/widget/diffEditor/features/revertButtonsFeature';
import { CSSStyle, ObservableElementSizeObserver, applyStyle, applyViewZones, readHotReloadableExport, translatePosition } from 'vs/editor/browser/widget/diffEditor/utils';
import { CSSStyle, ObservableElementSizeObserver, applyStyle, applyViewZones, translatePosition } from 'vs/editor/browser/widget/diffEditor/utils';
import { readHotReloadableExport } from 'vs/base/common/hotReloadHelpers';
import { bindContextKey } from 'vs/platform/observable/common/platformObservableUtils';
import { IDiffEditorOptions } from 'vs/editor/common/config/editorOptions';
import { IDimension } from 'vs/editor/common/core/dimension';
Expand Down
26 changes: 1 addition & 25 deletions src/vs/editor/browser/widget/diffEditor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import { IDimension } from 'vs/base/browser/dom';
import { findLast } from 'vs/base/common/arraysFind';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { isHotReloadEnabled, registerHotReloadHandler } from 'vs/base/common/hotReload';
import { Disposable, DisposableStore, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { IObservable, IReader, ISettableObservable, autorun, autorunHandleChanges, autorunOpts, autorunWithStore, observableSignalFromEvent, observableValue, transaction } from 'vs/base/common/observable';
import { IObservable, ISettableObservable, autorun, autorunHandleChanges, autorunOpts, autorunWithStore, observableValue, transaction } from 'vs/base/common/observable';
import { ElementSizeObserver } from 'vs/editor/browser/config/elementSizeObserver';
import { ICodeEditor, IOverlayWidget, IViewZone } from 'vs/editor/browser/editorBrowser';
import { Position } from 'vs/editor/common/core/position';
Expand Down Expand Up @@ -298,29 +297,6 @@ export function applyStyle(domNode: HTMLElement, style: Partial<{ [TKey in keyof
});
}

export function readHotReloadableExport<T>(value: T, reader: IReader | undefined): T {
observeHotReloadableExports([value], reader);
return value;
}

export function observeHotReloadableExports(values: any[], reader: IReader | undefined): void {
if (isHotReloadEnabled()) {
const o = observableSignalFromEvent(
'reload',
event => registerHotReloadHandler(({ oldExports }) => {
if (![...Object.values(oldExports)].some(v => values.includes(v))) {
return undefined;
}
return (_newExports) => {
event(undefined);
return true;
};
})
);
o.read(reader);
}
}

export function applyViewZones(editor: ICodeEditor, viewZones: IObservable<IObservableViewZone[]>, setIsUpdating?: (isUpdatingViewZones: boolean) => void, zoneIds?: Set<string>): IDisposable {
const store = new DisposableStore();
const lastViewZoneIds: string[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { Dimension } from 'vs/base/browser/dom';
import { Disposable } from 'vs/base/common/lifecycle';
import { derived, derivedWithStore, observableValue, recomputeInitiallyAndOnChange } from 'vs/base/common/observable';
import { readHotReloadableExport } from 'vs/editor/browser/widget/diffEditor/utils';
import { readHotReloadableExport } from 'vs/base/common/hotReloadHelpers';
import { IMultiDiffEditorModel } from 'vs/editor/browser/widget/multiDiffEditor/model';
import { IMultiDiffEditorViewState, IMultiDiffResourceId, MultiDiffEditorWidgetImpl } from 'vs/editor/browser/widget/multiDiffEditor/multiDiffEditorWidgetImpl';
import { MultiDiffEditorViewModel } from './multiDiffEditorViewModel';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { derived, derivedObservableWithCache, IReader, ISettableObservable, obse
import { derivedDisposable, derivedWithSetter } from 'vs/base/common/observableInternal/derived';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { observableCodeEditor } from 'vs/editor/browser/observableCodeEditor';
import { readHotReloadableExport } from 'vs/editor/browser/widget/diffEditor/utils';
import { readHotReloadableExport } from 'vs/base/common/hotReloadHelpers';
import { Selection } from 'vs/editor/common/core/selection';
import { ILanguageFeatureDebounceService } from 'vs/editor/common/services/languageFeatureDebounce';
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { PLAINTEXT_LANGUAGE_ID } from 'vs/editor/common/languages/modesRegistry'
import { IModelDeltaDecoration } from 'vs/editor/common/model';
import { TextModel } from 'vs/editor/common/model/textModel';
import { ContextMenuController } from 'vs/editor/contrib/contextmenu/browser/contextmenu';
import { PlaceholderTextContribution } from 'vs/editor/contrib/placeholderText/browser/placeholderText.contribution';
import { PlaceholderTextContribution } from '../../placeholderText/browser/placeholderTextContribution';
import { SuggestController } from 'vs/editor/contrib/suggest/browser/suggestController';
import { MenuWorkbenchToolBar } from 'vs/platform/actions/browser/toolbar';
import { MenuId } from 'vs/platform/actions/common/actions';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,14 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { structuralEquals } from 'vs/base/common/equals';
import { Disposable } from 'vs/base/common/lifecycle';
import { derived, derivedOpts } from 'vs/base/common/observable';
import 'vs/css!./placeholderText';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorContributionInstantiation, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { observableCodeEditor } from 'vs/editor/browser/observableCodeEditor';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { ghostTextForeground } from 'vs/editor/common/core/editorColorRegistry';
import { Range } from 'vs/editor/common/core/range';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { IModelDeltaDecoration, InjectedTextCursorStops } from 'vs/editor/common/model';
import { localize } from 'vs/nls';
import { registerColor } from 'vs/platform/theme/common/colorUtils';
import { PlaceholderTextContribution } from './placeholderTextContribution';
import { wrapInReloadableClass } from 'vs/platform/observable/common/wrapInReloadableClass';

/**
* Use the editor option to set the placeholder text.
*/
export class PlaceholderTextContribution extends Disposable implements IEditorContribution {
public static get(editor: ICodeEditor): PlaceholderTextContribution {
return editor.getContribution<PlaceholderTextContribution>(PlaceholderTextContribution.ID)!;
}
registerEditorContribution(PlaceholderTextContribution.ID, wrapInReloadableClass(() => PlaceholderTextContribution), EditorContributionInstantiation.Eager);

public static readonly ID = 'editor.contrib.placeholderText';
private readonly _editorObs = observableCodeEditor(this._editor);

private readonly _placeholderText = this._editorObs.getOption(EditorOption.placeholder);

private readonly _decorationOptions = derivedOpts<{ placeholder: string } | undefined>({ owner: this, equalsFn: structuralEquals }, reader => {
const p = this._placeholderText.read(reader);
if (!p) { return undefined; }
if (!this._editorObs.valueIsEmpty.read(reader)) { return undefined; }

return { placeholder: p };
});

private readonly _decorations = derived<IModelDeltaDecoration[]>(this, (reader) => {
const options = this._decorationOptions.read(reader);
if (!options) { return []; }

return [{
range: new Range(1, 1, 1, 1),
options: {
description: 'placeholder',
showIfCollapsed: true,
after: {
content: options.placeholder,
cursorStops: InjectedTextCursorStops.None,
inlineClassName: 'placeholder-text'
}
}
}];
});

constructor(
private readonly _editor: ICodeEditor,
) {
super();

this._register(this._editorObs.setDecorations(this._decorations));
}
}

registerEditorContribution(PlaceholderTextContribution.ID, PlaceholderTextContribution, EditorContributionInstantiation.Eager);

registerColor('editor.placeholder.foreground', { dark: ghostTextForeground, light: ghostTextForeground, hcDark: ghostTextForeground, hcLight: ghostTextForeground }, localize('placeholderForeground', 'Foreground color of the placeholder text in the editor.'));
registerColor('editor.placeholder.foreground', ghostTextForeground, localize('placeholderForeground', 'Foreground color of the placeholder text in the editor.'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { structuralEquals } from 'vs/base/common/equals';
import { Disposable } from 'vs/base/common/lifecycle';
import { derived, derivedOpts } from 'vs/base/common/observable';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { observableCodeEditor } from 'vs/editor/browser/observableCodeEditor';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { Range } from 'vs/editor/common/core/range';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { IModelDeltaDecoration, InjectedTextCursorStops } from 'vs/editor/common/model';

/**
* Use the editor option to set the placeholder text.
*/
export class PlaceholderTextContribution extends Disposable implements IEditorContribution {
public static get(editor: ICodeEditor): PlaceholderTextContribution {
return editor.getContribution<PlaceholderTextContribution>(PlaceholderTextContribution.ID)!;
}

public static readonly ID = 'editor.contrib.placeholderText';
private readonly _editorObs = observableCodeEditor(this._editor);

private readonly _placeholderText = this._editorObs.getOption(EditorOption.placeholder);

private readonly _decorationOptions = derivedOpts<{ placeholder: string } | undefined>({ owner: this, equalsFn: structuralEquals }, reader => {
const p = this._placeholderText.read(reader);
if (!p) { return undefined; }
if (!this._editorObs.valueIsEmpty.read(reader)) { return undefined; }

return { placeholder: p };
});

private readonly _decorations = derived<IModelDeltaDecoration[]>(this, (reader) => {
const options = this._decorationOptions.read(reader);
if (!options) { return []; }

return [{
range: new Range(1, 1, 1, 1),
options: {
description: 'placeholder',
showIfCollapsed: true,
after: {
content: options.placeholder,
cursorStops: InjectedTextCursorStops.None,
inlineClassName: 'placeholder-text'
}
}
}];
});

constructor(
private readonly _editor: ICodeEditor,
) {
super();

this._register(this._editorObs.setDecorations(this._decorations));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { isHotReloadEnabled } from 'vs/base/common/hotReload';
import { readHotReloadableExport } from 'vs/base/common/hotReloadHelpers';
import { IDisposable } from 'vs/base/common/lifecycle';
import { autorunWithStore } from 'vs/base/common/observable';
import { readHotReloadableExport } from 'vs/editor/browser/widget/diffEditor/utils';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';

/**
Expand Down Expand Up @@ -34,10 +33,9 @@ export function wrapInReloadableClass(getClass: () => (new (...args: any[]) => a
}
};
}

class BaseClass {
constructor(
@IInstantiationService protected readonly instantiationService: IInstantiationService,
@IInstantiationService protected readonly instantiationService: IInstantiationService
) {
this.init();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { registerWorkbenchContribution2, WorkbenchPhase } from 'vs/workbench/com
import { AccessibilitySignalLineDebuggerContribution } from 'vs/workbench/contrib/accessibilitySignals/browser/accessibilitySignalDebuggerContribution';
import { ShowAccessibilityAnnouncementHelp, ShowSignalSoundHelp } from 'vs/workbench/contrib/accessibilitySignals/browser/commands';
import { EditorTextPropertySignalsContribution } from 'vs/workbench/contrib/accessibilitySignals/browser/editorTextPropertySignalsContribution';
import { wrapInReloadableClass } from 'vs/workbench/contrib/accessibilitySignals/browser/reloadableWorkbenchContribution';
import { wrapInReloadableClass } from 'vs/platform/observable/common/wrapInReloadableClass';

registerSingleton(IAccessibilitySignalService, AccessibilitySignalService, InstantiationType.Delayed);

Expand Down

0 comments on commit b213c86

Please sign in to comment.