Improve IDE language detection logic

This commit is contained in:
Kristóf Tóth 2018-06-15 13:16:28 +02:00
parent 2b4b09f60b
commit 20bae07036

View File

@ -10,6 +10,7 @@ import { DeploymentNotificationService } from '../services/deployment-notificati
import { config } from '../config'; import { config } from '../config';
import { CommandMessage } from '../message-types/command-message'; import { CommandMessage } from '../message-types/command-message';
import { LanguageMap } from './language-map'; import { LanguageMap } from './language-map';
import { filter, first } from 'rxjs/operators';
enum DeployButtonState { enum DeployButtonState {
@ -55,11 +56,13 @@ export class IdeComponent implements OnInit {
@Output() newLogs = new EventEmitter<any>(); @Output() newLogs = new EventEmitter<any>();
command_handlers = {'reload': this.reloadHandler.bind(this), command_handlers = {
'read': this.readHandler.bind(this), 'reload': this.reloadHandler.bind(this),
'select': this.selectHandler.bind(this), 'read': this.readHandler.bind(this),
'write': this.writeHandler.bind(this), 'select': this.selectHandler.bind(this),
'selectdir': this.selectdirHandler.bind(this)}; 'write': this.writeHandler.bind(this),
'selectdir': this.selectdirHandler.bind(this)
};
constructor(private webSocketService: WebSocketService, constructor(private webSocketService: WebSocketService,
private changeDetectorRef: ChangeDetectorRef, private changeDetectorRef: ChangeDetectorRef,
@ -69,6 +72,7 @@ export class IdeComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.webSocketService.connect(); this.webSocketService.connect();
this.subscribeWS(); this.subscribeWS();
this.subscribeFirstLanguageDetection();
this.requestCode(); this.requestCode();
this.initProcessManagerService(); this.initProcessManagerService();
this.resetAutoSaveCountdown(); this.resetAutoSaveCountdown();
@ -81,6 +85,15 @@ export class IdeComponent implements OnInit {
}); });
} }
subscribeFirstLanguageDetection() {
this.webSocketService.observeKey<CommandMessage>(this.key_id).pipe(
filter(message => message.data.command === 'read'),
first()
).subscribe(
() => this.autoDetectEditorLanguageIfEnabled(this.filename)
);
}
initProcessManagerService() { initProcessManagerService() {
this.processManagerService.init(); this.processManagerService.init();
this.processManagerService.subscribeCallback( this.processManagerService.subscribeCallback(
@ -101,7 +114,7 @@ export class IdeComponent implements OnInit {
this.processManagerService.subscribeErrorCallback( this.processManagerService.subscribeErrorCallback(
config.ide.deployProcessName, config.ide.deployProcessName,
(event) => this.setDeployButtonState(DeployButtonState.FAILED) () => this.setDeployButtonState(DeployButtonState.FAILED)
); );
} }
@ -114,6 +127,7 @@ export class IdeComponent implements OnInit {
selectHandler(data: IDECommand) { selectHandler(data: IDECommand) {
this.updateFileData(data); this.updateFileData(data);
this.autoDetectEditorLanguageIfEnabled(this.filename);
} }
reloadHandler(data: CommandMessage) { reloadHandler(data: CommandMessage) {
@ -124,9 +138,6 @@ export class IdeComponent implements OnInit {
if (this.codeState === CodeState.SAVED) { if (this.codeState === CodeState.SAVED) {
this.updateFileData(data); this.updateFileData(data);
} }
if (config.ide.autoDetectFileLanguage) {
this.autoDetectEditorLanguage(data.filename);
}
} }
writeHandler(data: CommandMessage) { writeHandler(data: CommandMessage) {
@ -137,7 +148,10 @@ export class IdeComponent implements OnInit {
this.updateFileData(data); this.updateFileData(data);
} }
autoDetectEditorLanguage(filename: string) { autoDetectEditorLanguageIfEnabled(filename: string) {
if (!config.ide.autoDetectFileLanguage) {
return;
}
const extension = filename.substr(filename.lastIndexOf('.') + 1); const extension = filename.substr(filename.lastIndexOf('.') + 1);
let language = LanguageMap[extension]; let language = LanguageMap[extension];
language = (language === undefined) ? 'text' : language; language = (language === undefined) ? 'text' : language;