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 { 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<any>();
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<CommandMessage>(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;