Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
掘金者说
vscode
提交
74d033e9
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,发现更多精彩内容 >>
提交
74d033e9
编写于
1月 25, 2016
作者:
J
Johannes Rieken
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
allow to register for the same scheme twice. lifo behaviour
上级
078397f1
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
155 addition
and
63 deletion
+155
-63
extensions/vscode-api-tests/src/workspace.test.ts
extensions/vscode-api-tests/src/workspace.test.ts
+59
-11
src/vs/workbench/api/node/extHostDocuments.ts
src/vs/workbench/api/node/extHostDocuments.ts
+53
-46
src/vs/workbench/common/editor/resourceEditorInput.ts
src/vs/workbench/common/editor/resourceEditorInput.ts
+43
-6
未找到文件。
extensions/vscode-api-tests/src/workspace.test.ts
浏览文件 @
74d033e9
...
...
@@ -147,27 +147,75 @@ suite('workspace-namespace', () => {
workspace
.
registerTextDocumentContentProvider
(
'
file
'
,
{
provideTextDocumentContent
()
{
return
null
;
}
});
});
// missing scheme
return
workspace
.
openTextDocument
(
Uri
.
parse
(
'
notThere://foo/far/boo/bar
'
)).
then
(()
=>
{
assert
.
ok
(
false
,
'
expected failure
'
)
},
err
=>
{
// expected
})
});
test
(
'
registerTextDocumentContentProvider, multiple
'
,
function
()
{
// duplicate registration
let
registration
=
workspace
.
registerTextDocumentContentProvider
(
'
foo
'
,
{
let
registration
1
=
workspace
.
registerTextDocumentContentProvider
(
'
foo
'
,
{
provideTextDocumentContent
(
uri
)
{
return
uri
.
toString
();
if
(
uri
.
authority
===
'
foo
'
)
{
return
'
1
'
}
}
});
assert
.
throws
(
function
()
{
workspace
.
registerTextDocumentContentProvider
(
'
foo
'
,
{
provideTextDocumentContent
()
{
return
null
;
}
});
let
registration2
=
workspace
.
registerTextDocumentContentProvider
(
'
foo
'
,
{
provideTextDocumentContent
(
uri
)
{
if
(
uri
.
authority
===
'
bar
'
)
{
return
'
2
'
}
}
});
// unregister & register
registration
.
dispose
();
registration
=
workspace
.
registerTextDocumentContentProvider
(
'
foo
'
,
{
provideTextDocumentContent
()
{
return
null
;
}
});
registration
.
dispose
();
return
Promise
.
all
([
workspace
.
openTextDocument
(
Uri
.
parse
(
'
foo://foo/bla
'
)).
then
(
doc
=>
{
assert
.
equal
(
doc
.
getText
(),
'
1
'
)
}),
workspace
.
openTextDocument
(
Uri
.
parse
(
'
foo://bar/bla
'
)).
then
(
doc
=>
{
assert
.
equal
(
doc
.
getText
(),
'
2
'
)
})
]).
then
(()
=>
{
registration1
.
dispose
();
registration2
.
dispose
();
});
});
// missing scheme
return
workspace
.
openTextDocument
(
Uri
.
parse
(
'
notThere://foo/far/boo/bar
'
)).
then
(()
=>
{
test
(
'
registerTextDocumentContentProvider, evil provider
'
,
function
()
{
// duplicate registration
let
registration1
=
workspace
.
registerTextDocumentContentProvider
(
'
foo
'
,
{
provideTextDocumentContent
(
uri
)
{
return
'
1
'
;
}
});
let
registration2
=
workspace
.
registerTextDocumentContentProvider
(
'
foo
'
,
{
provideTextDocumentContent
(
uri
):
string
{
throw
new
Error
(
'
fail
'
)
}
});
return
workspace
.
openTextDocument
(
Uri
.
parse
(
'
foo://foo/bla
'
)).
then
(
doc
=>
{
assert
.
equal
(
doc
.
getText
(),
'
1
'
);
registration1
.
dispose
();
registration2
.
dispose
();
});
});
test
(
'
registerTextDocumentContentProvider, invalid text
'
,
function
()
{
let
registration
=
workspace
.
registerTextDocumentContentProvider
(
'
foo
'
,
{
provideTextDocumentContent
(
uri
)
{
return
<
any
>
123
}
});
return
workspace
.
openTextDocument
(
Uri
.
parse
(
'
foo://auth/path
'
)).
then
(()
=>
{
assert
.
ok
(
false
,
'
expected failure
'
)
},
err
=>
{
// expected
})
registration
.
dispose
();
});
});
test
(
'
registerTextDocumentContentProvider, show virtual document
'
,
function
()
{
...
...
src/vs/workbench/api/node/extHostDocuments.ts
浏览文件 @
74d033e9
...
...
@@ -52,6 +52,8 @@ export function getWordDefinitionFor(modeId: string): RegExp {
@
Remotable
.
PluginHostContext
(
'
ExtHostModelService
'
)
export
class
ExtHostModelService
{
private
static
_handlePool
:
number
=
0
;
private
_onDidAddDocumentEventEmitter
:
Emitter
<
vscode
.
TextDocument
>
;
public
onDidAddDocument
:
Event
<
vscode
.
TextDocument
>
;
...
...
@@ -66,7 +68,7 @@ export class ExtHostModelService {
private
_documentData
:
{
[
modelUri
:
string
]:
ExtHostDocumentData
;
};
private
_documentLoader
:
{
[
modelUri
:
string
]:
TPromise
<
ExtHostDocumentData
>
};
private
_documentContentProviders
:
{
[
scheme
:
string
]:
vscode
.
TextDocumentContentProvider
};
private
_documentContentProviders
:
{
[
handle
:
number
]:
vscode
.
TextDocumentContentProvider
;
};
private
_proxy
:
MainThreadDocuments
;
...
...
@@ -131,54 +133,51 @@ export class ExtHostModelService {
}
public
registerTextDocumentContentProvider
(
scheme
:
string
,
provider
:
vscode
.
TextDocumentContentProvider
):
vscode
.
Disposable
{
if
(
scheme
===
'
file
'
||
scheme
===
'
untitled
'
||
this
.
_documentContentProviders
[
scheme
]
)
{
if
(
scheme
===
'
file
'
||
scheme
===
'
untitled
'
)
{
throw
new
Error
(
`scheme '
${
scheme
}
' already registered`
);
}
this
.
_documentContentProviders
[
scheme
]
=
provider
;
this
.
_proxy
.
$registerTextContentProvider
(
scheme
);
const
handle
=
ExtHostModelService
.
_handlePool
++
;
this
.
_documentContentProviders
[
handle
]
=
provider
;
this
.
_proxy
.
$registerTextContentProvider
(
handle
,
scheme
);
let
subscription
:
IDisposable
;
if
(
typeof
provider
.
onDidChange
===
'
function
'
)
{
subscription
=
provider
.
onDidChange
(
uri
=>
{
if
(
this
.
_documentData
[
uri
.
toString
()])
{
this
.
$provideTextDocumentContent
(
<
URI
>
uri
).
then
(
value
=>
{
this
.
$provideTextDocumentContent
(
handle
,
<
URI
>
uri
).
then
(
value
=>
{
return
this
.
_proxy
.
$onVirtualDocumentChange
(
<
URI
>
uri
,
value
);
},
onUnexpectedError
);
}
});
}
return
new
Disposable
(()
=>
{
this
.
_proxy
.
$unregisterTextContentProvider
(
scheme
);
this
.
_documentContentProviders
[
scheme
]
=
undefined
;
// keep the knowledge of that scheme
if
(
delete
this
.
_documentContentProviders
[
handle
])
{
this
.
_proxy
.
$unregisterTextContentProvider
(
handle
);
}
if
(
subscription
)
{
subscription
.
dispose
();
subscription
=
undefined
;
}
});
}
$provideTextDocumentContent
(
uri
:
URI
):
TPromise
<
string
>
{
const
provider
=
this
.
_documentContentProviders
[
uri
.
schem
e
];
$provideTextDocumentContent
(
handle
:
number
,
uri
:
URI
):
TPromise
<
string
>
{
const
provider
=
this
.
_documentContentProviders
[
handl
e
];
if
(
!
provider
)
{
return
TPromise
.
wrapError
<
string
>
(
`unsupported uri-scheme:
${
uri
.
scheme
}
`
);
}
return
asWinJsPromise
(
token
=>
provider
.
provideTextDocumentContent
(
uri
,
token
)).
then
(
value
=>
{
if
(
typeof
value
!==
'
string
'
)
{
return
TPromise
.
wrapError
(
'
received illegal value from text document provider
'
);
}
return
value
;
});
return
asWinJsPromise
(
token
=>
provider
.
provideTextDocumentContent
(
uri
,
token
));
}
$getUnreferencedDocuments
():
TPromise
<
URI
[]
>
{
const
result
:
URI
[]
=
[];
for
(
let
key
in
this
.
_documentData
)
{
let
uri
=
URI
.
parse
(
key
);
if
(
this
.
_documentContentProviders
[
uri
.
scheme
]
&&
!
this
.
_documentData
[
key
].
isDocumentReferenced
)
{
result
.
push
(
uri
);
$isDocumentReferenced
(
uri
:
URI
):
TPromise
<
boolean
>
{
const
key
=
uri
.
toString
();
const
document
=
this
.
_documentData
[
key
];
if
(
document
)
{
return
TPromise
.
as
(
document
.
isDocumentReferenced
);
}
}
return
TPromise
.
as
(
result
);
}
public
_acceptModelAdd
(
initData
:
IModelAddedData
):
void
{
let
data
=
new
ExtHostDocumentData
(
this
.
_proxy
,
initData
.
url
,
initData
.
value
.
lines
,
initData
.
value
.
EOL
,
initData
.
modeId
,
initData
.
versionId
,
initData
.
isDirty
);
...
...
@@ -468,7 +467,8 @@ export class MainThreadDocuments {
private
_modelToDisposeMap
:
{
[
modelUrl
:
string
]:
IDisposable
;
};
private
_proxy
:
ExtHostModelService
;
private
_modelIsSynced
:
{
[
modelId
:
string
]:
boolean
;
};
private
_resourceContentProvider
:
{
[
scheme
:
string
]:
IDisposable
};
private
_resourceContentProvider
:
{
[
handle
:
number
]:
IDisposable
};
private
_virtualDocumentSet
:
{
[
resource
:
string
]:
boolean
};
constructor
(
@
IThreadService
threadService
:
IThreadService
,
...
...
@@ -509,6 +509,7 @@ export class MainThreadDocuments {
this
.
_modelToDisposeMap
=
Object
.
create
(
null
);
this
.
_resourceContentProvider
=
Object
.
create
(
null
);
this
.
_virtualDocumentSet
=
Object
.
create
(
null
);
}
public
dispose
():
void
{
...
...
@@ -630,22 +631,26 @@ export class MainThreadDocuments {
// --- virtual document logic
$registerTextContentProvider
(
scheme
:
string
):
void
{
this
.
_resourceContentProvider
[
schem
e
]
=
ResourceEditorInput
.
registerResourceContentProvider
(
scheme
,
{
$registerTextContentProvider
(
handle
:
number
,
scheme
:
string
):
void
{
this
.
_resourceContentProvider
[
handl
e
]
=
ResourceEditorInput
.
registerResourceContentProvider
(
scheme
,
{
provideTextContent
:
(
uri
:
URI
):
TPromise
<
EditorCommon
.
IModel
>
=>
{
return
this
.
_proxy
.
$provideTextDocumentContent
(
uri
).
then
(
value
=>
{
return
this
.
_proxy
.
$provideTextDocumentContent
(
handle
,
uri
).
then
(
value
=>
{
if
(
value
)
{
this
.
_virtualDocumentSet
[
uri
.
toString
()]
=
true
;
const
firstLineText
=
value
.
substr
(
0
,
1
+
value
.
search
(
/
\r?\n
/
));
const
mode
=
this
.
_modeService
.
getOrCreateModeByFilenameOrFirstLine
(
uri
.
fsPath
,
firstLineText
);
return
this
.
_modelService
.
createModel
(
value
,
mode
,
uri
);
}
});
}
});
}
$unregisterTextContentProvider
(
scheme
:
string
):
void
{
const
registration
=
this
.
_resourceContentProvider
[
schem
e
];
$unregisterTextContentProvider
(
handle
:
number
):
void
{
const
registration
=
this
.
_resourceContentProvider
[
handl
e
];
if
(
registration
)
{
registration
.
dispose
();
delete
this
.
_resourceContentProvider
[
handle
];
}
}
...
...
@@ -657,23 +662,25 @@ export class MainThreadDocuments {
}
private
_runDocumentCleanup
():
void
{
this
.
_proxy
.
$getUnreferencedDocuments
().
then
(
resources
=>
{
const
toBeDisposed
:
URI
[]
=
[];
const
promises
=
resources
.
map
(
resource
=>
{
TPromise
.
join
(
Object
.
keys
(
this
.
_virtualDocumentSet
).
map
(
key
=>
{
let
resource
=
URI
.
parse
(
key
);
return
this
.
_proxy
.
$isDocumentReferenced
(
resource
).
then
(
referenced
=>
{
if
(
!
referenced
)
{
return
this
.
_editorService
.
inputToType
({
resource
}).
then
(
input
=>
{
if
(
!
this
.
_editorService
.
isVisible
(
input
,
true
))
{
toBeDisposed
.
push
(
resource
);
}
});
}
});
return
TPromise
.
join
(
promises
).
then
(()
=>
{
})).
then
(()
=>
{
for
(
let
resource
of
toBeDisposed
)
{
this
.
_modelService
.
destroyModel
(
resource
);
delete
this
.
_virtualDocumentSet
[
resource
.
toString
()];
}
});
},
onUnexpectedError
);
}
}
src/vs/workbench/common/editor/resourceEditorInput.ts
浏览文件 @
74d033e9
...
...
@@ -5,6 +5,7 @@
'
use strict
'
;
import
{
TPromise
}
from
'
vs/base/common/winjs.base
'
;
import
{
sequence
}
from
'
vs/base/common/async
'
;
import
{
EditorModel
,
EditorInput
}
from
'
vs/workbench/common/editor
'
;
import
{
ResourceEditorModel
}
from
'
vs/workbench/common/editor/resourceEditorModel
'
;
import
{
IModel
}
from
'
vs/editor/common/editorCommon
'
;
...
...
@@ -31,11 +32,28 @@ export class ResourceEditorInput extends EditorInput {
// todo@joh,ben this should maybe be a service that is in charge of loading/resolving a uri from a scheme
private
static
loadingModels
:
{
[
uri
:
string
]:
TPromise
<
IModel
>
}
=
Object
.
create
(
null
);
private
static
registry
:
{
[
scheme
:
string
]:
IResourceEditorContentProvider
}
=
Object
.
create
(
null
);
private
static
registry
:
{
[
scheme
:
string
]:
IResourceEditorContentProvider
[]
}
=
Object
.
create
(
null
);
public
static
registerResourceContentProvider
(
scheme
:
string
,
provider
:
IResourceEditorContentProvider
):
IDisposable
{
ResourceEditorInput
.
registry
[
scheme
]
=
provider
;
return
{
dispose
()
{
delete
ResourceEditorInput
.
registry
[
scheme
]
}
};
let
array
=
ResourceEditorInput
.
registry
[
scheme
];
if
(
!
array
)
{
array
=
[
provider
];
ResourceEditorInput
.
registry
[
scheme
]
=
array
;
}
else
{
array
.
unshift
(
provider
);
}
return
{
dispose
()
{
let
array
=
ResourceEditorInput
.
registry
[
scheme
];
let
idx
=
array
.
indexOf
(
provider
);
if
(
idx
>=
0
)
{
array
.
splice
(
idx
,
1
);
if
(
array
.
length
===
0
)
{
delete
ResourceEditorInput
.
registry
[
scheme
];
}
}
}
};
}
private
static
getOrCreateModel
(
modelService
:
IModelService
,
resource
:
URI
):
TPromise
<
IModel
>
{
...
...
@@ -49,8 +67,8 @@ export class ResourceEditorInput extends EditorInput {
// make sure we have a provider this scheme
// the resource uses
const
provider
=
ResourceEditorInput
.
registry
[
resource
.
scheme
];
if
(
!
provider
)
{
const
array
=
ResourceEditorInput
.
registry
[
resource
.
scheme
];
if
(
!
array
)
{
return
TPromise
.
wrapError
(
`No model with uri '
${
resource
}
' nor a resolver for the scheme '
${
resource
.
scheme
}
'.`
);
}
...
...
@@ -59,7 +77,26 @@ export class ResourceEditorInput extends EditorInput {
// twice
ResourceEditorInput
.
loadingModels
[
resource
.
toString
()]
=
loadingModel
=
new
TPromise
<
IModel
>
((
resolve
,
reject
)
=>
{
provider
.
provideTextContent
(
resource
).
then
(
resolve
,
reject
);
let
result
:
IModel
;
let
lastError
:
any
;
sequence
(
array
.
map
(
provider
=>
{
return
()
=>
{
if
(
!
result
)
{
return
provider
.
provideTextContent
(
resource
).
then
(
value
=>
{
result
=
value
;
},
err
=>
{
lastError
=
err
;
});
}
}
})).
then
(()
=>
{
if
(
!
result
&&
lastError
)
{
reject
(
lastError
);
}
else
{
resolve
(
result
);
}
},
reject
);
},
function
()
{
// no cancellation when caching promises
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录