feat(server): Add blockUntilNotNil to allow waiting for indexes

This commit is contained in:
Myzel394 2025-06-17 19:40:59 +02:00
parent 65c54595bc
commit 942a9e1d48
Signed by: Myzel394
GPG Key ID: 2EB4DBF6ECFC36A0
4 changed files with 35 additions and 3 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

30
server/utils/time.go Normal file
View File

@ -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)
}