From 7c13230aff2e56a53a113f96656ce6efbb62fcdb Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 30 Nov 2017 16:37:23 -0800 Subject: [PATCH] Setup basic typescript.isManagedFile context --- extensions/typescript/package.json | 14 ++--- extensions/typescript/src/extension.ts | 3 ++ .../typescript/src/features/taskProvider.ts | 2 +- .../src/utils/managedFileContext.ts | 51 +++++++++++++++++++ 4 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 extensions/typescript/src/utils/managedFileContext.ts diff --git a/extensions/typescript/package.json b/extensions/typescript/package.json index 81d96290eaf..f5fd312d2ba 100644 --- a/extensions/typescript/package.json +++ b/extensions/typescript/package.json @@ -483,23 +483,23 @@ "commandPalette": [ { "command": "typescript.reloadProjects", - "when": "editorLangId == 'typescript'" + "when": "editorLangId == typescript && typescript.isManagedFile" }, { "command": "typescript.reloadProjects", - "when": "editorLangId == typescriptreact" + "when": "editorLangId == typescriptreact && typescript.isManagedFile" }, { "command": "javascript.reloadProjects", - "when": "editorLangId == 'javascript'" + "when": "editorLangId == javascript && typescript.isManagedFile" }, { "command": "javascript.reloadProjects", - "when": "editorLangId == javascriptreact" + "when": "editorLangId == javascriptreact && typescript.isManagedFile" }, { "command": "typescript.goToProjectConfig", - "when": "editorLangId == 'typescript'" + "when": "editorLangId == typescript && typescript.isManagedFile" }, { "command": "typescript.goToProjectConfig", @@ -507,11 +507,11 @@ }, { "command": "javascript.goToProjectConfig", - "when": "editorLangId == 'javascript'" + "when": "editorLangId == javascript && typescript.isManagedFile" }, { "command": "javascript.goToProjectConfig", - "when": "editorLangId == javascriptreact" + "when": "editorLangId == javascriptreact && typescript.isManagedFile" } ] }, diff --git a/extensions/typescript/src/extension.ts b/extensions/typescript/src/extension.ts index e07d7524799..66dbe6e8191 100644 --- a/extensions/typescript/src/extension.ts +++ b/extensions/typescript/src/extension.ts @@ -14,6 +14,7 @@ import * as ProjectStatus from './utils/projectStatus'; import * as languageModeIds from './utils/languageModeIds'; import * as languageConfigurations from './utils/languageConfigurations'; import { standardLanguageDescriptions } from './utils/languageDescription'; +import ManagedFileContextManager from './utils/managedFileContext'; export function activate( context: vscode.ExtensionContext @@ -25,10 +26,12 @@ export function activate( const lazyClientHost = createLazyClientHost(context, plugins, commandManager); + context.subscriptions.push(new ManagedFileContextManager(resource => lazyClientHost().serviceClient.normalizePath(resource))); registerCommands(commandManager, lazyClientHost); context.subscriptions.push(new TypeScriptTaskProviderManager(() => lazyClientHost().serviceClient)); context.subscriptions.push(vscode.languages.setLanguageConfiguration(languageModeIds.jsxTags, languageConfigurations.jsxTags)); + const supportedLanguage = [].concat.apply([], standardLanguageDescriptions.map(x => x.modeIds).concat(plugins.map(x => x.languages))); function didOpenTextDocument(textDocument: vscode.TextDocument): boolean { if (supportedLanguage.indexOf(textDocument.languageId) >= 0) { diff --git a/extensions/typescript/src/features/taskProvider.ts b/extensions/typescript/src/features/taskProvider.ts index 9f59f171d40..452fcec9e08 100644 --- a/extensions/typescript/src/features/taskProvider.ts +++ b/extensions/typescript/src/features/taskProvider.ts @@ -235,7 +235,7 @@ class TscTaskProvider implements vscode.TaskProvider { } /** - * Manages registrations of TypeScript task provides with VScode. + * Manages registrations of TypeScript task providers with VS Code. */ export default class TypeScriptTaskProviderManager { private taskProviderSub: vscode.Disposable | undefined = undefined; diff --git a/extensions/typescript/src/utils/managedFileContext.ts b/extensions/typescript/src/utils/managedFileContext.ts new file mode 100644 index 00000000000..a4aeca7dfae --- /dev/null +++ b/extensions/typescript/src/utils/managedFileContext.ts @@ -0,0 +1,51 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as vscode from 'vscode'; +import * as languageModeIds from './languageModeIds'; + +/** + * When clause context set when the current file is managed by vscode's built-in typescript extension. + */ +const isManagedFile_contextName = 'typescript.isManagedFile'; + +export default class ManagedFileContextManager { + + private isInManagedFileContext: boolean = false; + + private readonly onDidChangeActiveTextEditorSub: vscode.Disposable; + + public constructor( + private readonly normalizePath: (resource: vscode.Uri) => string | null + ) { + this.onDidChangeActiveTextEditorSub = vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this); + + if (vscode.window.activeTextEditor) { + this.onDidChangeActiveTextEditor(vscode.window.activeTextEditor); + } + } + + public dispose() { + this.onDidChangeActiveTextEditorSub.dispose(); + } + + private onDidChangeActiveTextEditor(editor: vscode.TextEditor): any { + const isManagedFile = isSupportedLanguageMode(editor.document) && this.normalizePath(editor.document.uri) !== null; + this.updateContext(isManagedFile); + } + + private updateContext(newValue: boolean) { + if (newValue === this.isInManagedFileContext) { + return; + } + + vscode.commands.executeCommand('setContext', isManagedFile_contextName, newValue); + this.isInManagedFileContext = newValue; + } +} + +function isSupportedLanguageMode(doc: vscode.TextDocument) { + return vscode.languages.match([languageModeIds.typescript, languageModeIds.typescriptreact, languageModeIds.javascript, languageModeIds.javascript], doc) > 0; +}