提交 e170425f 编写于 作者: A Alex Dima

Fixes #8167: Do not use IWorkspaceContextService in compat web worker anymore

上级 5d5b3b44
......@@ -13,6 +13,7 @@ import * as modes from 'vs/editor/common/modes';
import {CompatMode, ModeWorkerManager} from 'vs/editor/common/modes/abstractMode';
import {wireCancellationToken} from 'vs/base/common/async';
import {ICompatWorkerService, CompatWorkerAttr} from 'vs/editor/common/services/compatWorkerService';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
export class OutputMode extends CompatMode {
......@@ -21,7 +22,8 @@ export class OutputMode extends CompatMode {
constructor(
descriptor: IModeDescriptor,
@IInstantiationService instantiationService: IInstantiationService,
@ICompatWorkerService compatWorkerService: ICompatWorkerService
@ICompatWorkerService compatWorkerService: ICompatWorkerService,
@IWorkspaceContextService contextService:IWorkspaceContextService
) {
super(descriptor.id, compatWorkerService);
this._modeWorkerManager = new ModeWorkerManager<OutputWorker>(descriptor, 'vs/workbench/parts/output/common/outputWorker', 'OutputWorker', null, instantiationService);
......@@ -31,12 +33,24 @@ export class OutputMode extends CompatMode {
return wireCancellationToken(token, this._provideLinks(model.uri));
}
});
if (compatWorkerService.isInMainThread) {
let workspace = contextService.getWorkspace();
if (workspace) {
this._configure(workspace.resource);
}
}
}
private _worker<T>(runner: (worker: OutputWorker) => TPromise<T>): TPromise<T> {
return this._modeWorkerManager.worker(runner);
}
static $_configure = CompatWorkerAttr(OutputMode, OutputMode.prototype._configure);
private _configure(workspaceResource: URI): TPromise<void> {
return this._worker((w) => w.configure(workspaceResource));
}
static $_provideLinks = CompatWorkerAttr(OutputMode, OutputMode.prototype._provideLinks);
private _provideLinks(resource: URI): TPromise<modes.ILink[]> {
return this._worker((w) => w.provideLinks(resource));
......
......@@ -5,7 +5,6 @@
'use strict';
import {TPromise} from 'vs/base/common/winjs.base';
import {IMarkerService} from 'vs/platform/markers/common/markers';
import {IResourceService} from 'vs/editor/common/services/resourceService';
import URI from 'vs/base/common/uri';
import strings = require('vs/base/common/strings');
......@@ -13,35 +12,34 @@ import arrays = require('vs/base/common/arrays');
import paths = require('vs/base/common/paths');
import {ILink} from 'vs/editor/common/modes';
import {Range} from 'vs/editor/common/core/range';
import {IWorkspaceContextService, IWorkspace} from 'vs/platform/workspace/common/workspace';
export interface IResourceCreator {
toResource: (workspaceRelativePath: string) => URI;
}
/**
* A base class of text editor worker that helps with detecting links in the text that point to files in the workspace.
*/
export class OutputWorker {
private _contextService: IWorkspaceContextService;
private _workspaceResource: URI;
private patterns: RegExp[];
private resourceService:IResourceService;
private markerService: IMarkerService;
private _modeId: string;
constructor(
modeId: string,
@IResourceService resourceService: IResourceService,
@IMarkerService markerService: IMarkerService,
@IWorkspaceContextService contextService:IWorkspaceContextService
@IResourceService resourceService: IResourceService
) {
this._modeId = modeId;
this.resourceService = resourceService;
this.markerService = markerService;
this._contextService = contextService;
let workspace = this._contextService.getWorkspace();
this.patterns = workspace ? OutputWorker.createPatterns(workspace) : [];
this._workspaceResource = null;
this.patterns = [];
}
public get contextService(): IWorkspaceContextService {
return this._contextService;
public configure(workspaceResource: URI): TPromise<void> {
this._workspaceResource = workspaceResource;
this.patterns = OutputWorker.createPatterns(this._workspaceResource);
return TPromise.as(void 0);
}
public provideLinks(resource: URI): TPromise<ILink[]> {
......@@ -52,19 +50,28 @@ export class OutputWorker {
let model = this.resourceService.get(resource);
let resourceCreator: IResourceCreator = {
toResource: (workspaceRelativePath: string): URI => {
if (typeof workspaceRelativePath === 'string' && this._workspaceResource) {
return URI.file(paths.join(this._workspaceResource.fsPath, workspaceRelativePath));
}
return null;
}
};
for (let i = 1, lineCount = model.getLineCount(); i <= lineCount; i++) {
links.push(...OutputWorker.detectLinks(model.getLineContent(i), i, this.patterns, this._contextService));
links.push(...OutputWorker.detectLinks(model.getLineContent(i), i, this.patterns, resourceCreator));
}
return TPromise.as(links);
}
public static createPatterns(workspace: IWorkspace): RegExp[] {
public static createPatterns(workspaceResource: URI): RegExp[] {
let patterns: RegExp[] = [];
let workspaceRootVariants = arrays.distinct([
paths.normalize(workspace.resource.fsPath, true),
paths.normalize(workspace.resource.fsPath, false)
paths.normalize(workspaceResource.fsPath, true),
paths.normalize(workspaceResource.fsPath, false)
]);
workspaceRootVariants.forEach((workspaceRoot) => {
......@@ -93,7 +100,7 @@ export class OutputWorker {
/**
* Detect links. Made public static to allow for tests.
*/
public static detectLinks(line: string, lineIndex: number, patterns: RegExp[], contextService: IWorkspaceContextService): ILink[] {
public static detectLinks(line: string, lineIndex: number, patterns: RegExp[], contextService: IResourceCreator): ILink[] {
let links: ILink[] = [];
patterns.forEach((pattern) => {
......
......@@ -22,21 +22,13 @@ function toOSPath(p: string): string {
suite('Workbench - OutputWorker', () => {
test('OutputWorker - Link detection', function () {
let patternsSlash = OutputWorker.createPatterns({
id: 'foo',
name: 'foo',
resource: URI.file('C:/Users/someone/AppData/Local/Temp/_monacodata_9888/workspaces/mankala'),
uid: 0,
mtime: 0
});
let patternsBackSlash = OutputWorker.createPatterns({
id: 'foo',
name: 'foo',
resource: URI.file('C:\\Users\\someone\\AppData\\Local\\Temp\\_monacodata_9888\\workspaces\\mankala'),
uid: 0,
mtime: 0
});
let patternsSlash = OutputWorker.createPatterns(
URI.file('C:/Users/someone/AppData/Local/Temp/_monacodata_9888/workspaces/mankala')
);
let patternsBackSlash = OutputWorker.createPatterns(
URI.file('C:\\Users\\someone\\AppData\\Local\\Temp\\_monacodata_9888\\workspaces\\mankala')
);
let contextService = new TestContextService();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册