From c416ea6789d9024c21c241c22a27451d0d64233b Mon Sep 17 00:00:00 2001 From: Geequlim Date: Wed, 26 Jun 2019 12:08:10 +0800 Subject: [PATCH] Allow retry connect to editor after ignored retry --- package.json | 5 +++ src/godot-tools.ts | 56 +++++++++++++++---------------- src/lsp/GDScriptLanguageClient.ts | 28 ++++++++++++++++ 3 files changed, 61 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 73b008e..935c6bc 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,11 @@ "type": "string", "default": "", "description": "The absolute path to the Godot editor executable" + }, + "godot-tool.check_status": { + "type": "string", + "default": "", + "description": "The absolute path to the Godot editor executable" } } }, diff --git a/src/godot-tools.ts b/src/godot-tools.ts index c7f38fb..291035f 100644 --- a/src/godot-tools.ts +++ b/src/godot-tools.ts @@ -1,19 +1,13 @@ import * as vscode from "vscode"; import * as path from 'path'; import * as fs from 'fs'; -import GDScriptLanguageClient from "./lsp/GDScriptLanguageClient"; +import GDScriptLanguageClient, { ClientStatus } from "./lsp/GDScriptLanguageClient"; import { get_configuration, set_configuration } from "./utils"; import { MessageIO } from "./lsp/MessageIO"; const CONFIG_CONTAINER = "godot_tools"; const TOOL_NAME = "GodotTools"; -enum ClientStatus { - PENDING, - DISCONNECTED, - CONNECTED, -} - export class GodotTools { private context: vscode.ExtensionContext; @@ -27,8 +21,7 @@ export class GodotTools { this.context = p_context; this.client = new GDScriptLanguageClient(); this.message_handler = new MessageHandler(this.client.io); - this.client.io.on('disconnected', this.on_client_disconnected.bind(this)); - this.client.io.on('connected', this.on_server_connected.bind(this)); + this.client.watch_status(this.on_client_status_changed.bind(this)); this.connection_status = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right); } @@ -39,9 +32,12 @@ export class GodotTools { vscode.commands.registerCommand("godot-tool.run_project", ()=>{ this.open_workspace_with_editor().catch(err=>vscode.window.showErrorMessage(err)); }); - this.connection_status.text = "starting"; + vscode.commands.registerCommand("godot-tool.check_status", this.check_status.bind(this)); + + this.connection_status.text = "$(sync) Initializing"; + this.connection_status.command = "godot-tool.check_status"; this.connection_status.show(); - this.try_connect_to_server(); + this.client.connect_to_server(); } @@ -115,12 +111,21 @@ export class GodotTools { }); } - private try_connect_to_server() { - this.client.connect_to_server(); - this.update_client_status(ClientStatus.PENDING); + private check_status() { + switch (this.client.status) { + case ClientStatus.PENDING: + vscode.window.showInformationMessage("Connecting to GDScript language server"); + break; + case ClientStatus.CONNECTED: + vscode.window.showInformationMessage("Connected to GDScript language server"); + break; + case ClientStatus.DISCONNECTED: + this.retry_connect_client(); + break; + } } - private update_client_status(status: ClientStatus) { + private on_client_status_changed(status: ClientStatus) { this.connection_status.color = vscode.ThemeColor; switch (status) { case ClientStatus.PENDING: @@ -130,39 +135,34 @@ export class GodotTools { case ClientStatus.CONNECTED: this.connection_status.text = `$(check) Connected`; this.connection_status.tooltip = `Connected to GDScript Language Server`; + // activate language client + this.context.subscriptions.push(this.client.start()); break; case ClientStatus.DISCONNECTED: this.connection_status.text = `$(x) Disconnected`; this.connection_status.tooltip = `Disconnect to GDScript Language Server`; + // retry + this.retry_connect_client(); break; default: break; } } - private on_server_connected() { - this.context.subscriptions.push(this.client.start()); - this.update_client_status(ClientStatus.CONNECTED); - } - - private on_client_disconnected() { - this.update_client_status(ClientStatus.DISCONNECTED); + private retry_connect_client() { vscode.window.showErrorMessage(`Failed connect to GDScript Language Server`, 'Open Godot Editor', 'Retry', 'Ignore').then(item=>{ if (item == 'Retry') { - this.try_connect_to_server(); + this.client.connect_to_server(); } else if (item == 'Open Godot Editor') { - this.update_client_status(ClientStatus.PENDING); + this.client.status = ClientStatus.PENDING; this.open_workspace_with_editor("-e").then(()=>{ setTimeout(()=>{ - this.try_connect_to_server(); + this.client.connect_to_server(); }, 10 * 1000); }); } }); } - - - }; const CUSTOM_MESSAGE = "gdscrip_client/"; diff --git a/src/lsp/GDScriptLanguageClient.ts b/src/lsp/GDScriptLanguageClient.ts index 1399666..3ae2f83 100644 --- a/src/lsp/GDScriptLanguageClient.ts +++ b/src/lsp/GDScriptLanguageClient.ts @@ -30,15 +30,43 @@ const serverOptions: ServerOptions = () => { }); }; +export enum ClientStatus { + PENDING, + DISCONNECTED, + CONNECTED, +} export default class GDScriptLanguageClient extends LanguageClient { public io: MessageIO = io; + private _status : ClientStatus; + private _status_changed_callbacks: ((v : ClientStatus)=>void)[] = []; + + public get status() : ClientStatus { return this._status; } + public set status(v : ClientStatus) { + if (this._status != v) { + this._status = v; + for (const callback of this._status_changed_callbacks) { + callback(v); + } + } + } + + public watch_status(callback: (v : ClientStatus)=>void) { + if (this._status_changed_callbacks.indexOf(callback) == -1) { + this._status_changed_callbacks.push(callback); + } + } + constructor() { super(`GDScriptLanguageClient`, serverOptions, getClientOptions()); + this.status = ClientStatus.PENDING; + this.io.on('disconnected', ()=> this.status = ClientStatus.DISCONNECTED); + this.io.on('connected', ()=> this.status = ClientStatus.CONNECTED); } connect_to_server() { + this.status = ClientStatus.PENDING; io.connect_to_language_server(); } };