openerService.ts 2.4 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';
B
Benjamin Pasero 已提交
12
import {normalize} from 'vs/base/common/paths';
13
import {ICommandService, CommandsRegistry} from 'vs/platform/commands/common/commands';
B
Benjamin Pasero 已提交
14
import {IOpenerService} from 'vs/platform/opener/common/opener';
15 16 17

export class OpenerService implements IOpenerService {

18
	_serviceBrand: any;
19 20 21

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

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

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

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

		} else {
B
Benjamin Pasero 已提交
49 50 51 52 53 54 55 56 57 58 59
			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
60
				};
B
Benjamin Pasero 已提交
61 62
				// remove fragment
				resource = resource.with({ fragment: '' });
B
Benjamin Pasero 已提交
63
			} else if (resource.scheme === Schemas.file) {
B
💄  
Benjamin Pasero 已提交
64
				resource = URI.file(normalize(resource.fsPath)); // workaround for non-normalized paths (https://github.com/Microsoft/vscode/issues/12954)
B
Benjamin Pasero 已提交
65 66
			}
			promise = this._editorService.openEditor({ resource, options: { selection, } }, options && options.openToSide);
67 68
		}

69
		return TPromise.as(promise);
70 71
	}
}