openerService.ts 2.2 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 48 49 50 51 52 53 54 55

		} else {
			promise = this._editorService.resolveEditorModel({ resource }).then(model => {
				if (!model) {
					return;
				}
				let selection: {
					startLineNumber: number;
					startColumn: number;
				};
56 57 58 59
				const match = /^L?(\d+)(?:,(\d+))?/.exec(fragment);
				if (match) {
					// support file:///some/file.js#73,84
					// support file:///some/file.js#L73
60
					selection = {
61 62
						startLineNumber: parseInt(match[1]),
						startColumn: match[2] ? parseInt(match[2]) : 1
63
					};
64 65
					// remove fragment
					resource = resource.with({ fragment: '' });
66
				}
67
				return this._editorService.openEditor({ resource, options: { selection, } }, options && options.openToSide);
68 69 70
			});
		}

71
		return TPromise.as(promise);
72 73
	}
}