Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
d3073a20
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,体验更适合开发者的 AI 搜索 >>
提交
d3073a20
编写于
1月 18, 2016
作者:
A
Alex Dima
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add tests for textAreaState
上级
161eb75e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
397 addition
and
1 deletion
+397
-1
src/vs/editor/common/controller/textAreaState.ts
src/vs/editor/common/controller/textAreaState.ts
+14
-1
src/vs/editor/test/common/controller/textAreaState.test.ts
src/vs/editor/test/common/controller/textAreaState.test.ts
+286
-0
src/vs/editor/test/common/mocks/mockTextAreaWrapper.ts
src/vs/editor/test/common/mocks/mockTextAreaWrapper.ts
+97
-0
未找到文件。
src/vs/editor/common/controller/textAreaState.ts
浏览文件 @
d3073a20
...
...
@@ -69,7 +69,17 @@ export class TextAreaState {
}
public
toString
():
string
{
return
'
[ <
'
+
this
.
value
+
'
>, selectionStart:
'
+
this
.
selectionStart
+
'
, selectionEnd:
'
+
this
.
selectionEnd
+
'
]
'
;
return
'
[ <
'
+
this
.
value
+
'
>, selectionStart:
'
+
this
.
selectionStart
+
'
, selectionEnd:
'
+
this
.
selectionEnd
+
'
, isInOverwriteMode:
'
+
this
.
isInOverwriteMode
+
'
, selectionToken:
'
+
this
.
selectionToken
+
'
]
'
;
}
public
equals
(
other
:
TextAreaState
):
boolean
{
return
(
this
.
value
===
other
.
value
&&
this
.
selectionStart
===
other
.
selectionStart
&&
this
.
selectionEnd
===
other
.
selectionEnd
&&
this
.
isInOverwriteMode
===
other
.
isInOverwriteMode
&&
this
.
selectionToken
===
other
.
selectionToken
);
}
public
static
fromTextArea
(
textArea
:
ITextAreaWrapper
,
selectionToken
:
number
):
TextAreaState
{
...
...
@@ -153,6 +163,9 @@ export class TextAreaState {
}
public
extractNewText
(
previousState
:
TextAreaState
):
string
{
// console.log('-----------')
// console.log('prev:' + String(previousState));
// console.log('curr:' + String(this));
if
(
this
.
selectionStart
!==
this
.
selectionEnd
)
{
// There is a selection in the textarea => ignore input
return
''
;
...
...
src/vs/editor/test/common/controller/textAreaState.test.ts
0 → 100644
浏览文件 @
d3073a20
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'
use strict
'
;
import
assert
=
require
(
'
assert
'
);
import
{
Position
}
from
'
vs/editor/common/core/position
'
;
import
{
Range
}
from
'
vs/editor/common/core/range
'
;
import
{
EndOfLinePreference
,
IEditorRange
,
IRange
,
IEditorPosition
}
from
'
vs/editor/common/editorCommon
'
;
import
{
ISimpleModel
,
TextAreaState
}
from
'
vs/editor/common/controller/textAreaState
'
;
import
{
MockTextAreaWrapper
}
from
'
vs/editor/test/common/mocks/mockTextAreaWrapper
'
;
suite
(
'
TextAreaState
'
,
()
=>
{
function
assertTextAreaState
(
actual
:
TextAreaState
,
value
:
string
,
selectionStart
:
number
,
selectionEnd
:
number
,
isInOverwriteMode
:
boolean
,
selectionToken
:
number
):
void
{
let
desired
=
new
TextAreaState
(
value
,
selectionStart
,
selectionEnd
,
isInOverwriteMode
,
selectionToken
);
assert
.
ok
(
desired
.
equals
(
actual
),
desired
.
toString
()
+
'
==
'
+
actual
.
toString
());
}
test
(
'
fromTextArea
'
,
()
=>
{
let
textArea
=
new
MockTextAreaWrapper
();
textArea
.
_value
=
'
Hello world!
'
;
textArea
.
_selectionStart
=
1
;
textArea
.
_selectionEnd
=
12
;
textArea
.
_isInOverwriteMode
=
false
;
let
actual
=
TextAreaState
.
fromTextArea
(
textArea
,
3
);
assertTextAreaState
(
actual
,
'
Hello world!
'
,
1
,
12
,
false
,
3
);
assert
.
equal
(
actual
.
getValue
(),
'
Hello world!
'
);
assert
.
equal
(
actual
.
getSelectionStart
(),
1
);
assert
.
equal
(
actual
.
getSelectionToken
(),
3
);
actual
.
resetSelection
();
assertTextAreaState
(
actual
,
'
Hello world!
'
,
12
,
12
,
false
,
3
);
textArea
.
dispose
();
});
test
(
'
applyToTextArea
'
,
()
=>
{
let
textArea
=
new
MockTextAreaWrapper
();
textArea
.
_value
=
'
Hello world!
'
;
textArea
.
_selectionStart
=
1
;
textArea
.
_selectionEnd
=
12
;
textArea
.
_isInOverwriteMode
=
false
;
let
state
=
new
TextAreaState
(
'
Hi world!
'
,
2
,
2
,
false
,
0
);
state
.
applyToTextArea
(
textArea
,
false
);
assert
.
equal
(
textArea
.
_value
,
'
Hi world!
'
);
assert
.
equal
(
textArea
.
_selectionStart
,
9
);
assert
.
equal
(
textArea
.
_selectionEnd
,
9
);
state
=
new
TextAreaState
(
'
Hi world!
'
,
3
,
3
,
false
,
0
);
state
.
applyToTextArea
(
textArea
,
false
);
assert
.
equal
(
textArea
.
_value
,
'
Hi world!
'
);
assert
.
equal
(
textArea
.
_selectionStart
,
9
);
assert
.
equal
(
textArea
.
_selectionEnd
,
9
);
state
=
new
TextAreaState
(
'
Hi world!
'
,
0
,
2
,
false
,
0
);
state
.
applyToTextArea
(
textArea
,
true
);
assert
.
equal
(
textArea
.
_value
,
'
Hi world!
'
);
assert
.
equal
(
textArea
.
_selectionStart
,
0
);
assert
.
equal
(
textArea
.
_selectionEnd
,
2
);
textArea
.
dispose
();
});
function
testExtractNewText
(
prevState
:
TextAreaState
,
value
:
string
,
selectionStart
:
number
,
selectionEnd
:
number
,
isInOverwriteMode
:
boolean
,
expected
:
string
):
void
{
let
textArea
=
new
MockTextAreaWrapper
();
textArea
.
_value
=
value
;
textArea
.
_selectionStart
=
selectionStart
;
textArea
.
_selectionEnd
=
selectionEnd
;
textArea
.
_isInOverwriteMode
=
isInOverwriteMode
;
let
newState
=
TextAreaState
.
fromTextArea
(
textArea
,
prevState
?
prevState
.
getSelectionToken
()
:
0
);
let
actual
=
newState
.
extractNewText
(
prevState
);
assert
.
equal
(
actual
,
expected
);
textArea
.
dispose
();
}
test
(
'
extractNewText - no previous state with selection
'
,
()
=>
{
testExtractNewText
(
null
,
'
a
'
,
0
,
1
,
false
,
''
);
});
test
(
'
extractNewText - no previous state without selection
'
,
()
=>
{
testExtractNewText
(
null
,
'
a
'
,
1
,
1
,
false
,
'
a
'
);
});
test
(
'
extractNewText - typing does not cause a selection
'
,
()
=>
{
testExtractNewText
(
new
TextAreaState
(
''
,
0
,
0
,
false
,
0
),
'
a
'
,
0
,
1
,
false
,
''
);
});
test
(
'
extractNewText - had the textarea empty
'
,
()
=>
{
testExtractNewText
(
new
TextAreaState
(
''
,
0
,
0
,
false
,
0
),
'
a
'
,
1
,
1
,
false
,
'
a
'
);
});
test
(
'
extractNewText - had the entire line selected
'
,
()
=>
{
testExtractNewText
(
new
TextAreaState
(
'
Hello world!
'
,
0
,
12
,
false
,
0
),
'
H
'
,
1
,
1
,
false
,
'
H
'
);
});
test
(
'
extractNewText - had previous text 1
'
,
()
=>
{
testExtractNewText
(
new
TextAreaState
(
'
Hello world!
'
,
12
,
12
,
false
,
0
),
'
Hello world!a
'
,
13
,
13
,
false
,
'
a
'
);
});
test
(
'
extractNewText - had previous text 2
'
,
()
=>
{
testExtractNewText
(
new
TextAreaState
(
'
Hello world!
'
,
0
,
0
,
false
,
0
),
'
aHello world!
'
,
1
,
1
,
false
,
'
a
'
);
});
test
(
'
extractNewText - had previous text 3
'
,
()
=>
{
testExtractNewText
(
new
TextAreaState
(
'
Hello world!
'
,
6
,
11
,
false
,
0
),
'
Hello other!
'
,
11
,
11
,
false
,
'
other
'
);
});
test
(
'
extractNewText - IME
'
,
()
=>
{
testExtractNewText
(
new
TextAreaState
(
''
,
0
,
0
,
false
,
0
),
'
これは
'
,
3
,
3
,
false
,
'
これは
'
);
});
test
(
'
extractNewText - isInOverwriteMode
'
,
()
=>
{
testExtractNewText
(
new
TextAreaState
(
'
Hello world!
'
,
0
,
0
,
false
,
0
),
'
Aello world!
'
,
1
,
1
,
true
,
'
A
'
);
});
function
testFromEditorSelectionAndPreviousState
(
eol
:
string
,
lines
:
string
[],
range
:
Range
,
prevSelectionToken
:
number
):
TextAreaState
{
let
model
=
new
SimpleModel
(
lines
,
eol
);
return
TextAreaState
.
fromEditorSelectionAndPreviousState
(
model
,
range
,
prevSelectionToken
);
}
test
(
'
fromEditorSelectionAndPreviousState - no selection on first line
'
,
()
=>
{
let
actual
=
testFromEditorSelectionAndPreviousState
(
'
\n
'
,
[
'
Just a line
'
,
'
And another line
'
],
new
Range
(
1
,
1
,
1
,
1
),
0
);
assertTextAreaState
(
actual
,
'
Just a line
'
,
0
,
11
,
false
,
1
);
});
test
(
'
fromEditorSelectionAndPreviousState - no selection on second line
'
,
()
=>
{
let
actual
=
testFromEditorSelectionAndPreviousState
(
'
\n
'
,
[
'
Just a line
'
,
'
And another line
'
,
'
And yet another line
'
,
],
new
Range
(
2
,
1
,
2
,
1
),
0
);
assertTextAreaState
(
actual
,
'
And another line
'
,
0
,
16
,
false
,
2
);
});
test
(
'
fromEditorSelectionAndPreviousState - on a long line with selectionToken mismatch
'
,
()
=>
{
let
aLongLine
=
'
a
'
;
for
(
let
i
=
0
;
i
<
10
;
i
++
)
{
aLongLine
=
aLongLine
+
aLongLine
;
}
let
actual
=
testFromEditorSelectionAndPreviousState
(
'
\n
'
,
[
'
Just a line
'
,
aLongLine
,
'
And yet another line
'
,
],
new
Range
(
2
,
500
,
2
,
500
),
0
);
assertTextAreaState
(
actual
,
'
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa…aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
'
,
0
,
201
,
false
,
2
);
});
test
(
'
fromEditorSelectionAndPreviousState - on a long line with same selectionToken
'
,
()
=>
{
let
aLongLine
=
'
a
'
;
for
(
let
i
=
0
;
i
<
10
;
i
++
)
{
aLongLine
=
aLongLine
+
aLongLine
;
}
let
actual
=
testFromEditorSelectionAndPreviousState
(
'
\n
'
,
[
'
Just a line
'
,
aLongLine
,
'
And yet another line
'
,
],
new
Range
(
2
,
500
,
2
,
500
),
2
);
assertTextAreaState
(
actual
,
'
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
'
,
100
,
100
,
false
,
2
);
});
});
class
SimpleModel
implements
ISimpleModel
{
private
_lines
:
string
[];
private
_eol
:
string
;
constructor
(
lines
:
string
[],
eol
:
string
)
{
this
.
_lines
=
lines
;
this
.
_eol
=
eol
;
}
public
getLineMaxColumn
(
lineNumber
:
number
):
number
{
return
this
.
_lines
[
lineNumber
-
1
].
length
+
1
;
}
private
_getEndOfLine
(
eol
:
EndOfLinePreference
):
string
{
switch
(
eol
)
{
case
EndOfLinePreference
.
LF
:
return
'
\n
'
;
case
EndOfLinePreference
.
CRLF
:
return
'
\r\n
'
;
case
EndOfLinePreference
.
TextDefined
:
return
this
.
_eol
;
}
throw
new
Error
(
'
Unknown EOL preference
'
);
}
public
getValueInRange
(
range
:
IRange
,
eol
:
EndOfLinePreference
):
string
{
if
(
Range
.
isEmpty
(
range
))
{
return
''
;
}
if
(
range
.
startLineNumber
===
range
.
endLineNumber
)
{
return
this
.
_lines
[
range
.
startLineNumber
-
1
].
substring
(
range
.
startColumn
-
1
,
range
.
endColumn
-
1
);
}
var
lineEnding
=
this
.
_getEndOfLine
(
eol
),
startLineIndex
=
range
.
startLineNumber
-
1
,
endLineIndex
=
range
.
endLineNumber
-
1
,
resultLines
:
string
[]
=
[];
resultLines
.
push
(
this
.
_lines
[
startLineIndex
].
substring
(
range
.
startColumn
-
1
));
for
(
var
i
=
startLineIndex
+
1
;
i
<
endLineIndex
;
i
++
)
{
resultLines
.
push
(
this
.
_lines
[
i
]);
}
resultLines
.
push
(
this
.
_lines
[
endLineIndex
].
substring
(
0
,
range
.
endColumn
-
1
));
return
resultLines
.
join
(
lineEnding
);
}
public
getModelLineContent
(
lineNumber
:
number
):
string
{
return
this
.
_lines
[
lineNumber
-
1
];
}
public
getLineCount
():
number
{
return
this
.
_lines
.
length
;
}
public
convertViewPositionToModelPosition
(
viewLineNumber
:
number
,
viewColumn
:
number
):
IEditorPosition
{
return
new
Position
(
viewLineNumber
,
viewColumn
);
}
}
src/vs/editor/test/common/mocks/mockTextAreaWrapper.ts
0 → 100644
浏览文件 @
d3073a20
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'
use strict
'
;
import
{
ITextAreaWrapper
,
IClipboardEvent
,
IKeyboardEventWrapper
,
ITextAreaStyle
}
from
'
vs/editor/common/controller/textAreaState
'
;
import
Event
,
{
Emitter
}
from
'
vs/base/common/event
'
;
import
{
Disposable
}
from
'
vs/base/common/lifecycle
'
;
export
class
MockTextAreaWrapper
extends
Disposable
implements
ITextAreaWrapper
{
private
_onKeyDown
=
this
.
_register
(
new
Emitter
<
IKeyboardEventWrapper
>
());
public
onKeyDown
:
Event
<
IKeyboardEventWrapper
>
=
this
.
_onKeyDown
.
event
;
private
_onKeyUp
=
this
.
_register
(
new
Emitter
<
IKeyboardEventWrapper
>
());
public
onKeyUp
:
Event
<
IKeyboardEventWrapper
>
=
this
.
_onKeyUp
.
event
;
private
_onKeyPress
=
this
.
_register
(
new
Emitter
<
IKeyboardEventWrapper
>
());
public
onKeyPress
:
Event
<
IKeyboardEventWrapper
>
=
this
.
_onKeyPress
.
event
;
private
_onCompositionStart
=
this
.
_register
(
new
Emitter
<
void
>
());
public
onCompositionStart
:
Event
<
void
>
=
this
.
_onCompositionStart
.
event
;
private
_onCompositionEnd
=
this
.
_register
(
new
Emitter
<
void
>
());
public
onCompositionEnd
:
Event
<
void
>
=
this
.
_onCompositionEnd
.
event
;
private
_onInput
=
this
.
_register
(
new
Emitter
<
void
>
());
public
onInput
:
Event
<
void
>
=
this
.
_onInput
.
event
;
private
_onCut
=
this
.
_register
(
new
Emitter
<
IClipboardEvent
>
());
public
onCut
:
Event
<
IClipboardEvent
>
=
this
.
_onCut
.
event
;
private
_onCopy
=
this
.
_register
(
new
Emitter
<
IClipboardEvent
>
());
public
onCopy
:
Event
<
IClipboardEvent
>
=
this
.
_onCopy
.
event
;
private
_onPaste
=
this
.
_register
(
new
Emitter
<
IClipboardEvent
>
());
public
onPaste
:
Event
<
IClipboardEvent
>
=
this
.
_onPaste
.
event
;
public
_value
:
string
;
public
_selectionStart
:
number
;
public
_selectionEnd
:
number
;
public
_lastSetStyle
:
ITextAreaStyle
;
public
_isInOverwriteMode
:
boolean
;
constructor
()
{
super
();
this
.
_value
=
''
;
this
.
_selectionStart
=
0
;
this
.
_selectionEnd
=
0
;
this
.
_lastSetStyle
=
null
;
this
.
_isInOverwriteMode
=
false
;
}
public
get
value
():
string
{
return
this
.
_value
;
}
public
set
value
(
value
:
string
)
{
this
.
_value
=
value
;
this
.
_selectionStart
=
this
.
_value
.
length
;
this
.
_selectionEnd
=
this
.
_value
.
length
;
}
public
get
selectionStart
():
number
{
return
this
.
_selectionStart
;
}
public
get
selectionEnd
():
number
{
return
this
.
_selectionEnd
;
}
public
setSelectionRange
(
selectionStart
:
number
,
selectionEnd
:
number
):
void
{
if
(
selectionStart
<
0
)
{
selectionStart
=
0
;
}
if
(
selectionStart
>
this
.
_value
.
length
)
{
selectionStart
=
this
.
_value
.
length
;
}
if
(
selectionEnd
<
0
)
{
selectionEnd
=
0
;
}
if
(
selectionEnd
>
this
.
_value
.
length
)
{
selectionEnd
=
this
.
_value
.
length
;
}
this
.
_selectionStart
=
selectionStart
;
this
.
_selectionEnd
=
selectionEnd
;
}
public
setStyle
(
style
:
ITextAreaStyle
):
void
{
this
.
_lastSetStyle
=
style
;
}
public
isInOverwriteMode
():
boolean
{
return
this
.
_isInOverwriteMode
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录