From 2f4d29158999a057c8658771b13a85f39957aa3a Mon Sep 17 00:00:00 2001 From: Anton Kosyakov Date: Fri, 19 Jul 2019 09:53:27 +0000 Subject: [PATCH] [plugin] fix #5753: redetect languages on new grammar Signed-off-by: Anton Kosyakov --- CHANGELOG.md | 1 + packages/monaco/src/browser/monaco-editor.ts | 15 ++++++++++++++- .../main/browser/plugin-contribution-handler.ts | 10 ++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe1ad73c7fb7d..3e2735fe794e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [plugin] added support of debug activation events [#5645](https://github.com/theia-ide/theia/pull/5645) - [security] Bump lodash.mergewith from 4.6.1 to 4.6.2 - [plugin] Fixed `Converting circular structure to JSON` Error [#5661](https://github.com/theia-ide/theia/pull/5661) +- [plugin] fixed auto detection of new languages [#5753](https://github.com/theia-ide/theia/issues/5753) Breaking changes: diff --git a/packages/monaco/src/browser/monaco-editor.ts b/packages/monaco/src/browser/monaco-editor.ts index 68f6f7ad4d000..eeb8dfdec69c7 100644 --- a/packages/monaco/src/browser/monaco-editor.ts +++ b/packages/monaco/src/browser/monaco-editor.ts @@ -109,7 +109,7 @@ export class MonacoEditor implements TextEditor { protected addHandlers(codeEditor: IStandaloneCodeEditor): void { this.toDispose.push(codeEditor.onDidChangeModelLanguage(e => - this.onLanguageChangedEmitter.fire(e.newLanguage) + this.fireLanguageChanged(e.newLanguage) )); this.toDispose.push(codeEditor.onDidChangeConfiguration(() => this.refresh())); this.toDispose.push(codeEditor.onDidChangeModel(() => this.refresh())); @@ -447,12 +447,20 @@ export class MonacoEditor implements TextEditor { this.editor.restoreViewState(state as monaco.editor.ICodeEditorViewState); } + /* `true` because it is derived from an URI during the instantiation */ + protected _languageAutoDetected = true; + + get languageAutoDeteceted(): boolean { + return this._languageAutoDetected; + } + async detectLanguage(): Promise { const filename = this.uri.path.toString(); const modeService = monaco.services.StaticServices.modeService.get(); const firstLine = this.document.textEditorModel.getLineContent(1); const mode = await modeService.getOrCreateModeByFilenameOrFirstLine(filename, firstLine); this.setLanguage(mode.getId()); + this._languageAutoDetected = true; } setLanguage(languageId: string): void { @@ -461,6 +469,11 @@ export class MonacoEditor implements TextEditor { } } + protected fireLanguageChanged(langaugeId: string): void { + this._languageAutoDetected = false; + this.onLanguageChangedEmitter.fire(langaugeId); + } + getResourceUri(): URI { return this.uri; } diff --git a/packages/plugin-ext/src/main/browser/plugin-contribution-handler.ts b/packages/plugin-ext/src/main/browser/plugin-contribution-handler.ts index 7019d13fa62bf..a363e65c6bb1c 100644 --- a/packages/plugin-ext/src/main/browser/plugin-contribution-handler.ts +++ b/packages/plugin-ext/src/main/browser/plugin-contribution-handler.ts @@ -29,12 +29,17 @@ import { CommandRegistry, Command, CommandHandler } from '@theia/core/lib/common import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable'; import { Emitter } from '@theia/core/lib/common/event'; import { TaskDefinitionRegistry, ProblemMatcherRegistry, ProblemPatternRegistry } from '@theia/task/lib/browser'; +import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor'; +import { EditorManager } from '@theia/editor/lib/browser'; @injectable() export class PluginContributionHandler { private injections = new Map(); + @inject(EditorManager) + private readonly editorManager: EditorManager; + @inject(TextmateRegistry) private readonly grammarsRegistry: TextmateRegistry; @@ -140,6 +145,11 @@ export class PluginContributionHandler { }); monaco.languages.onLanguage(grammar.language, () => this.monacoTextmateService.activateLanguage(grammar.language!)); } + for (const editor of MonacoEditor.getAll(this.editorManager)) { + if (editor.languageAutoDeteceted) { + editor.detectLanguage(); + } + } } }