提交 6ea319fa 编写于 作者: J Johannes Rieken

move the logic of getOutlineEntries into one place

上级 1ef99fc7
...@@ -5,14 +5,73 @@ ...@@ -5,14 +5,73 @@
'use strict'; 'use strict';
import {onUnexpectedError} from 'vs/base/common/errors';
import {TPromise} from 'vs/base/common/winjs.base';
import {Range} from 'vs/editor/common/core/range';
import {IModel} from 'vs/editor/common/editorCommon';
import {IOutlineEntry, IOutlineSupport} from 'vs/editor/common/modes'; import {IOutlineEntry, IOutlineSupport} from 'vs/editor/common/modes';
import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry'; import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry';
const QuickOutineRegistry = new LanguageFeatureRegistry<IOutlineSupport>('outlineSupport'); const OutlineRegistry = new LanguageFeatureRegistry<IOutlineSupport>('outlineSupport');
export { export {
OutlineRegistry,
IOutlineEntry, IOutlineEntry,
IOutlineSupport IOutlineSupport
} }
export default QuickOutineRegistry; export function getOutlineEntries(model: IModel): TPromise<{ entries: IOutlineEntry[], outlineGroupLabel: { [n: string]: string;} }> {
\ No newline at end of file
let resource = model.getAssociatedResource();
let groupLabels: { [n: string]: string } = Object.create(null);
let entries: IOutlineEntry[] = [];
let promises = OutlineRegistry.all(<IModel>model).map(support => {
if (support.outlineGroupLabel) {
for (var key in support.outlineGroupLabel) {
if (Object.prototype.hasOwnProperty.call(support.outlineGroupLabel, key)) {
groupLabels[key] = support.outlineGroupLabel[key];
}
}
}
return support.getOutline(resource).then(result => {
if (Array.isArray(result)) {
entries.push(...result);
}
}, err => {
onUnexpectedError(err);
});
});
return TPromise.join(promises).then(() => {
let flatEntries: IOutlineEntry[] = [];
flatten(flatEntries, entries, '');
flatEntries.sort(compareEntriesUsingStart);
return {
entries: flatEntries,
outlineGroupLabel: groupLabels
}
});
}
function compareEntriesUsingStart(a: IOutlineEntry, b: IOutlineEntry): number{
return Range.compareRangesUsingStarts(a.range, b.range);
}
function flatten(bucket: IOutlineEntry[], entries: IOutlineEntry[], overrideContainerLabel: string): void {
for (let entry of entries) {
bucket.push({
type: entry.type,
range: entry.range,
label: entry.label,
icon: entry.icon,
containerLabel: entry.containerLabel || overrideContainerLabel
});
if (entry.children) {
flatten(bucket, entry.children, entry.label);
}
}
}
...@@ -27,7 +27,7 @@ import ExtraInfoRegistry from 'vs/editor/contrib/hover/common/hover'; ...@@ -27,7 +27,7 @@ import ExtraInfoRegistry from 'vs/editor/contrib/hover/common/hover';
import DocumentHighlighterRegistry from 'vs/editor/contrib/wordHighlighter/common/wordHighlighter'; import DocumentHighlighterRegistry from 'vs/editor/contrib/wordHighlighter/common/wordHighlighter';
import ReferenceSearchRegistry from 'vs/editor/contrib/referenceSearch/common/referenceSearch'; import ReferenceSearchRegistry from 'vs/editor/contrib/referenceSearch/common/referenceSearch';
import QuickFixRegistry from 'vs/editor/contrib/quickFix/common/quickFix'; import QuickFixRegistry from 'vs/editor/contrib/quickFix/common/quickFix';
import QuickOutlineRegistry, {IOutlineEntry, IOutlineSupport} from 'vs/editor/contrib/quickOpen/common/quickOpen'; import {OutlineRegistry, IOutlineEntry, IOutlineSupport} from 'vs/editor/contrib/quickOpen/common/quickOpen';
import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry'; import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry';
import {NavigateTypesSupportRegistry, INavigateTypesSupport, ITypeBearing} from 'vs/workbench/parts/search/common/search' import {NavigateTypesSupportRegistry, INavigateTypesSupport, ITypeBearing} from 'vs/workbench/parts/search/common/search'
import {RenameRegistry} from 'vs/editor/contrib/rename/common/rename'; import {RenameRegistry} from 'vs/editor/contrib/rename/common/rename';
...@@ -544,7 +544,7 @@ export class ExtensionHostDocumentSymbols extends AbstractExtensionHostFeature<v ...@@ -544,7 +544,7 @@ export class ExtensionHostDocumentSymbols extends AbstractExtensionHostFeature<v
export class MainThreadDocumentSymbols extends AbstractMainThreadFeature<IOutlineSupport> implements IOutlineSupport { export class MainThreadDocumentSymbols extends AbstractMainThreadFeature<IOutlineSupport> implements IOutlineSupport {
constructor( @IThreadService threadService: IThreadService) { constructor( @IThreadService threadService: IThreadService) {
super('vscode.executeDocumentSymbolProvider', QuickOutlineRegistry, threadService); super('vscode.executeDocumentSymbolProvider', OutlineRegistry, threadService);
} }
getOutline(resource: URI): TPromise<IOutlineEntry[]>{ getOutline(resource: URI): TPromise<IOutlineEntry[]>{
...@@ -1262,7 +1262,7 @@ export namespace LanguageFeatures { ...@@ -1262,7 +1262,7 @@ export namespace LanguageFeatures {
threadService.getRemotable(MainThreadReferenceSearch); threadService.getRemotable(MainThreadReferenceSearch);
threadService.getRemotable(MainThreadCodeActions); threadService.getRemotable(MainThreadCodeActions);
threadService.getRemotable(MainThreadCodeLens); threadService.getRemotable(MainThreadCodeLens);
threadService.getRemotable(MainThreadDocumentSymbols); // threadService.getRemotable(MainThreadDocumentSymbols);
threadService.getRemotable(MainThreadWorkspaceSymbols); threadService.getRemotable(MainThreadWorkspaceSymbols);
threadService.getRemotable(MainThreadRename); threadService.getRemotable(MainThreadRename);
threadService.getRemotable(MainThreadFormatDocument); threadService.getRemotable(MainThreadFormatDocument);
...@@ -1280,7 +1280,7 @@ export namespace LanguageFeatures { ...@@ -1280,7 +1280,7 @@ export namespace LanguageFeatures {
referenceSearch: new ExtensionHostReferenceSearch(threadService), referenceSearch: new ExtensionHostReferenceSearch(threadService),
codeActions: new ExtensionHostCodeActions(threadService), codeActions: new ExtensionHostCodeActions(threadService),
codeLens: threadService.getRemotable(ExtensionHostCodeLens), codeLens: threadService.getRemotable(ExtensionHostCodeLens),
documentSymbols: new ExtensionHostDocumentSymbols(threadService), // documentSymbols: new ExtensionHostDocumentSymbols(threadService),
workspaceSymbols: new ExtensionHostWorkspaceSymbols(threadService), workspaceSymbols: new ExtensionHostWorkspaceSymbols(threadService),
rename: new ExtensionHostRename(threadService), rename: new ExtensionHostRename(threadService),
formatDocument: new ExtHostFormatDocument(threadService), formatDocument: new ExtHostFormatDocument(threadService),
......
...@@ -31,7 +31,7 @@ import {IQuickOpenService} from 'vs/workbench/services/quickopen/browser/quickOp ...@@ -31,7 +31,7 @@ import {IQuickOpenService} from 'vs/workbench/services/quickopen/browser/quickOp
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace'; import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import {Position} from 'vs/platform/editor/common/editor'; import {Position} from 'vs/platform/editor/common/editor';
import {KeyMod, KeyCode} from 'vs/base/common/keyCodes'; import {KeyMod, KeyCode} from 'vs/base/common/keyCodes';
import QuickOpenRegistry from 'vs/editor/contrib/quickOpen/common/quickOpen'; import {OutlineRegistry, getOutlineEntries} from 'vs/editor/contrib/quickOpen/common/quickOpen';
const ACTION_ID = 'workbench.action.gotoSymbol'; const ACTION_ID = 'workbench.action.gotoSymbol';
const ACTION_LABEL = nls.localize('gotoSymbol', "Go to Symbol..."); const ACTION_LABEL = nls.localize('gotoSymbol', "Go to Symbol...");
...@@ -432,7 +432,7 @@ export class GotoSymbolHandler extends QuickOpenHandler { ...@@ -432,7 +432,7 @@ export class GotoSymbolHandler extends QuickOpenHandler {
if (model && types.isFunction((<ITokenizedModel>model).getMode)) { if (model && types.isFunction((<ITokenizedModel>model).getMode)) {
canRun = QuickOpenRegistry.has(<IModel> model); canRun = OutlineRegistry.has(<IModel> model);
} }
} }
...@@ -527,34 +527,7 @@ export class GotoSymbolHandler extends QuickOpenHandler { ...@@ -527,34 +527,7 @@ export class GotoSymbolHandler extends QuickOpenHandler {
return TPromise.as(this.outlineToModelCache[modelId]); return TPromise.as(this.outlineToModelCache[modelId]);
} }
let groupLabels: { [n: string]: string } = Object.create(null); return getOutlineEntries(<IModel>model).then(outline => {
let entries: IOutlineEntry[] = [];
let resource = (<IModel>model).getAssociatedResource();
let promises = QuickOpenRegistry.all(<IModel>model).map(support => {
if (support.outlineGroupLabel) {
for (var key in support.outlineGroupLabel) {
if (Object.prototype.hasOwnProperty.call(support.outlineGroupLabel, key)) {
groupLabels[key] = support.outlineGroupLabel[key];
}
}
}
return support.getOutline(resource).then(result => {
if (Array.isArray(result)) {
entries.push(...result);
}
}, err => {
errors.onUnexpectedError(err);
});
});
return TPromise.join(promises).then(() => {
let outline = {
entries,
outlineGroupLabel: groupLabels
};
let model = new OutlineModel(outline, this.toQuickOpenEntries(outline)); let model = new OutlineModel(outline, this.toQuickOpenEntries(outline));
......
...@@ -18,7 +18,7 @@ import * as LF from 'vs/workbench/api/common/languageFeatures'; ...@@ -18,7 +18,7 @@ import * as LF from 'vs/workbench/api/common/languageFeatures';
import {PluginHostCommands, MainThreadCommands} from 'vs/workbench/api/common/pluginHostCommands'; import {PluginHostCommands, MainThreadCommands} from 'vs/workbench/api/common/pluginHostCommands';
import {PluginHostModelService} from 'vs/workbench/api/common/pluginHostDocuments'; import {PluginHostModelService} from 'vs/workbench/api/common/pluginHostDocuments';
import {SyncDescriptor0} from 'vs/platform/instantiation/common/descriptors'; import {SyncDescriptor0} from 'vs/platform/instantiation/common/descriptors';
import QuickOutlineRegistry from 'vs/editor/contrib/quickOpen/common/quickOpen'; import {OutlineRegistry} from 'vs/editor/contrib/quickOpen/common/quickOpen';
import {LanguageSelector, ModelLike} from 'vs/editor/common/modes/languageSelector'; import {LanguageSelector, ModelLike} from 'vs/editor/common/modes/languageSelector';
class ThreadService extends NullThreadService { class ThreadService extends NullThreadService {
...@@ -85,17 +85,17 @@ suite('ExtHostLanguageFeatures', function() { ...@@ -85,17 +85,17 @@ suite('ExtHostLanguageFeatures', function() {
// register // register
assert.equal(QuickOutlineRegistry.all(model).length, 0); assert.equal(OutlineRegistry.all(model).length, 0);
let disposable = extHost.register('far', { let disposable = extHost.register('far', {
provideDocumentSymbols() { provideDocumentSymbols() {
return []; return [];
} }
}); });
assert.equal(QuickOutlineRegistry.all(model).length, 1); assert.equal(OutlineRegistry.all(model).length, 1);
// deregister // deregister
disposable.dispose(); disposable.dispose();
assert.equal(QuickOutlineRegistry.all(model).length, 0); assert.equal(OutlineRegistry.all(model).length, 0);
// all extension host provider appear as one // all extension host provider appear as one
disposable = extHost.register('far', { disposable = extHost.register('far', {
...@@ -108,12 +108,12 @@ suite('ExtHostLanguageFeatures', function() { ...@@ -108,12 +108,12 @@ suite('ExtHostLanguageFeatures', function() {
return []; return [];
} }
}); });
assert.equal(QuickOutlineRegistry.all(model).length, 1); assert.equal(OutlineRegistry.all(model).length, 1);
disposable.dispose(); disposable.dispose();
assert.equal(QuickOutlineRegistry.all(model).length, 1); assert.equal(OutlineRegistry.all(model).length, 1);
disposable2.dispose(); disposable2.dispose();
assert.equal(QuickOutlineRegistry.all(model).length, 0); assert.equal(OutlineRegistry.all(model).length, 0);
}); });
test('DocumentSymbols, evil provider', function(done) { test('DocumentSymbols, evil provider', function(done) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册