Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
bca2236b
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,发现更多精彩内容 >>
提交
bca2236b
编写于
7月 25, 2017
作者:
J
Johannes Rieken
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
normalize selected text variable when resolving it, #31124
上级
24d0f5f8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
110 addition
and
23 deletion
+110
-23
src/vs/editor/contrib/snippet/browser/snippetVariables.ts
src/vs/editor/contrib/snippet/browser/snippetVariables.ts
+30
-2
src/vs/editor/contrib/snippet/test/browser/snippetSession.test.ts
...ditor/contrib/snippet/test/browser/snippetSession.test.ts
+48
-0
src/vs/editor/contrib/snippet/test/browser/snippetVariables.test.ts
...tor/contrib/snippet/test/browser/snippetVariables.test.ts
+32
-21
未找到文件。
src/vs/editor/contrib/snippet/browser/snippetVariables.ts
浏览文件 @
bca2236b
...
...
@@ -8,7 +8,8 @@
import
{
basename
,
dirname
}
from
'
vs/base/common/paths
'
;
import
{
IModel
}
from
'
vs/editor/common/editorCommon
'
;
import
{
Selection
}
from
'
vs/editor/common/core/selection
'
;
import
{
VariableResolver
,
Variable
}
from
'
vs/editor/contrib/snippet/browser/snippetParser
'
;
import
{
VariableResolver
,
Variable
,
Text
}
from
'
vs/editor/contrib/snippet/browser/snippetParser
'
;
import
{
getLeadingWhitespace
,
commonPrefixLength
}
from
'
vs/base/common/strings
'
;
export
class
EditorSnippetVariableResolver
implements
VariableResolver
{
...
...
@@ -36,7 +37,34 @@ export class EditorSnippetVariableResolver implements VariableResolver {
const
{
name
}
=
variable
;
if
(
name
===
'
SELECTION
'
||
name
===
'
TM_SELECTED_TEXT
'
)
{
return
this
.
_model
.
getValueInRange
(
this
.
_selection
)
||
undefined
;
let
value
=
this
.
_model
.
getValueInRange
(
this
.
_selection
)
||
undefined
;
if
(
value
&&
this
.
_selection
.
startLineNumber
!==
this
.
_selection
.
endLineNumber
)
{
// Selection is a multiline string which we indentation we now
// need to adjust. We compare the indentation of this variable
// with the indentation at the editor position and add potential
// extra indentation to the value
const
line
=
this
.
_model
.
getLineContent
(
this
.
_selection
.
startLineNumber
);
const
lineLeadingWhitespace
=
getLeadingWhitespace
(
line
,
0
,
this
.
_selection
.
startColumn
-
1
);
let
varLeadingWhitespace
=
lineLeadingWhitespace
;
variable
.
snippet
.
walk
(
marker
=>
{
if
(
marker
===
variable
)
{
return
false
;
}
if
(
marker
instanceof
Text
)
{
varLeadingWhitespace
=
getLeadingWhitespace
(
marker
.
value
.
split
(
/
\r\n
|
\r
|
\n
/
).
pop
());
}
return
true
;
});
const
whitespaceCommonLength
=
commonPrefixLength
(
varLeadingWhitespace
,
lineLeadingWhitespace
);
value
=
value
.
replace
(
/
(\r\n
|
\r
|
\n)(
.*
)
/g
,
(
m
,
newline
,
rest
)
=>
`
${
newline
}${
varLeadingWhitespace
.
substr
(
whitespaceCommonLength
)}${
rest
}
`
);
}
return
value
;
}
else
if
(
name
===
'
TM_CURRENT_LINE
'
)
{
return
this
.
_model
.
getLineContent
(
this
.
_selection
.
positionLineNumber
);
...
...
src/vs/editor/contrib/snippet/test/browser/snippetSession.test.ts
浏览文件 @
bca2236b
...
...
@@ -462,5 +462,53 @@ suite('SnippetSession', function () {
assert
.
equal
(
editor
.
getModel
().
getValue
(),
'
test 1
\n
test 2
\n
test 3
\n
test 4
\n
'
);
});
test
(
'
Snippet variable text isn
\'
t whitespace normalised, #31124
'
,
function
()
{
editor
.
getModel
().
setValue
([
'
start
'
,
'
\t\t
-one
'
,
'
\t\t
-two
'
,
'
end
'
].
join
(
'
\n
'
));
editor
.
getModel
().
updateOptions
({
insertSpaces
:
false
});
editor
.
setSelection
(
new
Selection
(
2
,
2
,
3
,
7
));
new
SnippetSession
(
editor
,
'
<div>
\n\t
$TM_SELECTED_TEXT
\n
</div>$0
'
).
insert
();
let
expected
=
[
'
start
'
,
'
\t
<div>
'
,
'
\t\t\t
-one
'
,
'
\t\t\t
-two
'
,
'
\t
</div>
'
,
'
end
'
].
join
(
'
\n
'
);
assert
.
equal
(
editor
.
getModel
().
getValue
(),
expected
);
editor
.
getModel
().
setValue
([
'
start
'
,
'
\t\t
-one
'
,
'
\t
-two
'
,
'
end
'
].
join
(
'
\n
'
));
editor
.
getModel
().
updateOptions
({
insertSpaces
:
false
});
editor
.
setSelection
(
new
Selection
(
2
,
2
,
3
,
7
));
new
SnippetSession
(
editor
,
'
<div>
\n\t
$TM_SELECTED_TEXT
\n
</div>$0
'
).
insert
();
expected
=
[
'
start
'
,
'
\t
<div>
'
,
'
\t\t\t
-one
'
,
'
\t\t
-two
'
,
'
\t
</div>
'
,
'
end
'
].
join
(
'
\n
'
);
assert
.
equal
(
editor
.
getModel
().
getValue
(),
expected
);
});
});
src/vs/editor/contrib/snippet/test/browser/snippetVariables.test.ts
浏览文件 @
bca2236b
...
...
@@ -31,59 +31,70 @@ suite('Snippet Variables Resolver', function () {
model
.
dispose
();
});
function
assertVariableResolve
(
resolver
:
EditorSnippetVariableResolver
,
varName
:
string
,
expected
:
string
)
{
const
snippet
=
new
SnippetParser
().
parse
(
`$
${
varName
}
`
);
const
variable
=
<
Variable
>
snippet
.
children
[
0
];
variable
.
resolve
(
resolver
);
if
(
variable
.
children
.
length
===
0
)
{
assert
.
equal
(
undefined
,
expected
);
}
else
{
assert
.
equal
(
variable
.
toString
(),
expected
);
}
}
test
(
'
editor variables, basics
'
,
function
()
{
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_FILENAME
'
))
,
'
text.txt
'
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
something
'
))
,
undefined
);
assert
VariableResolve
(
resolver
,
'
TM_FILENAME
'
,
'
text.txt
'
);
assert
VariableResolve
(
resolver
,
'
something
'
,
undefined
);
});
test
(
'
editor variables, file/dir
'
,
function
()
{
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_FILENAME
'
))
,
'
text.txt
'
);
assert
VariableResolve
(
resolver
,
'
TM_FILENAME
'
,
'
text.txt
'
);
if
(
!
isWindows
)
{
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_DIRECTORY
'
))
,
'
/foo/files
'
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_FILEPATH
'
))
,
'
/foo/files/text.txt
'
);
assert
VariableResolve
(
resolver
,
'
TM_DIRECTORY
'
,
'
/foo/files
'
);
assert
VariableResolve
(
resolver
,
'
TM_FILEPATH
'
,
'
/foo/files/text.txt
'
);
}
resolver
=
new
EditorSnippetVariableResolver
(
Model
.
createFromString
(
''
,
undefined
,
undefined
,
URI
.
parse
(
'
http://www.pb.o/abc/def/ghi
'
)),
new
Selection
(
1
,
1
,
1
,
1
)
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_FILENAME
'
))
,
'
ghi
'
);
assert
VariableResolve
(
resolver
,
'
TM_FILENAME
'
,
'
ghi
'
);
if
(
!
isWindows
)
{
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_DIRECTORY
'
))
,
'
/abc/def
'
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_FILEPATH
'
))
,
'
/abc/def/ghi
'
);
assert
VariableResolve
(
resolver
,
'
TM_DIRECTORY
'
,
'
/abc/def
'
);
assert
VariableResolve
(
resolver
,
'
TM_FILEPATH
'
,
'
/abc/def/ghi
'
);
}
resolver
=
new
EditorSnippetVariableResolver
(
Model
.
createFromString
(
''
,
undefined
,
undefined
,
URI
.
parse
(
'
mem:fff.ts
'
)),
new
Selection
(
1
,
1
,
1
,
1
)
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_DIRECTORY
'
))
,
''
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_FILEPATH
'
))
,
'
fff.ts
'
);
assert
VariableResolve
(
resolver
,
'
TM_DIRECTORY
'
,
''
);
assert
VariableResolve
(
resolver
,
'
TM_FILEPATH
'
,
'
fff.ts
'
);
});
test
(
'
editor variables, selection
'
,
function
()
{
resolver
=
new
EditorSnippetVariableResolver
(
model
,
new
Selection
(
1
,
2
,
2
,
3
));
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_SELECTED_TEXT
'
))
,
'
his is line one
\n
th
'
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_CURRENT_LINE
'
))
,
'
this is line two
'
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_LINE_INDEX
'
))
,
'
1
'
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_LINE_NUMBER
'
))
,
'
2
'
);
assert
VariableResolve
(
resolver
,
'
TM_SELECTED_TEXT
'
,
'
his is line one
\n
th
'
);
assert
VariableResolve
(
resolver
,
'
TM_CURRENT_LINE
'
,
'
this is line two
'
);
assert
VariableResolve
(
resolver
,
'
TM_LINE_INDEX
'
,
'
1
'
);
assert
VariableResolve
(
resolver
,
'
TM_LINE_NUMBER
'
,
'
2
'
);
resolver
=
new
EditorSnippetVariableResolver
(
model
,
new
Selection
(
2
,
3
,
1
,
2
));
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_SELECTED_TEXT
'
))
,
'
his is line one
\n
th
'
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_CURRENT_LINE
'
))
,
'
this is line one
'
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_LINE_INDEX
'
))
,
'
0
'
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_LINE_NUMBER
'
))
,
'
1
'
);
assert
VariableResolve
(
resolver
,
'
TM_SELECTED_TEXT
'
,
'
his is line one
\n
th
'
);
assert
VariableResolve
(
resolver
,
'
TM_CURRENT_LINE
'
,
'
this is line one
'
);
assert
VariableResolve
(
resolver
,
'
TM_LINE_INDEX
'
,
'
0
'
);
assert
VariableResolve
(
resolver
,
'
TM_LINE_NUMBER
'
,
'
1
'
);
resolver
=
new
EditorSnippetVariableResolver
(
model
,
new
Selection
(
1
,
2
,
1
,
2
));
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_SELECTED_TEXT
'
))
,
undefined
);
assert
VariableResolve
(
resolver
,
'
TM_SELECTED_TEXT
'
,
undefined
);
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_CURRENT_WORD
'
))
,
'
this
'
);
assert
VariableResolve
(
resolver
,
'
TM_CURRENT_WORD
'
,
'
this
'
);
resolver
=
new
EditorSnippetVariableResolver
(
model
,
new
Selection
(
3
,
1
,
3
,
1
));
assert
.
equal
(
resolver
.
resolve
(
new
Variable
(
'
TM_CURRENT_WORD
'
))
,
undefined
);
assert
VariableResolve
(
resolver
,
'
TM_CURRENT_WORD
'
,
undefined
);
});
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录