Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
掘金者说
vscode
提交
19a881b7
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,发现更多精彩内容 >>
提交
19a881b7
编写于
3月 25, 2017
作者:
A
Alex Dima
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixes #20891: All cursors should do the same thing when typing
上级
c2eba7cc
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
149 addition
and
88 deletion
+149
-88
src/vs/editor/common/controller/cursor.ts
src/vs/editor/common/controller/cursor.ts
+5
-1
src/vs/editor/common/controller/cursorTypeOperations.ts
src/vs/editor/common/controller/cursorTypeOperations.ts
+122
-87
src/vs/editor/test/common/controller/cursor.test.ts
src/vs/editor/test/common/controller/cursor.test.ts
+22
-0
未找到文件。
src/vs/editor/common/controller/cursor.ts
浏览文件 @
19a881b7
...
...
@@ -1399,7 +1399,11 @@ export class Cursor extends EventEmitter {
// Here we must interpret each typed character individually, that's why we create a new context
ctx
.
hasExecutedCommands
=
this
.
_createAndInterpretHandlerCtx
(
ctx
.
eventSource
,
ctx
.
eventData
,
(
charHandlerCtx
:
IMultipleCursorOperationContext
)
=>
{
this
.
_applyEditForAll
(
charHandlerCtx
,
(
cursor
)
=>
TypeOperations
.
typeWithInterceptors
(
cursor
.
config
,
cursor
.
model
,
cursor
.
modelState
,
chr
));
// Decide what all cursors will do up-front
const
cursors
=
this
.
cursors
.
getAll
();
const
states
=
cursors
.
map
(
cursor
=>
cursor
.
modelState
);
const
editOperations
=
TypeOperations
.
typeWithInterceptors
(
cursors
[
0
].
config
,
cursors
[
0
].
model
,
states
,
chr
);
this
.
_applyEditForAll
(
charHandlerCtx
,
(
cursor
,
cursorIndex
)
=>
editOperations
[
cursorIndex
]);
// The last typed character gets to win
ctx
.
cursorPositionChangeReason
=
charHandlerCtx
.
cursorPositionChangeReason
;
...
...
src/vs/editor/common/controller/cursorTypeOperations.ts
浏览文件 @
19a881b7
...
...
@@ -273,37 +273,32 @@ export class TypeOperations {
});
}
private
static
_typeInterceptorEnter
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
ch
:
string
):
EditOperationResult
{
if
(
ch
!==
'
\n
'
)
{
return
null
;
}
return
TypeOperations
.
_enter
(
config
,
model
,
false
,
cursor
.
selection
);
}
private
static
isAutoClosingCloseCharType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
ch
:
string
):
boolean
{
if
(
!
config
.
autoClosingBrackets
)
{
private
static
_isAutoClosingCloseCharType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursors
:
SingleCursorState
[],
ch
:
string
):
boolean
{
if
(
!
config
.
autoClosingBrackets
||
!
config
.
autoClosingPairsClose
.
hasOwnProperty
(
ch
))
{
return
false
;
}
const
selection
=
cursor
.
selection
;
const
position
=
cursor
.
position
;
for
(
let
i
=
0
,
len
=
cursors
.
length
;
i
<
len
;
i
++
)
{
const
cursor
=
cursors
[
i
];
const
selection
=
cursor
.
selection
;
if
(
!
selection
.
isEmpty
()
||
!
config
.
autoClosingPairsClose
.
hasOwnProperty
(
ch
))
{
return
false
;
}
if
(
!
selection
.
isEmpty
(
))
{
return
false
;
}
const
lineText
=
model
.
getLineContent
(
position
.
lineNumber
);
const
afterCharacter
=
lineText
.
charAt
(
position
.
column
-
1
);
const
position
=
cursor
.
position
;
const
lineText
=
model
.
getLineContent
(
position
.
lineNumber
);
const
afterCharacter
=
lineText
.
charAt
(
position
.
column
-
1
);
if
(
afterCharacter
!==
ch
)
{
return
false
;
if
(
afterCharacter
!==
ch
)
{
return
false
;
}
}
return
true
;
}
private
static
runAutoClosingCloseCharType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
ch
:
string
):
EditOperationResult
{
private
static
_
runAutoClosingCloseCharType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
ch
:
string
):
EditOperationResult
{
const
position
=
cursor
.
position
;
const
typeSelection
=
new
Range
(
position
.
lineNumber
,
position
.
column
,
position
.
lineNumber
,
position
.
column
+
1
);
return
new
EditOperationResult
(
new
ReplaceCommand
(
typeSelection
,
ch
),
{
...
...
@@ -312,55 +307,61 @@ export class TypeOperations {
});
}
private
static
isAutoClosingOpenCharType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
ch
:
string
):
boolean
{
if
(
!
config
.
autoClosingBrackets
)
{
return
false
;
}
const
selection
=
cursor
.
selection
;
const
position
=
cursor
.
position
;
if
(
!
selection
.
isEmpty
()
||
!
config
.
autoClosingPairsOpen
.
hasOwnProperty
(
ch
))
{
private
static
_isAutoClosingOpenCharType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursors
:
SingleCursorState
[],
ch
:
string
):
boolean
{
if
(
!
config
.
autoClosingBrackets
||
!
config
.
autoClosingPairsOpen
.
hasOwnProperty
(
ch
))
{
return
false
;
}
const
lineText
=
model
.
getLineContent
(
position
.
lineNumber
);
const
afterCharacter
=
lineText
.
charAt
(
position
.
column
-
1
)
;
// Only consider auto closing the pair if a space follows or if another autoclosed pair follows
if
(
afterCharacter
)
{
const
thisBraceIsSymmetric
=
(
config
.
autoClosingPairsOpen
[
ch
]
===
ch
);
for
(
let
i
=
0
,
len
=
cursors
.
length
;
i
<
len
;
i
++
)
{
const
cursor
=
cursors
[
i
]
;
const
selection
=
cursor
.
selection
;
if
(
!
selection
.
isEmpty
())
{
return
false
;
}
let
isBeforeCloseBrace
=
false
;
for
(
let
otherCloseBrace
in
config
.
autoClosingPairsClose
)
{
const
otherBraceIsSymmetric
=
(
config
.
autoClosingPairsOpen
[
otherCloseBrace
]
===
otherCloseBrace
);
if
(
!
thisBraceIsSymmetric
&&
otherBraceIsSymmetric
)
{
continue
;
const
position
=
cursor
.
position
;
const
lineText
=
model
.
getLineContent
(
position
.
lineNumber
);
const
afterCharacter
=
lineText
.
charAt
(
position
.
column
-
1
);
// Only consider auto closing the pair if a space follows or if another autoclosed pair follows
if
(
afterCharacter
)
{
const
thisBraceIsSymmetric
=
(
config
.
autoClosingPairsOpen
[
ch
]
===
ch
);
let
isBeforeCloseBrace
=
false
;
for
(
let
otherCloseBrace
in
config
.
autoClosingPairsClose
)
{
const
otherBraceIsSymmetric
=
(
config
.
autoClosingPairsOpen
[
otherCloseBrace
]
===
otherCloseBrace
);
if
(
!
thisBraceIsSymmetric
&&
otherBraceIsSymmetric
)
{
continue
;
}
if
(
afterCharacter
===
otherCloseBrace
)
{
isBeforeCloseBrace
=
true
;
break
;
}
}
if
(
afterCharacter
===
otherCloseBrace
)
{
isBeforeCloseBrace
=
true
;
break
;
if
(
!
isBeforeCloseBrace
&&
!
/
\s
/
.
test
(
afterCharacter
))
{
return
false
;
}
}
if
(
!
isBeforeCloseBrace
&&
!
/
\s
/
.
test
(
afterCharacter
))
{
return
false
;
}
}
model
.
forceTokenization
(
position
.
lineNumber
);
const
lineTokens
=
model
.
getLineTokens
(
position
.
lineNumber
);
model
.
forceTokenization
(
position
.
lineNumber
);
const
lineTokens
=
model
.
getLineTokens
(
position
.
lineNumber
);
let
shouldAutoClosePair
=
false
;
try
{
shouldAutoClosePair
=
LanguageConfigurationRegistry
.
shouldAutoClosePair
(
ch
,
lineTokens
,
position
.
column
);
}
catch
(
e
)
{
onUnexpectedError
(
e
);
let
shouldAutoClosePair
=
false
;
try
{
shouldAutoClosePair
=
LanguageConfigurationRegistry
.
shouldAutoClosePair
(
ch
,
lineTokens
,
position
.
column
);
}
catch
(
e
)
{
onUnexpectedError
(
e
);
}
if
(
!
shouldAutoClosePair
)
{
return
false
;
}
}
return
shouldAutoClosePair
;
return
true
;
}
private
static
runAutoClosingOpenCharType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
ch
:
string
):
EditOperationResult
{
private
static
_
runAutoClosingOpenCharType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
ch
:
string
):
EditOperationResult
{
const
selection
=
cursor
.
selection
;
const
closeCharacter
=
config
.
autoClosingPairsOpen
[
ch
];
return
new
EditOperationResult
(
new
ReplaceCommandWithOffsetCursorState
(
selection
,
ch
+
closeCharacter
,
0
,
-
closeCharacter
.
length
),
{
...
...
@@ -369,35 +370,42 @@ export class TypeOperations {
});
}
private
static
isSurroundSelectionType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
ch
:
string
):
boolean
{
if
(
!
config
.
autoClosingBrackets
)
{
private
static
_isSurroundSelectionType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursors
:
SingleCursorState
[]
,
ch
:
string
):
boolean
{
if
(
!
config
.
autoClosingBrackets
||
!
config
.
surroundingPairs
.
hasOwnProperty
(
ch
)
)
{
return
false
;
}
const
selection
=
cursor
.
selection
;
for
(
let
i
=
0
,
len
=
cursors
.
length
;
i
<
len
;
i
++
)
{
const
cursor
=
cursors
[
i
];
const
selection
=
cursor
.
selection
;
if
(
selection
.
isEmpty
()
||
!
config
.
surroundingPairs
.
hasOwnProperty
(
ch
))
{
return
false
;
}
if
(
selection
.
isEmpty
())
{
return
false
;
}
let
selectionContainsOnlyWhitespace
=
true
;
let
selectionContainsOnlyWhitespace
=
true
;
for
(
let
lineNumber
=
selection
.
startLineNumber
;
lineNumber
<=
selection
.
endLineNumber
;
lineNumber
++
)
{
const
lineText
=
model
.
getLineContent
(
lineNumber
);
const
startIndex
=
(
lineNumber
===
selection
.
startLineNumber
?
selection
.
startColumn
-
1
:
0
);
const
endIndex
=
(
lineNumber
===
selection
.
endLineNumber
?
selection
.
endColumn
-
1
:
lineText
.
length
);
const
selectedText
=
lineText
.
substring
(
startIndex
,
endIndex
);
if
(
/
[^
\t]
/
.
test
(
selectedText
))
{
// this selected text contains something other than whitespace
selectionContainsOnlyWhitespace
=
false
;
break
;
for
(
let
lineNumber
=
selection
.
startLineNumber
;
lineNumber
<=
selection
.
endLineNumber
;
lineNumber
++
)
{
const
lineText
=
model
.
getLineContent
(
lineNumber
);
const
startIndex
=
(
lineNumber
===
selection
.
startLineNumber
?
selection
.
startColumn
-
1
:
0
);
const
endIndex
=
(
lineNumber
===
selection
.
endLineNumber
?
selection
.
endColumn
-
1
:
lineText
.
length
);
const
selectedText
=
lineText
.
substring
(
startIndex
,
endIndex
);
if
(
/
[^
\t]
/
.
test
(
selectedText
))
{
// this selected text contains something other than whitespace
selectionContainsOnlyWhitespace
=
false
;
break
;
}
}
if
(
selectionContainsOnlyWhitespace
)
{
return
false
;
}
}
return
(
!
selectionContainsOnlyWhitespace
)
;
return
true
;
}
private
static
runSurroundSelectionType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
ch
:
string
):
EditOperationResult
{
private
static
_
runSurroundSelectionType
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
ch
:
string
):
EditOperationResult
{
const
selection
=
cursor
.
selection
;
const
closeCharacter
=
config
.
surroundingPairs
[
ch
];
...
...
@@ -468,24 +476,51 @@ export class TypeOperations {
return
null
;
}
public
static
typeWithInterceptors
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
ch
:
string
):
EditOperationResult
{
let
r
:
EditOperationResult
=
null
;
public
static
typeWithInterceptors
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
s
:
SingleCursorState
[],
ch
:
string
):
EditOperationResult
[]
{
let
r
2
:
EditOperationResult
[]
=
[]
;
r
=
r
||
this
.
_typeInterceptorEnter
(
config
,
model
,
cursor
,
ch
);
if
(
ch
===
'
\n
'
)
{
for
(
let
i
=
0
,
len
=
cursors
.
length
;
i
<
len
;
i
++
)
{
r2
[
i
]
=
TypeOperations
.
_enter
(
config
,
model
,
false
,
cursors
[
i
].
selection
);
}
return
r2
;
}
if
(
this
.
_isAutoClosingCloseCharType
(
config
,
model
,
cursors
,
ch
))
{
for
(
let
i
=
0
,
len
=
cursors
.
length
;
i
<
len
;
i
++
)
{
r2
[
i
]
=
this
.
_runAutoClosingCloseCharType
(
config
,
model
,
cursors
[
i
],
ch
);
}
return
r2
;
}
if
(
!
r
&&
this
.
isAutoClosingCloseCharType
(
config
,
model
,
cursor
,
ch
))
{
r
=
this
.
runAutoClosingCloseCharType
(
config
,
model
,
cursor
,
ch
);
if
(
this
.
_isAutoClosingOpenCharType
(
config
,
model
,
cursors
,
ch
))
{
for
(
let
i
=
0
,
len
=
cursors
.
length
;
i
<
len
;
i
++
)
{
r2
[
i
]
=
this
.
_runAutoClosingOpenCharType
(
config
,
model
,
cursors
[
i
],
ch
);
}
return
r2
;
}
if
(
!
r
&&
this
.
isAutoClosingOpenCharType
(
config
,
model
,
cursor
,
ch
))
{
r
=
this
.
runAutoClosingOpenCharType
(
config
,
model
,
cursor
,
ch
);
if
(
this
.
_isSurroundSelectionType
(
config
,
model
,
cursors
,
ch
))
{
for
(
let
i
=
0
,
len
=
cursors
.
length
;
i
<
len
;
i
++
)
{
r2
[
i
]
=
this
.
_runSurroundSelectionType
(
config
,
model
,
cursors
[
i
],
ch
);
}
return
r2
;
}
if
(
!
r
&&
this
.
isSurroundSelectionType
(
config
,
model
,
cursor
,
ch
))
{
r
=
this
.
runSurroundSelectionType
(
config
,
model
,
cursor
,
ch
);
// Electric characters make sense only when dealing with a single cursor,
// as multiple cursors typing brackets for example would interfer with bracket matching
if
(
cursors
.
length
===
1
)
{
const
r
=
this
.
_typeInterceptorElectricChar
(
config
,
model
,
cursors
[
0
],
ch
);
if
(
r
)
{
r2
[
0
]
=
r
;
return
r2
;
}
}
r
=
r
||
this
.
_typeInterceptorElectricChar
(
config
,
model
,
cursor
,
ch
);
r
=
r
||
this
.
typeWithoutInterceptors
(
config
,
model
,
cursor
,
ch
);
return
r
;
for
(
let
i
=
0
,
len
=
cursors
.
length
;
i
<
len
;
i
++
)
{
r2
[
i
]
=
this
.
typeWithoutInterceptors
(
config
,
model
,
cursors
[
i
],
ch
);
}
return
r2
;
}
public
static
typeWithoutInterceptors
(
config
:
CursorConfiguration
,
model
:
ITokenizedModel
,
cursor
:
SingleCursorState
,
str
:
string
):
EditOperationResult
{
...
...
src/vs/editor/test/common/controller/cursor.test.ts
浏览文件 @
19a881b7
...
...
@@ -3627,4 +3627,26 @@ suite('autoClosingPairs', () => {
});
mode
.
dispose
();
});
test
(
'
issue #20891: All cursors should do the same thing
'
,
()
=>
{
let
mode
=
new
AutoClosingMode
();
usingCursor
({
text
:
[
'
var a = asd
'
],
languageIdentifier
:
mode
.
getLanguageIdentifier
()
},
(
model
,
cursor
)
=>
{
cursor
.
setSelections
(
'
test
'
,
[
new
Selection
(
1
,
9
,
1
,
9
),
new
Selection
(
1
,
12
,
1
,
12
),
]);
// type a `
cursorCommand
(
cursor
,
H
.
Type
,
{
text
:
'
`
'
},
'
keyboard
'
);
assert
.
equal
(
model
.
getValue
(),
'
var a = `asd`
'
);
});
mode
.
dispose
();
});
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录