Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
78e11f23
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,发现更多精彩内容 >>
提交
78e11f23
编写于
12月 01, 2015
作者:
J
Johannes Rieken
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commands: executeFormatDocumentProvider, executeFormatRangeProvider
上级
a7f719e4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
163 addition
and
31 deletion
+163
-31
src/vs/editor/contrib/format/common/format.ts
src/vs/editor/contrib/format/common/format.ts
+59
-2
src/vs/editor/contrib/format/common/formatActions.ts
src/vs/editor/contrib/format/common/formatActions.ts
+12
-24
src/vs/workbench/api/common/extHostLanguageFeatureCommands.ts
...vs/workbench/api/common/extHostLanguageFeatureCommands.ts
+29
-5
src/vs/workbench/test/common/api/extHostLanguageFeatures.test.ts
...workbench/test/common/api/extHostLanguageFeatures.test.ts
+63
-0
未找到文件。
src/vs/editor/contrib/format/common/format.ts
浏览文件 @
78e11f23
...
...
@@ -5,10 +5,67 @@
'
use strict
'
;
import
{
IFormattingSupport
}
from
'
vs/editor/common/modes
'
;
import
{
IFormattingSupport
,
IFormattingOptions
}
from
'
vs/editor/common/modes
'
;
import
LanguageFeatureRegistry
from
'
vs/editor/common/modes/languageFeatureRegistry
'
;
import
{
onUnexpectedError
,
illegalArgument
}
from
'
vs/base/common/errors
'
;
import
URI
from
'
vs/base/common/uri
'
;
import
{
IAction
,
Action
}
from
'
vs/base/common/actions
'
;
import
{
IModelService
}
from
'
vs/editor/common/services/modelService
'
;
import
{
TPromise
}
from
'
vs/base/common/winjs.base
'
;
import
{
IModel
,
IRange
,
IPosition
,
ISingleEditOperation
}
from
'
vs/editor/common/editorCommon
'
;
import
{
Range
}
from
'
vs/editor/common/core/range
'
;
import
{
CommonEditorRegistry
}
from
'
vs/editor/common/editorCommonExtensions
'
;
export
const
FormatRegistry
=
new
LanguageFeatureRegistry
<
IFormattingSupport
>
(
'
formattingSupport
'
);
export
const
FormatOnTypeRegistry
=
new
LanguageFeatureRegistry
<
IFormattingSupport
>
(
'
formattingSupport
'
);
export
{
IFormattingSupport
};
\ No newline at end of file
export
{
IFormattingSupport
};
export
function
formatRange
(
model
:
IModel
,
range
:
IRange
,
options
:
IFormattingOptions
):
TPromise
<
ISingleEditOperation
[]
>
{
const
support
=
FormatRegistry
.
ordered
(
model
)[
0
];
if
(
!
support
)
{
return
;
}
return
support
.
formatRange
(
model
.
getAssociatedResource
(),
range
,
options
);
}
export
function
formatDocument
(
model
:
IModel
,
options
:
IFormattingOptions
):
TPromise
<
ISingleEditOperation
[]
>
{
const
support
=
FormatRegistry
.
ordered
(
model
)[
0
];
if
(
!
support
)
{
return
;
}
if
(
typeof
support
.
formatDocument
!==
'
function
'
)
{
if
(
typeof
support
.
formatRange
===
'
function
'
)
{
return
formatRange
(
model
,
model
.
getFullModelRange
(),
options
);
}
else
{
return
;
}
}
return
support
.
formatDocument
(
model
.
getAssociatedResource
(),
options
);
}
CommonEditorRegistry
.
registerLanguageCommand
(
'
_executeFormatRangeProvider
'
,
function
(
accessor
,
args
)
{
const
{
resource
,
range
,
options
}
=
args
;
if
(
!
URI
.
isURI
(
resource
)
||
!
Range
.
isIRange
(
range
))
{
throw
illegalArgument
();
}
const
model
=
accessor
.
get
(
IModelService
).
getModel
(
resource
);
if
(
!
model
)
{
throw
illegalArgument
(
'
resource
'
);
}
return
formatRange
(
model
,
range
,
options
);
});
CommonEditorRegistry
.
registerLanguageCommand
(
'
_executeFormatDocumentProvider
'
,
function
(
accessor
,
args
)
{
const
{
resource
,
options
}
=
args
;
if
(
!
URI
.
isURI
(
resource
))
{
throw
illegalArgument
(
'
resource
'
);
}
const
model
=
accessor
.
get
(
IModelService
).
getModel
(
resource
);
if
(
!
model
)
{
throw
illegalArgument
(
'
resource
'
);
}
return
formatDocument
(
model
,
options
)
});
\ No newline at end of file
src/vs/editor/contrib/format/common/formatActions.ts
浏览文件 @
78e11f23
...
...
@@ -15,7 +15,7 @@ import formatCommand = require('./formatCommand');
import
{
Range
}
from
'
vs/editor/common/core/range
'
;
import
{
INullService
}
from
'
vs/platform/instantiation/common/instantiation
'
;
import
{
KeyMod
,
KeyCode
}
from
'
vs/base/common/keyCodes
'
;
import
{
FormatOnTypeRegistry
,
FormatRegistry
,
IFormattingSupport
}
from
'
../common/format
'
;
import
{
FormatOnTypeRegistry
,
FormatRegistry
,
IFormattingSupport
,
formatRange
,
formatDocument
}
from
'
../common/format
'
;
interface
IFormatOnTypeResult
{
range
:
EditorCommon
.
IEditorRange
;
...
...
@@ -170,31 +170,19 @@ export class FormatAction extends EditorAction {
public
run
():
TPromise
<
boolean
>
{
var
model
=
this
.
editor
.
getModel
(),
formattingSupport
=
FormatRegistry
.
ordered
(
model
)[
0
],
canFormatRange
=
typeof
formattingSupport
.
formatRange
===
'
function
'
,
canFormatDocument
=
typeof
formattingSupport
.
formatDocument
===
'
function
'
,
editorSelection
=
this
.
editor
.
getSelection
();
var
options
=
this
.
editor
.
getIndentationOptions
(),
formattingPromise
:
TPromise
<
EditorCommon
.
ISingleEditOperation
[]
>
;
if
(
canFormatRange
)
{
// format a selection/range
var
formatRange
:
EditorCommon
.
IEditorRange
=
editorSelection
;
if
(
!
formatRange
.
isEmpty
())
{
// Fix the selection to include the entire line to improve formatting results
formatRange
.
startColumn
=
1
;
}
else
{
formatRange
=
model
.
getFullModelRange
();
}
formattingPromise
=
formattingSupport
.
formatRange
(
model
.
getAssociatedResource
(),
formatRange
,
options
);
const
model
=
this
.
editor
.
getModel
(),
editorSelection
=
this
.
editor
.
getSelection
(),
options
=
this
.
editor
.
getIndentationOptions
();
let
formattingPromise
:
TPromise
<
EditorCommon
.
ISingleEditOperation
[]
>
;
}
else
if
(
canFormatDocument
)
{
// format the whole document
formattingPromise
=
formattingSupport
.
formatDocument
(
model
.
getAssociatedResource
(),
options
);
if
(
editorSelection
.
isEmpty
())
{
formattingPromise
=
formatDocument
(
model
,
options
);
}
else
{
// broken support?
formattingPromise
=
formatRange
(
model
,
editorSelection
,
options
);
}
if
(
!
formattingPromise
)
{
return
TPromise
.
as
(
false
);
}
...
...
src/vs/workbench/api/common/extHostLanguageFeatureCommands.ts
浏览文件 @
78e11f23
...
...
@@ -48,9 +48,9 @@ import {ICodeLensData} from 'vs/editor/contrib/codelens/common/codelens';
// vscode.executeCompletionItemProvider
// vscode.executeCodeActionProvider
// vscode.executeCodeLensProvider
// vscode.executeFormatDocumentProvider
// vscode.executeFormatRangeProvider
// vscode.executeFormatOnTypeProvider
export
class
ExtHostLanguageFeatureCommands
{
...
...
@@ -72,6 +72,8 @@ export class ExtHostLanguageFeatureCommands {
this
.
_register
(
'
vscode.executeCompletionItemProvider
'
,
this
.
_executeCompletionItemProvider
);
this
.
_register
(
'
vscode.executeCodeActionProvider
'
,
this
.
_executeCodeActionProvider
);
this
.
_register
(
'
vscode.executeCodeLensProvider
'
,
this
.
_executeCodeLensProvider
);
this
.
_register
(
'
vscode.executeFormatDocumentProvider
'
,
this
.
_executeFormatDocumentProvider
);
this
.
_register
(
'
vscode.executeFormatRangeProvider
'
,
this
.
_executeFormatRangeProvider
);
}
private
_register
(
id
:
string
,
callback
:
(...
args
:
any
[])
=>
any
):
void
{
...
...
@@ -217,10 +219,7 @@ export class ExtHostLanguageFeatureCommands {
}
private
_executeCodeLensProvider
(
resource
:
URI
):
Thenable
<
vscode
.
CodeLens
[]
>
{
const
args
=
{
resource
};
const
args
=
{
resource
};
return
this
.
_commands
.
executeCommand
<
ICodeLensData
[]
>
(
'
_executeCodeLensProvider
'
,
args
).
then
(
value
=>
{
if
(
Array
.
isArray
(
value
))
{
return
value
.
map
(
item
=>
{
...
...
@@ -230,4 +229,29 @@ export class ExtHostLanguageFeatureCommands {
}
});
}
private
_executeFormatDocumentProvider
(
resource
:
URI
,
options
:
vscode
.
FormattingOptions
):
Thenable
<
vscode
.
TextEdit
[]
>
{
const
args
=
{
resource
,
options
};
return
this
.
_commands
.
executeCommand
<
ISingleEditOperation
[]
>
(
'
_executeFormatDocumentProvider
'
,
args
).
then
(
value
=>
{
if
(
Array
.
isArray
(
value
))
{
return
value
.
map
(
edit
=>
new
types
.
TextEdit
(
typeConverters
.
toRange
(
edit
.
range
),
edit
.
text
));
}
});
}
private
_executeFormatRangeProvider
(
resource
:
URI
,
range
:
types
.
Range
,
options
:
vscode
.
FormattingOptions
):
Thenable
<
vscode
.
TextEdit
[]
>
{
const
args
=
{
resource
,
range
:
typeConverters
.
fromRange
(
range
),
options
};
return
this
.
_commands
.
executeCommand
<
ISingleEditOperation
[]
>
(
'
_executeFormatRangeProvider
'
,
args
).
then
(
value
=>
{
if
(
Array
.
isArray
(
value
))
{
return
value
.
map
(
edit
=>
new
types
.
TextEdit
(
typeConverters
.
toRange
(
edit
.
range
),
edit
.
text
));
}
});
}
}
\ No newline at end of file
src/vs/workbench/test/common/api/extHostLanguageFeatures.test.ts
浏览文件 @
78e11f23
...
...
@@ -37,6 +37,7 @@ import {getNavigateToItems} from 'vs/workbench/parts/search/common/search';
import
{
rename
}
from
'
vs/editor/contrib/rename/common/rename
'
;
import
{
getParameterHints
}
from
'
vs/editor/contrib/parameterHints/common/parameterHints
'
;
import
{
suggest
}
from
'
vs/editor/contrib/suggest/common/suggest
'
;
import
{
formatDocument
,
formatRange
}
from
'
vs/editor/contrib/format/common/format
'
;
const
defaultSelector
=
{
scheme
:
'
far
'
};
const
model
:
EditorCommon
.
IModel
=
new
EditorModel
(
...
...
@@ -907,4 +908,66 @@ suite('ExtHostLanguageFeatures', function() {
});
});
});
// --- format
test
(
'
Format Doc, data conversion
'
,
function
(
done
)
{
disposables
.
push
(
extHost
.
registerDocumentFormattingEditProvider
(
defaultSelector
,
<
vscode
.
DocumentFormattingEditProvider
>
{
provideDocumentFormattingEdits
():
any
{
return
[
new
types
.
TextEdit
(
new
types
.
Range
(
0
,
0
,
1
,
1
),
'
testing
'
)];
}
}));
threadService
.
sync
().
then
(()
=>
{
formatDocument
(
model
,
{
insertSpaces
:
true
,
tabSize
:
4
}).
then
(
value
=>
{
assert
.
equal
(
value
.
length
,
1
);
let
[
first
]
=
value
;
assert
.
equal
(
first
.
text
,
'
testing
'
);
assert
.
deepEqual
(
first
.
range
,
{
startLineNumber
:
1
,
startColumn
:
1
,
endLineNumber
:
2
,
endColumn
:
2
});
done
();
});
});
});
test
(
'
Format Doc, evil provider
'
,
function
(
done
)
{
disposables
.
push
(
extHost
.
registerDocumentFormattingEditProvider
(
defaultSelector
,
<
vscode
.
DocumentFormattingEditProvider
>
{
provideDocumentFormattingEdits
():
any
{
throw
new
Error
(
'
evil
'
);
}
}));
threadService
.
sync
().
then
(()
=>
{
formatDocument
(
model
,
{
insertSpaces
:
true
,
tabSize
:
4
}).
then
(
undefined
,
err
=>
done
());
});
});
test
(
'
Format Range, data conversion
'
,
function
(
done
)
{
disposables
.
push
(
extHost
.
registerDocumentRangeFormattingEditProvider
(
defaultSelector
,
<
vscode
.
DocumentRangeFormattingEditProvider
>
{
provideDocumentRangeFormattingEdits
():
any
{
return
[
new
types
.
TextEdit
(
new
types
.
Range
(
0
,
0
,
1
,
1
),
'
testing
'
)];
}
}));
threadService
.
sync
().
then
(()
=>
{
formatRange
(
model
,
{
startLineNumber
:
1
,
startColumn
:
1
,
endLineNumber
:
1
,
endColumn
:
1
},
{
insertSpaces
:
true
,
tabSize
:
4
}).
then
(
value
=>
{
assert
.
equal
(
value
.
length
,
1
);
let
[
first
]
=
value
;
assert
.
equal
(
first
.
text
,
'
testing
'
);
assert
.
deepEqual
(
first
.
range
,
{
startLineNumber
:
1
,
startColumn
:
1
,
endLineNumber
:
2
,
endColumn
:
2
});
done
();
});
});
})
test
(
'
Format Range, evil provider
'
,
function
(
done
)
{
disposables
.
push
(
extHost
.
registerDocumentRangeFormattingEditProvider
(
defaultSelector
,
<
vscode
.
DocumentRangeFormattingEditProvider
>
{
provideDocumentRangeFormattingEdits
():
any
{
throw
new
Error
(
'
evil
'
);
}
}));
threadService
.
sync
().
then
(()
=>
{
formatRange
(
model
,
{
startLineNumber
:
1
,
startColumn
:
1
,
endLineNumber
:
1
,
endColumn
:
1
},
{
insertSpaces
:
true
,
tabSize
:
4
}).
then
(
undefined
,
err
=>
done
());
});
})
});
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录