|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as nls from 'vs/nls'; |
| 7 | +import * as path from 'vs/base/common/path'; |
| 8 | +import { Disposable, DisposableStore } from 'vs/base/common/lifecycle'; |
| 9 | +import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; |
| 10 | +import { registerEditorContribution } from 'vs/editor/browser/editorExtensions'; |
| 11 | +import { IEditorContribution } from 'vs/editor/common/editorCommon'; |
| 12 | +import { INotificationService, Severity } from 'vs/platform/notification/common/notification'; |
| 13 | +import { IOpenerService } from 'vs/platform/opener/common/opener'; |
| 14 | +import { URI } from 'vs/base/common/uri'; |
| 15 | +import { ITextModel } from 'vs/editor/common/model'; |
| 16 | +import { IWorkbenchThemeService } from 'vs/workbench/services/themes/common/workbenchThemeService'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Shows a message when semantic tokens are shown the first time. |
| 20 | + */ |
| 21 | +export class SemanticTokensHelp extends Disposable implements IEditorContribution { |
| 22 | + |
| 23 | + public static readonly ID = 'editor.contrib.semanticHighlightHelp'; |
| 24 | + |
| 25 | + constructor( |
| 26 | + _editor: ICodeEditor, |
| 27 | + @INotificationService _notificationService: INotificationService, |
| 28 | + @IOpenerService _openerService: IOpenerService, |
| 29 | + @IWorkbenchThemeService _themeService: IWorkbenchThemeService |
| 30 | + ) { |
| 31 | + super(); |
| 32 | + |
| 33 | + const toDispose = this._register(new DisposableStore()); |
| 34 | + const localToDispose = toDispose.add(new DisposableStore()); |
| 35 | + const installChangeTokenListener = (model: ITextModel) => { |
| 36 | + localToDispose.add(model.onDidChangeTokens((e) => { |
| 37 | + if (!e.semanticTokensApplied) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + toDispose.dispose(); // uninstall all listeners, makes sure the notification is only shown once per window |
| 42 | + |
| 43 | + const message = nls.localize( |
| 44 | + { |
| 45 | + key: 'semanticTokensHelp', |
| 46 | + comment: [ |
| 47 | + 'Variable 0 will be a file name.', |
| 48 | + 'Variable 1 will be a theme name.' |
| 49 | + ] |
| 50 | + }, |
| 51 | + "Semantic highlighting has been applied to '{0}' as the theme '{1}' has semantic highlighting enabled.", |
| 52 | + path.basename(model.uri.path), _themeService.getColorTheme().label |
| 53 | + ); |
| 54 | + |
| 55 | + _notificationService.prompt(Severity.Info, message, [ |
| 56 | + { |
| 57 | + label: nls.localize('learnMoreButton', "Learn More"), |
| 58 | + run: () => { |
| 59 | + const url = 'https://go.microsoft.com/fwlink/?linkid=2122588'; |
| 60 | + |
| 61 | + _openerService.open(URI.parse(url)); |
| 62 | + } |
| 63 | + } |
| 64 | + ], { neverShowAgain: { id: 'editor.contrib.semanticTokensHelp' } }); |
| 65 | + })); |
| 66 | + }; |
| 67 | + |
| 68 | + |
| 69 | + const model = _editor.getModel(); |
| 70 | + if (model !== null) { |
| 71 | + installChangeTokenListener(model); |
| 72 | + } |
| 73 | + |
| 74 | + toDispose.add(_editor.onDidChangeModel((e) => { |
| 75 | + localToDispose.clear(); |
| 76 | + |
| 77 | + const model = _editor.getModel(); |
| 78 | + if (!model) { |
| 79 | + return; |
| 80 | + } |
| 81 | + installChangeTokenListener(model); |
| 82 | + })); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +registerEditorContribution(SemanticTokensHelp.ID, SemanticTokensHelp); |
0 commit comments