[go: nahoru, domu]

Skip to content

Commit

Permalink
fix: merge editor conflict action error (opensumi#2163)
Browse files Browse the repository at this point in the history
* fix: merge editor conflict action error

* fix: ci
  • Loading branch information
Ricbet authored Jan 9, 2023
1 parent 3e302a3 commit 3a8c886
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,14 @@ export class DocumentMapping extends Disposable {
const values = this.ensureSort(this.adjacentComputeRangeMap.values());

for (const range of values) {
if (range.isTouches(oppositeRange) && (isAllowContact ? true : !range.isContact(oppositeRange))) {
const condition = () => {
if (isAllowContact) {
return range.isTouches(oppositeRange) || range.isContact(oppositeRange);
}
return range.isTouches(oppositeRange) && !range.isContact(oppositeRange);
};

if (condition()) {
return range;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { uuid } from '@opensumi/ide-core-common';
import { Constants, uuid } from '@opensumi/ide-core-common';
import { IRange } from '@opensumi/monaco-editor-core';
import { Position } from '@opensumi/monaco-editor-core/esm/vs/editor/common/core/position';
import { LineRange as MonacoLineRange } from '@opensumi/monaco-editor-core/esm/vs/editor/common/diff/linesDiffComputer';

import { ETurnDirection, IRangeContrast, LineRangeType } from '../types';
Expand Down Expand Up @@ -228,14 +229,23 @@ export class LineRange extends MonacoLineRange implements IRangeContrast {
return this.retainState(child).setId(preId);
}

public toRange(startColumn = 0, endColumn: number = Number.MAX_SAFE_INTEGER): IRange {
public toRange(): IRange {
return InnerRange.fromPositions(
new Position(this.startLineNumber, 1),
new Position(this.endLineNumberExclusive, 1),
).setType(this._type);
}

public toInclusiveRange(startColumn?: number, endColumn?: number): IRange {
if (this.isEmpty) {
return InnerRange.fromPositions({ lineNumber: this.startLineNumber, column: startColumn }).setType(this._type);
return InnerRange.fromPositions(
new Position(this.startLineNumber, startColumn ?? 1),
new Position(this.startLineNumber, endColumn ?? 1),
).setType(this._type);
}

return InnerRange.fromPositions(
{ lineNumber: this.startLineNumber, column: startColumn },
{ lineNumber: this.endLineNumberExclusive - 1, column: endColumn },
new Position(this.startLineNumber, startColumn ?? 1),
new Position(this.endLineNumberExclusive - 1, endColumn ?? Constants.MAX_SAFE_SMALL_INTEGER),
).setType(this._type);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Disposable, Event } from '@opensumi/ide-core-common';
import { IEditorMouseEvent, MouseTargetType } from '@opensumi/monaco-editor-core/esm/vs/editor/browser/editorBrowser';
import { ISingleEditOperation } from '@opensumi/monaco-editor-core/esm/vs/editor/common/core/editOperation';
import { IRange } from '@opensumi/monaco-editor-core/esm/vs/editor/common/core/range';
import { Position } from '@opensumi/monaco-editor-core/esm/vs/editor/common/core/position';

import { MappingManagerService } from '../mapping-manager.service';
import { DocumentMapping } from '../model/document-mapping';
import { InnerRange } from '../model/inner-range';
import { LineRange } from '../model/line-range';
import {
IConflictActionsEvent,
Expand All @@ -22,6 +22,8 @@ import {
import { BaseCodeEditor } from './editors/baseCodeEditor';
import { ResultCodeEditor } from './editors/resultCodeEditor';

type TLineRangeEdit = Array<{ range: LineRange; text: string | null }>;

export class ActionsManager extends Disposable {
private currentView: BaseCodeEditor | undefined;
private resultView: ResultCodeEditor | undefined;
Expand All @@ -31,16 +33,19 @@ export class ActionsManager extends Disposable {
super();
}

private applyLineRangeEdits(edits: ISingleEditOperation[]): void {
private applyLineRangeEdits(edits: TLineRangeEdit): void {
if (!this.resultView) {
return;
}
const model = this.resultView.getModel();
const eol = model!.getEOL();

if (!model) {
return;
}

const modelLineCount = model.getLineCount();

/**
* 暂时先不处理 undo 和 redo 的情况
* 可放在第二期来实现
Expand All @@ -50,10 +55,26 @@ export class ActionsManager extends Disposable {
edits.map((edit) => {
const { range, text } = edit;

if (range.endLineNumberExclusive <= modelLineCount) {
return {
range: range.toRange(),
text: text ? text + eol : null,
};
}

if (range.startLineNumber === 1) {
return {
range: InnerRange.fromPositions(new Position(1, 1), new Position(modelLineCount, Number.MAX_SAFE_INTEGER)),
text: text ? text + eol : null,
};
}

return {
range,
isAutoWhitespaceEdit: false,
text,
range: InnerRange.fromPositions(
new Position(range.startLineNumber - 1, Number.MAX_SAFE_INTEGER),
new Position(modelLineCount, Number.MAX_SAFE_INTEGER),
),
text: text ? eol + text : null,
};
}),
false,
Expand Down Expand Up @@ -96,7 +117,7 @@ export class ActionsManager extends Disposable {
applyText: string;
eol: string;
},
) => ISingleEditOperation[],
) => TLineRangeEdit,
): void {
const mapping = this.pickMapping(range);
if (!mapping) {
Expand All @@ -108,7 +129,7 @@ export class ActionsManager extends Disposable {
const model = viewEditor!.getModel()!;
const eol = model.getEOL();

const applyText = model.getValueInRange(range.toRange());
const applyText = model.getValueInRange(range.toInclusiveRange());
const oppositeRange = mapping.adjacentComputeRangeMap.get(range.id);

if (!oppositeRange) {
Expand Down Expand Up @@ -168,11 +189,7 @@ export class ActionsManager extends Disposable {
const { text } = metaData;

// 为 null 则说明是删除文本
if (text) {
this.applyLineRangeEdits([{ range: range.toRange(), text: text + (range.isEmpty ? eol : '') }]);
} else {
this.applyLineRangeEdits([{ range: range.deltaStart(-1).toRange(Number.MAX_SAFE_INTEGER), text: null }]);
}
this.applyLineRangeEdits([{ range, text }]);

this.resultView?.updateActions();

Expand Down Expand Up @@ -231,7 +248,7 @@ export class ActionsManager extends Disposable {
this.applyLineRangeEdits([
{
text,
range: range.toRange(),
range,
},
]);

Expand Down Expand Up @@ -281,13 +298,8 @@ export class ActionsManager extends Disposable {
this.incomingView.onDidConflictActions,
)(({ range, action }) => {
if (action === ACCEPT_CURRENT_ACTIONS) {
this.handleAcceptChange(range, (range, oppositeRange, { applyText, eol }) => [
{
range: range.isEmpty
? oppositeRange.deltaStart(-1).toRange(Number.MAX_SAFE_INTEGER)
: oppositeRange.toRange(),
text: applyText + (oppositeRange.isEmpty ? eol : ''),
},
this.handleAcceptChange(range, (_range, oppositeRange, { applyText }) => [
{ range: oppositeRange, text: applyText },
]);
}

Expand Down Expand Up @@ -316,8 +328,8 @@ export class ActionsManager extends Disposable {
range: LineRange.fromPositions(
oppositeRange.endLineNumberExclusive,
oppositeRange.endLineNumberExclusive,
).toRange(),
text: applyText + eol,
),
text: applyText,
},
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,21 @@ export class ResultCodeEditor extends BaseCodeEditor {
deltaEdits.forEach((edits) => {
const { startLineNumber, endLineNumber, offset } = edits;

const toLineRange = LineRange.fromPositions(startLineNumber, endLineNumber);
const toLineRange = LineRange.fromPositions(startLineNumber, endLineNumber + Math.max(0, offset));
const { [EditorViewType.CURRENT]: includeLeftRange, [EditorViewType.INCOMING]: includeRightRange } =
this.mappingManagerService.findIncludeRanges(toLineRange);
/**
* 这里需要处理 touch 的情况(也就是 toLineRange 与 documentMapping 里的某一个 lineRange 有重叠的部分)
* 那么就要以当前 touch range 的结果作为要 delta 的起点
*/
const { [EditorViewType.CURRENT]: touchTurnLeftRange, [EditorViewType.INCOMING]: touchTurnRightRange } =
this.mappingManagerService.findTouchesRanges(toLineRange, false);
this.mappingManagerService.findTouchesRanges(toLineRange);
const { [EditorViewType.CURRENT]: nextTurnLeftRange, [EditorViewType.INCOMING]: nextTurnRightRange } =
this.mappingManagerService.findNextLineRanges(toLineRange);

if (includeLeftRange) {
this.documentMappingTurnLeft.deltaEndAdjacentQueue(includeLeftRange, offset);
} else if (touchTurnLeftRange && !toLineRange.isAfter(touchTurnLeftRange)) {
} else if (touchTurnLeftRange) {
this.documentMappingTurnLeft.deltaEndAdjacentQueue(touchTurnLeftRange, offset);
} else if (nextTurnLeftRange) {
const reverse = this.documentMappingTurnLeft.reverse(nextTurnLeftRange);
Expand All @@ -124,7 +124,7 @@ export class ResultCodeEditor extends BaseCodeEditor {

if (includeRightRange) {
this.documentMappingTurnRight.deltaEndAdjacentQueue(includeRightRange, offset);
} else if (touchTurnRightRange && !toLineRange.isAfter(touchTurnRightRange)) {
} else if (touchTurnRightRange) {
this.documentMappingTurnRight.deltaEndAdjacentQueue(touchTurnRightRange, offset);
} else if (nextTurnRightRange) {
const reverse = this.documentMappingTurnRight.reverse(nextTurnRightRange);
Expand Down Expand Up @@ -400,7 +400,7 @@ export class ResultCodeEditor extends BaseCodeEditor {
diffRanges.forEach((range) => {
this.timeMachineDocument.record(range.id, {
range,
text: range.isEmpty ? null : this.editor.getModel()!.getValueInRange(range.toRange()),
text: range.isEmpty ? null : this.editor.getModel()!.getValueInRange(range.toInclusiveRange()),
});
});
}
Expand Down

0 comments on commit 3a8c886

Please sign in to comment.