-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement conflict actions operations (#2014)
* feat: implement conflict actions operations * feat: implement conflict actions operations * refactor: decorations type * fix: conflict-actions * chore: improve code * refactor: document mapping * chore: rename type * chore: add note
- Loading branch information
Showing
18 changed files
with
712 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 65 additions & 18 deletions
83
packages/monaco/src/browser/contrib/merge-editor/model/conflict-actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,103 @@ | ||
import { Injectable, Optional } from '@opensumi/di'; | ||
import { Disposable } from '@opensumi/ide-core-common'; | ||
import { IEditorDecorationsCollection } from '@opensumi/monaco-editor-core/esm/vs/editor/common/editorCommon'; | ||
import { ModelDecorationOptions } from '@opensumi/monaco-editor-core/esm/vs/editor/common/model/textModel'; | ||
|
||
import { ICodeEditor, IModelDeltaDecoration } from '../../../monaco-api/editor'; | ||
import { IActionsDescription } from '../types'; | ||
import { BaseCodeEditor } from '../view/editors/baseCodeEditor'; | ||
|
||
import { LineRange } from './line-range'; | ||
export interface IActionsDecoration { | ||
id: string; | ||
readonly editorDecoration: IModelDeltaDecoration; | ||
} | ||
|
||
@Injectable({ multiple: false }) | ||
export class ConflictActions extends Disposable { | ||
private decorationsCollection: IEditorDecorationsCollection; | ||
private actionsCollect: Map<number, LineRange>; | ||
private deltaDecoration: IActionsDecoration[] = []; | ||
|
||
private actionsCollect: Map<number, IActionsDescription> = new Map(); | ||
|
||
private get editor(): ICodeEditor { | ||
return this.codeEditor.getEditor(); | ||
} | ||
|
||
constructor(@Optional() private readonly codeEditor: BaseCodeEditor) { | ||
super(); | ||
|
||
this.decorationsCollection = this.editor.createDecorationsCollection(); | ||
this.actionsCollect = new Map(); | ||
} | ||
|
||
public override dispose(): void { | ||
super.dispose(); | ||
this.decorationsCollection.clear(); | ||
this.actionsCollect.clear(); | ||
} | ||
|
||
public clearDecorations(): void { | ||
this.editor.changeDecorations((accessor) => { | ||
for (const decoration of this.deltaDecoration) { | ||
accessor.removeDecoration(decoration.id); | ||
} | ||
this.deltaDecoration = []; | ||
}); | ||
} | ||
|
||
public clearDecorationsById(id: string): void { | ||
this.editor.changeDecorations((accessor) => { | ||
if (id) { | ||
accessor.removeDecoration(id); | ||
|
||
this.deltaDecoration = this.deltaDecoration.filter((d) => d.id !== id); | ||
} | ||
}); | ||
} | ||
|
||
public setActions(actions: IActionsDescription[]): void { | ||
const newDecorations: IModelDeltaDecoration[] = actions.map((action) => { | ||
const newDecorations: IActionsDecoration[] = actions.map((action) => { | ||
const { range } = action; | ||
this.actionsCollect.set(range.startLineNumber, range); | ||
this.actionsCollect.set(range.startLineNumber, action); | ||
|
||
return { | ||
range: { | ||
startLineNumber: range.startLineNumber, | ||
startColumn: 0, | ||
endLineNumber: range.startLineNumber, | ||
endColumn: 0, | ||
id: '', | ||
editorDecoration: { | ||
range: { | ||
startLineNumber: range.startLineNumber, | ||
startColumn: 0, | ||
endLineNumber: range.startLineNumber, | ||
endColumn: 0, | ||
}, | ||
options: ModelDecorationOptions.register({ | ||
description: range.id, | ||
...action.decorationOptions, | ||
}), | ||
}, | ||
options: ModelDecorationOptions.register(action.decorationOptions), | ||
}; | ||
}); | ||
|
||
this.decorationsCollection.set(newDecorations); | ||
this.editor.changeDecorations((accessor) => { | ||
accessor | ||
.deltaDecorations( | ||
this.deltaDecoration.map((d) => d.id), | ||
newDecorations.map((d) => d.editorDecoration), | ||
) | ||
.forEach((id, i) => (newDecorations[i].id = id)); | ||
this.deltaDecoration = newDecorations; | ||
}); | ||
} | ||
|
||
public clearActions(line: number): void { | ||
if (this.actionsCollect.has(line)) { | ||
const actions = this.actionsCollect.get(line); | ||
|
||
const matchDecoration = this.deltaDecoration.find( | ||
(d) => d.editorDecoration.options.description === actions!.range.id, | ||
); | ||
if (matchDecoration) { | ||
this.clearDecorationsById(matchDecoration.id); | ||
} | ||
|
||
this.actionsCollect.delete(line); | ||
} | ||
} | ||
|
||
public getActions(line: number): LineRange | undefined { | ||
public getActions(line: number): IActionsDescription | undefined { | ||
return this.actionsCollect.get(line); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.