Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
9d34bc55
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,发现更多精彩内容 >>
提交
9d34bc55
编写于
3月 20, 2017
作者:
S
Sandeep Somavarapu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#22799 Enhance json edit to support removing elements from array
上级
a92ddb56
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
46 addition
and
1 deletion
+46
-1
src/vs/base/common/jsonEdit.ts
src/vs/base/common/jsonEdit.ts
+21
-1
src/vs/base/test/common/jsonEdit.test.ts
src/vs/base/test/common/jsonEdit.test.ts
+25
-0
未找到文件。
src/vs/base/common/jsonEdit.ts
浏览文件 @
9d34bc55
...
...
@@ -81,6 +81,7 @@ export function setProperty(text: string, path: JSONPath, value: any, formatting
}
else
if
(
parent
.
type
===
'
array
'
&&
typeof
lastSegment
===
'
number
'
)
{
let
insertIndex
=
lastSegment
;
if
(
insertIndex
===
-
1
)
{
// Insert
let
newProperty
=
`
${
JSON
.
stringify
(
value
)}
`
;
let
edit
:
Edit
;
if
(
parent
.
children
.
length
===
0
)
{
...
...
@@ -91,7 +92,26 @@ export function setProperty(text: string, path: JSONPath, value: any, formatting
}
return
withFormatting
(
text
,
edit
,
formattingOptions
);
}
else
{
throw
new
Error
(
'
Array modification not supported yet
'
);
if
(
value
===
void
0
&&
parent
.
children
.
length
>=
0
)
{
//Removal
let
removalIndex
=
lastSegment
;
let
toRemove
=
parent
.
children
[
removalIndex
];
let
edit
:
Edit
;
if
(
parent
.
children
.
length
===
1
)
{
// only item
edit
=
{
offset
:
parent
.
offset
+
1
,
length
:
parent
.
length
-
2
,
content
:
''
};
}
else
if
(
parent
.
children
.
length
-
1
===
removalIndex
)
{
// last item
let
previous
=
parent
.
children
[
removalIndex
-
1
];
let
offset
=
previous
.
offset
+
previous
.
length
;
edit
=
{
offset
,
length
:
parent
.
length
-
2
-
offset
,
content
:
''
};
}
else
{
edit
=
{
offset
:
toRemove
.
offset
,
length
:
parent
.
children
[
removalIndex
+
1
].
offset
-
toRemove
.
offset
,
content
:
''
};
}
return
withFormatting
(
text
,
edit
,
formattingOptions
);
}
else
{
throw
new
Error
(
'
Array modification not supported yet
'
);
}
}
}
else
{
throw
new
Error
(
`Can not add
${
typeof
lastSegment
!==
'
number
'
?
'
index
'
:
'
property
'
}
to parent of type
${
parent
.
type
}
`
);
...
...
src/vs/base/test/common/jsonEdit.test.ts
浏览文件 @
9d34bc55
...
...
@@ -131,4 +131,29 @@ suite('JSON - edits', () => {
let
edits
=
setProperty
(
content
,
[
-
1
],
'
bar
'
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
[
\n
1,
\n
2,
\n
"bar"
\n
]
'
);
});
test
(
'
remove item in array with one item
'
,
()
=>
{
let
content
=
'
[
\n
1
\n
]
'
;
let
edits
=
setProperty
(
content
,
[
0
],
void
0
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
[]
'
);
});
test
(
'
remove item in the middle of the array
'
,
()
=>
{
let
content
=
'
[
\n
1,
\n
2,
\n
3
\n
]
'
;
let
edits
=
setProperty
(
content
,
[
1
],
void
0
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
[
\n
1,
\n
3
\n
]
'
);
});
test
(
'
remove last item in the array
'
,
()
=>
{
let
content
=
'
[
\n
1,
\n
2,
\n
"bar"
\n
]
'
;
let
edits
=
setProperty
(
content
,
[
2
],
void
0
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
[
\n
1,
\n
2
\n
]
'
);
});
test
(
'
remove last item in the array if ends with comman
'
,
()
=>
{
let
content
=
'
[
\n
1,
\n
"foo",
\n
"bar",
\n
]
'
;
let
edits
=
setProperty
(
content
,
[
2
],
void
0
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
[
\n
1,
\n
"foo"
\n
]
'
);
});
});
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录