123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var __param = (this && this.__param) || function (paramIndex, decorator) {
- return function (target, key) { decorator(target, key, paramIndex); }
- };
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- import * as async from '../../../base/common/async.js';
- import { CancellationToken } from '../../../base/common/cancellation.js';
- import { onUnexpectedError } from '../../../base/common/errors.js';
- import { MarkdownString } from '../../../base/common/htmlContent.js';
- import { DisposableStore } from '../../../base/common/lifecycle.js';
- import { Schemas } from '../../../base/common/network.js';
- import * as platform from '../../../base/common/platform.js';
- import * as resources from '../../../base/common/resources.js';
- import { URI } from '../../../base/common/uri.js';
- import './links.css';
- import { EditorAction, registerEditorAction, registerEditorContribution } from '../../browser/editorExtensions.js';
- import { ModelDecorationOptions } from '../../common/model/textModel.js';
- import { LinkProviderRegistry } from '../../common/modes.js';
- import { ClickLinkGesture } from '../gotoSymbol/link/clickLinkGesture.js';
- import { getLinks } from './getLinks.js';
- import * as nls from '../../../nls.js';
- import { INotificationService } from '../../../platform/notification/common/notification.js';
- import { IOpenerService } from '../../../platform/opener/common/opener.js';
- import { editorActiveLinkForeground } from '../../../platform/theme/common/colorRegistry.js';
- import { registerThemingParticipant } from '../../../platform/theme/common/themeService.js';
- function getHoverMessage(link, useMetaKey) {
- const executeCmd = link.url && /^command:/i.test(link.url.toString());
- const label = link.tooltip
- ? link.tooltip
- : executeCmd
- ? nls.localize('links.navigate.executeCmd', 'Execute command')
- : nls.localize('links.navigate.follow', 'Follow link');
- const kb = useMetaKey
- ? platform.isMacintosh
- ? nls.localize('links.navigate.kb.meta.mac', "cmd + click")
- : nls.localize('links.navigate.kb.meta', "ctrl + click")
- : platform.isMacintosh
- ? nls.localize('links.navigate.kb.alt.mac', "option + click")
- : nls.localize('links.navigate.kb.alt', "alt + click");
- if (link.url) {
- let nativeLabel = '';
- if (/^command:/i.test(link.url.toString())) {
- // Don't show complete command arguments in the native tooltip
- const match = link.url.toString().match(/^command:([^?#]+)/);
- if (match) {
- const commandId = match[1];
- const nativeLabelText = nls.localize('tooltip.explanation', "Execute command {0}", commandId);
- nativeLabel = ` "${nativeLabelText}"`;
- }
- }
- const hoverMessage = new MarkdownString('', true).appendMarkdown(`[${label}](${link.url.toString(true).replace(/ /g, '%20')}${nativeLabel}) (${kb})`);
- return hoverMessage;
- }
- else {
- return new MarkdownString().appendText(`${label} (${kb})`);
- }
- }
- const decoration = {
- general: ModelDecorationOptions.register({
- description: 'detected-link',
- stickiness: 1 /* NeverGrowsWhenTypingAtEdges */,
- collapseOnReplaceEdit: true,
- inlineClassName: 'detected-link'
- }),
- active: ModelDecorationOptions.register({
- description: 'detected-link-active',
- stickiness: 1 /* NeverGrowsWhenTypingAtEdges */,
- collapseOnReplaceEdit: true,
- inlineClassName: 'detected-link-active'
- })
- };
- class LinkOccurrence {
- constructor(link, decorationId) {
- this.link = link;
- this.decorationId = decorationId;
- }
- static decoration(link, useMetaKey) {
- return {
- range: link.range,
- options: LinkOccurrence._getOptions(link, useMetaKey, false)
- };
- }
- static _getOptions(link, useMetaKey, isActive) {
- const options = Object.assign({}, (isActive ? decoration.active : decoration.general));
- options.hoverMessage = getHoverMessage(link, useMetaKey);
- return options;
- }
- activate(changeAccessor, useMetaKey) {
- changeAccessor.changeDecorationOptions(this.decorationId, LinkOccurrence._getOptions(this.link, useMetaKey, true));
- }
- deactivate(changeAccessor, useMetaKey) {
- changeAccessor.changeDecorationOptions(this.decorationId, LinkOccurrence._getOptions(this.link, useMetaKey, false));
- }
- }
- let LinkDetector = class LinkDetector {
- constructor(editor, openerService, notificationService) {
- this.listenersToRemove = new DisposableStore();
- this.editor = editor;
- this.openerService = openerService;
- this.notificationService = notificationService;
- let clickLinkGesture = new ClickLinkGesture(editor);
- this.listenersToRemove.add(clickLinkGesture);
- this.listenersToRemove.add(clickLinkGesture.onMouseMoveOrRelevantKeyDown(([mouseEvent, keyboardEvent]) => {
- this._onEditorMouseMove(mouseEvent, keyboardEvent);
- }));
- this.listenersToRemove.add(clickLinkGesture.onExecute((e) => {
- this.onEditorMouseUp(e);
- }));
- this.listenersToRemove.add(clickLinkGesture.onCancel((e) => {
- this.cleanUpActiveLinkDecoration();
- }));
- this.enabled = editor.getOption(62 /* links */);
- this.listenersToRemove.add(editor.onDidChangeConfiguration((e) => {
- const enabled = editor.getOption(62 /* links */);
- if (this.enabled === enabled) {
- // No change in our configuration option
- return;
- }
- this.enabled = enabled;
- // Remove any links (for the getting disabled case)
- this.updateDecorations([]);
- // Stop any computation (for the getting disabled case)
- this.stop();
- // Start computing (for the getting enabled case)
- this.beginCompute();
- }));
- this.listenersToRemove.add(editor.onDidChangeModelContent((e) => this.onChange()));
- this.listenersToRemove.add(editor.onDidChangeModel((e) => this.onModelChanged()));
- this.listenersToRemove.add(editor.onDidChangeModelLanguage((e) => this.onModelModeChanged()));
- this.listenersToRemove.add(LinkProviderRegistry.onDidChange((e) => this.onModelModeChanged()));
- this.timeout = new async.TimeoutTimer();
- this.computePromise = null;
- this.activeLinksList = null;
- this.currentOccurrences = {};
- this.activeLinkDecorationId = null;
- this.beginCompute();
- }
- static get(editor) {
- return editor.getContribution(LinkDetector.ID);
- }
- onModelChanged() {
- this.currentOccurrences = {};
- this.activeLinkDecorationId = null;
- this.stop();
- this.beginCompute();
- }
- onModelModeChanged() {
- this.stop();
- this.beginCompute();
- }
- onChange() {
- this.timeout.setIfNotSet(() => this.beginCompute(), LinkDetector.RECOMPUTE_TIME);
- }
- beginCompute() {
- return __awaiter(this, void 0, void 0, function* () {
- if (!this.editor.hasModel() || !this.enabled) {
- return;
- }
- const model = this.editor.getModel();
- if (!LinkProviderRegistry.has(model)) {
- return;
- }
- if (this.activeLinksList) {
- this.activeLinksList.dispose();
- this.activeLinksList = null;
- }
- this.computePromise = async.createCancelablePromise(token => getLinks(model, token));
- try {
- this.activeLinksList = yield this.computePromise;
- this.updateDecorations(this.activeLinksList.links);
- }
- catch (err) {
- onUnexpectedError(err);
- }
- finally {
- this.computePromise = null;
- }
- });
- }
- updateDecorations(links) {
- const useMetaKey = (this.editor.getOption(69 /* multiCursorModifier */) === 'altKey');
- let oldDecorations = [];
- let keys = Object.keys(this.currentOccurrences);
- for (let i = 0, len = keys.length; i < len; i++) {
- let decorationId = keys[i];
- let occurance = this.currentOccurrences[decorationId];
- oldDecorations.push(occurance.decorationId);
- }
- let newDecorations = [];
- if (links) {
- // Not sure why this is sometimes null
- for (const link of links) {
- newDecorations.push(LinkOccurrence.decoration(link, useMetaKey));
- }
- }
- let decorations = this.editor.deltaDecorations(oldDecorations, newDecorations);
- this.currentOccurrences = {};
- this.activeLinkDecorationId = null;
- for (let i = 0, len = decorations.length; i < len; i++) {
- let occurance = new LinkOccurrence(links[i], decorations[i]);
- this.currentOccurrences[occurance.decorationId] = occurance;
- }
- }
- _onEditorMouseMove(mouseEvent, withKey) {
- const useMetaKey = (this.editor.getOption(69 /* multiCursorModifier */) === 'altKey');
- if (this.isEnabled(mouseEvent, withKey)) {
- this.cleanUpActiveLinkDecoration(); // always remove previous link decoration as their can only be one
- const occurrence = this.getLinkOccurrence(mouseEvent.target.position);
- if (occurrence) {
- this.editor.changeDecorations((changeAccessor) => {
- occurrence.activate(changeAccessor, useMetaKey);
- this.activeLinkDecorationId = occurrence.decorationId;
- });
- }
- }
- else {
- this.cleanUpActiveLinkDecoration();
- }
- }
- cleanUpActiveLinkDecoration() {
- const useMetaKey = (this.editor.getOption(69 /* multiCursorModifier */) === 'altKey');
- if (this.activeLinkDecorationId) {
- const occurrence = this.currentOccurrences[this.activeLinkDecorationId];
- if (occurrence) {
- this.editor.changeDecorations((changeAccessor) => {
- occurrence.deactivate(changeAccessor, useMetaKey);
- });
- }
- this.activeLinkDecorationId = null;
- }
- }
- onEditorMouseUp(mouseEvent) {
- if (!this.isEnabled(mouseEvent)) {
- return;
- }
- const occurrence = this.getLinkOccurrence(mouseEvent.target.position);
- if (!occurrence) {
- return;
- }
- this.openLinkOccurrence(occurrence, mouseEvent.hasSideBySideModifier, true /* from user gesture */);
- }
- openLinkOccurrence(occurrence, openToSide, fromUserGesture = false) {
- if (!this.openerService) {
- return;
- }
- const { link } = occurrence;
- link.resolve(CancellationToken.None).then(uri => {
- // Support for relative file URIs of the shape file://./relativeFile.txt or file:///./relativeFile.txt
- if (typeof uri === 'string' && this.editor.hasModel()) {
- const modelUri = this.editor.getModel().uri;
- if (modelUri.scheme === Schemas.file && uri.startsWith(`${Schemas.file}:`)) {
- const parsedUri = URI.parse(uri);
- if (parsedUri.scheme === Schemas.file) {
- const fsPath = resources.originalFSPath(parsedUri);
- let relativePath = null;
- if (fsPath.startsWith('/./')) {
- relativePath = `.${fsPath.substr(1)}`;
- }
- else if (fsPath.startsWith('//./')) {
- relativePath = `.${fsPath.substr(2)}`;
- }
- if (relativePath) {
- uri = resources.joinPath(modelUri, relativePath);
- }
- }
- }
- }
- return this.openerService.open(uri, { openToSide, fromUserGesture, allowContributedOpeners: true, allowCommands: true });
- }, err => {
- const messageOrError = err instanceof Error ? err.message : err;
- // different error cases
- if (messageOrError === 'invalid') {
- this.notificationService.warn(nls.localize('invalid.url', 'Failed to open this link because it is not well-formed: {0}', link.url.toString()));
- }
- else if (messageOrError === 'missing') {
- this.notificationService.warn(nls.localize('missing.url', 'Failed to open this link because its target is missing.'));
- }
- else {
- onUnexpectedError(err);
- }
- });
- }
- getLinkOccurrence(position) {
- if (!this.editor.hasModel() || !position) {
- return null;
- }
- const decorations = this.editor.getModel().getDecorationsInRange({
- startLineNumber: position.lineNumber,
- startColumn: position.column,
- endLineNumber: position.lineNumber,
- endColumn: position.column
- }, 0, true);
- for (const decoration of decorations) {
- const currentOccurrence = this.currentOccurrences[decoration.id];
- if (currentOccurrence) {
- return currentOccurrence;
- }
- }
- return null;
- }
- isEnabled(mouseEvent, withKey) {
- return Boolean((mouseEvent.target.type === 6 /* CONTENT_TEXT */)
- && (mouseEvent.hasTriggerModifier || (withKey && withKey.keyCodeIsTriggerKey)));
- }
- stop() {
- var _a;
- this.timeout.cancel();
- if (this.activeLinksList) {
- (_a = this.activeLinksList) === null || _a === void 0 ? void 0 : _a.dispose();
- this.activeLinksList = null;
- }
- if (this.computePromise) {
- this.computePromise.cancel();
- this.computePromise = null;
- }
- }
- dispose() {
- this.listenersToRemove.dispose();
- this.stop();
- this.timeout.dispose();
- }
- };
- LinkDetector.ID = 'editor.linkDetector';
- LinkDetector.RECOMPUTE_TIME = 1000; // ms
- LinkDetector = __decorate([
- __param(1, IOpenerService),
- __param(2, INotificationService)
- ], LinkDetector);
- export { LinkDetector };
- class OpenLinkAction extends EditorAction {
- constructor() {
- super({
- id: 'editor.action.openLink',
- label: nls.localize('label', "Open Link"),
- alias: 'Open Link',
- precondition: undefined
- });
- }
- run(accessor, editor) {
- let linkDetector = LinkDetector.get(editor);
- if (!linkDetector) {
- return;
- }
- if (!editor.hasModel()) {
- return;
- }
- let selections = editor.getSelections();
- for (let sel of selections) {
- let link = linkDetector.getLinkOccurrence(sel.getEndPosition());
- if (link) {
- linkDetector.openLinkOccurrence(link, false);
- }
- }
- }
- }
- registerEditorContribution(LinkDetector.ID, LinkDetector);
- registerEditorAction(OpenLinkAction);
- registerThemingParticipant((theme, collector) => {
- const activeLinkForeground = theme.getColor(editorActiveLinkForeground);
- if (activeLinkForeground) {
- collector.addRule(`.monaco-editor .detected-link-active { color: ${activeLinkForeground} !important; }`);
- }
- });
|