This is an automated email from the ASF dual-hosted git repository.

sdedic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 084770067c Provide a waitable command that completes after NBLS fully 
starts & registers server-provided commands
     new ce4ed99adf Merge pull request #5615 from 
sdedic/vscode/startup-complete-condition
084770067c is described below

commit 084770067c6d06dfcf142447616b84472d795a45
Author: Svata Dedic <[email protected]>
AuthorDate: Mon Mar 6 13:37:00 2023 +0100

    Provide a waitable command that completes after NBLS fully starts & 
registers server-provided commands
---
 java/java.lsp.server/vscode/src/extension.ts | 54 +++++++++++++++++++++++-----
 1 file changed, 46 insertions(+), 8 deletions(-)

diff --git a/java/java.lsp.server/vscode/src/extension.ts 
b/java/java.lsp.server/vscode/src/extension.ts
index f74dde140b..9f86ce9219 100644
--- a/java/java.lsp.server/vscode/src/extension.ts
+++ b/java/java.lsp.server/vscode/src/extension.ts
@@ -134,7 +134,7 @@ export function findClusters(myPath : string): string[] {
 // for tests only !
 export function awaitClient() : Promise<NbLanguageClient> {
     const c : Promise<NbLanguageClient> = client;
-    if (c) {
+    if (c && !(c instanceof InitialPromise)) {
         return c;
     }
     let nbcode = vscode.extensions.getExtension('asf.apache-netbeans-java');
@@ -142,7 +142,7 @@ export function awaitClient() : Promise<NbLanguageClient> {
         return Promise.reject(new Error("Extension not installed."));
     }
     const t : Thenable<NbLanguageClient> = nbcode.activate().then(nc => {
-        if (client === undefined) {
+        if (client === undefined || client instanceof InitialPromise) {
             throw new Error("Client not available");
         } else {
             return client;
@@ -292,9 +292,29 @@ function wrapCommandWithProgress(lsCommand : string, title 
: string, log? : vsco
     });
 }
 
+/**
+ * Just a simple promise subclass, so I can test for the 'initial promise' 
value:
+ * unlike all other promises, that must be fullfilled in order to e.g. 
properly stop the server or otherwise communicate with it,
+ * the initial one needs to be largely ignored in the launching/mgmt code, BUT 
should be available to normal commands / features.
+ */
+class InitialPromise extends Promise<NbLanguageClient> {
+    constructor(f : (resolve: (value: NbLanguageClient | 
PromiseLike<NbLanguageClient>) => void, reject: (reason?: any) => void) => 
void) {
+        super(f);
+    }
+}
+
 export function activate(context: ExtensionContext): VSNetBeansAPI {
     let log = vscode.window.createOutputChannel("Apache NetBeans Language 
Server");
 
+    var clientResolve : (x : NbLanguageClient) => void;
+    var clientReject : (err : any) => void;
+
+    // establish a waitable Promise, export the callbacks so they can be 
called after activation.
+    client = new InitialPromise((resolve, reject) => {
+        clientResolve = resolve;
+        clientReject = reject;
+    });
+
     function checkConflict(): void {
         let conf = workspace.getConfiguration();
         if (conf.get("netbeans.conflict.check") && 
conf.get("netbeans.javaSupport.enabled")) {
@@ -325,10 +345,10 @@ export function activate(context: ExtensionContext): 
VSNetBeansAPI {
             const newClusters = findClusters(context.extensionPath).sort();
             if (newClusters.length !== currentClusters.length || 
newClusters.find((value, index) => value !== currentClusters[index])) {
                 currentClusters = newClusters;
-                activateWithJDK(specifiedJDK, context, log, true);
+                activateWithJDK(specifiedJDK, context, log, true, 
clientResolve, clientReject);
             }
         }));
-        activateWithJDK(specifiedJDK, context, log, true);
+        activateWithJDK(specifiedJDK, context, log, true, clientResolve, 
clientReject);
     });
 
     //register debugger:
@@ -538,6 +558,9 @@ export function activate(context: ExtensionContext): 
VSNetBeansAPI {
     context.subscriptions.push(commands.registerCommand('java.package.test', 
async (uri, launchConfiguration?) => {
         await runDebug(true, true, uri, undefined, launchConfiguration);
     }));
+    
context.subscriptions.push(commands.registerCommand('nbls.startup.condition', 
async () => {
+        return client;
+    }));
 
     // register completions:
     launchConfigurations.registerCompletion(context);
@@ -557,7 +580,8 @@ let maintenance : Promise<void> | null;
  */
 let activationPending : boolean = false;
 
-function activateWithJDK(specifiedJDK: string | null, context: 
ExtensionContext, log : vscode.OutputChannel, notifyKill: boolean): void {
+function activateWithJDK(specifiedJDK: string | null, context: 
ExtensionContext, log : vscode.OutputChannel, notifyKill: boolean, 
+    clientResolve? : (x : NbLanguageClient) => void, clientReject? : (x : any) 
=> void): void {
     if (activationPending) {
         // do not activate more than once in parallel.
         handleLog(log, "Server activation requested repeatedly, ignoring...");
@@ -566,9 +590,23 @@ function activateWithJDK(specifiedJDK: string | null, 
context: ExtensionContext,
     let oldClient = client;
     let setClient : [(c : NbLanguageClient) => void, (err : any) => void];
     client = new Promise<NbLanguageClient>((clientOK, clientErr) => {
-        setClient = [ clientOK, clientErr ];
+        setClient = [
+            function (c : NbLanguageClient) {
+                clientOK(c);
+                if (clientResolve) {
+                    clientResolve(c);
+                }
+            }, function (err) {
+                clientErr(err);
+                if (clientReject) {
+                    clientReject(err);
+                }
+            }
+        ]
+        //setClient = [ clientOK, clientErr ];
     });
     const a : Promise<void> | null = maintenance;
+
     commands.executeCommand('setContext', 'nbJavaLSReady', false);
     commands.executeCommand('setContext', 'dbAddConnectionPresent', true);
     activationPending = true;
@@ -1177,12 +1215,12 @@ function doActivateWithJDK(specifiedJDK: string | null, 
context: ExtensionContex
     }
 }
 
-function stopClient(clinetPromise: Promise<LanguageClient>): Thenable<void> {
+function stopClient(clientPromise: Promise<LanguageClient>): Thenable<void> {
     if (testAdapter) {
         testAdapter.dispose();
         testAdapter = undefined;
     }
-    return clinetPromise ? clinetPromise.then(c => c.stop()) : 
Promise.resolve();
+    return clientPromise && !(clientPromise instanceof InitialPromise) ? 
clientPromise.then(c => c.stop()) : Promise.resolve();
 }
 
 export function deactivate(): Thenable<void> {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to