Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
8902a30b
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,发现更多精彩内容 >>
提交
8902a30b
编写于
6月 19, 2017
作者:
R
Ramya Achutha Rao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Consolidate emmet expand action and emmet completion
上级
7194723e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
113 addition
and
79 deletion
+113
-79
extensions/emmet/src/abbreviationActions.ts
extensions/emmet/src/abbreviationActions.ts
+92
-8
extensions/emmet/src/emmetCompletionProvider.ts
extensions/emmet/src/emmetCompletionProvider.ts
+17
-61
extensions/emmet/src/util.ts
extensions/emmet/src/util.ts
+4
-10
未找到文件。
extensions/emmet/src/abbreviationActions.ts
浏览文件 @
8902a30b
...
...
@@ -5,7 +5,13 @@
import
*
as
vscode
from
'
vscode
'
;
import
{
expand
}
from
'
@emmetio/expand-abbreviation
'
;
import
{
getSyntax
,
getProfile
,
extractAbbreviation
}
from
'
./util
'
;
import
*
as
extract
from
'
@emmetio/extract-abbreviation
'
;
import
parseStylesheet
from
'
@emmetio/css-parser
'
;
import
parse
from
'
@emmetio/html-matcher
'
;
import
Node
from
'
@emmetio/node
'
;
import
{
getSyntax
,
getProfile
,
isStyleSheet
,
getNode
,
getInnerRange
}
from
'
./util
'
;
import
{
DocumentStreamReader
}
from
'
./bufferStream
'
;
const
field
=
(
index
,
placeholder
)
=>
`
\$
{
${
index
}${
placeholder
?
'
:
'
+
placeholder
:
''
}
}`
;
...
...
@@ -42,19 +48,97 @@ export function expandAbbreviation() {
vscode
.
window
.
showInformationMessage
(
'
No editor is active
'
);
return
;
}
let
rangeToReplace
:
vscode
.
Range
=
editor
.
selection
;
let
abbr
=
editor
.
document
.
getText
(
rangeToReplace
);
let
syntax
=
getSyntax
(
editor
.
document
);
let
output
=
expandAbbreviationHelper
(
syntax
,
editor
.
document
,
editor
.
selection
);
if
(
output
)
{
editor
.
insertSnippet
(
new
vscode
.
SnippetString
(
output
.
expandedText
),
output
.
rangeToReplace
);
}
}
export
interface
ExpandAbbreviationHelperOutput
{
expandedText
:
string
;
rangeToReplace
:
vscode
.
Range
;
abbreviation
:
string
;
syntax
:
string
;
}
/**
* Expands abbreviation at given range in the given document
* @param syntax
* @param document
* @param rangeToReplace
*/
export
function
expandAbbreviationHelper
(
syntax
:
string
,
document
:
vscode
.
TextDocument
,
rangeToReplace
:
vscode
.
Range
):
ExpandAbbreviationHelperOutput
{
let
parseContent
=
isStyleSheet
(
syntax
)
?
parseStylesheet
:
parse
;
let
rootNode
:
Node
=
parseContent
(
new
DocumentStreamReader
(
document
));
let
currentNode
=
getNode
(
rootNode
,
rangeToReplace
.
end
);
if
(
forceCssSyntax
(
syntax
,
currentNode
,
rangeToReplace
.
end
))
{
syntax
=
'
css
'
;
}
else
if
(
!
isValidLocationForEmmetAbbreviation
(
currentNode
,
syntax
,
rangeToReplace
.
end
))
{
return
;
}
let
abbreviation
=
document
.
getText
(
rangeToReplace
);
if
(
rangeToReplace
.
isEmpty
)
{
[
rangeToReplace
,
abbr
]
=
extractAbbreviation
(
rangeToReplace
.
start
);
[
rangeToReplace
,
abbr
eviation
]
=
extractAbbreviation
(
document
,
rangeToReplace
.
start
);
}
let
syntax
=
getSyntax
(
editor
.
document
);
let
options
=
{
field
:
field
,
syntax
:
syntax
,
profile
:
getProfile
(
getSyntax
(
editor
.
document
)
),
profile
:
getProfile
(
syntax
),
addons
:
syntax
===
'
jsx
'
?
{
'
jsx
'
:
true
}
:
null
};
let
expandedText
=
expand
(
abbr
,
options
);
editor
.
insertSnippet
(
new
vscode
.
SnippetString
(
expandedText
),
rangeToReplace
);
let
expandedText
=
expand
(
abbreviation
,
options
);
return
{
expandedText
,
rangeToReplace
,
abbreviation
,
syntax
};
}
/**
* Extracts abbreviation from the given position in the given document
*/
function
extractAbbreviation
(
document
:
vscode
.
TextDocument
,
position
:
vscode
.
Position
):
[
vscode
.
Range
,
string
]
{
let
currentLine
=
document
.
lineAt
(
position
.
line
).
text
;
let
result
=
extract
(
currentLine
,
position
.
character
,
true
);
if
(
!
result
)
{
return
[
null
,
''
];
}
let
rangeToReplace
=
new
vscode
.
Range
(
position
.
line
,
result
.
location
,
position
.
line
,
result
.
location
+
result
.
abbreviation
.
length
);
return
[
rangeToReplace
,
result
.
abbreviation
];
}
/**
* Inside <style> tag, force use of css abbreviations
*/
function
forceCssSyntax
(
syntax
:
string
,
currentNode
:
Node
,
position
:
vscode
.
Position
):
boolean
{
return
!
isStyleSheet
(
syntax
)
&&
currentNode
&&
currentNode
.
close
&&
currentNode
.
name
===
'
style
'
&&
getInnerRange
(
currentNode
).
contains
(
position
);
}
/**
* Checks if given position is a valid location to expand emmet abbreviation
* @param currentNode parsed node at given position
* @param syntax syntax of the abbreviation
* @param position position to validate
*/
function
isValidLocationForEmmetAbbreviation
(
currentNode
:
Node
,
syntax
:
string
,
position
:
vscode
.
Position
):
boolean
{
if
(
!
currentNode
)
{
return
true
;
}
if
(
isStyleSheet
(
syntax
))
{
return
currentNode
.
type
!==
'
rule
'
||
(
currentNode
.
selectorToken
&&
position
.
isAfter
(
currentNode
.
selectorToken
.
end
));
}
if
(
currentNode
.
close
)
{
return
getInnerRange
(
currentNode
).
contains
(
position
);
}
return
false
;
}
\ No newline at end of file
extensions/emmet/src/emmetCompletionProvider.ts
浏览文件 @
8902a30b
...
...
@@ -6,11 +6,8 @@
import
*
as
vscode
from
'
vscode
'
;
import
{
expand
,
createSnippetsRegistry
}
from
'
@emmetio/expand-abbreviation
'
;
import
{
getSyntax
,
getProfile
,
extractAbbreviation
,
isStyleSheet
,
getNode
}
from
'
./util
'
;
import
parseStylesheet
from
'
@emmetio/css-parser
'
;
import
parse
from
'
@emmetio/html-matcher
'
;
import
Node
from
'
@emmetio/node
'
;
import
{
DocumentStreamReader
}
from
'
./bufferStream
'
;
import
{
getSyntax
,
isStyleSheet
}
from
'
./util
'
;
import
{
expandAbbreviationHelper
,
ExpandAbbreviationHelperOutput
}
from
'
./abbreviationActions
'
;
const
field
=
(
index
,
placeholder
)
=>
`
\$
{
${
index
}${
placeholder
?
'
:
'
+
placeholder
:
''
}
}`
;
const
snippetCompletionsCache
=
new
Map
<
string
,
vscode
.
CompletionItem
[]
>
();
...
...
@@ -23,65 +20,35 @@ export class EmmetCompletionItemProvider implements vscode.CompletionItemProvide
return
Promise
.
resolve
(
null
);
}
let
completionItems
:
vscode
.
CompletionItem
[]
=
[];
let
syntax
=
getSyntax
(
document
);
let
currentWord
=
getCurrentWord
(
document
,
position
);
let
parseContent
=
isStyleSheet
(
syntax
)
?
parseStylesheet
:
parse
;
let
rootNode
:
Node
=
parseContent
(
new
DocumentStreamReader
(
document
));
let
currentNode
=
getNode
(
rootNode
,
position
);
// Inside <style> tag, trigger css abbreviations
if
(
!
isStyleSheet
(
syntax
)
&&
currentNode
&&
currentNode
.
name
===
'
style
'
)
{
syntax
=
'
css
'
;
let
expandedAbbr
:
vscode
.
CompletionItem
;
if
(
vscode
.
workspace
.
getConfiguration
(
'
emmet
'
)[
'
showExpandedAbbreviation
'
])
{
let
output
:
ExpandAbbreviationHelperOutput
=
expandAbbreviationHelper
(
syntax
,
document
,
new
vscode
.
Range
(
position
,
position
));
if
(
output
)
{
expandedAbbr
=
new
vscode
.
CompletionItem
(
output
.
abbreviation
);
expandedAbbr
.
insertText
=
new
vscode
.
SnippetString
(
output
.
expandedText
);
expandedAbbr
.
documentation
=
removeTabStops
(
output
.
expandedText
);
expandedAbbr
.
range
=
output
.
rangeToReplace
;
expandedAbbr
.
detail
=
'
Expand Emmet Abbreviation
'
;
syntax
=
output
.
syntax
;
}
}
let
expandedAbbr
=
this
.
getExpandedAbbreviation
(
document
,
position
,
syntax
,
currentNode
);
let
completionItems
:
vscode
.
CompletionItem
[]
=
expandedAbbr
?
[
expandedAbbr
]
:
[];
if
(
!
isStyleSheet
(
syntax
))
{
if
(
expandedAbbr
)
{
// In non stylesheet like syntax, this extension returns expanded abbr plus posssible abbr completions
// To differentiate between the 2, the former is given CompletionItemKind.Value so that it gets a different icon
expandedAbbr
.
kind
=
vscode
.
CompletionItemKind
.
Value
;
}
let
currentWord
=
getCurrentWord
(
document
,
position
);
let
abbreviationSuggestions
=
this
.
getAbbreviationSuggestions
(
syntax
,
currentWord
,
(
expandedAbbr
&&
currentWord
===
expandedAbbr
.
label
));
completionItems
=
expandedAbbr
?
[
expandedAbbr
,
...
abbreviationSuggestions
]
:
abbreviationSuggestions
;
}
else
{
completionItems
=
expandedAbbr
?
[
expandedAbbr
]
:
[];
completionItems
=
completionItems
.
concat
(
abbreviationSuggestions
);
}
return
Promise
.
resolve
(
new
vscode
.
CompletionList
(
completionItems
,
true
));
}
getExpandedAbbreviation
(
document
:
vscode
.
TextDocument
,
position
:
vscode
.
Position
,
syntax
:
string
,
currentNode
:
Node
):
vscode
.
CompletionItem
{
if
(
!
vscode
.
workspace
.
getConfiguration
(
'
emmet
'
)[
'
showExpandedAbbreviation
'
])
{
return
;
}
let
[
rangeToReplace
,
wordToExpand
]
=
extractAbbreviation
(
position
);
if
(
!
rangeToReplace
||
!
wordToExpand
)
{
return
;
}
if
(
!
isValidLocationForEmmetAbbreviation
(
currentNode
,
syntax
,
position
))
{
return
;
}
let
expandedWord
=
expand
(
wordToExpand
,
{
field
:
field
,
syntax
:
syntax
,
profile
:
getProfile
(
syntax
),
addons
:
syntax
===
'
jsx
'
?
{
'
jsx
'
:
true
}
:
null
});
let
completionitem
=
new
vscode
.
CompletionItem
(
wordToExpand
);
completionitem
.
insertText
=
new
vscode
.
SnippetString
(
expandedWord
);
completionitem
.
documentation
=
removeTabStops
(
expandedWord
);
completionitem
.
range
=
rangeToReplace
;
completionitem
.
detail
=
'
Expand Emmet Abbreviation
'
;
return
completionitem
;
}
getAbbreviationSuggestions
(
syntax
:
string
,
prefix
:
string
,
skipExactMatch
:
boolean
)
{
if
(
!
vscode
.
workspace
.
getConfiguration
(
'
emmet
'
)[
'
showAbbreviationSuggestions
'
]
||
!
prefix
)
{
return
[];
...
...
@@ -131,17 +98,6 @@ function removeTabStops(expandedWord: string): string {
return
expandedWord
.
replace
(
/
\$\{\d
+
\}
/g
,
''
).
replace
(
/
\$\{\d
+:
([^\}]
+
)\}
/g
,
'
$1
'
);
}
function
isValidLocationForEmmetAbbreviation
(
currentNode
:
Node
,
syntax
:
string
,
position
:
vscode
.
Position
):
boolean
{
if
(
!
currentNode
)
{
return
true
;
}
if
(
isStyleSheet
(
syntax
))
{
return
currentNode
.
type
!==
'
rule
'
;
}
return
position
.
isAfter
(
currentNode
.
open
.
end
);
}
extensions/emmet/src/util.ts
浏览文件 @
8902a30b
...
...
@@ -6,7 +6,6 @@
import
*
as
vscode
from
'
vscode
'
;
import
parse
from
'
@emmetio/html-matcher
'
;
import
Node
from
'
@emmetio/node
'
;
import
*
as
extract
from
'
@emmetio/extract-abbreviation
'
;
import
{
DocumentStreamReader
}
from
'
./bufferStream
'
;
export
function
validate
(
allowStylesheet
:
boolean
=
true
):
boolean
{
...
...
@@ -112,16 +111,11 @@ export function getNode(root: Node, position: vscode.Position, includeNodeBounda
return
foundNode
;
}
export
function
extractAbbreviation
(
position
:
vscode
.
Position
):
[
vscode
.
Range
,
string
]
{
let
editor
=
vscode
.
window
.
activeTextEditor
;
let
currentLine
=
editor
.
document
.
lineAt
(
position
.
line
).
text
;
let
result
=
extract
(
currentLine
,
position
.
character
,
true
);
if
(
!
result
)
{
return
[
null
,
''
];
export
function
getInnerRange
(
currentNode
:
Node
):
vscode
.
Range
{
if
(
!
currentNode
.
close
)
{
return
;
}
let
rangeToReplace
=
new
vscode
.
Range
(
position
.
line
,
result
.
location
,
position
.
line
,
result
.
location
+
result
.
abbreviation
.
length
);
return
[
rangeToReplace
,
result
.
abbreviation
];
return
new
vscode
.
Range
(
currentNode
.
open
.
end
,
currentNode
.
close
.
start
);
}
export
function
getDeepestNode
(
node
:
Node
):
Node
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录