Skip to content

Commit

Permalink
Merge pull request #180493 from microsoft/joh/dangerous-ferret
Browse files Browse the repository at this point in the history
joh/dangerous ferret
  • Loading branch information
jrieken authored Apr 21, 2023
2 parents 6086d7c + 5490a49 commit a362f95
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 112 deletions.
27 changes: 16 additions & 11 deletions src/vs/editor/browser/widget/diffEditorWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { CodeEditorWidget, ICodeEditorWidgetOptions } from 'vs/editor/browser/wi
import { DiffReview } from 'vs/editor/browser/widget/diffReview';
import { IDiffLinesChange, InlineDiffMargin } from 'vs/editor/browser/widget/inlineDiffMargin';
import { WorkerBasedDocumentDiffProvider } from 'vs/editor/browser/widget/workerBasedDocumentDiffProvider';
import { boolean as validateBooleanOption, clampedInt, EditorFontLigatures, EditorLayoutInfo, EditorOption, EditorOptions, IDiffEditorOptions, stringSet as validateStringSetOption, ValidDiffEditorBaseOptions } from 'vs/editor/common/config/editorOptions';
import { boolean as validateBooleanOption, clampedInt, EditorFontLigatures, EditorLayoutInfo, EditorOption, EditorOptions, IDiffEditorOptions, stringSet as validateStringSetOption, ValidDiffEditorBaseOptions, clampedFloat } from 'vs/editor/common/config/editorOptions';
import { FontInfo } from 'vs/editor/common/config/fontInfo';
import { IDimension } from 'vs/editor/common/core/dimension';
import { IPosition, Position } from 'vs/editor/common/core/position';
Expand Down Expand Up @@ -273,6 +273,7 @@ export class DiffEditorWidget extends Disposable implements editorBrowser.IDiffE

this._options = validateDiffEditorOptions(options, {
enableSplitViewResizing: true,
splitViewDefaultRatio: 0.5,
renderSideBySide: true,
renderMarginRevertIcon: true,
maxComputationTime: 5000,
Expand Down Expand Up @@ -364,7 +365,7 @@ export class DiffEditorWidget extends Disposable implements editorBrowser.IDiffE
this._containerDomElement.appendChild(this._reviewPane.actionBarContainer.domNode);

if (this._options.renderSideBySide) {
this._setStrategy(new DiffEditorWidgetSideBySide(this._createDataSource(), this._options.enableSplitViewResizing));
this._setStrategy(new DiffEditorWidgetSideBySide(this._createDataSource(), this._options.enableSplitViewResizing, this._options.splitViewDefaultRatio));
} else {
this._setStrategy(new DiffEditorWidgetInline(this._createDataSource(), this._options.enableSplitViewResizing));
}
Expand Down Expand Up @@ -788,12 +789,12 @@ export class DiffEditorWidget extends Disposable implements editorBrowser.IDiffE
this._originalEditor.updateOptions(this._adjustOptionsForLeftHandSide(_newOptions));

// enableSplitViewResizing
this._strategy.setEnableSplitViewResizing(this._options.enableSplitViewResizing);
this._strategy.setEnableSplitViewResizing(this._options.enableSplitViewResizing, this._options.splitViewDefaultRatio);

// renderSideBySide
if (changed.renderSideBySide) {
if (this._options.renderSideBySide) {
this._setStrategy(new DiffEditorWidgetSideBySide(this._createDataSource(), this._options.enableSplitViewResizing));
this._setStrategy(new DiffEditorWidgetSideBySide(this._createDataSource(), this._options.enableSplitViewResizing, this._options.splitViewDefaultRatio));
} else {
this._setStrategy(new DiffEditorWidgetInline(this._createDataSource(), this._options.enableSplitViewResizing));
}
Expand Down Expand Up @@ -1584,7 +1585,7 @@ abstract class DiffEditorWidgetStyle extends Disposable {
protected abstract _getOriginalEditorDecorations(zones: IEditorsZones, lineChanges: ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean): IEditorDiffDecorations;
protected abstract _getModifiedEditorDecorations(zones: IEditorsZones, lineChanges: ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, renderMarginRevertIcon: boolean): IEditorDiffDecorations;

public abstract setEnableSplitViewResizing(enableSplitViewResizing: boolean): void;
public abstract setEnableSplitViewResizing(enableSplitViewResizing: boolean, defaultRatio: number): void;
public abstract layout(): number;

setBoundarySashes(_sashes: IBoundarySashes): void {
Expand Down Expand Up @@ -1974,14 +1975,16 @@ class DiffEditorWidgetSideBySide extends DiffEditorWidgetStyle implements IVerti

private _disableSash: boolean;
private readonly _sash: Sash;
private _defaultRatio: number;
private _sashRatio: number | null;
private _sashPosition: number | null;
private _startSashPosition: number | null;

constructor(dataSource: IDataSource, enableSplitViewResizing: boolean) {
constructor(dataSource: IDataSource, enableSplitViewResizing: boolean, defaultSashRatio: number) {
super(dataSource);

this._disableSash = (enableSplitViewResizing === false);
this._defaultRatio = defaultSashRatio;
this._sashRatio = null;
this._sashPosition = null;
this._startSashPosition = null;
Expand All @@ -1997,20 +2000,21 @@ class DiffEditorWidgetSideBySide extends DiffEditorWidgetStyle implements IVerti
this._sash.onDidReset(() => this._onSashReset());
}

public setEnableSplitViewResizing(enableSplitViewResizing: boolean): void {
public setEnableSplitViewResizing(enableSplitViewResizing: boolean, defaultRatio: number): void {
this._defaultRatio = defaultRatio;
const newDisableSash = (enableSplitViewResizing === false);
if (this._disableSash !== newDisableSash) {
this._disableSash = newDisableSash;
this._sash.state = this._disableSash ? SashState.Disabled : SashState.Enabled;
}
}

public layout(sashRatio: number | null = this._sashRatio): number {
public layout(sashRatio: number | null = this._sashRatio || this._defaultRatio): number {
const w = this._dataSource.getWidth();
const contentWidth = w - (this._dataSource.getOptions().renderOverviewRuler ? DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH : 0);

let sashPosition = Math.floor((sashRatio || 0.5) * contentWidth);
const midPoint = Math.floor(0.5 * contentWidth);
let sashPosition = Math.floor((sashRatio || this._defaultRatio) * contentWidth);
const midPoint = Math.floor(this._defaultRatio * contentWidth);

sashPosition = this._disableSash ? midPoint : sashPosition || midPoint;

Expand Down Expand Up @@ -2053,7 +2057,7 @@ class DiffEditorWidgetSideBySide extends DiffEditorWidgetStyle implements IVerti
}

private _onSashReset(): void {
this._sashRatio = 0.5;
this._sashRatio = this._defaultRatio;
this._dataSource.relayoutEditors();
this._sash.layout();
}
Expand Down Expand Up @@ -2749,6 +2753,7 @@ function getViewRange(model: ITextModel, viewModel: IViewModel, startLineNumber:
function validateDiffEditorOptions(options: Readonly<IDiffEditorOptions>, defaults: ValidDiffEditorBaseOptions): ValidDiffEditorBaseOptions {
return {
enableSplitViewResizing: validateBooleanOption(options.enableSplitViewResizing, defaults.enableSplitViewResizing),
splitViewDefaultRatio: clampedFloat(options.splitViewDefaultRatio, 0.5, 0.1, 0.9),
renderSideBySide: validateBooleanOption(options.renderSideBySide, defaults.renderSideBySide),
renderMarginRevertIcon: validateBooleanOption(options.renderMarginRevertIcon, defaults.renderMarginRevertIcon),
maxComputationTime: clampedInt(options.maxComputationTime, defaults.maxComputationTime, 0, Constants.MAX_SAFE_SMALL_INTEGER),
Expand Down
16 changes: 16 additions & 0 deletions src/vs/editor/common/config/editorOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,12 @@ export interface IDiffEditorBaseOptions {
* Defaults to true.
*/
enableSplitViewResizing?: boolean;
/**
* The default ratio when rendering side-by-side editors.
* Must be a number between 0 and 1, min sizes apply.
* Defaults to 0.5
*/
splitViewDefaultRatio?: number;
/**
* Render the differences in two side-by-side editors.
* Defaults to true.
Expand Down Expand Up @@ -1068,6 +1074,16 @@ class EditorIntOption<K extends EditorOption> extends SimpleEditorOption<K, numb
return EditorIntOption.clampedInt(input, this.defaultValue, this.minimum, this.maximum);
}
}
/**
* @internal
*/
export function clampedFloat<T extends number>(value: any, defaultValue: T, minimum: number, maximum: number): number | T {
if (typeof value === 'undefined') {
return defaultValue;
}
const r = EditorFloatOption.float(value, defaultValue);
return EditorFloatOption.clamp(r, minimum, maximum);
}

class EditorFloatOption<K extends EditorOption> extends SimpleEditorOption<K, number> {

Expand Down
6 changes: 6 additions & 0 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3777,6 +3777,12 @@ declare namespace monaco.editor {
* Defaults to true.
*/
enableSplitViewResizing?: boolean;
/**
* The default ratio when rendering side-by-side editors.
* Must be a number between 0 and 1, min sizes apply.
* Defaults to 0.5
*/
splitViewDefaultRatio?: number;
/**
* Render the differences in two side-by-side editors.
* Defaults to true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EditorAction2 } from 'vs/editor/browser/editorExtensions';
import { EmbeddedCodeEditorWidget, EmbeddedDiffEditorWidget } from 'vs/editor/browser/widget/embeddedCodeEditorWidget';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { InteractiveEditorController, InteractiveEditorRunOptions, Recording } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorController';
import { CTX_INTERACTIVE_EDITOR_FOCUSED, CTX_INTERACTIVE_EDITOR_HAS_ACTIVE_REQUEST, CTX_INTERACTIVE_EDITOR_HAS_PROVIDER, CTX_INTERACTIVE_EDITOR_INNER_CURSOR_FIRST, CTX_INTERACTIVE_EDITOR_INNER_CURSOR_LAST, CTX_INTERACTIVE_EDITOR_EMPTY, CTX_INTERACTIVE_EDITOR_OUTER_CURSOR_POSITION, CTX_INTERACTIVE_EDITOR_VISIBLE, MENU_INTERACTIVE_EDITOR_WIDGET, CTX_INTERACTIVE_EDITOR_LAST_EDIT_TYPE, MENU_INTERACTIVE_EDITOR_WIDGET_UNDO, MENU_INTERACTIVE_EDITOR_WIDGET_STATUS, CTX_INTERACTIVE_EDITOR_LAST_FEEDBACK, CTX_INTERACTIVE_EDITOR_INLNE_DIFF, CTX_INTERACTIVE_EDITOR_HAS_RESPONSE, CTX_INTERACTIVE_EDITOR_EDIT_MODE } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
import { CTX_INTERACTIVE_EDITOR_FOCUSED, CTX_INTERACTIVE_EDITOR_HAS_ACTIVE_REQUEST, CTX_INTERACTIVE_EDITOR_HAS_PROVIDER, CTX_INTERACTIVE_EDITOR_INNER_CURSOR_FIRST, CTX_INTERACTIVE_EDITOR_INNER_CURSOR_LAST, CTX_INTERACTIVE_EDITOR_EMPTY, CTX_INTERACTIVE_EDITOR_OUTER_CURSOR_POSITION, CTX_INTERACTIVE_EDITOR_VISIBLE, MENU_INTERACTIVE_EDITOR_WIDGET, CTX_INTERACTIVE_EDITOR_LAST_EDIT_TYPE, MENU_INTERACTIVE_EDITOR_WIDGET_UNDO, MENU_INTERACTIVE_EDITOR_WIDGET_STATUS, CTX_INTERACTIVE_EDITOR_LAST_FEEDBACK, CTX_INTERACTIVE_EDITOR_INLNE_DIFF, CTX_INTERACTIVE_EDITOR_HAS_RESPONSE, CTX_INTERACTIVE_EDITOR_EDIT_MODE, EditMode } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
import { localize } from 'vs/nls';
import { IAction2Options } from 'vs/platform/actions/common/actions';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
Expand Down Expand Up @@ -342,7 +342,8 @@ export class FeebackHelpfulCommand extends AbstractInteractiveEditorAction {
toggled: CTX_INTERACTIVE_EDITOR_LAST_FEEDBACK.isEqualTo('helpful'),
menu: {
id: MENU_INTERACTIVE_EDITOR_WIDGET_STATUS,
group: '1_feedback',
when: CTX_INTERACTIVE_EDITOR_HAS_RESPONSE,
group: '2_feedback',
order: 1
}
});
Expand All @@ -363,7 +364,8 @@ export class FeebackUnhelpfulCommand extends AbstractInteractiveEditorAction {
toggled: CTX_INTERACTIVE_EDITOR_LAST_FEEDBACK.isEqualTo('unhelpful'),
menu: {
id: MENU_INTERACTIVE_EDITOR_WIDGET_STATUS,
group: '1_feedback',
when: CTX_INTERACTIVE_EDITOR_HAS_RESPONSE,
group: '2_feedback',
order: 2
}
});
Expand All @@ -385,9 +387,9 @@ export class ToggleInlineDiff extends AbstractInteractiveEditorAction {
toggled: CTX_INTERACTIVE_EDITOR_INLNE_DIFF,
menu: {
id: MENU_INTERACTIVE_EDITOR_WIDGET_STATUS,
when: CTX_INTERACTIVE_EDITOR_EDIT_MODE.isEqualTo('direct'),
group: '0_main',
order: 10
when: CTX_INTERACTIVE_EDITOR_EDIT_MODE.isEqualTo(EditMode.Live),
group: '1_main',
order: 1
}
});
}
Expand All @@ -404,14 +406,14 @@ export class ApplyPreviewEdits extends AbstractInteractiveEditorAction {
id: 'interactiveEditor.applyEdits',
title: localize('applyEdits', 'Apply Changes'),
icon: Codicon.check,
precondition: ContextKeyExpr.and(CTX_INTERACTIVE_EDITOR_VISIBLE),
precondition: ContextKeyExpr.and(CTX_INTERACTIVE_EDITOR_VISIBLE, CTX_INTERACTIVE_EDITOR_HAS_RESPONSE),
keybinding: {
weight: KeybindingWeight.EditorContrib + 10,
primary: KeyMod.CtrlCmd | KeyCode.Enter
},
menu: {
id: MENU_INTERACTIVE_EDITOR_WIDGET_STATUS,
// when: CTX_INTERACTIVE_EDITOR_EDIT_MODE.isEqualTo('preview'),
when: CTX_INTERACTIVE_EDITOR_HAS_RESPONSE,
group: '0_main',
order: 0
}
Expand Down Expand Up @@ -447,15 +449,15 @@ export class CancelSessionAction extends AbstractInteractiveEditorAction {
},
menu: {
id: MENU_INTERACTIVE_EDITOR_WIDGET_STATUS,
// when: CTX_INTERACTIVE_EDITOR_EDIT_MODE.isEqualTo('preview'),
when: CTX_INTERACTIVE_EDITOR_HAS_RESPONSE,
group: '0_main',
order: 1
}
});
}

runInteractiveEditorCommand(_accessor: ServicesAccessor, ctrl: InteractiveEditorController, _editor: ICodeEditor, ..._args: any[]): void {
ctrl.cancelSession();
async runInteractiveEditorCommand(_accessor: ServicesAccessor, ctrl: InteractiveEditorController, _editor: ICodeEditor, ..._args: any[]): Promise<void> {
await ctrl.cancelSession();
}
}

Expand Down
Loading

0 comments on commit a362f95

Please sign in to comment.