Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
6819433c
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,发现更多精彩内容 >>
提交
6819433c
编写于
5月 09, 2017
作者:
J
Johannes Rieken
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
better decoration management, more tests, finish at final tab stop
上级
a86bfc7e
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
134 addition
and
31 deletion
+134
-31
src/vs/editor/contrib/snippet/browser/editorSnippets.ts
src/vs/editor/contrib/snippet/browser/editorSnippets.ts
+50
-31
src/vs/editor/contrib/snippet/test/browser/editorSnippets.test.ts
...ditor/contrib/snippet/test/browser/editorSnippets.test.ts
+84
-0
未找到文件。
src/vs/editor/contrib/snippet/browser/editorSnippets.ts
浏览文件 @
6819433c
...
...
@@ -6,7 +6,7 @@
'
use strict
'
;
import
{
getLeadingWhitespace
}
from
'
vs/base/common/strings
'
;
import
{
ICommonCodeEditor
,
IModel
,
TrackedRangeStickiness
,
IIdentifiedSingleEditOperation
}
from
'
vs/editor/common/editorCommon
'
;
import
{
ICommonCodeEditor
,
IModel
,
IModelDecorationOptions
,
TrackedRangeStickiness
,
IIdentifiedSingleEditOperation
}
from
'
vs/editor/common/editorCommon
'
;
import
{
EditOperation
}
from
'
vs/editor/common/core/editOperation
'
;
import
{
TextmateSnippet
,
Placeholder
,
SnippetParser
}
from
'
../common/snippetParser
'
;
import
{
Selection
}
from
'
vs/editor/common/core/selection
'
;
...
...
@@ -24,6 +24,9 @@ class OneSnippet {
private
_placeholderGroups
:
Placeholder
[][];
private
_placeholderGroupsIdx
:
number
;
private
static
readonly
_growingDecoration
:
IModelDecorationOptions
=
{
stickiness
:
TrackedRangeStickiness
.
AlwaysGrowsWhenTypingAtEdges
};
private
static
readonly
_fixedDecoration
:
IModelDecorationOptions
=
{
stickiness
:
TrackedRangeStickiness
.
NeverGrowsWhenTypingAtEdges
};
constructor
(
editor
:
ICommonCodeEditor
,
snippet
:
TextmateSnippet
,
offset
:
number
)
{
this
.
_editor
=
editor
;
this
.
_snippet
=
snippet
;
...
...
@@ -58,16 +61,7 @@ class OneSnippet {
const
end
=
model
.
getPositionAt
(
this
.
_offset
+
placeholderOffset
+
placeholderLen
);
const
range
=
new
Range
(
start
.
lineNumber
,
start
.
column
,
end
.
lineNumber
,
end
.
column
);
let
stickiness
:
TrackedRangeStickiness
;
if
(
placeholder
.
isFinalTabstop
)
{
stickiness
=
TrackedRangeStickiness
.
NeverGrowsWhenTypingAtEdges
;
}
else
if
(
lastRange
&&
lastRange
.
getEndPosition
().
equals
(
range
.
getStartPosition
()))
{
stickiness
=
TrackedRangeStickiness
.
GrowsOnlyWhenTypingAfter
;
}
else
{
stickiness
=
TrackedRangeStickiness
.
AlwaysGrowsWhenTypingAtEdges
;
}
const
handle
=
accessor
.
addDecoration
(
range
,
{
stickiness
});
const
handle
=
accessor
.
addDecoration
(
range
,
OneSnippet
.
_fixedDecoration
);
this
.
_placeholderDecorations
.
set
(
placeholder
,
handle
);
lastRange
=
range
;
...
...
@@ -91,27 +85,50 @@ class OneSnippet {
this
.
_init
();
const
prevGroupsIdx
=
this
.
_placeholderGroupsIdx
;
if
(
fwd
&&
this
.
_placeholderGroupsIdx
<
this
.
_placeholderGroups
.
length
-
1
)
{
this
.
_placeholderGroupsIdx
+=
1
;
return
this
.
_getCurrentPlaceholderSelections
();
}
else
if
(
!
fwd
&&
this
.
_placeholderGroupsIdx
>
0
)
{
this
.
_placeholderGroupsIdx
-=
1
;
return
this
.
_getCurrentPlaceholderSelections
();
}
else
{
return
undefined
;
return
[];
}
return
this
.
_editor
.
getModel
().
changeDecorations
(
accessor
=>
{
// change stickness to never grow when typing at its edges
// so that in-active tabstops never grow
if
(
prevGroupsIdx
!==
-
1
)
{
for
(
const
placeholder
of
this
.
_placeholderGroups
[
prevGroupsIdx
])
{
const
id
=
this
.
_placeholderDecorations
.
get
(
placeholder
);
accessor
.
changeDecorationOptions
(
id
,
OneSnippet
.
_fixedDecoration
);
}
}
private
_getCurrentPlaceholderSelections
():
Selection
[]
{
// change stickiness to always grow when typing at its edges
// because these decorations represent the currently active
// tabstop. Special case: reaching the final tab stop
const
selections
:
Selection
[]
=
[];
for
(
const
placeholder
of
this
.
_placeholderGroups
[
this
.
_placeholderGroupsIdx
])
{
const
handle
=
this
.
_placeholderDecorations
.
get
(
placeholder
);
const
range
=
this
.
_editor
.
getModel
().
getDecorationRange
(
handle
);
const
id
=
this
.
_placeholderDecorations
.
get
(
placeholder
);
const
range
=
this
.
_editor
.
getModel
().
getDecorationRange
(
id
);
selections
.
push
(
new
Selection
(
range
.
startLineNumber
,
range
.
startColumn
,
range
.
endLineNumber
,
range
.
endColumn
));
accessor
.
changeDecorationOptions
(
id
,
placeholder
.
isFinalTabstop
?
OneSnippet
.
_fixedDecoration
:
OneSnippet
.
_growingDecoration
);
}
return
selections
;
});
}
get
isAtFinalPlaceholder
()
{
if
(
this
.
_placeholderGroupsIdx
<
0
)
{
return
false
;
}
else
{
return
this
.
_placeholderGroups
[
this
.
_placeholderGroupsIdx
][
0
].
isFinalTabstop
;
}
}
}
...
...
@@ -161,32 +178,34 @@ export class SnippetSession {
this
.
_editor
.
setSelections
(
newSelections
);
}
dispose
():
void
{
dispose
(
this
.
_snippets
);
}
next
():
void
{
const
newSelections
=
this
.
_move
(
true
);
if
(
newSelections
.
length
>
0
)
{
this
.
_editor
.
setSelections
(
newSelections
);
}
}
prev
():
void
{
const
newSelections
=
this
.
_move
(
false
);
if
(
newSelections
.
length
>
0
)
{
this
.
_editor
.
setSelections
(
newSelections
);
}
}
private
_move
(
fwd
:
boolean
):
Selection
[]
{
const
selections
:
Selection
[]
=
[];
for
(
const
snippet
of
this
.
_snippets
)
{
const
oneSelection
=
snippet
.
move
(
fwd
);
if
(
!
oneSelection
)
{
if
(
fwd
)
{
this
.
stop
();
}
return
this
.
_editor
.
getSelections
();
}
selections
.
push
(...
oneSelection
);
}
return
selections
;
}
stop
():
void
{
dispose
(
this
.
_snippets
)
;
get
isAtFinalPlaceholder
()
{
return
this
.
_snippets
[
0
].
isAtFinalPlaceholder
;
}
}
src/vs/editor/contrib/snippet/test/browser/editorSnippets.test.ts
浏览文件 @
6819433c
...
...
@@ -59,6 +59,19 @@ suite('SnippetInsertion', function () {
assertSelections
(
editor
,
new
Selection
(
1
,
10
,
1
,
10
),
new
Selection
(
2
,
14
,
2
,
14
));
});
test
(
'
snippets, repeated tabstops
'
,
function
()
{
const
session
=
new
SnippetSession
(
editor
,
'
${1:abc}foo${1:abc}$0
'
);
assertSelections
(
editor
,
new
Selection
(
1
,
1
,
1
,
4
),
new
Selection
(
1
,
7
,
1
,
10
),
new
Selection
(
2
,
5
,
2
,
8
),
new
Selection
(
2
,
11
,
2
,
14
),
);
session
.
next
();
assertSelections
(
editor
,
new
Selection
(
1
,
10
,
1
,
10
),
new
Selection
(
2
,
14
,
2
,
14
),
);
});
test
(
'
snippets, selections and new text with newlines
'
,
()
=>
{
const
session
=
new
SnippetSession
(
editor
,
'
foo
\n\t
${1:bar}
\n
$0
'
);
...
...
@@ -141,5 +154,76 @@ suite('SnippetInsertion', function () {
assertSelections
(
editor
,
new
Selection
(
1
,
15
,
1
,
15
));
});
test
(
'
snippets, don
\'
t merge touching tabstops 1/2
'
,
function
()
{
const
session
=
new
SnippetSession
(
editor
,
'
$1$2$3$0
'
);
assertSelections
(
editor
,
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
2
,
5
,
2
,
5
));
session
.
next
();
assertSelections
(
editor
,
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
2
,
5
,
2
,
5
));
session
.
next
();
assertSelections
(
editor
,
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
2
,
5
,
2
,
5
));
session
.
next
();
assertSelections
(
editor
,
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
2
,
5
,
2
,
5
));
session
.
prev
();
session
.
prev
();
session
.
prev
();
assertSelections
(
editor
,
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
2
,
5
,
2
,
5
));
editor
.
trigger
(
'
test
'
,
'
type
'
,
{
text
:
'
111
'
});
session
.
next
();
editor
.
trigger
(
'
test
'
,
'
type
'
,
{
text
:
'
222
'
});
session
.
next
();
editor
.
trigger
(
'
test
'
,
'
type
'
,
{
text
:
'
333
'
});
session
.
next
();
assert
.
equal
(
model
.
getValue
(),
'
111222333function foo() {
\n
111222333console.log(a);
\n
}
'
);
assertSelections
(
editor
,
new
Selection
(
1
,
10
,
1
,
10
),
new
Selection
(
2
,
14
,
2
,
14
));
session
.
prev
();
assertSelections
(
editor
,
new
Selection
(
1
,
7
,
1
,
7
),
new
Selection
(
2
,
11
,
2
,
11
));
session
.
prev
();
assertSelections
(
editor
,
new
Selection
(
1
,
4
,
1
,
4
),
new
Selection
(
2
,
8
,
2
,
8
));
session
.
prev
();
assertSelections
(
editor
,
new
Selection
(
1
,
1
,
1
,
4
),
new
Selection
(
2
,
5
,
2
,
8
));
});
test
(
'
snippets, don
\'
t merge touching tabstops 2/2
'
,
function
()
{
const
session
=
new
SnippetSession
(
editor
,
'
$1$2$3$0
'
);
assertSelections
(
editor
,
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
2
,
5
,
2
,
5
));
editor
.
trigger
(
'
test
'
,
'
type
'
,
{
text
:
'
111
'
});
session
.
next
();
assertSelections
(
editor
,
new
Selection
(
1
,
4
,
1
,
4
),
new
Selection
(
2
,
8
,
2
,
8
));
editor
.
trigger
(
'
test
'
,
'
type
'
,
{
text
:
'
222
'
});
session
.
next
();
assertSelections
(
editor
,
new
Selection
(
1
,
7
,
1
,
7
),
new
Selection
(
2
,
11
,
2
,
11
));
editor
.
trigger
(
'
test
'
,
'
type
'
,
{
text
:
'
333
'
});
session
.
next
();
assert
.
equal
(
session
.
isAtFinalPlaceholder
,
true
);
});
test
(
'
snippets, gracefully move over final tabstop
'
,
function
()
{
const
session
=
new
SnippetSession
(
editor
,
'
${1}bar$0
'
);
assert
.
equal
(
session
.
isAtFinalPlaceholder
,
false
);
assertSelections
(
editor
,
new
Selection
(
1
,
1
,
1
,
1
),
new
Selection
(
2
,
5
,
2
,
5
));
session
.
next
();
assert
.
equal
(
session
.
isAtFinalPlaceholder
,
true
);
assertSelections
(
editor
,
new
Selection
(
1
,
4
,
1
,
4
),
new
Selection
(
2
,
8
,
2
,
8
));
session
.
next
();
assert
.
equal
(
session
.
isAtFinalPlaceholder
,
true
);
assertSelections
(
editor
,
new
Selection
(
1
,
4
,
1
,
4
),
new
Selection
(
2
,
8
,
2
,
8
));
});
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录