Skip to content

Commit

Permalink
[plugin] fix #5753: redetect languages on new grammar
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <[email protected]>
  • Loading branch information
akosyakov committed Jul 19, 2019
1 parent 3587c23 commit f6f0e3d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
15 changes: 14 additions & 1 deletion packages/monaco/src/browser/monaco-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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<void> {
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 {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]>();

@inject(EditorManager)
private readonly editorManager: EditorManager;

@inject(TextmateRegistry)
private readonly grammarsRegistry: TextmateRegistry;

Expand Down Expand Up @@ -109,7 +114,7 @@ export class PluginContributionHandler {
}
}

if (contributions.grammars) {
if (contributions.grammars && contributions.grammars.length) {
for (const grammar of contributions.grammars) {
if (grammar.injectTo) {
for (const injectScope of grammar.injectTo) {
Expand Down Expand Up @@ -141,6 +146,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();
}
}
}

if (contributions.viewsContainers) {
Expand Down

0 comments on commit f6f0e3d

Please sign in to comment.