Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
掘金者说
vscode
提交
616b9ad9
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,发现更多精彩内容 >>
提交
616b9ad9
编写于
11月 24, 2015
作者:
A
Alex Dima
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixes #144
上级
991b63f7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
363 addition
and
29 deletion
+363
-29
src/vs/editor/contrib/format/common/formatCommand.ts
src/vs/editor/contrib/format/common/formatCommand.ts
+33
-29
src/vs/editor/contrib/format/test/common/formatCommand.test.ts
...s/editor/contrib/format/test/common/formatCommand.test.ts
+295
-0
src/vs/editor/test/common/model/editableTextModel.test.ts
src/vs/editor/test/common/model/editableTextModel.test.ts
+35
-0
未找到文件。
src/vs/editor/contrib/format/common/formatCommand.ts
浏览文件 @
616b9ad9
...
...
@@ -23,8 +23,7 @@ export class EditOperationsCommand implements EditorCommon.ICommand {
public
getEditOperations
(
model
:
EditorCommon
.
ITokenizedModel
,
builder
:
EditorCommon
.
IEditOperationBuilder
):
void
{
this
.
_edits
// We know that this edit.range comes from the mirror model, so it should only contain \n and no \r's
.
map
((
edit
)
=>
this
.
fixLineTerminators
(
edit
,
model
)
)
.
map
((
edit
)
=>
this
.
trimEdit
(
edit
,
model
))
.
map
((
edit
)
=>
EditOperationsCommand
.
trimEdit
(
edit
,
model
))
.
filter
((
edit
)
=>
edit
!==
null
)
// produced above in case the edit.text is identical to the existing text
.
forEach
((
edit
)
=>
builder
.
addEditOperation
(
Range
.
lift
(
edit
.
range
),
edit
.
text
));
...
...
@@ -50,9 +49,8 @@ export class EditOperationsCommand implements EditorCommon.ICommand {
return
helper
.
getTrackedSelection
(
this
.
_selectionId
);
}
private
fixLineTerminators
(
edit
:
EditorCommon
.
ISingleEditOperation
,
model
:
EditorCommon
.
ITokenizedModel
)
:
EditorCommon
.
ISingleEditOperation
{
static
fixLineTerminators
(
edit
:
EditorCommon
.
ISingleEditOperation
,
model
:
EditorCommon
.
ITokenizedModel
):
void
{
edit
.
text
=
edit
.
text
.
replace
(
/
\r\n
|
\r
|
\n
/g
,
model
.
getEOL
());
return
edit
;
}
/**
...
...
@@ -63,42 +61,48 @@ export class EditOperationsCommand implements EditorCommon.ICommand {
* bug #15108. There the cursor was jumping since the tracked selection was in the middle of the range edit
* and was lost.
*/
private
trimEdit
(
edit
:
EditorCommon
.
ISingleEditOperation
,
model
:
EditorCommon
.
ITokenizedModel
):
EditorCommon
.
ISingleEditOperation
{
static
trimEdit
(
edit
:
EditorCommon
.
ISingleEditOperation
,
model
:
EditorCommon
.
ITokenizedModel
):
EditorCommon
.
ISingleEditOperation
{
var
currentText
=
model
.
getValueInRange
(
edit
.
range
);
this
.
fixLineTerminators
(
edit
,
model
);
return
this
.
_trimEdit
(
model
.
validateRange
(
edit
.
range
),
edit
.
text
,
edit
.
forceMoveMarkers
,
model
);
}
static
_trimEdit
(
editRange
:
Range
,
editText
:
string
,
editForceMoveMarkers
:
boolean
,
model
:
EditorCommon
.
ITokenizedModel
):
EditorCommon
.
ISingleEditOperation
{
let
currentText
=
model
.
getValueInRange
(
editRange
);
// Find the equal characters in the front
var
commonPrefixLength
=
Strings
.
commonPrefixLength
(
edit
.
t
ext
,
currentText
);
let
commonPrefixLength
=
Strings
.
commonPrefixLength
(
editT
ext
,
currentText
);
// If the two strings are identical, return no edit
if
(
commonPrefixLength
===
currentText
.
length
&&
commonPrefixLength
===
edit
.
t
ext
.
length
)
{
// If the two strings are identical, return no edit
(no-op)
if
(
commonPrefixLength
===
currentText
.
length
&&
commonPrefixLength
===
edit
T
ext
.
length
)
{
return
null
;
}
// Only compute a common suffix if none of the strings is already fully contained in the prefix
var
commonSuffixLength
=
0
;
if
(
commonPrefixLength
!==
currentText
.
length
&&
commonPrefixLength
!==
edit
.
text
.
length
)
{
commonSuffixLength
=
Strings
.
commonSuffixLength
(
edit
.
text
,
currentText
);
if
(
commonPrefixLength
>
0
)
{
// Apply front trimming
let
newStartPosition
=
model
.
modifyPosition
(
editRange
.
getStartPosition
(),
commonPrefixLength
);
editRange
=
new
Range
(
newStartPosition
.
lineNumber
,
newStartPosition
.
column
,
editRange
.
endLineNumber
,
editRange
.
endColumn
);
editText
=
editText
.
substring
(
commonPrefixLength
);
currentText
=
currentText
.
substr
(
commonPrefixLength
);
}
// Adjust start position
var
newStartPosition
=
new
Position
(
edit
.
range
.
startLineNumber
,
edit
.
range
.
startColumn
);
newStartPosition
=
model
.
modifyPosition
(
newStartPosition
,
commonPrefixLength
);
// Adjust end position
var
newEndPosition
=
new
Position
(
edit
.
range
.
endLineNumber
,
edit
.
range
.
endColumn
);
newEndPosition
=
model
.
modifyPosition
(
newEndPosition
,
-
commonSuffixLength
);
// Find the equal characters in the rear
let
commonSuffixLength
=
Strings
.
commonSuffixLength
(
editText
,
currentText
);
//Trim the text
var
newText
=
edit
.
text
.
slice
(
commonPrefixLength
,
edit
.
text
.
length
-
commonSuffixLength
);
if
(
commonSuffixLength
>
0
)
{
// Apply rear trimming
let
newEndPosition
=
model
.
modifyPosition
(
editRange
.
getEndPosition
(),
-
commonSuffixLength
);
editRange
=
new
Range
(
editRange
.
startLineNumber
,
editRange
.
startColumn
,
newEndPosition
.
lineNumber
,
newEndPosition
.
column
);
editText
=
editText
.
substring
(
0
,
editText
.
length
-
commonSuffixLength
);
currentText
=
currentText
.
substring
(
0
,
currentText
.
length
-
commonSuffixLength
);
}
return
{
text
:
newText
,
range
:
{
startLineNumber
:
newStartPosition
.
lineNumber
,
startColumn
:
newStartPosition
.
column
,
endLineNumber
:
newEndPosition
.
lineNumber
,
endColumn
:
newEndPosition
.
column
}};
text
:
editText
,
range
:
editRange
,
forceMoveMarkers
:
editForceMoveMarkers
};
}
}
src/vs/editor/contrib/format/test/common/formatCommand.test.ts
0 → 100644
浏览文件 @
616b9ad9
/*---------------------------------------------------------------------------------------------
* 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
{
Range
}
from
'
vs/editor/common/core/range
'
;
import
TU
=
require
(
'
vs/editor/test/common/commands/commandTestUtils
'
);
import
{
EditOperationsCommand
}
from
'
vs/editor/contrib/format/common/formatCommand
'
;
import
{
Selection
}
from
'
vs/editor/common/core/selection
'
;
import
{
Model
}
from
'
vs/editor/common/model/model
'
;
import
EditorCommon
=
require
(
'
vs/editor/common/editorCommon
'
);
function
editOp
(
startLineNumber
:
number
,
startColumn
:
number
,
endLineNumber
:
number
,
endColumn
:
number
,
text
:
string
[]):
EditorCommon
.
ISingleEditOperation
{
return
{
range
:
new
Range
(
startLineNumber
,
startColumn
,
endLineNumber
,
endColumn
),
text
:
text
.
join
(
'
\n
'
),
forceMoveMarkers
:
false
};
}
suite
(
'
FormatCommand.trimEdit
'
,
()
=>
{
function
testTrimEdit
(
lines
:
string
[],
edit
:
EditorCommon
.
ISingleEditOperation
,
expected
:
EditorCommon
.
ISingleEditOperation
):
void
{
let
model
=
new
Model
(
lines
.
join
(
'
\n
'
),
null
);
let
actual
=
EditOperationsCommand
.
trimEdit
(
edit
,
model
);
assert
.
deepEqual
(
actual
,
expected
);
model
.
dispose
();
}
test
(
'
single-line no-op
'
,
()
=>
{
testTrimEdit
(
[
'
some text
'
,
'
some other text
'
],
editOp
(
1
,
1
,
1
,
10
,
[
'
some text
'
]),
null
)
});
test
(
'
multi-line no-op 1
'
,
()
=>
{
testTrimEdit
(
[
'
some text
'
,
'
some other text
'
],
editOp
(
1
,
1
,
2
,
16
,
[
'
some text
'
,
'
some other text
'
]),
null
)
});
test
(
'
multi-line no-op 2
'
,
()
=>
{
testTrimEdit
(
[
'
some text
'
,
'
some other text
'
],
editOp
(
1
,
1
,
2
,
1
,
[
'
some text
'
,
''
]),
null
)
});
test
(
'
simple prefix, no suffix
'
,
()
=>
{
testTrimEdit
(
[
'
some text
'
,
'
some other text
'
],
editOp
(
1
,
1
,
1
,
10
,
[
'
some interesting thing
'
]),
editOp
(
1
,
6
,
1
,
10
,
[
'
interesting thing
'
])
)
});
test
(
'
whole line prefix, no suffix
'
,
()
=>
{
testTrimEdit
(
[
'
some text
'
,
'
some other text
'
],
editOp
(
1
,
1
,
1
,
10
,
[
'
some text
'
,
'
interesting thing
'
]),
editOp
(
1
,
10
,
1
,
10
,
[
''
,
'
interesting thing
'
])
)
});
test
(
'
multi-line prefix, no suffix
'
,
()
=>
{
testTrimEdit
(
[
'
some text
'
,
'
some other text
'
],
editOp
(
1
,
1
,
2
,
16
,
[
'
some text
'
,
'
some other interesting thing
'
]),
editOp
(
2
,
12
,
2
,
16
,
[
'
interesting thing
'
])
)
});
test
(
'
no prefix, simple suffix
'
,
()
=>
{
testTrimEdit
(
[
'
some text
'
,
'
some other text
'
],
editOp
(
1
,
1
,
1
,
10
,
[
'
interesting text
'
]),
editOp
(
1
,
1
,
1
,
5
,
[
'
interesting
'
])
)
});
test
(
'
no prefix, whole line suffix
'
,
()
=>
{
testTrimEdit
(
[
'
some text
'
,
'
some other text
'
],
editOp
(
1
,
1
,
1
,
10
,
[
'
interesting thing
'
,
'
some text
'
]),
editOp
(
1
,
1
,
1
,
1
,
[
'
interesting thing
'
,
''
])
)
});
test
(
'
no prefix, multi-line suffix
'
,
()
=>
{
testTrimEdit
(
[
'
some text
'
,
'
some other text
'
],
editOp
(
1
,
1
,
2
,
16
,
[
'
interesting thing text
'
,
'
some other text
'
]),
editOp
(
1
,
1
,
1
,
5
,
[
'
interesting thing
'
])
)
});
test
(
'
no overlapping prefix & suffix
'
,
()
=>
{
testTrimEdit
(
[
'
some cool text
'
],
editOp
(
1
,
1
,
1
,
15
,
[
'
some interesting text
'
]),
editOp
(
1
,
6
,
1
,
10
,
[
'
interesting
'
])
)
});
test
(
'
overlapping prefix & suffix 1
'
,
()
=>
{
testTrimEdit
(
[
'
some cool text
'
],
editOp
(
1
,
1
,
1
,
15
,
[
'
some cool cool text
'
]),
editOp
(
1
,
11
,
1
,
11
,
[
'
cool
'
])
)
});
test
(
'
overlapping prefix & suffix 2
'
,
()
=>
{
testTrimEdit
(
[
'
some cool cool text
'
],
editOp
(
1
,
1
,
1
,
29
,
[
'
some cool text
'
]),
editOp
(
1
,
11
,
1
,
16
,
[
''
])
)
});
});
suite
(
'
FormatCommand
'
,
()
=>
{
function
testFormatCommand
(
lines
:
string
[],
selection
:
Selection
,
edits
:
EditorCommon
.
ISingleEditOperation
[],
expectedLines
:
string
[],
expectedSelection
:
Selection
):
void
{
TU
.
testCommand
(
lines
,
null
,
selection
,
(
sel
)
=>
new
EditOperationsCommand
(
edits
,
sel
),
expectedLines
,
expectedSelection
);
}
test
(
'
no-op
'
,
()
=>
{
testFormatCommand
(
[
'
some text
'
,
'
some other text
'
],
new
Selection
(
2
,
1
,
2
,
5
),
[
editOp
(
1
,
1
,
2
,
16
,
[
'
some text
'
,
'
some other text
'
])
],
[
'
some text
'
,
'
some other text
'
],
new
Selection
(
2
,
1
,
2
,
5
)
);
});
test
(
'
trim beginning
'
,
()
=>
{
testFormatCommand
(
[
'
some text
'
,
'
some other text
'
],
new
Selection
(
2
,
1
,
2
,
5
),
[
editOp
(
1
,
1
,
2
,
16
,
[
'
some text
'
,
'
some new other text
'
])
],
[
'
some text
'
,
'
some new other text
'
],
new
Selection
(
2
,
1
,
2
,
5
)
);
});
test
(
'
issue #144
'
,
()
=>
{
testFormatCommand
(
[
'
package caddy
'
,
''
,
'
func main() {
'
,
'
\t
fmt.Println("Hello World! :)")
'
,
'
}
'
,
''
],
new
Selection
(
1
,
1
,
1
,
1
),
[
editOp
(
1
,
1
,
6
,
1
,
[
'
package caddy
'
,
''
,
'
import "fmt"
'
,
''
,
'
func main() {
'
,
'
\t
fmt.Println("Hello World! :)")
'
,
'
}
'
,
''
])
],
[
'
package caddy
'
,
''
,
'
import "fmt"
'
,
''
,
'
func main() {
'
,
'
\t
fmt.Println("Hello World! :)")
'
,
'
}
'
,
''
],
new
Selection
(
1
,
1
,
1
,
1
)
);
});
});
\ No newline at end of file
src/vs/editor/test/common/model/editableTextModel.test.ts
浏览文件 @
616b9ad9
...
...
@@ -1139,6 +1139,41 @@ suite('EditorModel - EditableTextModel.applyEdits', () => {
);
});
test
(
'
issue #144
'
,
()
=>
{
testApplyEdits
(
[
'
package caddy
'
,
''
,
'
func main() {
'
,
'
\t
fmt.Println("Hello World! :)")
'
,
'
}
'
,
''
],
[
editOp
(
1
,
1
,
6
,
1
,
[
'
package caddy
'
,
''
,
'
import "fmt"
'
,
''
,
'
func main() {
'
,
'
\t
fmt.Println("Hello World! :)")
'
,
'
}
'
,
''
])
],
[
'
package caddy
'
,
''
,
'
import "fmt"
'
,
''
,
'
func main() {
'
,
'
\t
fmt.Println("Hello World! :)")
'
,
'
}
'
,
''
]
);
});
function
assertSyncedModels
(
text
:
string
,
callback
:(
model
:
EditableTextModel
,
assertMirrorModels
:()
=>
void
)
=>
void
,
setup
:(
model
:
EditableTextModel
)
=>
void
=
null
):
void
{
var
model
=
new
EditableTextModel
([],
TextModel
.
toRawText
(
text
),
null
);
model
.
setEOL
(
EditorCommon
.
EndOfLineSequence
.
LF
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录