Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
掘金者说
vscode
提交
e71d1e88
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,发现更多精彩内容 >>
提交
e71d1e88
编写于
9月 22, 2016
作者:
J
Johannes Rieken
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
first cut of format on save
上级
53ff2cf4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
86 addition
and
40 deletion
+86
-40
src/vs/editor/contrib/format/common/format.ts
src/vs/editor/contrib/format/common/format.ts
+27
-1
src/vs/workbench/api/node/mainThreadSaveParticipant.ts
src/vs/workbench/api/node/mainThreadSaveParticipant.ts
+59
-39
未找到文件。
src/vs/editor/contrib/format/common/format.ts
浏览文件 @
e71d1e88
...
...
@@ -9,12 +9,13 @@ import {illegalArgument} from 'vs/base/common/errors';
import
URI
from
'
vs/base/common/uri
'
;
import
{
TPromise
}
from
'
vs/base/common/winjs.base
'
;
import
{
Range
}
from
'
vs/editor/common/core/range
'
;
import
{
IReadOnlyModel
,
ISingleEditOperation
}
from
'
vs/editor/common/editorCommon
'
;
import
{
IReadOnlyModel
,
ISingleEditOperation
,
IModel
,
ICommonCodeEditor
}
from
'
vs/editor/common/editorCommon
'
;
import
{
CommonEditorRegistry
}
from
'
vs/editor/common/editorCommonExtensions
'
;
import
{
DocumentFormattingEditProviderRegistry
,
DocumentRangeFormattingEditProviderRegistry
,
OnTypeFormattingEditProviderRegistry
,
FormattingOptions
}
from
'
vs/editor/common/modes
'
;
import
{
IModelService
}
from
'
vs/editor/common/services/modelService
'
;
import
{
asWinJsPromise
}
from
'
vs/base/common/async
'
;
import
{
Position
}
from
'
vs/editor/common/core/position
'
;
import
{
EditOperationsCommand
}
from
'
./formatCommand
'
;
export
function
getDocumentRangeFormattingEdits
(
model
:
IReadOnlyModel
,
range
:
Range
,
options
:
FormattingOptions
):
TPromise
<
ISingleEditOperation
[]
>
{
const
[
support
]
=
DocumentRangeFormattingEditProviderRegistry
.
ordered
(
model
);
...
...
@@ -85,3 +86,28 @@ CommonEditorRegistry.registerDefaultLanguageCommand('_executeFormatOnTypeProvide
}
return
getOnTypeFormattingEdits
(
model
,
position
,
ch
,
options
);
});
export
function
formatDocument
(
model
:
IModel
,
editor
:
ICommonCodeEditor
)
{
const
{
tabSize
,
insertSpaces
}
=
model
.
getOptions
();
return
getDocumentRangeFormattingEdits
(
model
,
model
.
getFullModelRange
(),
{
tabSize
,
insertSpaces
}).
then
(
edits
=>
{
if
(
!
edits
)
{
return
;
}
if
(
!
editor
)
{
model
.
applyEdits
(
edits
.
map
(({
text
,
range
})
=>
({
text
,
range
:
Range
.
lift
(
range
),
identifier
:
undefined
,
forceMoveMarkers
:
true
})));
}
else
{
editor
.
executeCommand
(
'
format.document
'
,
new
EditOperationsCommand
(
edits
,
editor
.
getSelection
()));
}
});
};
src/vs/workbench/api/node/mainThreadSaveParticipant.ts
浏览文件 @
e71d1e88
...
...
@@ -5,47 +5,30 @@
'
use strict
'
;
import
{
IDisposable
,
dispose
}
from
'
vs/base/common/lifecycle
'
;
import
{
TPromise
}
from
'
vs/base/common/winjs.base
'
;
import
{
ICodeEditorService
}
from
'
vs/editor/common/services/codeEditorService
'
;
import
{
IThreadService
}
from
'
vs/workbench/services/thread/common/threadService
'
;
import
{
ISaveParticipant
,
ITextFileEditorModel
}
from
'
vs/workbench/parts/files/common/files
'
;
import
{
I
FilesConfiguration
}
from
'
vs/platform/files/common/files
'
;
import
{
I
InstantiationService
}
from
'
vs/platform/instantiation/common/instantiation
'
;
import
{
IPosition
,
IModel
}
from
'
vs/editor/common/editorCommon
'
;
import
{
Selection
}
from
'
vs/editor/common/core/selection
'
;
import
{
trimTrailingWhitespace
}
from
'
vs/editor/common/commands/trimTrailingWhitespaceCommand
'
;
import
{
formatDocument
}
from
'
vs/editor/contrib/format/common/format
'
;
import
{
IConfigurationService
}
from
'
vs/platform/configuration/common/configuration
'
;
import
{
TextFileEditorModel
}
from
'
vs/workbench/parts/files/common/editors/textFileEditorModel
'
;
import
{
ExtHostContext
,
ExtHostDocumentSaveParticipantShape
}
from
'
./extHost.protocol
'
;
// TODO@joh move this to the extension host
class
TrimWhitespaceParticipant
{
private
trimTrailingWhitespace
:
boolean
=
false
;
private
toUnbind
:
IDisposable
[]
=
[];
class
TrimWhitespaceParticipant
implements
ISaveParticipant
{
constructor
(
@
IConfigurationService
private
configurationService
:
IConfigurationService
,
@
ICodeEditorService
private
codeEditorService
:
ICodeEditorService
)
{
this
.
registerListeners
();
this
.
onConfigurationChange
(
this
.
configurationService
.
getConfiguration
<
IFilesConfiguration
>
());
}
public
dispose
():
void
{
this
.
toUnbind
=
dispose
(
this
.
toUnbind
);
}
private
registerListeners
():
void
{
this
.
toUnbind
.
push
(
this
.
configurationService
.
onDidUpdateConfiguration
(
e
=>
this
.
onConfigurationChange
(
e
.
config
)));
}
private
onConfigurationChange
(
configuration
:
IFilesConfiguration
):
void
{
this
.
trimTrailingWhitespace
=
configuration
&&
configuration
.
files
&&
configuration
.
files
.
trimTrailingWhitespace
;
// Nothing
}
public
participate
(
model
:
ITextFileEditorModel
,
env
:
{
isAutoSaved
:
boolean
}):
any
{
if
(
this
.
trimTrailingWhitespac
e
)
{
if
(
this
.
configurationService
.
lookup
(
'
files.trimTrailingWhitespace
'
).
valu
e
)
{
this
.
doTrimTrailingWhitespace
(
model
.
textEditorModel
,
env
.
isAutoSaved
);
}
}
...
...
@@ -89,34 +72,71 @@ class TrimWhitespaceParticipant {
}
}
class
FormatOnSaveParticipant
implements
ISaveParticipant
{
constructor
(
@
ICodeEditorService
private
_editorService
:
ICodeEditorService
,
@
IConfigurationService
private
_configurationService
:
IConfigurationService
)
{
// Nothing
}
participate
(
editorModel
:
ITextFileEditorModel
,
env
:
{
isAutoSaved
:
boolean
}):
TPromise
<
any
>
{
if
(
true
||
this
.
_configurationService
.
lookup
(
'
files.formatOnSave
'
).
value
)
{
const
model
:
IModel
=
editorModel
.
textEditorModel
;
const
editor
=
this
.
_findEditor
(
model
);
return
formatDocument
(
model
,
editor
);
}
}
private
_findEditor
(
model
:
IModel
)
{
for
(
const
editor
of
this
.
_editorService
.
listCodeEditors
())
{
if
(
editor
.
getModel
()
===
model
)
{
return
editor
;
}
}
}
}
class
ExtHostSaveParticipant
implements
ISaveParticipant
{
private
_proxy
:
ExtHostDocumentSaveParticipantShape
;
constructor
(
@
IThreadService
threadService
:
IThreadService
)
{
this
.
_proxy
=
threadService
.
get
(
ExtHostContext
.
ExtHostDocumentSaveParticipant
);
}
participate
(
editorModel
:
ITextFileEditorModel
,
env
:
{
isAutoSaved
:
boolean
}):
TPromise
<
any
>
{
return
this
.
_proxy
.
$participateInSave
(
editorModel
.
getResource
());
}
}
// The save participant can change a model before its saved to support various scenarios like trimming trailing whitespace
export
class
SaveParticipant
implements
ISaveParticipant
{
private
_mainThreadSaveParticipant
:
TrimWhitespaceParticipant
;
private
_extHostSaveParticipant
:
ExtHostDocumentSaveParticipantShape
;
private
_saveParticipants
:
ISaveParticipant
[];
constructor
(
@
IConfigurationService
configurationService
:
IConfigurationService
,
@
ICodeEditorService
codeEditorService
:
ICodeEditorService
,
@
IInstantiationService
instantiationService
:
IInstantiationService
,
@
IThreadService
threadService
:
IThreadService
)
{
this
.
_mainThreadSaveParticipant
=
new
TrimWhitespaceParticipant
(
configurationService
,
codeEditorService
);
this
.
_extHostSaveParticipant
=
threadService
.
get
(
ExtHostContext
.
ExtHostDocumentSaveParticipant
);
this
.
_saveParticipants
=
[
instantiationService
.
createInstance
(
TrimWhitespaceParticipant
),
instantiationService
.
createInstance
(
FormatOnSaveParticipant
),
instantiationService
.
createInstance
(
ExtHostSaveParticipant
)
];
// Hook into model
TextFileEditorModel
.
setSaveParticipant
(
this
);
}
dispose
()
{
this
.
_mainThreadSaveParticipant
.
dispose
();
}
participate
(
model
:
ITextFileEditorModel
,
env
:
{
isAutoSaved
:
boolean
}):
TPromise
<
any
>
{
try
{
this
.
_mainThreadSaveParticipant
.
participate
(
model
,
env
);
}
catch
(
err
)
{
// ignore
const
promises
:
TPromise
<
any
>
[]
=
[];
for
(
const
participant
of
this
.
_saveParticipants
)
{
promises
.
push
(
TPromise
.
as
(
participant
.
participate
(
model
,
env
)).
then
(
undefined
,
err
=>
{
console
.
error
(
err
);
}));
}
return
this
.
_extHostSaveParticipant
.
$participateInSave
(
model
.
getResource
()
);
return
TPromise
.
join
(
promises
);
}
}
\ No newline at end of file
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录