Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
9bc92b48
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,发现更多精彩内容 >>
未验证
提交
9bc92b48
编写于
2月 18, 2020
作者:
A
Alex Dima
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Scaffold IUndoRedoService
上级
69e0508b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
314 addition
and
0 deletion
+314
-0
src/vs/platform/undoRedo/common/undoRedo.ts
src/vs/platform/undoRedo/common/undoRedo.ts
+73
-0
src/vs/platform/undoRedo/common/undoRedoService.ts
src/vs/platform/undoRedo/common/undoRedoService.ts
+241
-0
未找到文件。
src/vs/platform/undoRedo/common/undoRedo.ts
0 → 100644
浏览文件 @
9bc92b48
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import
{
createDecorator
}
from
'
vs/platform/instantiation/common/instantiation
'
;
import
{
URI
}
from
'
vs/base/common/uri
'
;
export
const
IUndoRedoService
=
createDecorator
<
IUndoRedoService
>
(
'
undoRedoService
'
);
export
interface
IUndoRedoContext
{
replaceCurrentElement
(
others
:
IUndoRedoElement
[]):
void
;
}
export
interface
IUndoRedoElement
{
/**
* None, one or multiple resources that this undo/redo element impacts.
*/
readonly
resources
:
URI
[];
/**
* The label of the undo/redo element.
*/
readonly
label
:
string
;
/**
* Undo.
* Will always be called before `redo`.
* Can be called multiple times.
* e.g. `undo` -> `redo` -> `undo` -> `redo`
*/
undo
(
ctx
:
IUndoRedoContext
):
void
;
/**
* Redo.
* Will always be called after `undo`.
* Can be called multiple times.
* e.g. `undo` -> `redo` -> `undo` -> `redo`
*/
redo
(
ctx
:
IUndoRedoContext
):
void
;
/**
* Invalidate the edits concerning `resource`.
* i.e. the undo/redo stack for that particular resource has been destroyed.
*/
invalidate
(
resource
:
URI
):
boolean
;
}
export
interface
IUndoRedoService
{
_serviceBrand
:
undefined
;
/**
* Add a new element to the `undo` stack.
* This will destroy the `redo` stack.
*/
pushElement
(
element
:
IUndoRedoElement
):
void
;
/**
* Get the last pushed element. If the last pushed element has been undone, returns null.
*/
getLastElement
(
resource
:
URI
):
IUndoRedoElement
|
null
;
/**
* Remove elements that target `resource`.
*/
removeElements
(
resource
:
URI
):
void
;
canUndo
(
resource
:
URI
):
boolean
;
undo
(
resource
:
URI
):
void
;
redo
(
resource
:
URI
):
void
;
canRedo
(
resource
:
URI
):
boolean
;
}
src/vs/platform/undoRedo/common/undoRedoService.ts
0 → 100644
浏览文件 @
9bc92b48
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import
{
IUndoRedoService
,
IUndoRedoElement
}
from
'
vs/platform/undoRedo/common/undoRedo
'
;
import
{
URI
}
from
'
vs/base/common/uri
'
;
import
{
getComparisonKey
as
uriGetComparisonKey
}
from
'
vs/base/common/resources
'
;
import
{
onUnexpectedError
}
from
'
vs/base/common/errors
'
;
class
StackElement
{
public
readonly
actual
:
IUndoRedoElement
;
public
readonly
label
:
string
;
public
readonly
resources
:
URI
[];
public
readonly
strResources
:
string
[];
constructor
(
actual
:
IUndoRedoElement
)
{
this
.
actual
=
actual
;
this
.
label
=
actual
.
label
;
this
.
resources
=
actual
.
resources
;
this
.
strResources
=
this
.
resources
.
map
(
resource
=>
uriGetComparisonKey
(
resource
));
}
public
invalidate
(
resource
:
URI
):
void
{
if
(
this
.
resources
.
length
>
1
)
{
this
.
actual
.
invalidate
(
resource
);
}
}
}
class
ResourceEditStack
{
public
resource
:
URI
;
public
past
:
StackElement
[];
public
future
:
StackElement
[];
constructor
(
resource
:
URI
)
{
this
.
resource
=
resource
;
this
.
past
=
[];
this
.
future
=
[];
}
}
export
class
UndoRedoService
implements
IUndoRedoService
{
_serviceBrand
:
undefined
;
private
readonly
_editStacks
:
Map
<
string
,
ResourceEditStack
>
;
constructor
()
{
this
.
_editStacks
=
new
Map
<
string
,
ResourceEditStack
>
();
}
public
pushElement
(
_element
:
IUndoRedoElement
):
void
{
const
element
=
new
StackElement
(
_element
);
for
(
let
i
=
0
,
len
=
element
.
resources
.
length
;
i
<
len
;
i
++
)
{
const
resource
=
element
.
resources
[
i
];
const
strResource
=
element
.
strResources
[
i
];
let
editStack
:
ResourceEditStack
;
if
(
this
.
_editStacks
.
has
(
strResource
))
{
editStack
=
this
.
_editStacks
.
get
(
strResource
)
!
;
}
else
{
editStack
=
new
ResourceEditStack
(
resource
);
this
.
_editStacks
.
set
(
strResource
,
editStack
);
}
// remove the future
for
(
const
futureElement
of
editStack
.
future
)
{
futureElement
.
invalidate
(
resource
);
}
editStack
.
future
=
[];
editStack
.
past
.
push
(
element
);
}
}
public
getLastElement
(
resource
:
URI
):
IUndoRedoElement
|
null
{
const
strResource
=
uriGetComparisonKey
(
resource
);
if
(
this
.
_editStacks
.
has
(
strResource
))
{
const
editStack
=
this
.
_editStacks
.
get
(
strResource
)
!
;
if
(
editStack
.
future
.
length
>
0
)
{
return
null
;
}
if
(
editStack
.
past
.
length
===
0
)
{
return
null
;
}
return
editStack
.
past
[
editStack
.
past
.
length
-
1
].
actual
;
}
return
null
;
}
public
removeElements
(
resource
:
URI
):
void
{
const
strResource
=
uriGetComparisonKey
(
resource
);
if
(
this
.
_editStacks
.
has
(
strResource
))
{
const
editStack
=
this
.
_editStacks
.
get
(
strResource
)
!
;
for
(
const
pastElement
of
editStack
.
past
)
{
pastElement
.
invalidate
(
resource
);
}
for
(
const
futureElement
of
editStack
.
future
)
{
futureElement
.
invalidate
(
resource
);
}
this
.
_editStacks
.
delete
(
strResource
);
}
}
public
canUndo
(
resource
:
URI
):
boolean
{
const
strResource
=
uriGetComparisonKey
(
resource
);
if
(
this
.
_editStacks
.
has
(
strResource
))
{
const
editStack
=
this
.
_editStacks
.
get
(
strResource
)
!
;
return
(
editStack
.
past
.
length
>
0
);
}
return
false
;
}
public
undo
(
resource
:
URI
):
void
{
const
strResource
=
uriGetComparisonKey
(
resource
);
if
(
!
this
.
_editStacks
.
has
(
strResource
))
{
return
;
}
const
editStack
=
this
.
_editStacks
.
get
(
strResource
)
!
;
if
(
editStack
.
past
.
length
===
0
)
{
return
;
}
const
element
=
editStack
.
past
[
editStack
.
past
.
length
-
1
];
let
replaceCurrentElement
:
IUndoRedoElement
[]
|
null
=
null
as
IUndoRedoElement
[]
|
null
;
try
{
element
.
actual
.
undo
({
replaceCurrentElement
:
(
others
:
IUndoRedoElement
[]):
void
=>
{
replaceCurrentElement
=
others
;
}
});
}
catch
(
e
)
{
onUnexpectedError
(
e
);
editStack
.
past
.
pop
();
editStack
.
future
.
push
(
element
);
return
;
}
if
(
replaceCurrentElement
===
null
)
{
// regular case
editStack
.
past
.
pop
();
editStack
.
future
.
push
(
element
);
return
;
}
const
replaceCurrentElementMap
=
new
Map
<
string
,
StackElement
>
();
for
(
const
_replace
of
replaceCurrentElement
)
{
const
replace
=
new
StackElement
(
_replace
);
for
(
const
strResource
of
replace
.
strResources
)
{
replaceCurrentElementMap
.
set
(
strResource
,
replace
);
}
}
for
(
let
i
=
0
,
len
=
element
.
strResources
.
length
;
i
<
len
;
i
++
)
{
const
strResource
=
element
.
strResources
[
i
];
if
(
this
.
_editStacks
.
has
(
strResource
))
{
const
editStack
=
this
.
_editStacks
.
get
(
strResource
)
!
;
for
(
let
j
=
editStack
.
past
.
length
-
1
;
j
>=
0
;
j
--
)
{
if
(
editStack
.
past
[
j
]
===
element
)
{
if
(
replaceCurrentElementMap
.
has
(
strResource
))
{
editStack
.
past
[
j
]
=
replaceCurrentElementMap
.
get
(
strResource
)
!
;
}
else
{
editStack
.
past
.
splice
(
j
,
1
);
}
break
;
}
}
}
}
}
public
canRedo
(
resource
:
URI
):
boolean
{
const
strResource
=
uriGetComparisonKey
(
resource
);
if
(
this
.
_editStacks
.
has
(
strResource
))
{
const
editStack
=
this
.
_editStacks
.
get
(
strResource
)
!
;
return
(
editStack
.
future
.
length
>
0
);
}
return
false
;
}
redo
(
resource
:
URI
):
void
{
const
strResource
=
uriGetComparisonKey
(
resource
);
if
(
!
this
.
_editStacks
.
has
(
strResource
))
{
return
;
}
const
editStack
=
this
.
_editStacks
.
get
(
strResource
)
!
;
if
(
editStack
.
future
.
length
===
0
)
{
return
;
}
const
element
=
editStack
.
future
[
editStack
.
future
.
length
-
1
];
let
replaceCurrentElement
:
IUndoRedoElement
[]
|
null
=
null
as
IUndoRedoElement
[]
|
null
;
try
{
element
.
actual
.
redo
({
replaceCurrentElement
:
(
others
:
IUndoRedoElement
[]):
void
=>
{
replaceCurrentElement
=
others
;
}
});
}
catch
(
e
)
{
onUnexpectedError
(
e
);
editStack
.
future
.
pop
();
editStack
.
past
.
push
(
element
);
return
;
}
if
(
replaceCurrentElement
===
null
)
{
// regular case
editStack
.
future
.
pop
();
editStack
.
past
.
push
(
element
);
return
;
}
const
replaceCurrentElementMap
=
new
Map
<
string
,
StackElement
>
();
for
(
const
_replace
of
replaceCurrentElement
)
{
const
replace
=
new
StackElement
(
_replace
);
for
(
const
strResource
of
replace
.
strResources
)
{
replaceCurrentElementMap
.
set
(
strResource
,
replace
);
}
}
for
(
let
i
=
0
,
len
=
element
.
strResources
.
length
;
i
<
len
;
i
++
)
{
const
strResource
=
element
.
strResources
[
i
];
if
(
this
.
_editStacks
.
has
(
strResource
))
{
const
editStack
=
this
.
_editStacks
.
get
(
strResource
)
!
;
for
(
let
j
=
editStack
.
future
.
length
-
1
;
j
>=
0
;
j
--
)
{
if
(
editStack
.
future
[
j
]
===
element
)
{
if
(
replaceCurrentElementMap
.
has
(
strResource
))
{
editStack
.
future
[
j
]
=
replaceCurrentElementMap
.
get
(
strResource
)
!
;
}
else
{
editStack
.
future
.
splice
(
j
,
1
);
}
break
;
}
}
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录