openerService.ts 2.1 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import URI from 'vs/base/common/uri';
8
import {parse} from 'vs/base/common/marshalling';
9 10 11
import {Schemas} from 'vs/base/common/network';
import {TPromise} from 'vs/base/common/winjs.base';
import {IEditorService} from 'vs/platform/editor/common/editor';
12
import {ICommandService, CommandsRegistry} from 'vs/platform/commands/common/commands';
13 14 15 16
import {IOpenerService} from '../common/opener';

export class OpenerService implements IOpenerService {

17
	_serviceBrand: any;
18 19 20

	constructor(
		@IEditorService private _editorService: IEditorService,
21
		@ICommandService private _commandService: ICommandService
22 23 24 25
	) {
		//
	}

26
	open(resource: URI, options?: { openToSide?: boolean }): TPromise<any> {
27 28 29 30 31 32 33

		const {scheme, path, query, fragment} = resource;
		let promise: TPromise<any>;
		if (scheme === Schemas.http || scheme === Schemas.https) {
			// open http
			window.open(resource.toString(true));

34
		} else if (scheme === 'command' && CommandsRegistry.getCommand(path)) {
35
			// execute as command
36
			let args: any = [];
37
			try {
38
				args = parse(query);
39 40 41
				if (!Array.isArray(args)) {
					args = [args];
				}
42 43 44
			} catch (e) {
				//
			}
45
			promise = this._commandService.executeCommand(path, ...args);
46 47

		} else {
B
Benjamin Pasero 已提交
48 49 50 51 52 53 54 55 56 57 58
			let selection: {
				startLineNumber: number;
				startColumn: number;
			};
			const match = /^L?(\d+)(?:,(\d+))?/.exec(fragment);
			if (match) {
				// support file:///some/file.js#73,84
				// support file:///some/file.js#L73
				selection = {
					startLineNumber: parseInt(match[1]),
					startColumn: match[2] ? parseInt(match[2]) : 1
59
				};
B
Benjamin Pasero 已提交
60 61 62 63
				// remove fragment
				resource = resource.with({ fragment: '' });
			}
			promise = this._editorService.openEditor({ resource, options: { selection, } }, options && options.openToSide);
64 65
		}

66
		return TPromise.as(promise);
67 68
	}
}