Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
b10fc8fc
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,发现更多精彩内容 >>
提交
b10fc8fc
编写于
11月 28, 2018
作者:
J
Joao Moreno
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
object tree model: preserve collapse with identity provider
fixes #63149
上级
067f46af
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
86 addition
and
3 deletion
+86
-3
src/vs/base/browser/ui/tree/objectTreeModel.ts
src/vs/base/browser/ui/tree/objectTreeModel.ts
+55
-3
src/vs/base/test/browser/ui/tree/objectTreeModel.test.ts
src/vs/base/test/browser/ui/tree/objectTreeModel.test.ts
+31
-0
未找到文件。
src/vs/base/browser/ui/tree/objectTreeModel.ts
浏览文件 @
b10fc8fc
...
...
@@ -4,18 +4,24 @@
*--------------------------------------------------------------------------------------------*/
import
{
ISpliceable
}
from
'
vs/base/common/sequence
'
;
import
{
Iterator
,
ISequence
}
from
'
vs/base/common/iterator
'
;
import
{
Iterator
,
ISequence
,
getSequenceIterator
}
from
'
vs/base/common/iterator
'
;
import
{
IndexTreeModel
,
IIndexTreeModelOptions
}
from
'
vs/base/browser/ui/tree/indexTreeModel
'
;
import
{
Event
}
from
'
vs/base/common/event
'
;
import
{
ITreeModel
,
ITreeNode
,
ITreeElement
}
from
'
vs/base/browser/ui/tree/tree
'
;
import
{
IIdentityProvider
}
from
'
vs/base/browser/ui/list/list
'
;
export
interface
IObjectTreeModelOptions
<
T
,
TFilterData
>
extends
IIndexTreeModelOptions
<
T
,
TFilterData
>
{
}
export
interface
IObjectTreeModelOptions
<
T
,
TFilterData
>
extends
IIndexTreeModelOptions
<
T
,
TFilterData
>
{
identityProvider
?:
IIdentityProvider
<
T
>
;
}
export
class
ObjectTreeModel
<
T
extends
NonNullable
<
any
>
,
TFilterData
=
void
>
implements
ITreeModel
<
T
,
TFilterData
,
T
>
{
private
model
:
IndexTreeModel
<
T
,
TFilterData
>
;
private
nodes
=
new
Map
<
T
,
ITreeNode
<
T
,
TFilterData
>>
();
private
identityProvider
:
IIdentityProvider
<
T
>
|
undefined
=
undefined
;
private
nodesByIdentity
:
Map
<
string
,
ITreeNode
<
T
,
TFilterData
>>
|
undefined
=
undefined
;
readonly
onDidChangeCollapseState
:
Event
<
ITreeNode
<
T
,
TFilterData
>>
;
readonly
onDidChangeRenderNodeCount
:
Event
<
ITreeNode
<
T
,
TFilterData
>>
;
...
...
@@ -23,6 +29,12 @@ export class ObjectTreeModel<T extends NonNullable<any>, TFilterData = void> imp
constructor
(
list
:
ISpliceable
<
ITreeNode
<
T
,
TFilterData
>>
,
options
:
IObjectTreeModelOptions
<
T
,
TFilterData
>
=
{})
{
this
.
model
=
new
IndexTreeModel
(
list
,
options
);
this
.
identityProvider
=
options
.
identityProvider
;
if
(
this
.
identityProvider
)
{
this
.
nodesByIdentity
=
new
Map
();
}
this
.
onDidChangeCollapseState
=
this
.
model
.
onDidChangeCollapseState
;
this
.
onDidChangeRenderNodeCount
=
this
.
model
.
onDidChangeRenderNodeCount
;
}
...
...
@@ -35,11 +47,20 @@ export class ObjectTreeModel<T extends NonNullable<any>, TFilterData = void> imp
):
Iterator
<
ITreeElement
<
T
>>
{
const
location
=
this
.
getElementLocation
(
element
);
const
insertedElements
=
new
Set
<
T
>
();
let
createdNodesByIdentity
:
Map
<
string
,
ITreeNode
<
T
,
TFilterData
>>
|
undefined
;
if
(
this
.
identityProvider
)
{
createdNodesByIdentity
=
new
Map
();
}
const
_onDidCreateNode
=
(
node
:
ITreeNode
<
T
,
TFilterData
>
)
=>
{
insertedElements
.
add
(
node
.
element
);
this
.
nodes
.
set
(
node
.
element
,
node
);
if
(
createdNodesByIdentity
)
{
createdNodesByIdentity
.
set
(
this
.
identityProvider
!
.
getId
(
node
.
element
).
toString
(),
node
);
}
if
(
onDidCreateNode
)
{
onDidCreateNode
(
node
);
}
...
...
@@ -50,12 +71,43 @@ export class ObjectTreeModel<T extends NonNullable<any>, TFilterData = void> imp
this
.
nodes
.
delete
(
node
.
element
);
}
if
(
this
.
nodesByIdentity
)
{
this
.
nodesByIdentity
.
delete
(
this
.
identityProvider
!
.
getId
(
node
.
element
).
toString
());
}
if
(
onDidDeleteNode
)
{
onDidDeleteNode
(
node
);
}
};
return
this
.
model
.
splice
([...
location
,
0
],
Number
.
MAX_VALUE
,
children
,
_onDidCreateNode
,
_onDidDeleteNode
);
const
preserveCollapseState
=
(
elements
:
ISequence
<
ITreeElement
<
T
>>
):
ISequence
<
ITreeElement
<
T
>>
=>
{
const
iterator
=
getSequenceIterator
(
elements
);
return
Iterator
.
map
(
iterator
,
treeElement
=>
{
const
identity
=
this
.
identityProvider
!
.
getId
(
treeElement
.
element
).
toString
();
const
node
=
this
.
nodesByIdentity
!
.
get
(
identity
);
const
collapsible
=
typeof
treeElement
.
collapsible
===
'
boolean
'
?
treeElement
.
collapsible
:
(
typeof
treeElement
.
collapsed
!==
'
undefined
'
);
return
{
...
treeElement
,
collapsible
,
collapsed
:
typeof
treeElement
.
collapsed
===
'
undefined
'
?
(
node
?
(
collapsible
&&
node
.
collapsed
)
:
undefined
)
:
treeElement
.
collapsed
,
children
:
treeElement
.
children
&&
preserveCollapseState
(
treeElement
.
children
)
};
});
};
if
(
children
&&
this
.
identityProvider
)
{
children
=
preserveCollapseState
(
children
);
}
const
result
=
this
.
model
.
splice
([...
location
,
0
],
Number
.
MAX_VALUE
,
children
,
_onDidCreateNode
,
_onDidDeleteNode
);
if
(
createdNodesByIdentity
)
{
createdNodesByIdentity
.
forEach
((
node
,
identity
)
=>
this
.
nodesByIdentity
!
.
set
(
identity
,
node
));
}
return
result
;
}
getParentElement
(
ref
:
T
|
null
=
null
):
T
|
null
{
...
...
src/vs/base/test/browser/ui/tree/objectTreeModel.test.ts
浏览文件 @
b10fc8fc
...
...
@@ -140,4 +140,35 @@ suite('ObjectTreeModel', function () {
model
.
setCollapsed
(
1
,
false
);
assert
.
deepEqual
(
toArray
(
list
),
[
1
,
11
,
111
,
112
,
2
]);
});
test
(
'
collapse state is lost without an identity provider
'
,
()
=>
{
const
list
:
ITreeNode
<
string
>
[]
=
[];
const
model
=
new
ObjectTreeModel
<
string
>
(
toSpliceable
(
list
),
{
collapseByDefault
:
true
});
const
data
=
[{
element
:
'
father
'
,
children
:
[{
element
:
'
child
'
}]
}];
model
.
setChildren
(
null
,
data
);
assert
.
deepEqual
(
toArray
(
list
),
[
'
father
'
]);
model
.
setCollapsed
(
'
father
'
,
false
);
assert
.
deepEqual
(
toArray
(
list
),
[
'
father
'
,
'
child
'
]);
model
.
setChildren
(
null
,
data
);
assert
.
deepEqual
(
toArray
(
list
),
[
'
father
'
]);
});
test
(
'
collapse state is preserved with an identity provider
'
,
()
=>
{
const
list
:
ITreeNode
<
string
>
[]
=
[];
const
identityProvider
=
{
getId
:
(
name
:
string
)
=>
name
};
const
model
=
new
ObjectTreeModel
<
string
>
(
toSpliceable
(
list
),
{
collapseByDefault
:
true
,
identityProvider
});
const
data
=
[{
element
:
'
father
'
,
children
:
[{
element
:
'
child
'
}]
}];
model
.
setChildren
(
null
,
data
);
assert
.
deepEqual
(
toArray
(
list
),
[
'
father
'
]);
model
.
setCollapsed
(
'
father
'
,
false
);
assert
.
deepEqual
(
toArray
(
list
),
[
'
father
'
,
'
child
'
]);
model
.
setChildren
(
null
,
data
);
assert
.
deepEqual
(
toArray
(
list
),
[
'
father
'
,
'
child
'
]);
});
});
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录