Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
7be71103
V
vscode
项目概览
xxadev
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7be71103
编写于
11月 25, 2019
作者:
S
Sandeep Somavarapu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove text model and move to platform
上级
39464526
变更
4
展开全部
隐藏空白更改
内联
并排
Showing
4 changed file
with
382 addition
and
349 deletion
+382
-349
src/vs/platform/userDataSync/common/content.ts
src/vs/platform/userDataSync/common/content.ts
+64
-0
src/vs/platform/userDataSync/common/keybindingsMerge.ts
src/vs/platform/userDataSync/common/keybindingsMerge.ts
+259
-0
src/vs/platform/userDataSync/test/keybindingsMerge.test.ts
src/vs/platform/userDataSync/test/keybindingsMerge.test.ts
+59
-62
src/vs/workbench/services/keybinding/common/keybindingsMerge.ts
.../workbench/services/keybinding/common/keybindingsMerge.ts
+0
-287
未找到文件。
src/vs/platform/userDataSync/common/content.ts
0 → 100644
浏览文件 @
7be71103
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import
{
OperatingSystem
,
OS
}
from
'
vs/base/common/platform
'
;
import
{
JSONPath
}
from
'
vs/base/common/json
'
;
import
{
setProperty
}
from
'
vs/base/common/jsonEdit
'
;
export
function
edit
(
content
:
string
,
eol
:
string
,
originalPath
:
JSONPath
,
value
:
any
):
string
{
const
edit
=
setProperty
(
content
,
originalPath
,
value
,
{
tabSize
:
4
,
insertSpaces
:
false
,
eol
})[
0
];
if
(
edit
)
{
content
=
content
.
substring
(
0
,
edit
.
offset
)
+
edit
.
content
+
content
.
substring
(
edit
.
offset
+
edit
.
length
);
}
return
content
;
}
export
function
getLineStartOffset
(
content
:
string
,
eol
:
string
,
atOffset
:
number
):
number
{
let
lineStartingOffset
=
atOffset
;
while
(
lineStartingOffset
>=
0
)
{
if
(
content
.
charAt
(
lineStartingOffset
)
===
eol
.
charAt
(
eol
.
length
-
1
))
{
if
(
eol
.
length
===
1
)
{
return
lineStartingOffset
+
1
;
}
}
lineStartingOffset
--
;
if
(
eol
.
length
===
2
)
{
if
(
lineStartingOffset
>=
0
&&
content
.
charAt
(
lineStartingOffset
)
===
eol
.
charAt
(
0
))
{
return
lineStartingOffset
+
2
;
}
}
}
return
0
;
}
export
function
getLineEndOffset
(
content
:
string
,
eol
:
string
,
atOffset
:
number
):
number
{
let
lineEndOffset
=
atOffset
;
while
(
lineEndOffset
>=
0
)
{
if
(
content
.
charAt
(
lineEndOffset
)
===
eol
.
charAt
(
eol
.
length
-
1
))
{
if
(
eol
.
length
===
1
)
{
return
lineEndOffset
;
}
}
lineEndOffset
++
;
if
(
eol
.
length
===
2
)
{
if
(
lineEndOffset
>=
0
&&
content
.
charAt
(
lineEndOffset
)
===
eol
.
charAt
(
1
))
{
return
lineEndOffset
;
}
}
}
return
content
.
length
-
1
;
}
export
function
getEol
(
content
:
string
):
string
{
if
(
content
.
indexOf
(
'
\r\n
'
)
!==
-
1
)
{
return
'
\r\n
'
;
}
if
(
content
.
indexOf
(
'
\n
'
)
!==
-
1
)
{
return
'
\n
'
;
}
return
OS
===
OperatingSystem
.
Linux
||
OS
===
OperatingSystem
.
Macintosh
?
'
\n
'
:
'
\r\n
'
;
}
src/vs/platform/userDataSync/common/keybindingsMerge.ts
0 → 100644
浏览文件 @
7be71103
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import
*
as
objects
from
'
vs/base/common/objects
'
;
import
{
parse
,
findNodeAtLocation
,
parseTree
}
from
'
vs/base/common/json
'
;
import
{
values
,
keys
}
from
'
vs/base/common/map
'
;
import
{
IUserFriendlyKeybinding
}
from
'
vs/platform/keybinding/common/keybinding
'
;
import
{
firstIndex
as
findFirstIndex
,
equals
}
from
'
vs/base/common/arrays
'
;
import
{
ContextKeyExpr
}
from
'
vs/platform/contextkey/common/contextkey
'
;
import
*
as
contentUtil
from
'
vs/platform/userDataSync/common/content
'
;
export
function
mergeKeybindings
(
localContent
:
string
,
remoteContent
:
string
,
baseContent
:
string
|
null
):
{
mergeContent
:
string
,
hasChanges
:
boolean
,
hasConflicts
:
boolean
}
{
const
local
=
<
IUserFriendlyKeybinding
[]
>
parse
(
localContent
);
const
remote
=
<
IUserFriendlyKeybinding
[]
>
parse
(
remoteContent
);
const
base
=
baseContent
?
<
IUserFriendlyKeybinding
[]
>
parse
(
baseContent
)
:
null
;
const
byCommand
=
(
keybindings
:
IUserFriendlyKeybinding
[])
=>
{
const
map
:
Map
<
string
,
IUserFriendlyKeybinding
[]
>
=
new
Map
<
string
,
IUserFriendlyKeybinding
[]
>
();
for
(
const
keybinding
of
keybindings
)
{
const
command
=
keybinding
.
command
[
0
]
===
'
-
'
?
keybinding
.
command
.
substring
(
1
)
:
keybinding
.
command
;
let
value
=
map
.
get
(
command
);
if
(
!
value
)
{
value
=
[];
map
.
set
(
command
,
value
);
}
value
.
push
(
keybinding
);
}
return
map
;
};
const
localByCommand
=
byCommand
(
local
);
const
remoteByCommand
=
byCommand
(
remote
);
const
baseByCommand
=
base
?
byCommand
(
base
)
:
null
;
const
localToRemote
=
compare
(
localByCommand
,
remoteByCommand
);
if
(
localToRemote
.
added
.
size
===
0
&&
localToRemote
.
removed
.
size
===
0
&&
localToRemote
.
updated
.
size
===
0
)
{
// No changes found between local and remote.
return
{
mergeContent
:
localContent
,
hasChanges
:
false
,
hasConflicts
:
false
};
}
const
conflictCommands
:
Set
<
string
>
=
new
Set
<
string
>
();
const
baseToLocal
=
baseByCommand
?
compare
(
baseByCommand
,
localByCommand
)
:
{
added
:
keys
(
localByCommand
).
reduce
((
r
,
k
)
=>
{
r
.
add
(
k
);
return
r
;
},
new
Set
<
string
>
()),
removed
:
new
Set
<
string
>
(),
updated
:
new
Set
<
string
>
()
};
const
baseToRemote
=
baseByCommand
?
compare
(
baseByCommand
,
remoteByCommand
)
:
{
added
:
keys
(
remoteByCommand
).
reduce
((
r
,
k
)
=>
{
r
.
add
(
k
);
return
r
;
},
new
Set
<
string
>
()),
removed
:
new
Set
<
string
>
(),
updated
:
new
Set
<
string
>
()
};
const
eol
=
contentUtil
.
getEol
(
localContent
);
let
mergeContent
=
localContent
;
// Removed commands in Local
for
(
const
command
of
values
(
baseToLocal
.
removed
))
{
// Got updated in remote
if
(
baseToRemote
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
}
// Removed commands in Remote
for
(
const
command
of
values
(
baseToRemote
.
removed
))
{
if
(
conflictCommands
.
has
(
command
))
{
continue
;
}
// Got updated in local
if
(
baseToLocal
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
else
{
// remove the command
mergeContent
=
removeKeybindings
(
mergeContent
,
eol
,
command
);
}
}
// Added commands in Local
for
(
const
command
of
values
(
baseToLocal
.
added
))
{
if
(
conflictCommands
.
has
(
command
))
{
continue
;
}
// Got added in remote
if
(
baseToRemote
.
added
.
has
(
command
))
{
// Has different value
if
(
localToRemote
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
}
}
// Added commands in remote
for
(
const
command
of
values
(
baseToRemote
.
added
))
{
if
(
conflictCommands
.
has
(
command
))
{
continue
;
}
// Got added in local
if
(
baseToLocal
.
added
.
has
(
command
))
{
// Has different value
if
(
localToRemote
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
}
else
{
mergeContent
=
addKeybinding
(
mergeContent
,
eol
,
command
,
remoteByCommand
.
get
(
command
)
!
);
}
}
// Updated commands in Local
for
(
const
command
of
values
(
baseToLocal
.
updated
))
{
if
(
conflictCommands
.
has
(
command
))
{
continue
;
}
// Got updated in remote
if
(
baseToRemote
.
updated
.
has
(
command
))
{
// Has different value
if
(
localToRemote
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
}
}
// Updated commands in Remote
for
(
const
command
of
values
(
baseToRemote
.
updated
))
{
if
(
conflictCommands
.
has
(
command
))
{
continue
;
}
// Got updated in local
if
(
baseToLocal
.
updated
.
has
(
command
))
{
// Has different value
if
(
localToRemote
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
}
else
{
// update the command
mergeContent
=
updateKeybinding
(
mergeContent
,
eol
,
command
,
remoteByCommand
.
get
(
command
)
!
);
}
}
const
conflicts
:
{
command
:
string
,
local
:
IUserFriendlyKeybinding
[]
|
undefined
,
remote
:
IUserFriendlyKeybinding
[]
|
undefined
,
firstIndex
:
number
}[]
=
[];
for
(
const
command
of
values
(
conflictCommands
))
{
const
local
=
localByCommand
.
get
(
command
);
const
remote
=
remoteByCommand
.
get
(
command
);
mergeContent
=
updateKeybinding
(
mergeContent
,
eol
,
command
,
[...
local
||
[],
...
remote
||
[]]);
conflicts
.
push
({
command
,
local
,
remote
,
firstIndex
:
-
1
});
}
const
allKeybindings
=
<
IUserFriendlyKeybinding
[]
>
parse
(
mergeContent
);
for
(
const
conflict
of
conflicts
)
{
conflict
.
firstIndex
=
findFirstIndex
(
allKeybindings
,
keybinding
=>
keybinding
.
command
===
conflict
.
command
||
keybinding
.
command
===
`-
${
conflict
.
command
}
`
);
}
// Sort reverse so that conflicts content is added from last
conflicts
.
sort
((
a
,
b
)
=>
b
.
firstIndex
-
a
.
firstIndex
);
const
tree
=
parseTree
(
mergeContent
);
for
(
const
{
firstIndex
,
local
,
remote
}
of
conflicts
)
{
const
firstNode
=
findNodeAtLocation
(
tree
,
[
firstIndex
])
!
;
const
startLocalOffset
=
contentUtil
.
getLineStartOffset
(
mergeContent
,
eol
,
firstNode
.
offset
)
-
eol
.
length
;
let
endLocalOffset
=
startLocalOffset
;
let
remoteOffset
=
endLocalOffset
;
if
(
local
)
{
const
lastLocalValueNode
=
findNodeAtLocation
(
tree
,
[
firstIndex
+
local
.
length
-
1
])
!
;
endLocalOffset
=
contentUtil
.
getLineEndOffset
(
mergeContent
,
eol
,
lastLocalValueNode
.
offset
+
lastLocalValueNode
.
length
);
}
if
(
remote
)
{
const
lastRemoteValueNode
=
findNodeAtLocation
(
tree
,
[
firstIndex
+
(
local
?
local
.
length
:
0
)
+
remote
.
length
-
1
])
!
;
remoteOffset
=
contentUtil
.
getLineEndOffset
(
mergeContent
,
eol
,
lastRemoteValueNode
.
offset
+
lastRemoteValueNode
.
length
);
}
mergeContent
=
mergeContent
.
substring
(
0
,
startLocalOffset
)
+
`
${
eol
}
<<<<<<< local`
+
mergeContent
.
substring
(
startLocalOffset
,
endLocalOffset
)
+
`
${
eol
}
=======`
+
mergeContent
.
substring
(
endLocalOffset
,
remoteOffset
)
+
`
${
eol
}
>>>>>>> remote`
+
mergeContent
.
substring
(
remoteOffset
);
}
return
{
mergeContent
,
hasChanges
:
true
,
hasConflicts
:
conflicts
.
length
>
0
};
}
function
compare
(
from
:
Map
<
string
,
IUserFriendlyKeybinding
[]
>
,
to
:
Map
<
string
,
IUserFriendlyKeybinding
[]
>
):
{
added
:
Set
<
string
>
,
removed
:
Set
<
string
>
,
updated
:
Set
<
string
>
}
{
const
fromKeys
=
keys
(
from
);
const
toKeys
=
keys
(
to
);
const
added
=
toKeys
.
filter
(
key
=>
fromKeys
.
indexOf
(
key
)
===
-
1
).
reduce
((
r
,
key
)
=>
{
r
.
add
(
key
);
return
r
;
},
new
Set
<
string
>
());
const
removed
=
fromKeys
.
filter
(
key
=>
toKeys
.
indexOf
(
key
)
===
-
1
).
reduce
((
r
,
key
)
=>
{
r
.
add
(
key
);
return
r
;
},
new
Set
<
string
>
());
const
updated
:
Set
<
string
>
=
new
Set
<
string
>
();
for
(
const
key
of
fromKeys
)
{
if
(
removed
.
has
(
key
))
{
continue
;
}
const
value1
:
IUserFriendlyKeybinding
[]
=
from
.
get
(
key
)
!
;
const
value2
:
IUserFriendlyKeybinding
[]
=
to
.
get
(
key
)
!
;
if
(
!
areSameKeybindings
(
value1
,
value2
))
{
updated
.
add
(
key
);
}
}
return
{
added
,
removed
,
updated
};
}
function
areSameKeybindings
(
value1
:
IUserFriendlyKeybinding
[],
value2
:
IUserFriendlyKeybinding
[]):
boolean
{
// Compare entries adding keybindings
if
(
!
equals
(
value1
.
filter
(({
command
})
=>
command
[
0
]
!==
'
-
'
),
value2
.
filter
(({
command
})
=>
command
[
0
]
!==
'
-
'
),
(
a
,
b
)
=>
isSameKeybinding
(
a
,
b
)))
{
return
false
;
}
// Compare entries removing keybindings
if
(
!
equals
(
value1
.
filter
(({
command
})
=>
command
[
0
]
===
'
-
'
),
value2
.
filter
(({
command
})
=>
command
[
0
]
===
'
-
'
),
(
a
,
b
)
=>
isSameKeybinding
(
a
,
b
)))
{
return
false
;
}
return
true
;
}
function
isSameKeybinding
(
a
:
IUserFriendlyKeybinding
,
b
:
IUserFriendlyKeybinding
):
boolean
{
if
(
a
.
command
!==
b
.
command
)
{
return
false
;
}
if
(
a
.
key
!==
b
.
key
)
{
return
false
;
}
const
whenA
=
ContextKeyExpr
.
deserialize
(
a
.
when
);
const
whenB
=
ContextKeyExpr
.
deserialize
(
b
.
when
);
if
((
whenA
&&
!
whenB
)
||
(
!
whenA
&&
whenB
))
{
return
false
;
}
if
(
whenA
&&
whenB
&&
!
whenA
.
equals
(
whenB
))
{
return
false
;
}
if
(
!
objects
.
equals
(
a
.
args
,
b
.
args
))
{
return
false
;
}
return
true
;
}
function
addKeybinding
(
content
:
string
,
eol
:
string
,
command
:
string
,
keybindings
:
IUserFriendlyKeybinding
[]):
string
{
for
(
const
keybinding
of
keybindings
)
{
content
=
contentUtil
.
edit
(
content
,
eol
,
[
-
1
],
keybinding
);
}
return
content
;
}
function
removeKeybindings
(
content
:
string
,
eol
:
string
,
command
:
string
):
string
{
const
keybindings
=
<
IUserFriendlyKeybinding
[]
>
parse
(
content
);
for
(
let
index
=
keybindings
.
length
-
1
;
index
>=
0
;
index
--
)
{
if
(
keybindings
[
index
].
command
===
command
||
keybindings
[
index
].
command
===
`-
${
command
}
`
)
{
content
=
contentUtil
.
edit
(
content
,
eol
,
[
index
],
undefined
);
}
}
return
content
;
}
function
updateKeybinding
(
content
:
string
,
eol
:
string
,
command
:
string
,
keybindings
:
IUserFriendlyKeybinding
[]):
string
{
const
allKeybindings
=
<
IUserFriendlyKeybinding
[]
>
parse
(
content
);
const
location
=
findFirstIndex
(
allKeybindings
,
keybinding
=>
keybinding
.
command
===
command
||
keybinding
.
command
===
`-
${
command
}
`
);
// Remove all entries with this command
for
(
let
index
=
allKeybindings
.
length
-
1
;
index
>=
0
;
index
--
)
{
if
(
allKeybindings
[
index
].
command
===
command
||
allKeybindings
[
index
].
command
===
`-
${
command
}
`
)
{
content
=
contentUtil
.
edit
(
content
,
eol
,
[
index
],
undefined
);
}
}
// add all entries at the same location where the entry with this command was located.
for
(
let
index
=
keybindings
.
length
-
1
;
index
>=
0
;
index
--
)
{
content
=
contentUtil
.
edit
(
content
,
eol
,
[
location
],
keybindings
[
index
]);
}
return
content
;
}
src/vs/
workbench/services/keybinding/test/electron-browser
/keybindingsMerge.test.ts
→
src/vs/
platform/userDataSync/test
/keybindingsMerge.test.ts
浏览文件 @
7be71103
此差异已折叠。
点击以展开。
src/vs/workbench/services/keybinding/common/keybindingsMerge.ts
已删除
100644 → 0
浏览文件 @
39464526
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import
*
as
objects
from
'
vs/base/common/objects
'
;
import
{
parse
,
findNodeAtLocation
,
parseTree
,
JSONPath
}
from
'
vs/base/common/json
'
;
import
{
setProperty
}
from
'
vs/base/common/jsonEdit
'
;
import
{
IModelService
}
from
'
vs/editor/common/services/modelService
'
;
import
{
Position
}
from
'
vs/editor/common/core/position
'
;
import
{
values
,
keys
}
from
'
vs/base/common/map
'
;
import
{
IUserFriendlyKeybinding
}
from
'
vs/platform/keybinding/common/keybinding
'
;
import
{
firstIndex
as
findFirstIndex
,
equals
}
from
'
vs/base/common/arrays
'
;
import
{
registerSingleton
}
from
'
vs/platform/instantiation/common/extensions
'
;
import
{
IKeybindingsMergeService
}
from
'
vs/platform/userDataSync/common/userDataSync
'
;
import
{
ContextKeyExpr
}
from
'
vs/platform/contextkey/common/contextkey
'
;
export
class
KeybindingsMergeService
implements
IKeybindingsMergeService
{
_serviceBrand
:
undefined
;
constructor
(
@
IModelService
private
readonly
modelService
:
IModelService
)
{
}
async
merge
(
localContent
:
string
,
remoteContent
:
string
,
baseContent
:
string
|
null
):
Promise
<
{
mergeContent
:
string
,
hasChanges
:
boolean
,
hasConflicts
:
boolean
}
>
{
const
local
=
<
IUserFriendlyKeybinding
[]
>
parse
(
localContent
);
const
remote
=
<
IUserFriendlyKeybinding
[]
>
parse
(
remoteContent
);
const
base
=
baseContent
?
<
IUserFriendlyKeybinding
[]
>
parse
(
baseContent
)
:
null
;
const
byCommand
=
(
keybindings
:
IUserFriendlyKeybinding
[])
=>
{
const
map
:
Map
<
string
,
IUserFriendlyKeybinding
[]
>
=
new
Map
<
string
,
IUserFriendlyKeybinding
[]
>
();
for
(
const
keybinding
of
keybindings
)
{
const
command
=
keybinding
.
command
[
0
]
===
'
-
'
?
keybinding
.
command
.
substring
(
1
)
:
keybinding
.
command
;
let
value
=
map
.
get
(
command
);
if
(
!
value
)
{
value
=
[];
map
.
set
(
command
,
value
);
}
value
.
push
(
keybinding
);
}
return
map
;
};
const
localByCommand
=
byCommand
(
local
);
const
remoteByCommand
=
byCommand
(
remote
);
const
baseByCommand
=
base
?
byCommand
(
base
)
:
null
;
const
localToRemote
=
this
.
compare
(
localByCommand
,
remoteByCommand
);
if
(
localToRemote
.
added
.
size
===
0
&&
localToRemote
.
removed
.
size
===
0
&&
localToRemote
.
updated
.
size
===
0
)
{
// No changes found between local and remote.
return
{
mergeContent
:
localContent
,
hasChanges
:
false
,
hasConflicts
:
false
};
}
const
conflictCommands
:
Set
<
string
>
=
new
Set
<
string
>
();
const
baseToLocal
=
baseByCommand
?
this
.
compare
(
baseByCommand
,
localByCommand
)
:
{
added
:
keys
(
localByCommand
).
reduce
((
r
,
k
)
=>
{
r
.
add
(
k
);
return
r
;
},
new
Set
<
string
>
()),
removed
:
new
Set
<
string
>
(),
updated
:
new
Set
<
string
>
()
};
const
baseToRemote
=
baseByCommand
?
this
.
compare
(
baseByCommand
,
remoteByCommand
)
:
{
added
:
keys
(
remoteByCommand
).
reduce
((
r
,
k
)
=>
{
r
.
add
(
k
);
return
r
;
},
new
Set
<
string
>
()),
removed
:
new
Set
<
string
>
(),
updated
:
new
Set
<
string
>
()
};
let
mergeContent
=
localContent
;
const
eol
=
this
.
modelService
.
createModel
(
mergeContent
,
null
).
getEOL
();
// Removed commands in Local
for
(
const
command
of
values
(
baseToLocal
.
removed
))
{
// Got updated in remote
if
(
baseToRemote
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
}
// Removed commands in Remote
for
(
const
command
of
values
(
baseToRemote
.
removed
))
{
if
(
conflictCommands
.
has
(
command
))
{
continue
;
}
// Got updated in local
if
(
baseToLocal
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
else
{
// remove the command
mergeContent
=
this
.
removeKeybindings
(
mergeContent
,
eol
,
command
);
}
}
// Added commands in Local
for
(
const
command
of
values
(
baseToLocal
.
added
))
{
if
(
conflictCommands
.
has
(
command
))
{
continue
;
}
// Got added in remote
if
(
baseToRemote
.
added
.
has
(
command
))
{
// Has different value
if
(
localToRemote
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
}
}
// Added commands in remote
for
(
const
command
of
values
(
baseToRemote
.
added
))
{
if
(
conflictCommands
.
has
(
command
))
{
continue
;
}
// Got added in local
if
(
baseToLocal
.
added
.
has
(
command
))
{
// Has different value
if
(
localToRemote
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
}
else
{
mergeContent
=
this
.
addKeybinding
(
mergeContent
,
eol
,
command
,
remoteByCommand
.
get
(
command
)
!
);
}
}
// Updated commands in Local
for
(
const
command
of
values
(
baseToLocal
.
updated
))
{
if
(
conflictCommands
.
has
(
command
))
{
continue
;
}
// Got updated in remote
if
(
baseToRemote
.
updated
.
has
(
command
))
{
// Has different value
if
(
localToRemote
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
}
}
// Updated commands in Remote
for
(
const
command
of
values
(
baseToRemote
.
updated
))
{
if
(
conflictCommands
.
has
(
command
))
{
continue
;
}
// Got updated in local
if
(
baseToLocal
.
updated
.
has
(
command
))
{
// Has different value
if
(
localToRemote
.
updated
.
has
(
command
))
{
conflictCommands
.
add
(
command
);
}
}
else
{
// update the command
mergeContent
=
this
.
updateKeybinding
(
mergeContent
,
eol
,
command
,
remoteByCommand
.
get
(
command
)
!
);
}
}
const
conflicts
:
{
command
:
string
,
local
:
IUserFriendlyKeybinding
[]
|
undefined
,
remote
:
IUserFriendlyKeybinding
[]
|
undefined
,
firstIndex
:
number
}[]
=
[];
for
(
const
command
of
values
(
conflictCommands
))
{
const
local
=
localByCommand
.
get
(
command
);
const
remote
=
remoteByCommand
.
get
(
command
);
mergeContent
=
this
.
updateKeybinding
(
mergeContent
,
eol
,
command
,
[...
local
||
[],
...
remote
||
[]]);
conflicts
.
push
({
command
,
local
,
remote
,
firstIndex
:
-
1
});
}
const
allKeybindings
=
<
IUserFriendlyKeybinding
[]
>
parse
(
mergeContent
);
for
(
const
conflict
of
conflicts
)
{
conflict
.
firstIndex
=
findFirstIndex
(
allKeybindings
,
keybinding
=>
keybinding
.
command
===
conflict
.
command
||
keybinding
.
command
===
`-
${
conflict
.
command
}
`
);
}
// Sort reverse so that conflicts content is added from last
conflicts
.
sort
((
a
,
b
)
=>
b
.
firstIndex
-
a
.
firstIndex
);
const
model
=
this
.
modelService
.
createModel
(
mergeContent
,
null
);
const
tree
=
parseTree
(
mergeContent
);
for
(
const
{
firstIndex
,
local
,
remote
}
of
conflicts
)
{
const
firstNode
=
findNodeAtLocation
(
tree
,
[
firstIndex
])
!
;
const
fistNodePosition
=
model
.
getPositionAt
(
firstNode
.
offset
);
const
startLocalOffset
=
model
.
getOffsetAt
(
new
Position
(
fistNodePosition
.
lineNumber
-
1
,
model
.
getLineMaxColumn
(
fistNodePosition
.
lineNumber
-
1
)));
let
endLocalOffset
=
startLocalOffset
;
let
remoteOffset
=
endLocalOffset
;
if
(
local
)
{
const
lastLocalValueNode
=
findNodeAtLocation
(
tree
,
[
firstIndex
+
local
.
length
-
1
])
!
;
const
lastLocalValueEndPosition
=
model
.
getPositionAt
(
lastLocalValueNode
.
offset
+
lastLocalValueNode
.
length
);
endLocalOffset
=
model
.
getOffsetAt
(
new
Position
(
lastLocalValueEndPosition
.
lineNumber
,
model
.
getLineMaxColumn
(
lastLocalValueEndPosition
.
lineNumber
)));
}
if
(
remote
)
{
const
lastRemoteValueNode
=
findNodeAtLocation
(
tree
,
[
firstIndex
+
(
local
?
local
.
length
:
0
)
+
remote
.
length
-
1
])
!
;
const
lastRemoteValueEndPosition
=
model
.
getPositionAt
(
lastRemoteValueNode
.
offset
+
lastRemoteValueNode
.
length
);
remoteOffset
=
model
.
getOffsetAt
(
new
Position
(
lastRemoteValueEndPosition
.
lineNumber
,
model
.
getLineMaxColumn
(
lastRemoteValueEndPosition
.
lineNumber
)));
}
mergeContent
=
mergeContent
.
substring
(
0
,
startLocalOffset
)
+
`
${
eol
}
<<<<<<< local`
+
mergeContent
.
substring
(
startLocalOffset
,
endLocalOffset
)
+
`
${
eol
}
=======`
+
mergeContent
.
substring
(
endLocalOffset
,
remoteOffset
)
+
`
${
eol
}
>>>>>>> remote`
+
mergeContent
.
substring
(
remoteOffset
);
}
return
{
mergeContent
,
hasChanges
:
true
,
hasConflicts
:
conflicts
.
length
>
0
};
}
private
compare
(
from
:
Map
<
string
,
IUserFriendlyKeybinding
[]
>
,
to
:
Map
<
string
,
IUserFriendlyKeybinding
[]
>
):
{
added
:
Set
<
string
>
,
removed
:
Set
<
string
>
,
updated
:
Set
<
string
>
}
{
const
fromKeys
=
keys
(
from
);
const
toKeys
=
keys
(
to
);
const
added
=
toKeys
.
filter
(
key
=>
fromKeys
.
indexOf
(
key
)
===
-
1
).
reduce
((
r
,
key
)
=>
{
r
.
add
(
key
);
return
r
;
},
new
Set
<
string
>
());
const
removed
=
fromKeys
.
filter
(
key
=>
toKeys
.
indexOf
(
key
)
===
-
1
).
reduce
((
r
,
key
)
=>
{
r
.
add
(
key
);
return
r
;
},
new
Set
<
string
>
());
const
updated
:
Set
<
string
>
=
new
Set
<
string
>
();
for
(
const
key
of
fromKeys
)
{
if
(
removed
.
has
(
key
))
{
continue
;
}
const
value1
:
IUserFriendlyKeybinding
[]
=
from
.
get
(
key
)
!
;
const
value2
:
IUserFriendlyKeybinding
[]
=
to
.
get
(
key
)
!
;
if
(
!
this
.
areSameKeybindings
(
value1
,
value2
))
{
updated
.
add
(
key
);
}
}
return
{
added
,
removed
,
updated
};
}
private
areSameKeybindings
(
value1
:
IUserFriendlyKeybinding
[],
value2
:
IUserFriendlyKeybinding
[]):
boolean
{
// Compare entries adding keybindings
if
(
!
equals
(
value1
.
filter
(({
command
})
=>
command
[
0
]
!==
'
-
'
),
value2
.
filter
(({
command
})
=>
command
[
0
]
!==
'
-
'
),
(
a
,
b
)
=>
this
.
isSameKeybinding
(
a
,
b
)))
{
return
false
;
}
// Compare entries removing keybindings
if
(
!
equals
(
value1
.
filter
(({
command
})
=>
command
[
0
]
===
'
-
'
),
value2
.
filter
(({
command
})
=>
command
[
0
]
===
'
-
'
),
(
a
,
b
)
=>
this
.
isSameKeybinding
(
a
,
b
)))
{
return
false
;
}
return
true
;
}
private
isSameKeybinding
(
a
:
IUserFriendlyKeybinding
,
b
:
IUserFriendlyKeybinding
):
boolean
{
if
(
a
.
command
!==
b
.
command
)
{
return
false
;
}
if
(
a
.
key
!==
b
.
key
)
{
return
false
;
}
const
whenA
=
ContextKeyExpr
.
deserialize
(
a
.
when
);
const
whenB
=
ContextKeyExpr
.
deserialize
(
b
.
when
);
if
((
whenA
&&
!
whenB
)
||
(
!
whenA
&&
whenB
))
{
return
false
;
}
if
(
whenA
&&
whenB
&&
!
whenA
.
equals
(
whenB
))
{
return
false
;
}
if
(
!
objects
.
equals
(
a
.
args
,
b
.
args
))
{
return
false
;
}
return
true
;
}
private
addKeybinding
(
content
:
string
,
eol
:
string
,
command
:
string
,
keybindings
:
IUserFriendlyKeybinding
[]):
string
{
for
(
const
keybinding
of
keybindings
)
{
content
=
this
.
edit
(
content
,
eol
,
[
-
1
],
keybinding
);
}
return
content
;
}
private
removeKeybindings
(
content
:
string
,
eol
:
string
,
command
:
string
):
string
{
const
keybindings
=
<
IUserFriendlyKeybinding
[]
>
parse
(
content
);
for
(
let
index
=
keybindings
.
length
-
1
;
index
>=
0
;
index
--
)
{
if
(
keybindings
[
index
].
command
===
command
||
keybindings
[
index
].
command
===
`-
${
command
}
`
)
{
content
=
this
.
edit
(
content
,
eol
,
[
index
],
undefined
);
}
}
return
content
;
}
private
updateKeybinding
(
content
:
string
,
eol
:
string
,
command
:
string
,
keybindings
:
IUserFriendlyKeybinding
[]):
string
{
const
allKeybindings
=
<
IUserFriendlyKeybinding
[]
>
parse
(
content
);
const
location
=
findFirstIndex
(
allKeybindings
,
keybinding
=>
keybinding
.
command
===
command
||
keybinding
.
command
===
`-
${
command
}
`
);
// Remove all entries with this command
for
(
let
index
=
allKeybindings
.
length
-
1
;
index
>=
0
;
index
--
)
{
if
(
allKeybindings
[
index
].
command
===
command
||
allKeybindings
[
index
].
command
===
`-
${
command
}
`
)
{
content
=
this
.
edit
(
content
,
eol
,
[
index
],
undefined
);
}
}
// add all entries at the same location where the entry with this command was located.
for
(
let
index
=
keybindings
.
length
-
1
;
index
>=
0
;
index
--
)
{
content
=
this
.
edit
(
content
,
eol
,
[
location
],
keybindings
[
index
]);
}
return
content
;
}
private
edit
(
content
:
string
,
eol
:
string
,
originalPath
:
JSONPath
,
value
:
any
):
string
{
const
edit
=
setProperty
(
content
,
originalPath
,
value
,
{
tabSize
:
4
,
insertSpaces
:
false
,
eol
})[
0
];
if
(
edit
)
{
content
=
content
.
substring
(
0
,
edit
.
offset
)
+
edit
.
content
+
content
.
substring
(
edit
.
offset
+
edit
.
length
);
}
return
content
;
}
}
registerSingleton
(
IKeybindingsMergeService
,
KeybindingsMergeService
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录