Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
掘金者说
vscode
提交
56c808a6
V
vscode
项目概览
掘金者说
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
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,发现更多精彩内容 >>
未验证
提交
56c808a6
编写于
12月 30, 2020
作者:
R
Raymond Zhao
提交者:
GitHub
12月 30, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Emmet flatten DocumentStreamReader (#113602)
上级
b84858ba
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
55 addition
and
85 deletion
+55
-85
extensions/emmet/src/bufferStream.ts
extensions/emmet/src/bufferStream.ts
+25
-56
extensions/emmet/src/util.ts
extensions/emmet/src/util.ts
+30
-29
未找到文件。
extensions/emmet/src/bufferStream.ts
浏览文件 @
56c808a6
...
...
@@ -5,7 +5,7 @@
/* Based on @sergeche's work in his emmet plugin */
import
{
TextDocument
,
Position
,
Range
,
EndOfLine
}
from
'
vscode
'
;
import
{
TextDocument
}
from
'
vscode
'
;
/**
* A stream reader for VSCode's `TextDocument`
...
...
@@ -13,40 +13,37 @@ import { TextDocument, Position, Range, EndOfLine } from 'vscode';
*/
export
class
DocumentStreamReader
{
private
document
:
TextDocument
;
private
start
:
Position
;
private
_eof
:
Position
;
private
_sof
:
Position
;
public
pos
:
Position
;
private
_eol
:
string
;
constructor
(
document
:
TextDocument
,
pos
?:
Position
,
limit
?:
Range
)
{
private
start
:
number
;
private
_eof
:
number
;
private
_sof
:
number
;
public
pos
:
number
;
constructor
(
document
:
TextDocument
,
pos
?:
number
,
limit
?:
[
number
,
number
])
{
this
.
document
=
document
;
this
.
start
=
this
.
pos
=
pos
?
pos
:
new
Position
(
0
,
0
);
this
.
_sof
=
limit
?
limit
.
start
:
new
Position
(
0
,
0
);
this
.
_eof
=
limit
?
limit
.
end
:
new
Position
(
this
.
document
.
lineCount
-
1
,
this
.
_lineLength
(
this
.
document
.
lineCount
-
1
));
this
.
_eol
=
this
.
document
.
eol
===
EndOfLine
.
LF
?
'
\n
'
:
'
\r\n
'
;
this
.
start
=
this
.
pos
=
pos
?
pos
:
0
;
this
.
_sof
=
limit
?
limit
[
0
]
:
0
;
this
.
_eof
=
limit
?
limit
[
1
]
:
document
.
getText
().
length
;
}
/**
* Returns true only if the stream is at the start of the file.
*/
sof
():
boolean
{
return
this
.
pos
.
isBeforeOrEqual
(
this
.
_sof
)
;
return
this
.
pos
<=
this
.
_sof
;
}
/**
* Returns true only if the stream is at the end of the file.
*/
eof
():
boolean
{
return
this
.
pos
.
isAfterOrEqual
(
this
.
_eof
)
;
return
this
.
pos
>=
this
.
_eof
;
}
/**
* Creates a new stream instance which is limited to given range for given document
*/
limit
(
start
:
Position
,
end
:
Position
):
DocumentStreamReader
{
return
new
DocumentStreamReader
(
this
.
document
,
start
,
new
Range
(
start
,
end
)
);
limit
(
start
:
number
,
end
:
number
):
DocumentStreamReader
{
return
new
DocumentStreamReader
(
this
.
document
,
start
,
[
start
,
end
]
);
}
/**
...
...
@@ -57,8 +54,7 @@ export class DocumentStreamReader {
if
(
this
.
eof
())
{
return
NaN
;
}
const
line
=
this
.
document
.
lineAt
(
this
.
pos
.
line
).
text
;
return
this
.
pos
.
character
<
line
.
length
?
line
.
charCodeAt
(
this
.
pos
.
character
)
:
this
.
_eol
.
charCodeAt
(
this
.
pos
.
character
-
line
.
length
);
return
this
.
document
.
getText
().
charCodeAt
(
this
.
pos
);
}
/**
...
...
@@ -70,19 +66,12 @@ export class DocumentStreamReader {
return
NaN
;
}
const
line
=
this
.
document
.
lineAt
(
this
.
pos
.
line
).
text
;
let
code
:
number
;
if
(
this
.
pos
.
character
<
line
.
length
)
{
code
=
line
.
charCodeAt
(
this
.
pos
.
character
);
this
.
pos
=
this
.
pos
.
translate
(
0
,
1
);
}
else
{
code
=
this
.
_eol
.
charCodeAt
(
this
.
pos
.
character
-
line
.
length
);
this
.
pos
=
new
Position
(
this
.
pos
.
line
+
1
,
0
);
}
const
code
=
this
.
document
.
getText
().
charCodeAt
(
this
.
pos
);
this
.
pos
++
;
if
(
this
.
eof
())
{
// restrict pos to eof, if in case it got moved beyond eof
this
.
pos
=
new
Position
(
this
.
_eof
.
line
,
this
.
_eof
.
character
)
;
this
.
pos
=
this
.
_eof
;
}
return
code
;
...
...
@@ -92,20 +81,11 @@ export class DocumentStreamReader {
* Backs up the stream n characters. Backing it up further than the
* start of the current token will cause things to break, so be careful.
*/
backUp
(
n
:
number
)
{
let
row
=
this
.
pos
.
line
;
let
column
=
this
.
pos
.
character
;
column
-=
(
n
||
1
);
while
(
row
>=
0
&&
column
<
0
)
{
row
--
;
column
+=
this
.
_lineLength
(
row
);
backUp
(
n
:
number
):
number
{
this
.
pos
-=
n
;
if
(
this
.
pos
<
0
)
{
this
.
pos
=
0
;
}
this
.
pos
=
row
<
0
||
column
<
0
?
new
Position
(
0
,
0
)
:
new
Position
(
row
,
column
);
return
this
.
peek
();
}
...
...
@@ -120,29 +100,18 @@ export class DocumentStreamReader {
/**
* Returns contents for given range
*/
substring
(
from
:
Position
,
to
:
Position
):
string
{
return
this
.
document
.
getText
(
new
Range
(
from
,
to
)
);
substring
(
from
:
number
,
to
:
number
):
string
{
return
this
.
document
.
getText
(
).
substring
(
from
,
to
);
}
/**
* Creates error object with current stream state
*/
error
(
message
:
string
):
Error
{
const
err
=
new
Error
(
`
${
message
}
at row
${
this
.
pos
.
line
}
, column
${
this
.
pos
.
character
}
`
);
const
err
=
new
Error
(
`
${
message
}
at offset
${
this
.
pos
}
`
);
return
err
;
}
/**
* Returns line length of given row, including line ending
*/
_lineLength
(
row
:
number
):
number
{
if
(
row
===
this
.
document
.
lineCount
-
1
)
{
return
this
.
document
.
lineAt
(
row
).
text
.
length
;
}
return
this
.
document
.
lineAt
(
row
).
text
.
length
+
this
.
_eol
.
length
;
}
/**
* `match` can be a character code or a function that takes a character code
* and returns a boolean. If the next character in the stream 'matches'
...
...
@@ -167,6 +136,6 @@ export class DocumentStreamReader {
eatWhile
(
match
:
number
|
Function
):
boolean
{
const
start
=
this
.
pos
;
while
(
!
this
.
eof
()
&&
this
.
eat
(
match
))
{
}
return
!
this
.
pos
.
isEqual
(
start
)
;
return
this
.
pos
!==
start
;
}
}
extensions/emmet/src/util.ts
浏览文件 @
56c808a6
...
...
@@ -150,37 +150,39 @@ const star = 42;
*/
export
function
parsePartialStylesheet
(
document
:
vscode
.
TextDocument
,
position
:
vscode
.
Position
):
FlatStylesheet
|
undefined
{
const
isCSS
=
document
.
languageId
===
'
css
'
;
let
startPosition
=
new
vscode
.
Position
(
0
,
0
);
let
endPosition
=
new
vscode
.
Position
(
document
.
lineCount
-
1
,
document
.
lineAt
(
document
.
lineCount
-
1
).
text
.
length
);
const
limitCharacter
=
document
.
offsetAt
(
position
)
-
5000
;
const
limitPosition
=
limitCharacter
>
0
?
document
.
positionAt
(
limitCharacter
)
:
startPosition
;
const
stream
=
new
DocumentStreamReader
(
document
,
position
);
function
findOpeningCommentBeforePosition
(
pos
:
vscode
.
Position
):
vscode
.
Position
|
undefined
{
let
text
=
document
.
getText
(
new
vscode
.
Range
(
0
,
0
,
pos
.
line
,
pos
.
character
));
const
positionOffset
=
document
.
offsetAt
(
position
);
let
startOffset
=
0
;
let
endOffset
=
document
.
getText
().
length
;
const
limitCharacter
=
positionOffset
-
5000
;
const
limitOffset
=
limitCharacter
>
0
?
limitCharacter
:
startOffset
;
const
stream
=
new
DocumentStreamReader
(
document
,
positionOffset
);
function
findOpeningCommentBeforePosition
(
pos
:
number
):
number
|
undefined
{
const
text
=
document
.
getText
().
substring
(
0
,
pos
);
let
offset
=
text
.
lastIndexOf
(
'
/*
'
);
if
(
offset
===
-
1
)
{
return
;
}
return
document
.
positionAt
(
offset
)
;
return
offset
;
}
function
findClosingCommentAfterPosition
(
pos
:
vscode
.
Position
):
vscode
.
Position
|
undefined
{
let
text
=
document
.
getText
(
new
vscode
.
Range
(
pos
.
line
,
pos
.
character
,
document
.
lineCount
-
1
,
document
.
lineAt
(
document
.
lineCount
-
1
).
text
.
length
)
);
function
findClosingCommentAfterPosition
(
pos
:
number
):
number
|
undefined
{
const
text
=
document
.
getText
().
substring
(
pos
);
let
offset
=
text
.
indexOf
(
'
*/
'
);
if
(
offset
===
-
1
)
{
return
;
}
offset
+=
2
+
document
.
offsetAt
(
pos
)
;
return
document
.
positionAt
(
offset
)
;
offset
+=
2
+
pos
;
return
offset
;
}
function
consumeLineCommentBackwards
()
{
if
(
!
isCSS
&&
currentLine
!==
stream
.
pos
.
line
)
{
currentLine
=
stream
.
pos
.
line
;
let
startLineComment
=
document
.
lineAt
(
currentLine
).
text
.
indexOf
(
'
//
'
);
const
posLineNumber
=
document
.
positionAt
(
stream
.
pos
).
line
;
if
(
!
isCSS
&&
currentLine
!==
posLineNumber
)
{
currentLine
=
posLineNumber
;
const
startLineComment
=
document
.
lineAt
(
currentLine
).
text
.
indexOf
(
'
//
'
);
if
(
startLineComment
>
-
1
)
{
stream
.
pos
=
new
vscode
.
Position
(
currentLine
,
startLineComment
);
stream
.
pos
=
document
.
offsetAt
(
new
vscode
.
Position
(
currentLine
,
startLineComment
)
);
}
}
}
...
...
@@ -188,7 +190,7 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
function
consumeBlockCommentBackwards
()
{
if
(
stream
.
peek
()
===
slash
)
{
if
(
stream
.
backUp
(
1
)
===
star
)
{
stream
.
pos
=
findOpeningCommentBeforePosition
(
stream
.
pos
)
||
startPosition
;
stream
.
pos
=
findOpeningCommentBeforePosition
(
stream
.
pos
)
??
startOffset
;
}
else
{
stream
.
next
();
}
...
...
@@ -198,9 +200,10 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
function
consumeCommentForwards
()
{
if
(
stream
.
eat
(
slash
))
{
if
(
stream
.
eat
(
slash
)
&&
!
isCSS
)
{
stream
.
pos
=
new
vscode
.
Position
(
stream
.
pos
.
line
+
1
,
0
);
const
posLineNumber
=
document
.
positionAt
(
stream
.
pos
).
line
;
stream
.
pos
=
document
.
offsetAt
(
new
vscode
.
Position
(
posLineNumber
+
1
,
0
));
}
else
if
(
stream
.
eat
(
star
))
{
stream
.
pos
=
findClosingCommentAfterPosition
(
stream
.
pos
)
||
endPosition
;
stream
.
pos
=
findClosingCommentAfterPosition
(
stream
.
pos
)
??
endOffset
;
}
}
}
...
...
@@ -215,10 +218,10 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
}
if
(
!
stream
.
eof
())
{
end
Position
=
stream
.
pos
;
end
Offset
=
stream
.
pos
;
}
stream
.
pos
=
position
;
stream
.
pos
=
position
Offset
;
let
openBracesToFind
=
1
;
let
currentLine
=
position
.
line
;
let
exit
=
false
;
...
...
@@ -234,7 +237,7 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
case
closeBrace
:
if
(
isCSS
)
{
stream
.
next
();
start
Position
=
stream
.
pos
;
start
Offset
=
stream
.
pos
;
exit
=
true
;
}
else
{
openBracesToFind
++
;
...
...
@@ -247,17 +250,17 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
break
;
}
if
(
position
.
line
-
stream
.
pos
.
line
>
100
||
stream
.
pos
.
isBeforeOrEqual
(
limitPosition
))
{
if
(
position
.
line
-
document
.
positionAt
(
stream
.
pos
).
line
>
100
||
stream
.
pos
<=
limitOffset
)
{
exit
=
true
;
}
}
// We are at an opening brace. We need to include its selector.
currentLine
=
stream
.
pos
.
line
;
currentLine
=
document
.
positionAt
(
stream
.
pos
)
.
line
;
openBracesToFind
=
0
;
let
foundSelector
=
false
;
while
(
!
exit
&&
!
stream
.
sof
()
&&
!
foundSelector
&&
openBracesToFind
>=
0
)
{
consumeLineCommentBackwards
();
const
ch
=
stream
.
backUp
(
1
);
...
...
@@ -283,13 +286,11 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
}
if
(
!
stream
.
sof
()
&&
foundSelector
)
{
start
Position
=
stream
.
pos
;
start
Offset
=
stream
.
pos
;
}
}
try
{
const
startOffset
=
document
.
offsetAt
(
startPosition
);
const
endOffset
=
document
.
offsetAt
(
endPosition
);
const
buffer
=
'
'
.
repeat
(
startOffset
)
+
document
.
getText
().
substring
(
startOffset
,
endOffset
);
return
parseStylesheet
(
buffer
);
}
catch
(
e
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录