Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
bfbb4835
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,发现更多精彩内容 >>
提交
bfbb4835
编写于
9月 12, 2018
作者:
A
Alex Dima
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixes #55818: Tweak indentation guessing code to accomodate for code alignment
上级
5f491b47
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
62 addition
and
7 deletion
+62
-7
src/vs/editor/common/model/indentationGuesser.ts
src/vs/editor/common/model/indentationGuesser.ts
+36
-7
src/vs/editor/test/common/model/textModel.test.ts
src/vs/editor/test/common/model/textModel.test.ts
+26
-0
未找到文件。
src/vs/editor/common/model/indentationGuesser.ts
浏览文件 @
bfbb4835
...
...
@@ -7,10 +7,18 @@
import
{
CharCode
}
from
'
vs/base/common/charCode
'
;
import
{
ITextBuffer
}
from
'
vs/editor/common/model
'
;
class
SpacesDiffResult
{
public
spacesDiff
:
number
;
public
looksLikeAlignment
:
boolean
;
}
/**
* Compute the diff in spaces between two line's indentation.
*/
function
spacesDiff
(
a
:
string
,
aLength
:
number
,
b
:
string
,
bLength
:
number
):
number
{
function
spacesDiff
(
a
:
string
,
aLength
:
number
,
b
:
string
,
bLength
:
number
,
result
:
SpacesDiffResult
):
void
{
result
.
spacesDiff
=
0
;
result
.
looksLikeAlignment
=
false
;
// This can go both ways (e.g.):
// - a: "\t"
...
...
@@ -49,22 +57,35 @@ function spacesDiff(a: string, aLength: number, b: string, bLength: number): num
}
if
(
aSpacesCnt
>
0
&&
aTabsCount
>
0
)
{
return
0
;
return
;
}
if
(
bSpacesCnt
>
0
&&
bTabsCount
>
0
)
{
return
0
;
return
;
}
let
tabsDiff
=
Math
.
abs
(
aTabsCount
-
bTabsCount
);
let
spacesDiff
=
Math
.
abs
(
aSpacesCnt
-
bSpacesCnt
);
if
(
tabsDiff
===
0
)
{
return
spacesDiff
;
// check if the indentation difference might be caused by alignment reasons
// sometime folks like to align their code, but this should not be used as a hint
result
.
spacesDiff
=
spacesDiff
;
if
(
spacesDiff
>
0
&&
0
<=
bSpacesCnt
-
1
&&
bSpacesCnt
-
1
<
a
.
length
&&
bSpacesCnt
<
b
.
length
)
{
if
(
b
.
charCodeAt
(
bSpacesCnt
)
!==
CharCode
.
Space
&&
a
.
charCodeAt
(
bSpacesCnt
-
1
)
===
CharCode
.
Space
)
{
// This looks like an alignment desire: e.g.
// const a = b + c,
// d = b - c;
result
.
looksLikeAlignment
=
true
;
}
}
return
;
}
if
(
spacesDiff
%
tabsDiff
===
0
)
{
return
spacesDiff
/
tabsDiff
;
result
.
spacesDiff
=
spacesDiff
/
tabsDiff
;
return
;
}
return
0
;
}
/**
...
...
@@ -95,6 +116,7 @@ export function guessIndentation(source: ITextBuffer, defaultTabSize: number, de
const
MAX_ALLOWED_TAB_SIZE_GUESS
=
8
;
// max(2,4,6,8) = 8
let
spacesDiffCount
=
[
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
];
// `tabSize` scores
let
tmp
=
new
SpacesDiffResult
();
for
(
let
lineNumber
=
1
;
lineNumber
<=
linesCount
;
lineNumber
++
)
{
let
currentLineLength
=
source
.
getLineLength
(
lineNumber
);
...
...
@@ -134,7 +156,14 @@ export function guessIndentation(source: ITextBuffer, defaultTabSize: number, de
linesIndentedWithSpacesCount
++
;
}
let
currentSpacesDiff
=
spacesDiff
(
previousLineText
,
previousLineIndentation
,
currentLineText
,
currentLineIndentation
);
spacesDiff
(
previousLineText
,
previousLineIndentation
,
currentLineText
,
currentLineIndentation
,
tmp
);
if
(
tmp
.
looksLikeAlignment
)
{
// skip this line entirely
continue
;
}
let
currentSpacesDiff
=
tmp
.
spacesDiff
;
if
(
currentSpacesDiff
<=
MAX_ALLOWED_TAB_SIZE_GUESS
)
{
spacesDiffCount
[
currentSpacesDiff
]
++
;
}
...
...
src/vs/editor/test/common/model/textModel.test.ts
浏览文件 @
bfbb4835
...
...
@@ -550,6 +550,32 @@ suite('Editor Model - TextModel', () => {
]);
});
test
(
'
issue #55818: Broken indentation detection
'
,
()
=>
{
assertGuess
(
true
,
2
,
[
''
,
'
/* REQUIRE */
'
,
''
,
'
const foo = require (
\'
foo
\'
),
'
,
'
bar = require (
\'
bar
\'
);
'
,
''
,
'
/* MY FN */
'
,
''
,
'
function myFn () {
'
,
''
,
'
const asd = 1,
'
,
'
dsa = 2;
'
,
''
,
'
return bar ( foo ( asd ) );
'
,
''
,
'
}
'
,
''
,
'
/* EXPORT */
'
,
''
,
'
module.exports = myFn;
'
,
''
,
]);
});
test
(
'
validatePosition
'
,
()
=>
{
let
m
=
TextModel
.
createFromString
(
'
line one
\n
line two
'
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录