refactoring LSP to its own service
This commit is contained in:
parent
2a9be75835
commit
fc7b728fe4
@ -4,19 +4,30 @@ import { BehaviorSubject, ReplaySubject, Observable } from 'rxjs';
|
|||||||
import { ServiceMessage } from '../../types/service-message.type';
|
import { ServiceMessage } from '../../types/service-message.type';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class EditorsService {
|
export class EditorsService {
|
||||||
private dataSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1);
|
private messageSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1);
|
||||||
|
private activationSubject: ReplaySubject<string> = new ReplaySubject<string>(1);
|
||||||
|
|
||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
setData(data: ServiceMessage): void {
|
setData(data: ServiceMessage): void {
|
||||||
this.dataSubject.next(data);
|
this.messageSubject.next(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
getData$(): Observable<ServiceMessage> {
|
getData$(): Observable<ServiceMessage> {
|
||||||
return this.dataSubject.asObservable();
|
return this.messageSubject.asObservable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setActiveEditor(data: string): void {
|
||||||
|
this.activationSubject.next(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
newActiveEditor$(): Observable<string> {
|
||||||
|
return this.activationSubject.asObservable();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
77
src/app/common/services/lsp.service.ts
Normal file
77
src/app/common/services/lsp.service.ts
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { BehaviorSubject, ReplaySubject, Observable } from 'rxjs';
|
||||||
|
|
||||||
|
import { AceLanguageClient, LanguageClientConfig } from 'ace-linters/build/ace-language-client';
|
||||||
|
import { LanguageProvider } from "ace-linters";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class LSPService {
|
||||||
|
lspConfigData!: {};
|
||||||
|
languageProvider!: any;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.loadLSPService();
|
||||||
|
}
|
||||||
|
|
||||||
|
private loadLSPService() {
|
||||||
|
this.getLspConfigData().then((lspConfigData: string) => {
|
||||||
|
this.lspConfigData = JSON.parse(lspConfigData);
|
||||||
|
|
||||||
|
if (this.lspConfigData["message"]) {
|
||||||
|
console.log(
|
||||||
|
"Warning: LSP this.lspConfigData is a 'message'",
|
||||||
|
this.lspConfigData
|
||||||
|
);
|
||||||
|
|
||||||
|
this.lspConfigData = {};
|
||||||
|
}
|
||||||
|
}).then(() => {
|
||||||
|
this.loadLanguageProviders();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public registerEditor(editor: any): void {
|
||||||
|
this.languageProvider.registerEditor(editor);
|
||||||
|
}
|
||||||
|
|
||||||
|
private getLspConfigData(): Promise<string> {
|
||||||
|
return window.fs.getLspConfigData();
|
||||||
|
}
|
||||||
|
|
||||||
|
private loadLanguageProviders(): void {
|
||||||
|
this.languageProvider = this.getLanguageProviderWithClientServers();
|
||||||
|
// this.languageProvider = this.getLanguageProviderWithWebWorker();
|
||||||
|
}
|
||||||
|
|
||||||
|
private getLanguageProviderWithClientServers() {
|
||||||
|
let _initializationOptions = {};
|
||||||
|
|
||||||
|
if (Object.keys(this.lspConfigData).length !== 0) {
|
||||||
|
// _initializationOptions = this.lspConfigData[ this.editor.session.getMode() ]["initialization-options"];
|
||||||
|
_initializationOptions = this.lspConfigData[ "python" ]["initialization-options"];
|
||||||
|
}
|
||||||
|
|
||||||
|
let servers: LanguageClientConfig[] = [
|
||||||
|
{
|
||||||
|
module: () => import("ace-linters/build/language-client"),
|
||||||
|
modes: "python",
|
||||||
|
type: "socket",
|
||||||
|
socket: new WebSocket("ws://127.0.0.1:9999/python"),
|
||||||
|
// socket: new WebSocket("ws://127.0.0.1:9999/?name=pylsp"),
|
||||||
|
initializationOptions: _initializationOptions
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
return AceLanguageClient.for(servers);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private getLanguageProviderWithWebWorker() {
|
||||||
|
let worker = new Worker(new URL('./webworker.js', import.meta.url));
|
||||||
|
return LanguageProvider.create(worker);
|
||||||
|
}
|
||||||
|
}
|
@ -1,18 +1,15 @@
|
|||||||
import { Component, ElementRef, ViewChild, Input } from '@angular/core';
|
import { Component, ElementRef, ViewChild, Input } from '@angular/core';
|
||||||
|
|
||||||
import { AceLanguageClient, LanguageClientConfig } from 'ace-linters/build/ace-language-client';
|
|
||||||
|
|
||||||
// Import Ace and its modes/themes so that `ace` global is defined
|
// Import Ace and its modes/themes so that `ace` global is defined
|
||||||
import * as ace from 'ace-builds/src-noconflict/ace';
|
import * as ace from 'ace-builds/src-noconflict/ace';
|
||||||
import 'ace-builds/src-noconflict/theme-one_dark';
|
import 'ace-builds/src-noconflict/theme-one_dark';
|
||||||
import "ace-builds/src-noconflict/ext-language_tools";
|
import "ace-builds/src-noconflict/ext-language_tools";
|
||||||
|
|
||||||
import { LanguageProvider } from "ace-linters";
|
|
||||||
|
|
||||||
import { EditorSettings } from "../../common/configs/editor.config";
|
import { EditorSettings } from "../../common/configs/editor.config";
|
||||||
|
import { ServiceMessage } from '../../common/types/service-message.type';
|
||||||
|
|
||||||
import { EditorsService } from '../../common/services/editor/editors.service';
|
import { EditorsService } from '../../common/services/editor/editors.service';
|
||||||
import { ServiceMessage } from '../../common/types/service-message.type';
|
import { LSPService } from '../../common/services/lsp.service';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -24,8 +21,7 @@ import { ServiceMessage } from '../../common/types/service-message.type';
|
|||||||
templateUrl: './ace-editor.component.html',
|
templateUrl: './ace-editor.component.html',
|
||||||
styleUrl: './ace-editor.component.css',
|
styleUrl: './ace-editor.component.css',
|
||||||
host: {
|
host: {
|
||||||
'class': 'col',
|
'class': 'col'
|
||||||
'(click)': 'onClick($event)'
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
export class AceEditorComponent {
|
export class AceEditorComponent {
|
||||||
@ -34,16 +30,16 @@ export class AceEditorComponent {
|
|||||||
@ViewChild('editor') editorElm!: ElementRef;
|
@ViewChild('editor') editorElm!: ElementRef;
|
||||||
editor!: any;
|
editor!: any;
|
||||||
uuid!: string;
|
uuid!: string;
|
||||||
lspConfigData!: {};
|
|
||||||
|
|
||||||
|
|
||||||
constructor(private editorsService: EditorsService) {
|
constructor(
|
||||||
}
|
private editorsService: EditorsService,
|
||||||
|
private lspService: LSPService
|
||||||
|
) {}
|
||||||
|
|
||||||
|
|
||||||
public ngAfterViewInit(): void {
|
public ngAfterViewInit(): void {
|
||||||
this.loadAce();
|
this.loadAce();
|
||||||
this.loadLanguageProviders();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public loadAce(): void {
|
public loadAce(): void {
|
||||||
@ -52,52 +48,15 @@ export class AceEditorComponent {
|
|||||||
this.editor = ace.edit( this.editorElm.nativeElement );
|
this.editor = ace.edit( this.editorElm.nativeElement );
|
||||||
this.editor.setOptions( this.editorSettings.CONFIG );
|
this.editor.setOptions( this.editorSettings.CONFIG );
|
||||||
// this.editor.commands.addCommands( this.editorSettings.KEYBINDINGS );
|
// this.editor.commands.addCommands( this.editorSettings.KEYBINDINGS );
|
||||||
}
|
|
||||||
|
|
||||||
protected onClick(event: any) {
|
this.editor.on("focus", () => {
|
||||||
let message = new ServiceMessage();
|
this.editorsService.setActiveEditor(this.uuid);
|
||||||
message.action = "set-editor";
|
});
|
||||||
message.message = this.uuid;
|
|
||||||
message.uuid = this.uuid;
|
|
||||||
|
|
||||||
this.editorsService.setData(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public loadLanguageProviders(): void {
|
|
||||||
let languageProvider = this.getLanguageProviderWithClientServers();
|
|
||||||
// let languageProvider = this.getLanguageProviderWithWebWorker();
|
|
||||||
languageProvider.registerEditor(this.editor);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public registerEditorToLSP() {
|
||||||
|
this.lspService.registerEditor(this.editor);
|
||||||
public getLanguageProviderWithClientServers() {
|
|
||||||
let _initializationOptions = {};
|
|
||||||
|
|
||||||
if (Object.keys(this.lspConfigData).length !== 0) {
|
|
||||||
// _initializationOptions = this.lspConfigData[ this.editor.session.getMode() ]["initialization-options"];
|
|
||||||
_initializationOptions = this.lspConfigData[ "python" ]["initialization-options"];
|
|
||||||
}
|
|
||||||
|
|
||||||
let servers: LanguageClientConfig[] = [
|
|
||||||
{
|
|
||||||
module: () => import("ace-linters/build/language-client"),
|
|
||||||
modes: "python",
|
|
||||||
type: "socket",
|
|
||||||
socket: new WebSocket("ws://127.0.0.1:9999/python"),
|
|
||||||
// socket: new WebSocket("ws://127.0.0.1:9999/?name=pylsp"),
|
|
||||||
initializationOptions: _initializationOptions
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
return AceLanguageClient.for(servers);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public getLanguageProviderWithWebWorker() {
|
|
||||||
let worker = new Worker(new URL('./webworker.js', import.meta.url));
|
|
||||||
return LanguageProvider.create(worker);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -50,28 +50,19 @@ export class EditorsComponent {
|
|||||||
|
|
||||||
|
|
||||||
public ngAfterViewInit(): void {
|
public ngAfterViewInit(): void {
|
||||||
this.editorsService.getData$().pipe(
|
this.loadSubscribers();
|
||||||
takeUntil(this.unsubscribe)
|
|
||||||
).subscribe((data: ServiceMessage) => {
|
|
||||||
if (data.action === "set-editor")
|
|
||||||
this.activeEditor = data.uuid;
|
|
||||||
});
|
|
||||||
|
|
||||||
this.getLspConfigData().then((lspConfigData: string) => {
|
|
||||||
this.lspConfigData = JSON.parse(lspConfigData);
|
|
||||||
|
|
||||||
if (this.lspConfigData["message"]) {
|
|
||||||
console.log(
|
|
||||||
"Warning: LSP this.lspConfigData is a 'message'",
|
|
||||||
this.lspConfigData
|
|
||||||
);
|
|
||||||
this.lspConfigData = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
let editor = this.createEditor();
|
let editor = this.createEditor();
|
||||||
this.activeEditor = editor.instance.uuid;
|
this.activeEditor = editor.instance.uuid;
|
||||||
this.createEditor();
|
this.createEditor();
|
||||||
})
|
}
|
||||||
|
|
||||||
|
loadSubscribers() {
|
||||||
|
this.editorsService.newActiveEditor$().pipe(
|
||||||
|
takeUntil(this.unsubscribe)
|
||||||
|
).subscribe((uuid: string) => {
|
||||||
|
this.activeEditor = uuid;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy() {
|
ngOnDestroy() {
|
||||||
@ -79,15 +70,10 @@ export class EditorsComponent {
|
|||||||
this.unsubscribe.complete();
|
this.unsubscribe.complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
private getLspConfigData(): Promise<string> {
|
|
||||||
return window.fs.getLspConfigData();
|
|
||||||
}
|
|
||||||
|
|
||||||
private createEditor() {
|
private createEditor() {
|
||||||
const component = this.containerRef.createComponent(AceEditorComponent);
|
const component = this.containerRef.createComponent(AceEditorComponent);
|
||||||
component.instance.editorSettings = this.editorSettings;
|
component.instance.editorSettings = this.editorSettings;
|
||||||
component.instance.uuid = uuid.v4();
|
component.instance.uuid = uuid.v4();
|
||||||
component.instance.lspConfigData = this.lspConfigData;
|
|
||||||
this.editors.set(component.instance.uuid, component)
|
this.editors.set(component.instance.uuid, component)
|
||||||
|
|
||||||
return component;
|
return component;
|
||||||
@ -97,8 +83,10 @@ export class EditorsComponent {
|
|||||||
this.loadFilesList(event).then((session: EditSession | undefined | null) => {
|
this.loadFilesList(event).then((session: EditSession | undefined | null) => {
|
||||||
if ( !session ) return;
|
if ( !session ) return;
|
||||||
|
|
||||||
let editor = this.editors.get(this.activeEditor)?.instance.editor;
|
let editorComponent = this.editors.get(this.activeEditor)?.instance;
|
||||||
|
let editor = editorComponent.editor;
|
||||||
editor?.setSession(session);
|
editor?.setSession(session);
|
||||||
|
// editorComponent.registerEditorToLSP();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user