From 942a9e1d4865f1fe3e78f2ceeaeca19e74f8c659 Mon Sep 17 00:00:00 2001 From: Myzel394 Date: Tue, 17 Jun 2025 19:40:59 +0200 Subject: [PATCH] feat(server): Add blockUntilNotNil to allow waiting for indexes --- .../ssh_config/handlers/fetch-code-actions.go | 3 +- .../sshd_config/handlers/completions.go | 2 +- .../handlers/fetch-code-actions.go | 3 +- server/utils/time.go | 30 +++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 server/utils/time.go diff --git a/server/handlers/ssh_config/handlers/fetch-code-actions.go b/server/handlers/ssh_config/handlers/fetch-code-actions.go index 27a2a7f..8c9a737 100644 --- a/server/handlers/ssh_config/handlers/fetch-code-actions.go +++ b/server/handlers/ssh_config/handlers/fetch-code-actions.go @@ -2,6 +2,7 @@ package handlers import ( sshconfig "config-lsp/handlers/ssh_config" + "config-lsp/utils" protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -10,7 +11,7 @@ func FetchCodeActions( d *sshconfig.SSHDocument, params *protocol.CodeActionParams, ) []protocol.CodeAction { - if d.Indexes == nil { + if utils.BlockUntilIndexesNotNil(d.Indexes) == false { return nil } diff --git a/server/handlers/sshd_config/handlers/completions.go b/server/handlers/sshd_config/handlers/completions.go index 058359a..b790f78 100644 --- a/server/handlers/sshd_config/handlers/completions.go +++ b/server/handlers/sshd_config/handlers/completions.go @@ -18,7 +18,7 @@ func GetRootCompletions( parentMatchBlock *ast.SSHDMatchBlock, suggestValue bool, ) ([]protocol.CompletionItem, error) { - if d.Indexes == nil { + if utils.BlockUntilIndexesNotNil(d.Indexes) == false { return nil, nil } diff --git a/server/handlers/sshd_config/handlers/fetch-code-actions.go b/server/handlers/sshd_config/handlers/fetch-code-actions.go index 3713dff..072d8c9 100644 --- a/server/handlers/sshd_config/handlers/fetch-code-actions.go +++ b/server/handlers/sshd_config/handlers/fetch-code-actions.go @@ -2,6 +2,7 @@ package handlers import ( sshdconfig "config-lsp/handlers/sshd_config" + "config-lsp/utils" protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -10,7 +11,7 @@ func FetchCodeActions( d *sshdconfig.SSHDDocument, params *protocol.CodeActionParams, ) []protocol.CodeAction { - if d.Indexes == nil { + if utils.BlockUntilIndexesNotNil(d.Indexes) == false { return nil } diff --git a/server/utils/time.go b/server/utils/time.go new file mode 100644 index 0000000..77f4240 --- /dev/null +++ b/server/utils/time.go @@ -0,0 +1,30 @@ +package utils + +import "time" + +func BlockUntilNotNil(pointer any) { + for pointer == nil { + // This is a blocking call to wait until the pointer is not nil. + // It can be used in scenarios where the pointer is expected to be set by another goroutine. + } +} + +func BlockUntilNotNilTimeout(pointer any, timeout time.Duration) bool { + deadline := time.Now().Add(timeout) + + for pointer == nil { + if time.Now().After(deadline) { + return false + } + // This is a blocking call to wait until the pointer is not nil. + // It can be used in scenarios where the pointer is expected to be set by another goroutine. + } + + return true +} + +// Waits till the provided pointer is not nil. +// Has a default timeout of 3 seconds +func BlockUntilIndexesNotNil(d any) bool { + return BlockUntilNotNilTimeout(d, 3*time.Second) +}