Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
徽霖
Vscode
提交
078c3c18
V
Vscode
项目概览
徽霖
/
Vscode
通知
9
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
Vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
078c3c18
编写于
9月 20, 2016
作者:
J
Johannes Rieken
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix #9764
上级
0a0bfedb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
268 addition
and
90 deletion
+268
-90
src/vs/editor/contrib/snippet/common/snippetController.ts
src/vs/editor/contrib/snippet/common/snippetController.ts
+59
-23
src/vs/editor/contrib/snippet/test/common/snippetController.test.ts
...tor/contrib/snippet/test/common/snippetController.test.ts
+209
-67
未找到文件。
src/vs/editor/contrib/snippet/common/snippetController.ts
浏览文件 @
078c3c18
...
...
@@ -381,7 +381,7 @@ export class SnippetController {
public
run
(
snippet
:
CodeSnippet
,
overwriteBefore
:
number
,
overwriteAfter
:
number
,
stripPrefix
?:
boolean
):
void
{
this
.
_runAndRestoreController
(()
=>
{
if
(
snippet
.
isInsertOnly
/* || snippet.isSingleTabstopOnly*/
)
{
if
(
snippet
.
isInsertOnly
||
snippet
.
isSingleTabstopOnly
)
{
// Only inserts text, not placeholders, tabstops etc
// Only cursor endposition
this
.
_runForAllSelections
(
snippet
,
overwriteBefore
,
overwriteAfter
,
stripPrefix
);
...
...
@@ -483,41 +483,77 @@ export class SnippetController {
const
edits
:
editorCommon
.
IIdentifiedSingleEditOperation
[]
=
[];
const
selections
=
this
.
_editor
.
getSelections
();
let
lineDelta
=
0
;
let
columnDelta
=
0
;
const
model
=
this
.
_editor
.
getModel
();
let
totalDelta
=
0
;
const
newSelections
:
{
offset
:
number
;
i
:
number
}[]
=
[];
// sort selections by start position but remember where
// each selection came from
const
selectionEntries
=
selections
.
map
((
selection
,
i
)
=>
({
selection
,
i
}))
.
sort
((
a
,
b
)
=>
Range
.
compareRangesUsingStarts
(
a
.
selection
,
b
.
selection
));
for
(
const
{
selection
,
i
}
of
selectionEntries
)
{
let
{
adaptedSnippet
,
typeRange
}
=
SnippetController
.
_prepareSnippet
(
this
.
_editor
,
selection
,
snippet
,
overwriteBefore
,
overwriteAfter
,
stripPrefix
);
for
(
let
i
=
0
;
i
<
selections
.
length
;
i
++
)
{
let
{
adaptedSnippet
,
typeRange
}
=
SnippetController
.
_prepareSnippet
(
this
.
_editor
,
selections
[
i
],
snippet
,
overwriteBefore
,
overwriteAfter
,
stripPrefix
);
SnippetController
.
_addCommandForSnippet
(
this
.
_editor
.
getModel
(),
adaptedSnippet
,
typeRange
,
edits
);
if
(
i
===
0
&&
snippet
.
isSingleTabstopOnly
)
{
const
finalCursorPos
=
SnippetController
.
_getSnippetCursorOnly
(
adaptedSnippet
);
const
editEnd
=
typeRange
.
getEndPosition
();
editEnd
.
lineNumber
+=
adaptedSnippet
.
lines
.
length
-
1
;
editEnd
.
column
=
adaptedSnippet
.
lines
[
adaptedSnippet
.
lines
.
length
-
1
].
length
+
1
;
// compute new selection offset
// * get current offset
// * get length of snippet that we insert
// * get final cursor position of snippet that we insert (might not exist)
// * NEW selection offset is current + final cursor pos + inserts_until_here
let
offset
=
model
.
getOffsetAt
(
typeRange
.
getStartPosition
());
// inserts until here
offset
+=
totalDelta
;
// each snippet has a different length (because of whitespace changes)
let
snippetLength
=
(
adaptedSnippet
.
lines
.
length
-
1
)
*
model
.
getEOL
().
length
;
for
(
const
line
of
adaptedSnippet
.
lines
)
{
snippetLength
+=
line
.
length
;
}
// each snippet has a different cursor offset
const
finalCursorPos
=
SnippetController
.
_getSnippetCursorOnly
(
adaptedSnippet
);
if
(
finalCursorPos
)
{
let
finalCursorOffset
:
number
;
if
(
finalCursorPos
.
lineNumber
===
typeRange
.
startLineNumber
)
{
finalCursorOffset
=
finalCursorPos
.
column
-
typeRange
.
startColumn
;
}
else
{
finalCursorOffset
=
finalCursorPos
.
column
-
1
;
for
(
let
i
=
0
,
lineNumber
=
typeRange
.
startLineNumber
;
lineNumber
<
finalCursorPos
.
lineNumber
;
i
++
,
lineNumber
++
)
{
finalCursorOffset
+=
adaptedSnippet
.
lines
[
i
].
length
+
model
.
getEOL
().
length
;
}
}
offset
+=
finalCursorOffset
;
lineDelta
=
finalCursorPos
.
lineNumber
-
editEnd
.
lineNumber
;
columnDelta
=
finalCursorPos
.
column
-
editEnd
.
column
;
}
else
{
offset
+=
snippetLength
;
}
newSelections
.
push
({
offset
,
i
});
totalDelta
+=
(
snippetLength
-
model
.
getValueLengthInRange
(
typeRange
));
}
if
(
edits
.
length
===
0
)
{
return
;
}
const
cursorStateComputer
:
editorCommon
.
ICursorStateComputer
=
function
(
inverseEdits
)
{
return
inverseEdits
.
map
((
edit
,
i
)
=>
{
let
{
endLineNumber
,
endColumn
}
=
edit
.
range
;
endLineNumber
+=
lineDelta
;
endColumn
+=
columnDelta
;
return
new
Selection
(
endLineNumber
,
endColumn
,
endLineNumber
,
endColumn
);
});
const
cursorStateComputer
:
editorCommon
.
ICursorStateComputer
=
function
()
{
// create new selections from the new selection offsets
// and restore the order we had at the beginning
const
result
:
Selection
[]
=
[];
for
(
const
{
offset
,
i
}
of
newSelections
)
{
const
pos
=
model
.
getPositionAt
(
offset
);
result
[
i
]
=
new
Selection
(
pos
.
lineNumber
,
pos
.
column
,
pos
.
lineNumber
,
pos
.
column
);
}
return
result
;
};
const
model
=
this
.
_editor
.
getModel
();
model
.
pushStackElement
();
this
.
_editor
.
setSelections
(
model
.
pushEditOperations
(
selections
,
edits
,
cursorStateComputer
));
model
.
pushStackElement
();
...
...
src/vs/editor/contrib/snippet/test/common/snippetController.test.ts
浏览文件 @
078c3c18
...
...
@@ -23,14 +23,19 @@ class TestSnippetController extends SnippetController {
suite
(
'
SnippetController
'
,
()
=>
{
function
snippetTest
(
cb
:(
editor
:
MockCodeEditor
,
cursor
:
Cursor
,
codeSnippet
:
CodeSnippet
,
snippetController
:
TestSnippetController
)
=>
void
):
void
{
withMockCodeEditor
([
'
function test() {
'
,
'
\t
var x = 3;
'
,
'
\t
var arr = [];
'
,
'
\t
'
,
'
}
'
],
{},
(
editor
,
cursor
)
=>
{
function
snippetTest
(
cb
:
(
editor
:
MockCodeEditor
,
cursor
:
Cursor
,
codeSnippet
:
CodeSnippet
,
snippetController
:
TestSnippetController
)
=>
void
,
lines
?:
string
[]):
void
{
if
(
!
lines
)
{
lines
=
[
'
function test() {
'
,
'
\t
var x = 3;
'
,
'
\t
var arr = [];
'
,
'
\t
'
,
'
}
'
];
};
withMockCodeEditor
(
lines
,
{},
(
editor
,
cursor
)
=>
{
editor
.
getModel
().
updateOptions
({
insertSpaces
:
false
});
...
...
@@ -222,64 +227,201 @@ suite('SnippetController', () => {
});
test
(
'
Final tabstop with multiple selections
'
,
()
=>
{
// snippetTest((editor, cursor, codeSnippet, snippetController) => {
// editor.setSelections([
// new Selection(1, 1, 1, 1),
// new Selection(2, 1, 2, 1),
// ]);
// codeSnippet = CodeSnippet.fromInternal('foo{{}}');
// snippetController.run(codeSnippet, 0, 0, false);
// assert.equal(editor.getSelections().length, 2);
// const [first, second] = editor.getSelections();
// assert.ok(first.equalsRange({ startLineNumber: 1, startColumn: 4, endLineNumber: 1, endColumn: 4 }), first.toString());
// assert.ok(second.equalsRange({ startLineNumber: 2, startColumn: 4, endLineNumber: 2, endColumn: 4 }), second.toString());
// });
// snippetTest((editor, cursor, codeSnippet, snippetController) => {
// editor.setSelections([
// new Selection(1, 1, 1, 1),
// new Selection(2, 1, 2, 1),
// ]);
// codeSnippet = CodeSnippet.fromInternal('foo{{}}bar');
// snippetController.run(codeSnippet, 0, 0, false);
// assert.equal(editor.getSelections().length, 2);
// const [first, second] = editor.getSelections();
// assert.ok(first.equalsRange({ startLineNumber: 1, startColumn: 4, endLineNumber: 1, endColumn: 4 }), first.toString());
// assert.ok(second.equalsRange({ startLineNumber: 2, startColumn: 4, endLineNumber: 2, endColumn: 4 }), second.toString());
// });
// snippetTest((editor, cursor, codeSnippet, snippetController) => {
// editor.setSelections([
// new Selection(1, 1, 1, 1),
// new Selection(1, 5, 1, 5),
// ]);
// codeSnippet = CodeSnippet.fromInternal('foo{{}}bar');
// snippetController.run(codeSnippet, 0, 0, false);
// assert.equal(editor.getSelections().length, 2);
// const [first, second] = editor.getSelections();
// assert.ok(first.equalsRange({ startLineNumber: 1, startColumn: 4, endLineNumber: 1, endColumn: 4 }), first.toString());
// assert.ok(second.equalsRange({ startLineNumber: 1, startColumn: 14, endLineNumber: 1, endColumn: 14 }), second.toString());
// });
// snippetTest((editor, cursor, codeSnippet, snippetController) => {
// editor.setSelections([
// new Selection(1, 1, 1, 1),
// new Selection(1, 5, 1, 5),
// ]);
// codeSnippet = CodeSnippet.fromInternal('foo\n{{}}\nbar');
// snippetController.run(codeSnippet, 0, 0, false);
// assert.equal(editor.getSelections().length, 2);
// const [first, second] = editor.getSelections();
// assert.ok(first.equalsRange({ startLineNumber: 2, startColumn: 1, endLineNumber: 2, endColumn: 1 }), first.toString());
// assert.ok(second.equalsRange({ startLineNumber: 4, startColumn: 1, endLineNumber: 4, endColumn: 1 }), second.toString());
// });
snippetTest
((
editor
,
cursor
,
codeSnippet
,
snippetController
)
=>
{
editor
.
setSelections
([
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
2
,
1
,
2
,
1
),
]);
codeSnippet
=
CodeSnippet
.
fromInternal
(
'
foo{{}}
'
);
snippetController
.
run
(
codeSnippet
,
0
,
0
,
false
);
assert
.
equal
(
editor
.
getSelections
().
length
,
2
);
const
[
first
,
second
]
=
editor
.
getSelections
();
assert
.
ok
(
first
.
equalsRange
({
startLineNumber
:
1
,
startColumn
:
4
,
endLineNumber
:
1
,
endColumn
:
4
}),
first
.
toString
());
assert
.
ok
(
second
.
equalsRange
({
startLineNumber
:
2
,
startColumn
:
4
,
endLineNumber
:
2
,
endColumn
:
4
}),
second
.
toString
());
});
snippetTest
((
editor
,
cursor
,
codeSnippet
,
snippetController
)
=>
{
editor
.
setSelections
([
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
2
,
1
,
2
,
1
),
]);
codeSnippet
=
CodeSnippet
.
fromInternal
(
'
foo{{}}bar
'
);
snippetController
.
run
(
codeSnippet
,
0
,
0
,
false
);
assert
.
equal
(
editor
.
getSelections
().
length
,
2
);
const
[
first
,
second
]
=
editor
.
getSelections
();
assert
.
ok
(
first
.
equalsRange
({
startLineNumber
:
1
,
startColumn
:
4
,
endLineNumber
:
1
,
endColumn
:
4
}),
first
.
toString
());
assert
.
ok
(
second
.
equalsRange
({
startLineNumber
:
2
,
startColumn
:
4
,
endLineNumber
:
2
,
endColumn
:
4
}),
second
.
toString
());
});
snippetTest
((
editor
,
cursor
,
codeSnippet
,
snippetController
)
=>
{
editor
.
setSelections
([
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
1
,
5
,
1
,
5
),
]);
codeSnippet
=
CodeSnippet
.
fromInternal
(
'
foo{{}}bar
'
);
snippetController
.
run
(
codeSnippet
,
0
,
0
,
false
);
assert
.
equal
(
editor
.
getSelections
().
length
,
2
);
const
[
first
,
second
]
=
editor
.
getSelections
();
assert
.
ok
(
first
.
equalsRange
({
startLineNumber
:
1
,
startColumn
:
4
,
endLineNumber
:
1
,
endColumn
:
4
}),
first
.
toString
());
assert
.
ok
(
second
.
equalsRange
({
startLineNumber
:
1
,
startColumn
:
14
,
endLineNumber
:
1
,
endColumn
:
14
}),
second
.
toString
());
});
snippetTest
((
editor
,
cursor
,
codeSnippet
,
snippetController
)
=>
{
editor
.
setSelections
([
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
1
,
5
,
1
,
5
),
]);
codeSnippet
=
CodeSnippet
.
fromInternal
(
'
foo
\n
{{}}
\n
bar
'
);
snippetController
.
run
(
codeSnippet
,
0
,
0
,
false
);
assert
.
equal
(
editor
.
getSelections
().
length
,
2
);
const
[
first
,
second
]
=
editor
.
getSelections
();
assert
.
ok
(
first
.
equalsRange
({
startLineNumber
:
2
,
startColumn
:
1
,
endLineNumber
:
2
,
endColumn
:
1
}),
first
.
toString
());
assert
.
ok
(
second
.
equalsRange
({
startLineNumber
:
4
,
startColumn
:
1
,
endLineNumber
:
4
,
endColumn
:
1
}),
second
.
toString
());
});
snippetTest
((
editor
,
cursor
,
codeSnippet
,
snippetController
)
=>
{
editor
.
setSelections
([
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
1
,
5
,
1
,
5
),
]);
codeSnippet
=
CodeSnippet
.
fromInternal
(
'
foo
\n
{{}}
\n
bar
'
);
snippetController
.
run
(
codeSnippet
,
0
,
0
,
false
);
assert
.
equal
(
editor
.
getSelections
().
length
,
2
);
const
[
first
,
second
]
=
editor
.
getSelections
();
assert
.
ok
(
first
.
equalsRange
({
startLineNumber
:
2
,
startColumn
:
1
,
endLineNumber
:
2
,
endColumn
:
1
}),
first
.
toString
());
assert
.
ok
(
second
.
equalsRange
({
startLineNumber
:
4
,
startColumn
:
1
,
endLineNumber
:
4
,
endColumn
:
1
}),
second
.
toString
());
});
snippetTest
((
editor
,
cursor
,
codeSnippet
,
snippetController
)
=>
{
editor
.
setSelections
([
new
Selection
(
2
,
7
,
2
,
7
),
]);
codeSnippet
=
CodeSnippet
.
fromInternal
(
'
xo{{}}r
'
);
snippetController
.
run
(
codeSnippet
,
1
,
0
,
false
);
assert
.
equal
(
editor
.
getSelections
().
length
,
1
);
assert
.
ok
(
editor
.
getSelection
().
equalsRange
({
startLineNumber
:
2
,
startColumn
:
8
,
endColumn
:
8
,
endLineNumber
:
2
}));
});
});
test
(
'
Final tabstop, #11742 simple
'
,
()
=>
{
snippetTest
((
editor
,
cursor
,
codeSnippet
,
controller
)
=>
{
editor
.
setSelection
(
new
Selection
(
1
,
19
,
1
,
19
));
codeSnippet
=
CodeSnippet
.
fromTextmate
(
'
{{% url_**$1** %}}
'
);
controller
.
run
(
codeSnippet
,
2
,
0
,
true
);
assert
.
equal
(
editor
.
getSelections
().
length
,
1
);
assert
.
ok
(
editor
.
getSelection
().
equalsRange
({
startLineNumber
:
1
,
startColumn
:
27
,
endLineNumber
:
1
,
endColumn
:
27
}));
assert
.
equal
(
editor
.
getModel
().
getValue
(),
'
example example {{% url_**** %}}
'
);
},
[
'
example example sc
'
]);
snippetTest
((
editor
,
cursor
,
codeSnippet
,
controller
)
=>
{
editor
.
setSelection
(
new
Selection
(
1
,
3
,
1
,
3
));
codeSnippet
=
CodeSnippet
.
fromTextmate
([
'
afterEach((done) => {
'
,
'
\t
${1}test
'
,
'
});
'
].
join
(
'
\n
'
));
controller
.
run
(
codeSnippet
,
2
,
0
,
true
);
assert
.
equal
(
editor
.
getSelections
().
length
,
1
);
assert
.
ok
(
editor
.
getSelection
().
equalsRange
({
startLineNumber
:
2
,
startColumn
:
2
,
endLineNumber
:
2
,
endColumn
:
2
}),
editor
.
getSelection
().
toString
());
assert
.
equal
(
editor
.
getModel
().
getValue
(),
'
afterEach((done) => {
\n\t
test
\n
});
'
);
},
[
'
af
'
]);
snippetTest
((
editor
,
cursor
,
codeSnippet
,
controller
)
=>
{
editor
.
setSelection
(
new
Selection
(
1
,
3
,
1
,
3
));
codeSnippet
=
CodeSnippet
.
fromTextmate
([
'
afterEach((done) => {
'
,
'
${1}
\t
test
'
,
'
});
'
].
join
(
'
\n
'
));
controller
.
run
(
codeSnippet
,
2
,
0
,
true
);
assert
.
equal
(
editor
.
getSelections
().
length
,
1
);
assert
.
ok
(
editor
.
getSelection
().
equalsRange
({
startLineNumber
:
2
,
startColumn
:
1
,
endLineNumber
:
2
,
endColumn
:
1
}),
editor
.
getSelection
().
toString
());
assert
.
equal
(
editor
.
getModel
().
getValue
(),
'
afterEach((done) => {
\n\t
test
\n
});
'
);
},
[
'
af
'
]);
snippetTest
((
editor
,
cursor
,
codeSnippet
,
controller
)
=>
{
editor
.
setSelection
(
new
Selection
(
1
,
9
,
1
,
9
));
codeSnippet
=
CodeSnippet
.
fromTextmate
([
'
aft${1}er
'
].
join
(
'
\n
'
));
controller
.
run
(
codeSnippet
,
8
,
0
,
true
);
assert
.
equal
(
editor
.
getModel
().
getValue
(),
'
after
'
);
assert
.
equal
(
editor
.
getSelections
().
length
,
1
);
assert
.
ok
(
editor
.
getSelection
().
equalsRange
({
startLineNumber
:
1
,
startColumn
:
4
,
endLineNumber
:
1
,
endColumn
:
4
}),
editor
.
getSelection
().
toString
());
},
[
'
afterone
'
]);
});
test
(
'
Final tabstop, #11742 different indents
'
,
()
=>
{
snippetTest
((
editor
,
cursor
,
codeSnippet
,
controller
)
=>
{
editor
.
setSelections
([
new
Selection
(
2
,
4
,
2
,
4
),
new
Selection
(
1
,
3
,
1
,
3
)
]);
codeSnippet
=
CodeSnippet
.
fromTextmate
([
'
afterEach((done) => {
'
,
'
\t
${1}test
'
,
'
});
'
].
join
(
'
\n
'
));
controller
.
run
(
codeSnippet
,
2
,
0
,
true
);
assert
.
equal
(
editor
.
getSelections
().
length
,
2
);
const
[
first
,
second
]
=
editor
.
getSelections
();
assert
.
ok
(
first
.
equalsRange
({
startLineNumber
:
5
,
startColumn
:
3
,
endLineNumber
:
5
,
endColumn
:
3
}),
first
.
toString
());
assert
.
ok
(
second
.
equalsRange
({
startLineNumber
:
2
,
startColumn
:
2
,
endLineNumber
:
2
,
endColumn
:
2
}),
second
.
toString
());
},
[
'
af
'
,
'
\t
af
'
]);
});
test
(
'
Final tabstop, no tabstop
'
,
()
=>
{
snippetTest
((
editor
,
cursor
,
codeSnippet
,
controller
)
=>
{
editor
.
setSelections
([
new
Selection
(
1
,
3
,
1
,
3
)
]);
codeSnippet
=
CodeSnippet
.
fromTextmate
(
'
afterEach
'
);
controller
.
run
(
codeSnippet
,
2
,
0
,
true
);
assert
.
ok
(
editor
.
getSelection
().
equalsRange
({
startLineNumber
:
1
,
startColumn
:
10
,
endLineNumber
:
1
,
endColumn
:
10
}));
},
[
'
af
'
,
'
\t
af
'
]);
});
});
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录