Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
ea7232e7
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,发现更多精彩内容 >>
提交
ea7232e7
编写于
8月 06, 2018
作者:
J
Johannes Rieken
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
debt - use Promise and CancellationToken instead of TPromise
上级
bca795fc
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
35 addition
and
30 deletion
+35
-30
src/vs/editor/contrib/format/format.ts
src/vs/editor/contrib/format/format.ts
+12
-11
src/vs/editor/contrib/format/formatActions.ts
src/vs/editor/contrib/format/formatActions.ts
+13
-11
src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts
...rkbench/api/electron-browser/mainThreadSaveParticipant.ts
+4
-2
src/vs/workbench/test/electron-browser/api/extHostLanguageFeatures.test.ts
...test/electron-browser/api/extHostLanguageFeatures.test.ts
+6
-6
未找到文件。
src/vs/editor/contrib/format/format.ts
浏览文件 @
ea7232e7
...
...
@@ -12,8 +12,9 @@ import { ITextModel } from 'vs/editor/common/model';
import
{
registerDefaultLanguageCommand
,
registerLanguageCommand
}
from
'
vs/editor/browser/editorExtensions
'
;
import
{
DocumentFormattingEditProviderRegistry
,
DocumentRangeFormattingEditProviderRegistry
,
OnTypeFormattingEditProviderRegistry
,
FormattingOptions
,
TextEdit
}
from
'
vs/editor/common/modes
'
;
import
{
IModelService
}
from
'
vs/editor/common/services/modelService
'
;
import
{
asWinJsPromise
,
first
}
from
'
vs/base/common/async
'
;
import
{
asWinJsPromise
,
first
2
}
from
'
vs/base/common/async
'
;
import
{
Position
}
from
'
vs/editor/common/core/position
'
;
import
{
CancellationToken
}
from
'
vs/base/common/cancellation
'
;
export
class
NoProviderError
extends
Error
{
...
...
@@ -26,30 +27,30 @@ export class NoProviderError extends Error {
}
}
export
function
getDocumentRangeFormattingEdits
(
model
:
ITextModel
,
range
:
Range
,
options
:
FormattingOptions
):
TPromise
<
TextEdit
[],
NoProviderError
>
{
export
function
getDocumentRangeFormattingEdits
(
model
:
ITextModel
,
range
:
Range
,
options
:
FormattingOptions
,
token
:
CancellationToken
):
Promise
<
TextEdit
[]
>
{
const
providers
=
DocumentRangeFormattingEditProviderRegistry
.
ordered
(
model
);
if
(
providers
.
length
===
0
)
{
return
TPromise
.
wrapError
(
new
NoProviderError
());
return
Promise
.
reject
(
new
NoProviderError
());
}
return
first
(
providers
.
map
(
provider
=>
()
=>
{
return
asWinJsPromise
(
token
=>
provider
.
provideDocumentRangeFormattingEdits
(
model
,
range
,
options
,
token
))
return
first
2
(
providers
.
map
(
provider
=>
()
=>
{
return
Promise
.
resolve
(
provider
.
provideDocumentRangeFormattingEdits
(
model
,
range
,
options
,
token
))
.
then
(
undefined
,
onUnexpectedExternalError
);
}),
result
=>
!
isFalsyOrEmpty
(
result
));
}
export
function
getDocumentFormattingEdits
(
model
:
ITextModel
,
options
:
FormattingOptions
):
T
Promise
<
TextEdit
[]
>
{
export
function
getDocumentFormattingEdits
(
model
:
ITextModel
,
options
:
FormattingOptions
,
token
:
CancellationToken
):
Promise
<
TextEdit
[]
>
{
const
providers
=
DocumentFormattingEditProviderRegistry
.
ordered
(
model
);
// try range formatters when no document formatter is registered
if
(
providers
.
length
===
0
)
{
return
getDocumentRangeFormattingEdits
(
model
,
model
.
getFullModelRange
(),
options
);
return
getDocumentRangeFormattingEdits
(
model
,
model
.
getFullModelRange
(),
options
,
token
);
}
return
first
(
providers
.
map
(
provider
=>
()
=>
{
return
asWinJsPromise
(
token
=>
provider
.
provideDocumentFormattingEdits
(
model
,
options
,
token
))
return
first
2
(
providers
.
map
(
provider
=>
()
=>
{
return
Promise
.
resolve
(
provider
.
provideDocumentFormattingEdits
(
model
,
options
,
token
))
.
then
(
undefined
,
onUnexpectedExternalError
);
}),
result
=>
!
isFalsyOrEmpty
(
result
));
}
...
...
@@ -77,7 +78,7 @@ registerLanguageCommand('_executeFormatRangeProvider', function (accessor, args)
if
(
!
model
)
{
throw
illegalArgument
(
'
resource
'
);
}
return
getDocumentRangeFormattingEdits
(
model
,
Range
.
lift
(
range
),
options
);
return
getDocumentRangeFormattingEdits
(
model
,
Range
.
lift
(
range
),
options
,
CancellationToken
.
None
);
});
registerLanguageCommand
(
'
_executeFormatDocumentProvider
'
,
function
(
accessor
,
args
)
{
...
...
@@ -90,7 +91,7 @@ registerLanguageCommand('_executeFormatDocumentProvider', function (accessor, ar
throw
illegalArgument
(
'
resource
'
);
}
return
getDocumentFormattingEdits
(
model
,
options
);
return
getDocumentFormattingEdits
(
model
,
options
,
CancellationToken
.
None
);
});
registerDefaultLanguageCommand
(
'
_executeFormatOnTypeProvider
'
,
function
(
model
,
position
,
args
)
{
...
...
src/vs/editor/contrib/format/formatActions.ts
浏览文件 @
ea7232e7
...
...
@@ -27,6 +27,7 @@ import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import
{
ISingleEditOperation
}
from
'
vs/editor/common/model
'
;
import
{
INotificationService
}
from
'
vs/platform/notification/common/notification
'
;
import
{
KeybindingWeight
}
from
'
vs/platform/keybinding/common/keybindingsRegistry
'
;
import
{
CancellationToken
}
from
'
vs/base/common/cancellation
'
;
function
alertFormattingEdits
(
edits
:
ISingleEditOperation
[]):
void
{
...
...
@@ -239,7 +240,7 @@ class FormatOnPaste implements editorCommon.IEditorContribution {
const
{
tabSize
,
insertSpaces
}
=
model
.
getOptions
();
const
state
=
new
EditorState
(
this
.
editor
,
CodeEditorStateFlag
.
Value
|
CodeEditorStateFlag
.
Position
);
getDocumentRangeFormattingEdits
(
model
,
range
,
{
tabSize
,
insertSpaces
}).
then
(
edits
=>
{
getDocumentRangeFormattingEdits
(
model
,
range
,
{
tabSize
,
insertSpaces
}
,
CancellationToken
.
None
).
then
(
edits
=>
{
return
this
.
workerService
.
computeMoreMinimalEdits
(
model
.
uri
,
edits
);
}).
then
(
edits
=>
{
if
(
!
state
.
validate
(
this
.
editor
)
||
isFalsyOrEmpty
(
edits
))
{
...
...
@@ -267,7 +268,7 @@ export abstract class AbstractFormatAction extends EditorAction {
const
workerService
=
accessor
.
get
(
IEditorWorkerService
);
const
notificationService
=
accessor
.
get
(
INotificationService
);
const
formattingPromise
=
this
.
_getFormattingEdits
(
editor
);
const
formattingPromise
=
this
.
_getFormattingEdits
(
editor
,
CancellationToken
.
None
);
if
(
!
formattingPromise
)
{
return
TPromise
.
as
(
void
0
);
}
...
...
@@ -276,7 +277,7 @@ export abstract class AbstractFormatAction extends EditorAction {
const
state
=
new
EditorState
(
editor
,
CodeEditorStateFlag
.
Value
|
CodeEditorStateFlag
.
Position
);
// Receive formatted value from worker
return
formattingPromise
.
then
(
edits
=>
workerService
.
computeMoreMinimalEdits
(
editor
.
getModel
().
uri
,
edits
)).
then
(
edits
=>
{
return
TPromise
.
wrap
(
formattingPromise
)
.
then
(
edits
=>
workerService
.
computeMoreMinimalEdits
(
editor
.
getModel
().
uri
,
edits
)).
then
(
edits
=>
{
if
(
!
state
.
validate
(
editor
)
||
isFalsyOrEmpty
(
edits
))
{
return
;
}
...
...
@@ -293,7 +294,8 @@ export abstract class AbstractFormatAction extends EditorAction {
});
}
protected
abstract
_getFormattingEdits
(
editor
:
ICodeEditor
):
TPromise
<
ISingleEditOperation
[]
>
;
protected
abstract
_getFormattingEdits
(
editor
:
ICodeEditor
,
token
:
CancellationToken
):
Promise
<
ISingleEditOperation
[]
>
;
protected
_notifyNoProviderError
(
notificationService
:
INotificationService
,
language
:
string
):
void
{
notificationService
.
info
(
nls
.
localize
(
'
no.provider
'
,
"
There is no formatter for '{0}'-files installed.
"
,
language
));
}
...
...
@@ -322,10 +324,10 @@ export class FormatDocumentAction extends AbstractFormatAction {
});
}
protected
_getFormattingEdits
(
editor
:
ICodeEditor
):
T
Promise
<
ISingleEditOperation
[]
>
{
protected
_getFormattingEdits
(
editor
:
ICodeEditor
,
token
:
CancellationToken
):
Promise
<
ISingleEditOperation
[]
>
{
const
model
=
editor
.
getModel
();
const
{
tabSize
,
insertSpaces
}
=
model
.
getOptions
();
return
getDocumentFormattingEdits
(
model
,
{
tabSize
,
insertSpaces
});
return
getDocumentFormattingEdits
(
model
,
{
tabSize
,
insertSpaces
}
,
token
);
}
protected
_notifyNoProviderError
(
notificationService
:
INotificationService
,
language
:
string
):
void
{
...
...
@@ -354,10 +356,10 @@ export class FormatSelectionAction extends AbstractFormatAction {
});
}
protected
_getFormattingEdits
(
editor
:
ICodeEditor
):
T
Promise
<
ISingleEditOperation
[]
>
{
protected
_getFormattingEdits
(
editor
:
ICodeEditor
,
token
:
CancellationToken
):
Promise
<
ISingleEditOperation
[]
>
{
const
model
=
editor
.
getModel
();
const
{
tabSize
,
insertSpaces
}
=
model
.
getOptions
();
return
getDocumentRangeFormattingEdits
(
model
,
editor
.
getSelection
(),
{
tabSize
,
insertSpaces
});
return
getDocumentRangeFormattingEdits
(
model
,
editor
.
getSelection
(),
{
tabSize
,
insertSpaces
}
,
token
);
}
protected
_notifyNoProviderError
(
notificationService
:
INotificationService
,
language
:
string
):
void
{
...
...
@@ -379,14 +381,14 @@ CommandsRegistry.registerCommand('editor.action.format', accessor => {
constructor
()
{
super
({}
as
IActionOptions
);
}
_getFormattingEdits
(
editor
:
ICodeEditor
):
T
Promise
<
ISingleEditOperation
[]
>
{
_getFormattingEdits
(
editor
:
ICodeEditor
,
token
:
CancellationToken
):
Promise
<
ISingleEditOperation
[]
>
{
const
model
=
editor
.
getModel
();
const
editorSelection
=
editor
.
getSelection
();
const
{
tabSize
,
insertSpaces
}
=
model
.
getOptions
();
return
editorSelection
.
isEmpty
()
?
getDocumentFormattingEdits
(
model
,
{
tabSize
,
insertSpaces
})
:
getDocumentRangeFormattingEdits
(
model
,
editorSelection
,
{
tabSize
,
insertSpaces
});
?
getDocumentFormattingEdits
(
model
,
{
tabSize
,
insertSpaces
}
,
token
)
:
getDocumentRangeFormattingEdits
(
model
,
editorSelection
,
{
tabSize
,
insertSpaces
}
,
token
);
}
}().
run
(
accessor
,
editor
);
}
...
...
src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts
浏览文件 @
ea7232e7
...
...
@@ -37,6 +37,7 @@ import { applyCodeAction } from 'vs/editor/contrib/codeAction/codeActionCommands
import
{
getCodeActions
}
from
'
vs/editor/contrib/codeAction/codeAction
'
;
import
{
ICodeActionsOnSaveOptions
}
from
'
vs/editor/common/config/editorOptions
'
;
import
{
IBulkEditService
}
from
'
vs/editor/browser/services/bulkEditService
'
;
import
{
CancellationTokenSource
}
from
'
vs/base/common/cancellation
'
;
export
interface
ISaveParticipantParticipant
extends
ISaveParticipant
{
// progressMessage: string;
...
...
@@ -215,11 +216,12 @@ class FormatOnSaveParticipant implements ISaveParticipantParticipant {
const
timeout
=
this
.
_configurationService
.
getValue
(
'
editor.formatOnSaveTimeout
'
,
{
overrideIdentifier
:
model
.
getLanguageIdentifier
().
language
,
resource
:
editorModel
.
getResource
()
});
return
new
Promise
<
ISingleEditOperation
[]
>
((
resolve
,
reject
)
=>
{
let
request
=
getDocumentFormattingEdits
(
model
,
{
tabSize
,
insertSpaces
});
let
source
=
new
CancellationTokenSource
();
let
request
=
getDocumentFormattingEdits
(
model
,
{
tabSize
,
insertSpaces
},
source
.
token
);
setTimeout
(()
=>
{
reject
(
localize
(
'
timeout.formatOnSave
'
,
"
Aborted format on save after {0}ms
"
,
timeout
));
request
.
cancel
();
source
.
cancel
();
},
timeout
);
request
.
then
(
edits
=>
this
.
_editorWorkerService
.
computeMoreMinimalEdits
(
model
.
uri
,
edits
)).
then
(
resolve
,
err
=>
{
...
...
src/vs/workbench/test/electron-browser/api/extHostLanguageFeatures.test.ts
浏览文件 @
ea7232e7
...
...
@@ -1011,7 +1011,7 @@ suite('ExtHostLanguageFeatures', function () {
}));
return
rpcProtocol
.
sync
().
then
(()
=>
{
return
getDocumentFormattingEdits
(
model
,
{
insertSpaces
:
true
,
tabSize
:
4
}).
then
(
value
=>
{
return
getDocumentFormattingEdits
(
model
,
{
insertSpaces
:
true
,
tabSize
:
4
}
,
CancellationToken
.
None
).
then
(
value
=>
{
assert
.
equal
(
value
.
length
,
2
);
let
[
first
,
second
]
=
value
;
assert
.
equal
(
first
.
text
,
'
testing
'
);
...
...
@@ -1032,7 +1032,7 @@ suite('ExtHostLanguageFeatures', function () {
}));
return
rpcProtocol
.
sync
().
then
(()
=>
{
return
getDocumentFormattingEdits
(
model
,
{
insertSpaces
:
true
,
tabSize
:
4
});
return
getDocumentFormattingEdits
(
model
,
{
insertSpaces
:
true
,
tabSize
:
4
}
,
CancellationToken
.
None
);
});
});
...
...
@@ -1057,7 +1057,7 @@ suite('ExtHostLanguageFeatures', function () {
}));
return
rpcProtocol
.
sync
().
then
(()
=>
{
return
getDocumentFormattingEdits
(
model
,
{
insertSpaces
:
true
,
tabSize
:
4
}).
then
(
value
=>
{
return
getDocumentFormattingEdits
(
model
,
{
insertSpaces
:
true
,
tabSize
:
4
}
,
CancellationToken
.
None
).
then
(
value
=>
{
assert
.
equal
(
value
.
length
,
1
);
let
[
first
]
=
value
;
assert
.
equal
(
first
.
text
,
'
testing
'
);
...
...
@@ -1074,7 +1074,7 @@ suite('ExtHostLanguageFeatures', function () {
}));
return
rpcProtocol
.
sync
().
then
(()
=>
{
return
getDocumentRangeFormattingEdits
(
model
,
new
EditorRange
(
1
,
1
,
1
,
1
),
{
insertSpaces
:
true
,
tabSize
:
4
}).
then
(
value
=>
{
return
getDocumentRangeFormattingEdits
(
model
,
new
EditorRange
(
1
,
1
,
1
,
1
),
{
insertSpaces
:
true
,
tabSize
:
4
}
,
CancellationToken
.
None
).
then
(
value
=>
{
assert
.
equal
(
value
.
length
,
1
);
let
[
first
]
=
value
;
assert
.
equal
(
first
.
text
,
'
testing
'
);
...
...
@@ -1100,7 +1100,7 @@ suite('ExtHostLanguageFeatures', function () {
}
}));
return
rpcProtocol
.
sync
().
then
(()
=>
{
return
getDocumentRangeFormattingEdits
(
model
,
new
EditorRange
(
1
,
1
,
1
,
1
),
{
insertSpaces
:
true
,
tabSize
:
4
}).
then
(
value
=>
{
return
getDocumentRangeFormattingEdits
(
model
,
new
EditorRange
(
1
,
1
,
1
,
1
),
{
insertSpaces
:
true
,
tabSize
:
4
}
,
CancellationToken
.
None
).
then
(
value
=>
{
assert
.
equal
(
value
.
length
,
1
);
let
[
first
]
=
value
;
assert
.
equal
(
first
.
text
,
'
range2
'
);
...
...
@@ -1120,7 +1120,7 @@ suite('ExtHostLanguageFeatures', function () {
}));
return
rpcProtocol
.
sync
().
then
(()
=>
{
return
getDocumentRangeFormattingEdits
(
model
,
new
EditorRange
(
1
,
1
,
1
,
1
),
{
insertSpaces
:
true
,
tabSize
:
4
});
return
getDocumentRangeFormattingEdits
(
model
,
new
EditorRange
(
1
,
1
,
1
,
1
),
{
insertSpaces
:
true
,
tabSize
:
4
}
,
CancellationToken
.
None
);
});
});
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录