Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves merge editor code-lens #161830

Merged
merged 2 commits into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,7 +23,6 @@ import { EditorModel } from 'vs/workbench/common/editor/editorModel';
import { MergeEditorInputData } from 'vs/workbench/contrib/mergeEditor/browser/mergeEditorInput';
import { MergeDiffComputer } from 'vs/workbench/contrib/mergeEditor/browser/model/diffComputer';
import { InputData, MergeEditorModel } from 'vs/workbench/contrib/mergeEditor/browser/model/mergeEditorModel';
import { ProjectedDiffComputer } from 'vs/workbench/contrib/mergeEditor/browser/model/projectedDocumentDiffProvider';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { ITextFileEditorModel, ITextFileSaveOptions, ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';

Expand Down Expand Up @@ -109,7 +108,7 @@ export class TempFileMergeEditorModeFactory implements IMergeEditorInputModelFac
input2Data,
temporaryResultModel,
this._instantiationService.createInstance(MergeDiffComputer, diffProvider),
this._instantiationService.createInstance(MergeDiffComputer, this._instantiationService.createInstance(ProjectedDiffComputer, diffProvider)),
this._instantiationService.createInstance(MergeDiffComputer, diffProvider),
{
resetResult: true,
}
Expand Down Expand Up @@ -312,7 +311,7 @@ export class WorkspaceMergeEditorModeFactory implements IMergeEditorInputModelFa
input2Data,
result.object.textEditorModel,
this._instantiationService.createInstance(MergeDiffComputer, diffProvider),
this._instantiationService.createInstance(MergeDiffComputer, this._instantiationService.createInstance(ProjectedDiffComputer, diffProvider)),
this._instantiationService.createInstance(MergeDiffComputer, diffProvider),
{
resetResult: true
}
Expand Down
312 changes: 312 additions & 0 deletions src/vs/workbench/contrib/mergeEditor/browser/view/conflictActions.ts
@@ -0,0 +1,312 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { h, $, reset, createStyleSheet, isInShadowDOM } from 'vs/base/browser/dom';
import { renderLabelWithIcons } from 'vs/base/browser/ui/iconLabel/iconLabels';
import { hash } from 'vs/base/common/hash';
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
import { autorun, derived, IObservable, transaction } from 'vs/base/common/observable';
import { ContentWidgetPositionPreference, ICodeEditor, IContentWidget, IContentWidgetPosition } from 'vs/editor/browser/editorBrowser';
import { EditorOption, EDITOR_FONT_DEFAULTS } from 'vs/editor/common/config/editorOptions';
import { localize } from 'vs/nls';
import { ModifiedBaseRange, ModifiedBaseRangeState } from 'vs/workbench/contrib/mergeEditor/browser/model/modifiedBaseRange';
import { MergeEditorViewModel } from 'vs/workbench/contrib/mergeEditor/browser/view/viewModel';

export class ConflictActionsFactory extends Disposable {
private id = 0;
private readonly _styleClassName: string;
private readonly _styleElement: HTMLStyleElement;

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

this._register(this._editor.onDidChangeConfiguration((e) => {
if (e.hasChanged(EditorOption.fontInfo) || e.hasChanged(EditorOption.codeLensFontSize) || e.hasChanged(EditorOption.codeLensFontFamily)) {
this._updateLensStyle();
}
}));

this._styleClassName = '_conflictActionsFactory_' + hash(this._editor.getId()).toString(16);
this._styleElement = createStyleSheet(
isInShadowDOM(this._editor.getContainerDomNode())
? this._editor.getContainerDomNode()
: undefined
);

this._register(toDisposable(() => {
this._styleElement.remove();
}));

this._updateLensStyle();
}

private _updateLensStyle(): void {

const { codeLensHeight, fontSize } = this._getLayoutInfo();
const fontFamily = this._editor.getOption(EditorOption.codeLensFontFamily);
const editorFontInfo = this._editor.getOption(EditorOption.fontInfo);

const fontFamilyVar = `--codelens-font-family${this._styleClassName}`;
const fontFeaturesVar = `--codelens-font-features${this._styleClassName}`;

let newStyle = `
.${this._styleClassName} { line-height: ${codeLensHeight}px; font-size: ${fontSize}px; padding-right: ${Math.round(fontSize * 0.5)}px; font-feature-settings: var(${fontFeaturesVar}) }
.monaco-workbench .${this._styleClassName} span.codicon { line-height: ${codeLensHeight}px; font-size: ${fontSize}px; }
`;
if (fontFamily) {
newStyle += `${this._styleClassName} { font-family: var(${fontFamilyVar}), ${EDITOR_FONT_DEFAULTS.fontFamily}}`;
}
this._styleElement.textContent = newStyle;
this._editor.getContainerDomNode().style.setProperty(fontFamilyVar, fontFamily ?? 'inherit');
this._editor.getContainerDomNode().style.setProperty(fontFeaturesVar, editorFontInfo.fontFeatureSettings);
}

private _getLayoutInfo() {
const lineHeightFactor = Math.max(1.3, this._editor.getOption(EditorOption.lineHeight) / this._editor.getOption(EditorOption.fontSize));
let fontSize = this._editor.getOption(EditorOption.codeLensFontSize);
if (!fontSize || fontSize < 5) {
fontSize = (this._editor.getOption(EditorOption.fontSize) * .9) | 0;
}
return {
fontSize,
codeLensHeight: (fontSize * lineHeightFactor) | 0,
};
}

createContentWidget(lineNumber: number, viewModel: MergeEditorViewModel, modifiedBaseRange: ModifiedBaseRange, inputNumber: 1 | 2): IContentWidget {

function command(title: string, action: () => Promise<void>): IContentWidgetAction {
return {
text: title,
action
};
}

const items = derived('items', reader => {
const state = viewModel.model.getState(modifiedBaseRange).read(reader);
const handled = viewModel.model.isHandled(modifiedBaseRange).read(reader);
const model = viewModel.model;

const result: IContentWidgetAction[] = [];

const inputData = inputNumber === 1 ? viewModel.model.input1 : viewModel.model.input2;
const showNonConflictingChanges = viewModel.showNonConflictingChanges.read(reader);

if (!modifiedBaseRange.isConflicting && handled && !showNonConflictingChanges) {
return [];
}

const otherInputNumber = inputNumber === 1 ? 2 : 1;

if (!state.conflicting && !state.isInputIncluded(inputNumber)) {
result.push(
!state.isInputIncluded(inputNumber)
? command(localize('accept', "$(pass) Accept {0}", inputData.title), async () => {
transaction((tx) => {
model.setState(
modifiedBaseRange,
state.withInputValue(inputNumber, true),
true,
tx
);
});
})
: command(localize('remove', "$(error) Remove ${0}", inputData.title), async () => {
transaction((tx) => {
model.setState(
modifiedBaseRange,
state.withInputValue(inputNumber, false),
true,
tx
);
});
}),
);

if (modifiedBaseRange.canBeCombined && state.isEmpty) {
result.push(
state.input1 && state.input2
? command(localize('removeBoth', "$(error) Remove Both"), async () => {
transaction((tx) => {
model.setState(
modifiedBaseRange,
ModifiedBaseRangeState.default,
true,
tx
);
});
})
: command(localize('acceptBoth', "$(pass) Accept Both"), async () => {
transaction((tx) => {
model.setState(
modifiedBaseRange,
state
.withInputValue(inputNumber, true)
.withInputValue(otherInputNumber, true),
true,
tx
);
});
}),
);
}
}
return result;
});
return new ActionsContentWidget((this.id++).toString(), this._styleClassName, lineNumber, items);
}

createResultWidget(lineNumber: number, viewModel: MergeEditorViewModel, modifiedBaseRange: ModifiedBaseRange): IContentWidget {

function command(title: string, action: () => Promise<void>): IContentWidgetAction {
return {
text: title,
action
};
}

const items = derived('items', reader => {
const state = viewModel.model.getState(modifiedBaseRange).read(reader);
const model = viewModel.model;

const result: IContentWidgetAction[] = [];

const stateLabel = ((state: ModifiedBaseRangeState): string => {
if (state.conflicting) {
return localize('manualResolution', "Manual Resolution");
} else if (state.isEmpty) {
return localize('noChangesAccepted', 'No Changes Accepted');
} else {
const labels = [];
if (state.input1) {
labels.push(model.input1.title);
}
if (state.input2) {
labels.push(model.input2.title);
}
if (state.input2First) {
labels.reverse();
}
return `${labels.join(' + ')}`;
}
})(state);

result.push({
text: stateLabel,
action: async () => { },
});


const stateToggles: IContentWidgetAction[] = [];
if (state.input1) {
result.push(command(localize('remove', "$(error) Remove ${0}", model.input1.title), async () => {
transaction((tx) => {
model.setState(
modifiedBaseRange,
state.withInputValue(1, false),
true,
tx
);
});
}),
);
}
if (state.input2) {
result.push(command(localize('remove', "$(error) Remove ${0}", model.input2.title), async () => {
transaction((tx) => {
model.setState(
modifiedBaseRange,
state.withInputValue(2, false),
true,
tx
);
});
}),
);
}
if (state.input2First) {
stateToggles.reverse();
}
result.push(...stateToggles);



if (state.conflicting) {
result.push(
command(localize('resetToBase', "$(error) Reset to base"), async () => {
transaction((tx) => {
model.setState(
modifiedBaseRange,
ModifiedBaseRangeState.default,
true,
tx
);
});
})
);
}
return result;
});
return new ActionsContentWidget((this.id++).toString(), this._styleClassName, lineNumber, items);
}
}


interface IContentWidgetAction {
text: string;
hover?: string;
action: () => Promise<void>;
}

class ActionsContentWidget extends Disposable implements IContentWidget {
private readonly _domNode = h('div.merge-editor-conflict-actions').root;

constructor(
private readonly id: string,
className: string,
private readonly lineNumber: number,
items: IObservable<IContentWidgetAction[]>,
) {
super();

this._domNode.classList.add(className);

this._register(autorun('update commands', (reader) => {
const i = items.read(reader);
this.setState(i);
}));
}

private setState(items: IContentWidgetAction[]) {
const children: HTMLElement[] = [];
let isFirst = true;
for (const item of items) {
if (isFirst) {
isFirst = false;
} else {
children.push($('span', undefined, '\u00a0|\u00a0'));
}
const title = renderLabelWithIcons(item.text);
children.push($('a', { title: item.hover, role: 'button', onclick: () => item.action() }, ...title));
}

reset(this._domNode, ...children);
}

getId(): string {
return this.id;
}

getDomNode(): HTMLElement {
return this._domNode;
}

getPosition(): IContentWidgetPosition | null {
return {
position: { lineNumber: this.lineNumber, column: 1, },
preference: [ContentWidgetPositionPreference.BELOW],
};
}
}
Expand Up @@ -64,11 +64,6 @@ export abstract class CodeEditorView extends Disposable {
() => /** @description checkboxesVisible */ this.configurationService.getValue('mergeEditor.showCheckboxes') ?? true
);

protected readonly codeLensesVisible = observableFromEvent<boolean>(
this.configurationService.onDidChangeConfiguration,
() => /** @description codeLensesVisible */ this.configurationService.getValue('mergeEditor.showCodeLenses') ?? false
);

public readonly editor = this.instantiationService.createInstance(
CodeEditorWidget,
this.htmlElements.editor,
Expand Down