1+ import { URI , UriComponents } from '@opensumi/ide-core-common' ;
12import type { ICodeEditor } from '@opensumi/monaco-editor-core/esm/vs/editor/browser/editorBrowser' ;
23import type { IEditor } from '@opensumi/monaco-editor-core/esm/vs/editor/common/editorCommon' ;
34import type { IEditorModel } from '@opensumi/monaco-editor-core/esm/vs/editor/common/editorCommon' ;
@@ -6,6 +7,125 @@ export interface IMergeEditorEditor extends IEditor {
67 getOursEditor ( ) : ICodeEditor ;
78 getResultEditor ( ) : ICodeEditor ;
89 getTheirsEditor ( ) : ICodeEditor ;
9- open ( oursTextModel : IEditorModel , resultTextModel : IEditorModel , theirsTextModel : IEditorModel ) : Promise < void > ;
10+ open ( openMergeEditorArgs : IOpenMergeEditorArgs ) : Promise < void > ;
1011 compare ( ) : Promise < void > ;
1112}
13+
14+ export class MergeEditorInputData {
15+ static from ( data : string ) : MergeEditorInputData {
16+ try {
17+ const obj : MergeEditorInputData = JSON . parse ( data ) ;
18+ return new MergeEditorInputData ( obj . uri , obj . detail , obj . description ) ;
19+ } catch ( error ) {
20+ throw Error ( 'invalid MergeEditorInputData parse' ) ;
21+ }
22+ }
23+
24+ private _textModel : IEditorModel ;
25+ public get textModel ( ) : IEditorModel {
26+ return this . _textModel ;
27+ }
28+
29+ constructor ( readonly uri : URI , readonly detail ?: string | undefined , readonly description ?: string | undefined ) { }
30+
31+ public toString ( ) : string {
32+ return JSON . stringify ( {
33+ uri : this . uri . toString ( ) ,
34+ detail : this . detail ,
35+ description : this . description ,
36+ } ) ;
37+ }
38+
39+ public setTextModel ( model : IEditorModel ) : this {
40+ this . _textModel = model ;
41+ return this ;
42+ }
43+ }
44+
45+ export interface IOpenMergeEditorArgs {
46+ ancestor : {
47+ uri : URI ;
48+ textModel : IEditorModel ;
49+ } ;
50+ input1 : MergeEditorInputData ;
51+ input2 : MergeEditorInputData ;
52+ output : {
53+ uri : URI ;
54+ textModel : IEditorModel ;
55+ } ;
56+ }
57+
58+ interface IValidateOpenArgs {
59+ ancestor : URI ;
60+ input1 : MergeEditorInputData ;
61+ input2 : MergeEditorInputData ;
62+ output : URI ;
63+ }
64+
65+ export namespace IRelaxedOpenMergeEditorArgs {
66+ export const validate = ( args : unknown ) : IValidateOpenArgs => {
67+ if ( ! args || typeof args !== 'object' ) {
68+ throw new TypeError ( 'invalid argument' ) ;
69+ }
70+
71+ const obj = args as IValidateOpenArgs ;
72+ const ancestor = toUri ( obj . ancestor ) ;
73+ const output = toUri ( obj . output ) ;
74+ const input1 = toInputData ( obj . input1 ) ;
75+ const input2 = toInputData ( obj . input2 ) ;
76+ return { ancestor, input1, input2, output } ;
77+ } ;
78+
79+ export const toString = ( args : IValidateOpenArgs ) : string => {
80+ const { ancestor, input1, input2, output } = args ;
81+
82+ return JSON . stringify ( {
83+ ancestor : ancestor . toString ( ) ,
84+ input1 : input1 . toString ( ) ,
85+ input2 : input2 . toString ( ) ,
86+ output : output . toString ( ) ,
87+ } ) ;
88+ } ;
89+
90+ const toInputData = ( args : unknown ) : MergeEditorInputData => {
91+ if ( typeof args === 'string' ) {
92+ return new MergeEditorInputData ( URI . parse ( args ) , undefined , undefined ) ;
93+ }
94+ if ( ! args || typeof args !== 'object' ) {
95+ throw new TypeError ( 'invalid argument' ) ;
96+ }
97+
98+ if ( isUriComponents ( args ) ) {
99+ return new MergeEditorInputData ( URI . from ( args ) , undefined , undefined ) ;
100+ }
101+
102+ const obj = args as MergeEditorInputData ;
103+ const uri = toUri ( obj . uri ) ;
104+ const detail = obj . detail ;
105+ const description = obj . description ;
106+ return new MergeEditorInputData ( uri , detail , description ) ;
107+ } ;
108+
109+ const toUri = ( args : unknown ) : URI => {
110+ if ( typeof args === 'string' ) {
111+ return URI . parse ( args ) ;
112+ } else if ( args && typeof args === 'object' ) {
113+ return URI . from ( args as UriComponents ) ;
114+ }
115+ throw new TypeError ( 'invalid argument' ) ;
116+ } ;
117+
118+ const isUriComponents = ( args : unknown ) : args is UriComponents => {
119+ if ( ! args || typeof args !== 'object' ) {
120+ return false ;
121+ }
122+ const obj = args as UriComponents ;
123+ return (
124+ typeof obj . scheme === 'string' &&
125+ typeof obj . authority === 'string' &&
126+ typeof obj . path === 'string' &&
127+ typeof obj . query === 'string' &&
128+ typeof obj . fragment === 'string'
129+ ) ;
130+ } ;
131+ }
0 commit comments