From 20bae07036926a7548562c82d4dcf640a6598bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 15 Jun 2018 13:16:28 +0200 Subject: [PATCH] Improve IDE language detection logic --- src/app/ide/ide.component.ts | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/app/ide/ide.component.ts b/src/app/ide/ide.component.ts index efdf28d..a4bd127 100644 --- a/src/app/ide/ide.component.ts +++ b/src/app/ide/ide.component.ts @@ -10,6 +10,7 @@ import { DeploymentNotificationService } from '../services/deployment-notificati import { config } from '../config'; import { CommandMessage } from '../message-types/command-message'; import { LanguageMap } from './language-map'; +import { filter, first } from 'rxjs/operators'; enum DeployButtonState { @@ -55,11 +56,13 @@ export class IdeComponent implements OnInit { @Output() newLogs = new EventEmitter(); - command_handlers = {'reload': this.reloadHandler.bind(this), - 'read': this.readHandler.bind(this), - 'select': this.selectHandler.bind(this), - 'write': this.writeHandler.bind(this), - 'selectdir': this.selectdirHandler.bind(this)}; + command_handlers = { + 'reload': this.reloadHandler.bind(this), + 'read': this.readHandler.bind(this), + 'select': this.selectHandler.bind(this), + 'write': this.writeHandler.bind(this), + 'selectdir': this.selectdirHandler.bind(this) + }; constructor(private webSocketService: WebSocketService, private changeDetectorRef: ChangeDetectorRef, @@ -69,6 +72,7 @@ export class IdeComponent implements OnInit { ngOnInit() { this.webSocketService.connect(); this.subscribeWS(); + this.subscribeFirstLanguageDetection(); this.requestCode(); this.initProcessManagerService(); this.resetAutoSaveCountdown(); @@ -81,6 +85,15 @@ export class IdeComponent implements OnInit { }); } + subscribeFirstLanguageDetection() { + this.webSocketService.observeKey(this.key_id).pipe( + filter(message => message.data.command === 'read'), + first() + ).subscribe( + () => this.autoDetectEditorLanguageIfEnabled(this.filename) + ); + } + initProcessManagerService() { this.processManagerService.init(); this.processManagerService.subscribeCallback( @@ -101,7 +114,7 @@ export class IdeComponent implements OnInit { this.processManagerService.subscribeErrorCallback( config.ide.deployProcessName, - (event) => this.setDeployButtonState(DeployButtonState.FAILED) + () => this.setDeployButtonState(DeployButtonState.FAILED) ); } @@ -114,6 +127,7 @@ export class IdeComponent implements OnInit { selectHandler(data: IDECommand) { this.updateFileData(data); + this.autoDetectEditorLanguageIfEnabled(this.filename); } reloadHandler(data: CommandMessage) { @@ -124,9 +138,6 @@ export class IdeComponent implements OnInit { if (this.codeState === CodeState.SAVED) { this.updateFileData(data); } - if (config.ide.autoDetectFileLanguage) { - this.autoDetectEditorLanguage(data.filename); - } } writeHandler(data: CommandMessage) { @@ -137,7 +148,10 @@ export class IdeComponent implements OnInit { this.updateFileData(data); } - autoDetectEditorLanguage(filename: string) { + autoDetectEditorLanguageIfEnabled(filename: string) { + if (!config.ide.autoDetectFileLanguage) { + return; + } const extension = filename.substr(filename.lastIndexOf('.') + 1); let language = LanguageMap[extension]; language = (language === undefined) ? 'text' : language;