links.ts 11.1 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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 'vs/css!./links';
A
Alex Dima 已提交
9
import * as nls from 'vs/nls';
J
Johannes Rieken 已提交
10 11
import { onUnexpectedError } from 'vs/base/common/errors';
import { KeyCode } from 'vs/base/common/keyCodes';
A
Alex Dima 已提交
12 13
import * as platform from 'vs/base/common/platform';
import Severity from 'vs/base/common/severity';
J
Johannes Rieken 已提交
14 15 16 17
import { TPromise } from 'vs/base/common/winjs.base';
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { IMessageService } from 'vs/platform/message/common/message';
import { IOpenerService } from 'vs/platform/opener/common/opener';
A
Alex Dima 已提交
18
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
19 20 21 22 23 24 25
import { editorAction, ServicesAccessor, EditorAction } from 'vs/editor/common/editorCommonExtensions';
import { LinkProviderRegistry } from 'vs/editor/common/modes';
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
import { IEditorMouseEvent, ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { getLinks, Link } from 'vs/editor/contrib/links/common/links';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { editorContribution } from 'vs/editor/browser/editorBrowserExtensions';
E
Erich Gamma 已提交
26 27 28

class LinkOccurence {

29
	public static decoration(link: Link): editorCommon.IModelDeltaDecoration {
E
Erich Gamma 已提交
30 31 32 33
		return {
			range: {
				startLineNumber: link.range.startLineNumber,
				startColumn: link.range.startColumn,
34
				endLineNumber: link.range.endLineNumber,
E
Erich Gamma 已提交
35 36 37
				endColumn: link.range.endColumn
			},
			options: LinkOccurence._getOptions(link, false)
A
tslint  
Alex Dima 已提交
38
		};
E
Erich Gamma 已提交
39 40
	}

41
	private static _getOptions(link: Link, isActive: boolean): editorCommon.IModelDecorationOptions {
E
Erich Gamma 已提交
42 43 44 45 46 47 48 49 50
		var result = '';

		if (isActive) {
			result += LinkDetector.CLASS_NAME_ACTIVE;
		} else {
			result += LinkDetector.CLASS_NAME;
		}

		return {
A
Alex Dima 已提交
51
			stickiness: editorCommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges,
E
Erich Gamma 已提交
52 53 54 55 56
			inlineClassName: result,
			hoverMessage: LinkDetector.HOVER_MESSAGE_GENERAL
		};
	}

57 58
	public decorationId: string;
	public link: Link;
E
Erich Gamma 已提交
59

60
	constructor(link: Link, decorationId: string/*, changeAccessor:editorCommon.IModelDecorationsChangeAccessor*/) {
E
Erich Gamma 已提交
61 62 63 64
		this.link = link;
		this.decorationId = decorationId;
	}

65
	public activate(changeAccessor: editorCommon.IModelDecorationsChangeAccessor): void {
E
Erich Gamma 已提交
66 67 68
		changeAccessor.changeDecorationOptions(this.decorationId, LinkOccurence._getOptions(this.link, true));
	}

69
	public deactivate(changeAccessor: editorCommon.IModelDecorationsChangeAccessor): void {
E
Erich Gamma 已提交
70 71 72 73
		changeAccessor.changeDecorationOptions(this.decorationId, LinkOccurence._getOptions(this.link, false));
	}
}

74
@editorContribution
A
Alex Dima 已提交
75 76
class LinkDetector implements editorCommon.IEditorContribution {

A
Alex Dima 已提交
77 78
	private static ID: string = 'editor.linkDetector';

79
	public static get(editor: editorCommon.ICommonCodeEditor): LinkDetector {
A
Alex Dima 已提交
80
		return editor.getContribution<LinkDetector>(LinkDetector.ID);
A
Alex Dima 已提交
81 82
	}

E
Erich Gamma 已提交
83
	static RECOMPUTE_TIME = 1000; // ms
A
Alex Dima 已提交
84 85 86
	static TRIGGER_KEY_VALUE = platform.isMacintosh ? KeyCode.Meta : KeyCode.Ctrl;
	static TRIGGER_MODIFIER = platform.isMacintosh ? 'metaKey' : 'ctrlKey';
	static HOVER_MESSAGE_GENERAL = platform.isMacintosh ? nls.localize('links.navigate.mac', "Cmd + click to follow link") : nls.localize('links.navigate', "Ctrl + click to follow link");
E
Erich Gamma 已提交
87 88 89
	static CLASS_NAME = 'detected-link';
	static CLASS_NAME_ACTIVE = 'detected-link-active';

90 91 92 93 94 95 96 97
	private editor: ICodeEditor;
	private listenersToRemove: IDisposable[];
	private timeoutPromise: TPromise<void>;
	private computePromise: TPromise<void>;
	private activeLinkDecorationId: string;
	private lastMouseEvent: IEditorMouseEvent;
	private openerService: IOpenerService;
	private messageService: IMessageService;
98
	private editorWorkerService: IEditorWorkerService;
99
	private currentOccurences: { [decorationId: string]: LinkOccurence; };
E
Erich Gamma 已提交
100

101
	constructor(
102 103 104
		editor: ICodeEditor,
		@IOpenerService openerService: IOpenerService,
		@IMessageService messageService: IMessageService,
A
Alex Dima 已提交
105
		@IEditorWorkerService editorWorkerService: IEditorWorkerService
106
	) {
E
Erich Gamma 已提交
107
		this.editor = editor;
108
		this.openerService = openerService;
E
Erich Gamma 已提交
109
		this.messageService = messageService;
110
		this.editorWorkerService = editorWorkerService;
E
Erich Gamma 已提交
111
		this.listenersToRemove = [];
A
Alex Dima 已提交
112
		this.listenersToRemove.push(editor.onDidChangeModelContent((e) => this.onChange()));
A
Alex Dima 已提交
113
		this.listenersToRemove.push(editor.onDidChangeModel((e) => this.onModelChanged()));
A
Alex Dima 已提交
114
		this.listenersToRemove.push(editor.onDidChangeModelLanguage((e) => this.onModelModeChanged()));
115
		this.listenersToRemove.push(LinkProviderRegistry.onDidChange((e) => this.onModelModeChanged()));
116 117 118 119
		this.listenersToRemove.push(this.editor.onMouseUp((e: IEditorMouseEvent) => this.onEditorMouseUp(e)));
		this.listenersToRemove.push(this.editor.onMouseMove((e: IEditorMouseEvent) => this.onEditorMouseMove(e)));
		this.listenersToRemove.push(this.editor.onKeyDown((e: IKeyboardEvent) => this.onEditorKeyDown(e)));
		this.listenersToRemove.push(this.editor.onKeyUp((e: IKeyboardEvent) => this.onEditorKeyUp(e)));
E
Erich Gamma 已提交
120 121 122 123 124 125 126
		this.timeoutPromise = null;
		this.computePromise = null;
		this.currentOccurences = {};
		this.activeLinkDecorationId = null;
		this.beginCompute();
	}

A
Alex Dima 已提交
127 128 129 130
	public getId(): string {
		return LinkDetector.ID;
	}

E
Erich Gamma 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	public isComputing(): boolean {
		return TPromise.is(this.computePromise);
	}

	private onModelChanged(): void {
		this.lastMouseEvent = null;
		this.currentOccurences = {};
		this.activeLinkDecorationId = null;
		this.stop();
		this.beginCompute();
	}

	private onModelModeChanged(): void {
		this.stop();
		this.beginCompute();
	}

148
	private onChange(): void {
E
Erich Gamma 已提交
149 150 151 152 153 154 155 156 157
		if (!this.timeoutPromise) {
			this.timeoutPromise = TPromise.timeout(LinkDetector.RECOMPUTE_TIME);
			this.timeoutPromise.then(() => {
				this.timeoutPromise = null;
				this.beginCompute();
			});
		}
	}

158
	private beginCompute(): void {
E
Erich Gamma 已提交
159 160 161
		if (!this.editor.getModel()) {
			return;
		}
162

163 164
		if (!LinkProviderRegistry.has(this.editor.getModel())) {
			return;
E
Erich Gamma 已提交
165
		}
166

167
		this.computePromise = getLinks(this.editor.getModel()).then(links => {
168 169 170 171 172
			this.updateDecorations(links);
			this.computePromise = null;
		});
	}

173 174 175
	private updateDecorations(links: Link[]): void {
		this.editor.changeDecorations((changeAccessor: editorCommon.IModelDecorationsChangeAccessor) => {
			var oldDecorations: string[] = [];
A
Alex Dima 已提交
176 177 178 179 180
			let keys = Object.keys(this.currentOccurences);
			for (let i = 0, len = keys.length; i < len; i++) {
				let decorationId = keys[i];
				let occurance = this.currentOccurences[decorationId];
				oldDecorations.push(occurance.decorationId);
E
Erich Gamma 已提交
181 182
			}

183
			var newDecorations: editorCommon.IModelDeltaDecoration[] = [];
E
Erich Gamma 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
			if (links) {
				// Not sure why this is sometimes null
				for (var i = 0; i < links.length; i++) {
					newDecorations.push(LinkOccurence.decoration(links[i]));
				}
			}

			var decorations = changeAccessor.deltaDecorations(oldDecorations, newDecorations);

			this.currentOccurences = {};
			this.activeLinkDecorationId = null;
			for (let i = 0, len = decorations.length; i < len; i++) {
				var occurance = new LinkOccurence(links[i], decorations[i]);
				this.currentOccurences[occurance.decorationId] = occurance;
			}
		});
	}

202
	private onEditorKeyDown(e: IKeyboardEvent): void {
E
Erich Gamma 已提交
203 204 205 206 207
		if (e.keyCode === LinkDetector.TRIGGER_KEY_VALUE && this.lastMouseEvent) {
			this.onEditorMouseMove(this.lastMouseEvent, e);
		}
	}

208
	private onEditorKeyUp(e: IKeyboardEvent): void {
E
Erich Gamma 已提交
209 210 211 212 213
		if (e.keyCode === LinkDetector.TRIGGER_KEY_VALUE) {
			this.cleanUpActiveLinkDecoration();
		}
	}

214
	private onEditorMouseMove(mouseEvent: IEditorMouseEvent, withKey?: IKeyboardEvent): void {
E
Erich Gamma 已提交
215 216 217 218 219 220
		this.lastMouseEvent = mouseEvent;

		if (this.isEnabled(mouseEvent, withKey)) {
			this.cleanUpActiveLinkDecoration(); // always remove previous link decoration as their can only be one
			var occurence = this.getLinkOccurence(mouseEvent.target.position);
			if (occurence) {
221
				this.editor.changeDecorations((changeAccessor) => {
E
Erich Gamma 已提交
222 223 224 225 226 227 228 229 230
					occurence.activate(changeAccessor);
					this.activeLinkDecorationId = occurence.decorationId;
				});
			}
		} else {
			this.cleanUpActiveLinkDecoration();
		}
	}

231
	private cleanUpActiveLinkDecoration(): void {
E
Erich Gamma 已提交
232 233 234
		if (this.activeLinkDecorationId) {
			var occurence = this.currentOccurences[this.activeLinkDecorationId];
			if (occurence) {
235
				this.editor.changeDecorations((changeAccessor) => {
E
Erich Gamma 已提交
236 237 238 239 240 241 242 243
					occurence.deactivate(changeAccessor);
				});
			}

			this.activeLinkDecorationId = null;
		}
	}

244
	private onEditorMouseUp(mouseEvent: IEditorMouseEvent): void {
E
Erich Gamma 已提交
245 246 247 248 249 250 251 252 253 254
		if (!this.isEnabled(mouseEvent)) {
			return;
		}
		var occurence = this.getLinkOccurence(mouseEvent.target.position);
		if (!occurence) {
			return;
		}
		this.openLinkOccurence(occurence, mouseEvent.event.altKey);
	}

255
	public openLinkOccurence(occurence: LinkOccurence, openToSide: boolean): void {
E
Erich Gamma 已提交
256

257
		if (!this.openerService) {
E
Erich Gamma 已提交
258 259 260
			return;
		}

261 262 263 264 265
		const {link} = occurence;

		link.resolve().then(uri => {
			// open the uri
			return this.openerService.open(uri, { openToSide });
E
Erich Gamma 已提交
266

267 268 269 270 271 272 273 274 275
		}, err => {
			// different error cases
			if (err === 'invalid') {
				this.messageService.show(Severity.Warning, nls.localize('invalid.url', 'Sorry, failed to open this link because it is not well-formed: {0}', link.url));
			} else if (err === 'missing') {
				this.messageService.show(Severity.Warning, nls.localize('missing.url', 'Sorry, failed to open this link because its target is missing.'));
			} else {
				onUnexpectedError(err);
			}
B
Benjamin Pasero 已提交
276
		}).done(null, onUnexpectedError);
E
Erich Gamma 已提交
277 278
	}

A
Alex Dima 已提交
279
	public getLinkOccurence(position: editorCommon.IPosition): LinkOccurence {
E
Erich Gamma 已提交
280 281 282 283 284
		var decorations = this.editor.getModel().getDecorationsInRange({
			startLineNumber: position.lineNumber,
			startColumn: position.column,
			endLineNumber: position.lineNumber,
			endColumn: position.column
285
		}, 0, true);
E
Erich Gamma 已提交
286 287 288 289 290 291 292 293 294 295 296 297

		for (var i = 0; i < decorations.length; i++) {
			var decoration = decorations[i];
			var currentOccurence = this.currentOccurences[decoration.id];
			if (currentOccurence) {
				return currentOccurence;
			}
		}

		return null;
	}

298 299 300
	private isEnabled(mouseEvent: IEditorMouseEvent, withKey?: IKeyboardEvent): boolean {
		return mouseEvent.target.type === editorCommon.MouseTargetType.CONTENT_TEXT &&
			(mouseEvent.event[LinkDetector.TRIGGER_MODIFIER] || (withKey && withKey.keyCode === LinkDetector.TRIGGER_KEY_VALUE));
E
Erich Gamma 已提交
301 302
	}

303
	private stop(): void {
E
Erich Gamma 已提交
304 305 306 307 308 309 310 311 312 313
		if (this.timeoutPromise) {
			this.timeoutPromise.cancel();
			this.timeoutPromise = null;
		}
		if (this.computePromise) {
			this.computePromise.cancel();
			this.computePromise = null;
		}
	}

314
	public dispose(): void {
A
Alex Dima 已提交
315
		this.listenersToRemove = dispose(this.listenersToRemove);
E
Erich Gamma 已提交
316 317 318 319
		this.stop();
	}
}

A
Alex Dima 已提交
320
@editorAction
A
Alex Dima 已提交
321
class OpenLinkAction extends EditorAction {
E
Erich Gamma 已提交
322

A
Alex Dima 已提交
323
	constructor() {
324 325 326 327 328 329
		super({
			id: 'editor.action.openLink',
			label: nls.localize('label', "Open Link"),
			alias: 'Open Link',
			precondition: null
		});
E
Erich Gamma 已提交
330 331
	}

J
Johannes Rieken 已提交
332
	public run(accessor: ServicesAccessor, editor: editorCommon.ICommonCodeEditor): void {
A
Alex Dima 已提交
333
		let linkDetector = LinkDetector.get(editor);
334 335 336 337
		if (!linkDetector) {
			return;
		}

A
Alex Dima 已提交
338
		let link = linkDetector.getLinkOccurence(editor.getPosition());
339
		if (link) {
A
Alex Dima 已提交
340
			linkDetector.openLinkOccurence(link, false);
E
Erich Gamma 已提交
341 342 343
		}
	}
}