Newton-Editor/src/app/editor/lsp-manager/lsp-manager.component.ts

154 lines
4.6 KiB
TypeScript
Raw Normal View History

2025-07-07 02:40:24 +00:00
import { Component, ChangeDetectorRef, ElementRef, HostBinding, ViewChild, inject } from '@angular/core';
2025-07-05 20:21:08 +00:00
import { Subject, takeUntil } from 'rxjs';
import { LspManagerService } from '../../common/services/editor/lsp-manager/lsp-manager.service';
import { CodeViewComponent } from '../code-view/view.component';
2025-07-05 20:21:08 +00:00
import { ServiceMessage } from '../../common/types/service-message.type';
@Component({
selector: 'lsp-manager',
standalone: true,
imports: [
CodeViewComponent
2025-07-05 20:21:08 +00:00
],
templateUrl: './lsp-manager.component.html',
styleUrl: './lsp-manager.component.css',
host: {
'class': 'lsp-manager',
"(keyup)": "globalLspManagerKeyHandler($event)"
}
})
export class LspManagerComponent {
2025-07-07 02:40:24 +00:00
private unsubscribe: Subject<void> = new Subject();
private changeDetectorRef: ChangeDetectorRef = inject(ChangeDetectorRef);
2025-07-05 20:21:08 +00:00
2025-07-07 02:40:24 +00:00
lspManagerService: LspManagerService = inject(LspManagerService);
2025-07-05 20:21:08 +00:00
@HostBinding("class.hidden") isHidden: boolean = true;
2025-07-07 02:40:24 +00:00
@ViewChild('lspEditorComponent') lspEditorComponent!: CodeViewComponent;
@ViewChild('sessionEditorComponent') sessionEditorComponent!: CodeViewComponent;
lspTextEditor: any;
innerEditor: any;
editor: any;
2025-07-12 06:45:02 +00:00
activeFile: any;
2025-07-07 02:40:24 +00:00
2025-07-05 20:21:08 +00:00
constructor() {
}
private ngAfterViewInit(): void {
2025-07-12 06:45:02 +00:00
this.mapEditorsAndLoadConfig();
this.loadSubscribers();
}
private ngOnDestroy() {
this.unsubscribe.next();
this.unsubscribe.complete();
}
private mapEditorsAndLoadConfig() {
2025-07-07 02:40:24 +00:00
this.lspTextEditor = this.lspEditorComponent.editor;
this.innerEditor = this.sessionEditorComponent.editor;
this.lspManagerService.loadLspConfigData().then((lspConfigData) => {
this.lspTextEditor.session.setMode("ace/mode/json");
this.lspTextEditor.session.setValue(lspConfigData);
});
2025-07-05 20:21:08 +00:00
}
private loadSubscribers() {
this.lspManagerService.getMessage$().pipe(
takeUntil(this.unsubscribe)
).subscribe((message: ServiceMessage) => {
if (message.action === "toggle-lsp-manager") {
this.toggleLspManager(message);
} else if (message.action === "set-active-editor") {
this.setActiveEditor(message);
2025-07-12 06:45:02 +00:00
} else if (message.action === "editor-update") {
this.editorUpdate(message);
} else if (message.action === "close-file") {
this.closeFile(message);
2025-07-05 20:21:08 +00:00
}
});
}
2025-07-07 02:40:24 +00:00
public clearWorkspaceFolder() {
this.lspManagerService.workspaceFolder = "";
}
public setWorkspaceFolder() {
window.fs.chooseFolder().then((folder: string) => {
if (!folder) return;
this.lspManagerService.workspaceFolder = folder;
});
2025-07-05 20:21:08 +00:00
}
2025-07-12 06:45:02 +00:00
public createLanguageClient() {
let mode = this.lspManagerService.getMode(this.editor.session);
this.lspManagerService.createLanguageProviderWithClientServer(mode);
}
public registerEditorToLanguageClient() {
this.lspManagerService.registerEditorToLSPClient(this.editor);
this.lspManagerService.setSessionFilePath(
this.editor.session,
this.activeFile.path
);
}
2025-07-07 02:40:24 +00:00
2025-07-05 20:21:08 +00:00
public globalLspManagerKeyHandler(event: any) {
if (event.ctrlKey && event.shiftKey && event.key === "l") {
this.hideLspManager();
}
}
2025-07-07 02:40:24 +00:00
public hideLspManager() {
this.isHidden = true;
this.editor.focus();
}
2025-07-05 20:21:08 +00:00
private toggleLspManager(message: ServiceMessage) {
this.isHidden = !this.isHidden;
2025-07-07 02:40:24 +00:00
if (this.isHidden) return;
// Note: hack for issue with setActiveEditor TODO
setTimeout(() => {
this.innerEditor.setSession(this.editor.session);
}, 10);
2025-07-05 20:21:08 +00:00
}
private setActiveEditor(message: ServiceMessage) {
2025-07-12 06:45:02 +00:00
this.editor = message.rawData.editor;
this.activeFile = message.rawData.activeFile;
2025-07-07 02:40:24 +00:00
// TODO: figure out why this doesn't update the session consistently...
// It seems maybe bound to visible state as change detector ref didn't help either.
// this.innerEditor.setSession(this.editor.session);
2025-07-05 20:21:08 +00:00
}
2025-07-12 06:45:02 +00:00
private editorUpdate(message: ServiceMessage) {
this.editor.setSession(message.rawData.editor.getSession())
this.activeFile = message.rawData.activeFile;
this.lspManagerService.setSessionFilePath(
this.editor.session,
this.activeFile.path
);
}
private closeFile(message: ServiceMessage) {
this.lspManagerService.closeDocument(message.rawData);
}
2025-07-05 20:21:08 +00:00
}