Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
e32913ce
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,发现更多精彩内容 >>
提交
e32913ce
编写于
5月 19, 2016
作者:
M
Martin Aeschlimann
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
json: Add jsonEdit
上级
c08fb277
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
184 addition
and
0 deletion
+184
-0
src/vs/base/common/jsonEdit.ts
src/vs/base/common/jsonEdit.ts
+94
-0
src/vs/base/test/common/jsonEdit.test.ts
src/vs/base/test/common/jsonEdit.test.ts
+90
-0
未找到文件。
src/vs/base/common/jsonEdit.ts
0 → 100644
浏览文件 @
e32913ce
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'
use strict
'
;
import
{
ParseError
,
parseTree
,
Segment
,
findNodeAtLocation
}
from
'
vs/base/common/json
'
;
import
{
Edit
,
FormattingOptions
,
format
}
from
'
vs/base/common/jsonFormatter
'
;
export
function
removeProperty
(
text
:
string
,
segments
:
Segment
[],
formattingOptions
:
FormattingOptions
)
:
Edit
[]
{
return
setProperty
(
text
,
segments
,
void
0
,
formattingOptions
);
}
export
function
setProperty
(
text
:
string
,
segments
:
Segment
[],
value
:
any
,
formattingOptions
:
FormattingOptions
,
getInsertionIndex
?:
(
properties
:
string
[])
=>
number
)
:
Edit
[]
{
let
lastSegment
=
segments
.
pop
();
if
(
typeof
lastSegment
!==
'
string
'
)
{
throw
new
Error
(
'
Last segment must be a property name
'
);
}
let
errors
:
ParseError
[]
=
[];
let
node
=
parseTree
(
text
,
errors
);
if
(
segments
.
length
>
0
)
{
node
=
findNodeAtLocation
(
node
,
segments
);
if
(
node
===
void
0
)
{
throw
new
Error
(
'
Cannot find object
'
);
}
}
if
(
node
&&
node
.
type
===
'
object
'
)
{
let
existing
=
findNodeAtLocation
(
node
,
[
lastSegment
]);
if
(
existing
!==
void
0
)
{
if
(
value
===
void
0
)
{
// delete
let
propertyIndex
=
node
.
children
.
indexOf
(
existing
.
parent
);
let
removeBegin
:
number
;
let
removeEnd
=
existing
.
parent
.
offset
+
existing
.
parent
.
length
;
if
(
propertyIndex
>
0
)
{
// remove the comma of the previous node
let
previous
=
node
.
children
[
propertyIndex
-
1
];
removeBegin
=
previous
.
offset
+
previous
.
length
;
}
else
{
removeBegin
=
node
.
offset
+
1
;
if
(
node
.
children
.
length
>
1
)
{
// remove the comma of the next node
let
next
=
node
.
children
[
1
];
removeEnd
=
next
.
offset
;
}
}
return
withFormatting
(
text
,
{
offset
:
removeBegin
,
length
:
removeEnd
-
removeBegin
,
content
:
''
},
formattingOptions
);
}
else
{
// set value of existing property
return
[{
offset
:
existing
.
offset
,
length
:
existing
.
length
,
content
:
JSON
.
stringify
(
value
)
}];
}
}
else
{
if
(
value
===
void
0
)
{
// delete
throw
new
Error
(
`Property
${
lastSegment
}
does not exist.`
);
}
let
newProperty
=
`
${
JSON
.
stringify
(
lastSegment
)}
:
${
JSON
.
stringify
(
value
)}
`
;
let
index
=
getInsertionIndex
?
getInsertionIndex
(
node
.
children
.
map
(
p
=>
p
.
children
[
0
].
value
))
:
node
.
children
.
length
;
let
edit
:
Edit
;
if
(
index
>
0
)
{
let
previous
=
node
.
children
[
index
-
1
];
edit
=
{
offset
:
previous
.
offset
+
previous
.
length
,
length
:
0
,
content
:
'
,
'
+
newProperty
};
}
else
if
(
node
.
children
.
length
===
0
)
{
edit
=
{
offset
:
node
.
offset
+
1
,
length
:
0
,
content
:
newProperty
};
}
else
{
edit
=
{
offset
:
node
.
offset
+
1
,
length
:
0
,
content
:
newProperty
+
'
,
'
};
}
return
withFormatting
(
text
,
edit
,
formattingOptions
);
}
}
else
{
throw
new
Error
(
'
Path does not reference an object
'
);
}
}
function
withFormatting
(
text
:
string
,
edit
:
Edit
,
formattingOptions
:
FormattingOptions
)
:
Edit
[]
{
// apply the edit
let
newText
=
text
.
substring
(
0
,
edit
.
offset
)
+
edit
.
content
+
text
.
substring
(
edit
.
offset
+
edit
.
length
);
// format the new text
let
begin
=
edit
.
offset
;
let
end
=
edit
.
offset
+
edit
.
content
.
length
;
let
edits
=
format
(
newText
,
{
offset
:
begin
,
length
:
end
-
begin
},
formattingOptions
);
// apply the formatting edits and track the begin and end offsets of the changes
for
(
let
i
=
edits
.
length
-
1
;
i
>=
0
;
i
--
)
{
let
edit
=
edits
[
i
];
newText
=
newText
.
substring
(
0
,
edit
.
offset
)
+
edit
.
content
+
newText
.
substring
(
edit
.
offset
+
edit
.
length
);
begin
=
Math
.
min
(
begin
,
edit
.
offset
);
end
=
Math
.
max
(
end
,
edit
.
offset
+
edit
.
length
);
end
+=
edit
.
content
.
length
-
edit
.
length
;
}
// create a single edit with all changes
let
editLength
=
text
.
length
-
(
newText
.
length
-
end
)
-
begin
;
return
[{
offset
:
begin
,
length
:
editLength
,
content
:
newText
.
substring
(
begin
,
end
)
}];
}
src/vs/base/test/common/jsonEdit.test.ts
0 → 100644
浏览文件 @
e32913ce
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'
use strict
'
;
import
json
=
require
(
'
vs/base/common/json
'
);
import
{
FormattingOptions
,
Edit
}
from
'
vs/base/common/jsonFormatter
'
;
import
{
setProperty
,
removeProperty
}
from
'
vs/base/common/jsonEdit
'
;
import
assert
=
require
(
'
assert
'
);
suite
(
'
JSON - edits
'
,
()
=>
{
function
assertEdit
(
content
:
string
,
edits
:
Edit
[],
expected
:
string
)
{
assert
(
edits
);
let
lastEditOffset
=
content
.
length
;
for
(
let
i
=
edits
.
length
-
1
;
i
>=
0
;
i
--
)
{
let
edit
=
edits
[
i
];
assert
(
edit
.
offset
>=
0
&&
edit
.
length
>=
0
&&
edit
.
offset
+
edit
.
length
<=
content
.
length
)
assert
(
typeof
edit
.
content
===
'
string
'
);
assert
(
lastEditOffset
>=
edit
.
offset
+
edit
.
length
);
// make sure all edits are ordered
lastEditOffset
=
edit
.
offset
;
content
=
content
.
substring
(
0
,
edit
.
offset
)
+
edit
.
content
+
content
.
substring
(
edit
.
offset
+
edit
.
length
);
}
assert
.
equal
(
content
,
expected
);
}
let
formatterOptions
:
FormattingOptions
=
{
insertSpaces
:
true
,
tabSize
:
2
,
eol
:
'
\n
'
}
test
(
'
set property
'
,
()
=>
{
let
content
=
'
{
\n
"x": "y"
\n
}
'
;
let
edits
=
setProperty
(
content
,
[
'
x
'
],
'
bar
'
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
{
\n
"x": "bar"
\n
}
'
);
});
test
(
'
insert property
'
,
()
=>
{
let
content
=
"
{}
"
;
let
edits
=
setProperty
(
content
,
[
'
foo
'
],
'
bar
'
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
{
\n
"foo": "bar"
\n
}
'
);
content
=
"
{
\n
}
"
;
edits
=
setProperty
(
content
,
[
'
foo
'
],
'
bar
'
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
{
\n
"foo": "bar"
\n
}
'
);
content
=
"
{
\n
}
"
;
edits
=
setProperty
(
content
,
[
'
foo
'
],
'
bar
'
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
{
\n
"foo": "bar"
\n
}
'
);
content
=
'
{
\n
"x": "y"
\n
}
'
;
edits
=
setProperty
(
content
,
[
'
foo
'
],
'
bar
'
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
{
\n
"x": "y",
\n
"foo": "bar"
\n
}
'
);
edits
=
setProperty
(
content
,
[
'
x
'
],
'
bar
'
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
{
\n
"x": "bar"
\n
}
'
);
content
=
'
{
\n
"x": {
\n
"a": 1,
\n
"b": true
\n
}
\n
}
\n
'
;
edits
=
setProperty
(
content
,
[
'
x
'
],
'
bar
'
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
{
\n
"x": "bar"
\n
}
\n
'
);
edits
=
setProperty
(
content
,
[
'
x
'
,
'
b
'
],
'
bar
'
,
formatterOptions
);
assertEdit
(
content
,
edits
,
'
{
\n
"x": {
\n
"a": 1,
\n
"b": "bar"
\n
}
\n
}
\n
'
);
edits
=
setProperty
(
content
,
[
'
x
'
,
'
c
'
],
'
bar
'
,
formatterOptions
,
()
=>
0
);
assertEdit
(
content
,
edits
,
'
{
\n
"x": {
\n
"c": "bar",
\n
"a": 1,
\n
"b": true
\n
}
\n
}
\n
'
);
edits
=
setProperty
(
content
,
[
'
x
'
,
'
c
'
],
'
bar
'
,
formatterOptions
,
()
=>
1
);
assertEdit
(
content
,
edits
,
'
{
\n
"x": {
\n
"a": 1,
\n
"c": "bar",
\n
"b": true
\n
}
\n
}
\n
'
);
edits
=
setProperty
(
content
,
[
'
x
'
,
'
c
'
],
'
bar
'
,
formatterOptions
,
()
=>
2
);
assertEdit
(
content
,
edits
,
'
{
\n
"x": {
\n
"a": 1,
\n
"b": true,
\n
"c": "bar"
\n
}
\n
}
\n
'
);
});
test
(
'
remove property
'
,
()
=>
{
let
content
=
'
{
\n
"x": "y"
\n
}
'
;
let
edits
=
removeProperty
(
content
,
[
'
x
'
],
formatterOptions
);
assertEdit
(
content
,
edits
,
'
{}
'
);
content
=
'
{
\n
"x": "y", "a": []
\n
}
'
;
edits
=
removeProperty
(
content
,
[
'
x
'
],
formatterOptions
);
assertEdit
(
content
,
edits
,
'
{
\n
"a": []
\n
}
'
);
content
=
'
{
\n
"x": "y", "a": []
\n
}
'
;
edits
=
removeProperty
(
content
,
[
'
a
'
],
formatterOptions
);
assertEdit
(
content
,
edits
,
'
{
\n
"x": "y"
\n
}
'
);
});
});
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录