summaryrefslogtreecommitdiff
path: root/vim/pack/downloads/opt/lsp/test/common.vim
diff options
context:
space:
mode:
authorMarc Coquand <marc@mccd.space>2024-07-01 21:26:30 -0500
committerMarc Coquand <marc@mccd.space>2024-07-01 21:26:30 -0500
commit0a20357d4585da91d92252972f3eb7b715ff64ab (patch)
treec1e228d72b6331e89f72d99a1ba4ba1807d60381 /vim/pack/downloads/opt/lsp/test/common.vim
downloadbsd-0a20357d4585da91d92252972f3eb7b715ff64ab.tar.gz
bsd-0a20357d4585da91d92252972f3eb7b715ff64ab.tar.bz2
bsd-0a20357d4585da91d92252972f3eb7b715ff64ab.zip
initial commit
Diffstat (limited to 'vim/pack/downloads/opt/lsp/test/common.vim')
-rw-r--r--vim/pack/downloads/opt/lsp/test/common.vim166
1 files changed, 166 insertions, 0 deletions
diff --git a/vim/pack/downloads/opt/lsp/test/common.vim b/vim/pack/downloads/opt/lsp/test/common.vim
new file mode 100644
index 0000000..eeb852c
--- /dev/null
+++ b/vim/pack/downloads/opt/lsp/test/common.vim
@@ -0,0 +1,166 @@
+vim9script
+# Common routines used for running the unit tests
+
+# Load the LSP plugin. Also enable syntax, file type detection.
+def g:LoadLspPlugin()
+ syntax on
+ filetype on
+ filetype plugin on
+ filetype indent on
+
+ # Set the $LSP_PROFILE environment variable to profile the LSP plugin
+ var do_profile: bool = false
+ if exists('$LSP_PROFILE')
+ do_profile = true
+ endif
+
+ if do_profile
+ # profile the LSP plugin
+ profile start lsp_profile.txt
+ profile! file */lsp/*
+ endif
+
+ g:LSPTest = true
+ source ../plugin/lsp.vim
+enddef
+
+# The WaitFor*() functions are reused from the Vim test suite.
+#
+# Wait for up to five seconds for "assert" to return zero. "assert" must be a
+# (lambda) function containing one assert function. Example:
+# call WaitForAssert({-> assert_equal("dead", job_status(job)})
+#
+# A second argument can be used to specify a different timeout in msec.
+#
+# Return zero for success, one for failure (like the assert function).
+func g:WaitForAssert(assert, ...)
+ let timeout = get(a:000, 0, 5000)
+ if g:WaitForCommon(v:null, a:assert, timeout) < 0
+ return 1
+ endif
+ return 0
+endfunc
+
+# Either "expr" or "assert" is not v:null
+# Return the waiting time for success, -1 for failure.
+func g:WaitForCommon(expr, assert, timeout)
+ " using reltime() is more accurate, but not always available
+ let slept = 0
+ if exists('*reltimefloat')
+ let start = reltime()
+ endif
+
+ while 1
+ if type(a:expr) == v:t_func
+ let success = a:expr()
+ elseif type(a:assert) == v:t_func
+ let success = a:assert() == 0
+ else
+ let success = eval(a:expr)
+ endif
+ if success
+ return slept
+ endif
+
+ if slept >= a:timeout
+ break
+ endif
+ if type(a:assert) == v:t_func
+ " Remove the error added by the assert function.
+ call remove(v:errors, -1)
+ endif
+
+ sleep 10m
+ if exists('*reltimefloat')
+ let slept = float2nr(reltimefloat(reltime(start)) * 1000)
+ else
+ let slept += 10
+ endif
+ endwhile
+
+ return -1 " timed out
+endfunc
+
+# Wait for up to five seconds for "expr" to become true. "expr" can be a
+# stringified expression to evaluate, or a funcref without arguments.
+# Using a lambda works best. Example:
+# call WaitFor({-> status == "ok"})
+#
+# A second argument can be used to specify a different timeout in msec.
+#
+# When successful the time slept is returned.
+# When running into the timeout an exception is thrown, thus the function does
+# not return.
+func g:WaitFor(expr, ...)
+ let timeout = get(a:000, 0, 5000)
+ let slept = g:WaitForCommon(a:expr, v:null, timeout)
+ if slept < 0
+ throw 'WaitFor() timed out after ' .. timeout .. ' msec'
+ endif
+ return slept
+endfunc
+
+# Wait for diagnostic messages from the LSP server.
+# Waits for a maximum of (150 * 200) / 1000 = 30 seconds
+def g:WaitForDiags(errCount: number)
+ var retries = 0
+ while retries < 200
+ var d = lsp#lsp#ErrorCount()
+ if d.Error == errCount
+ break
+ endif
+ retries += 1
+ :sleep 150m
+ endwhile
+
+ assert_equal(errCount, lsp#lsp#ErrorCount().Error)
+ if lsp#lsp#ErrorCount().Error != errCount
+ :LspDiag show
+ assert_report(getloclist(0)->string())
+ :lclose
+ endif
+enddef
+
+# Wait for the LSP server to load and process a file. This works by waiting
+# for a certain number of diagnostic messages from the server.
+def g:WaitForServerFileLoad(diagCount: number)
+ :redraw!
+ var waitCount = diagCount
+ if waitCount == 0
+ # Introduce a temporary diagnostic
+ append('$', '-')
+ redraw!
+ waitCount = 1
+ endif
+ g:WaitForDiags(waitCount)
+ if waitCount != diagCount
+ # Remove the temporary line
+ deletebufline('%', '$')
+ redraw!
+ g:WaitForDiags(0)
+ endif
+enddef
+
+# Start the language server. Returns true on success and false on failure.
+# 'fname' is the name of a dummy file to start the server.
+def g:StartLangServerWithFile(fname: string): bool
+ # Edit a dummy file to start the LSP server
+ exe ':silent! edit ' .. fname
+ # Wait for the LSP server to become ready (max 10 seconds)
+ var maxcount = 100
+ while maxcount > 0 && !g:LspServerReady()
+ :sleep 100m
+ maxcount -= 1
+ endwhile
+ var serverStatus: bool = g:LspServerReady()
+ :bw!
+
+ if !serverStatus
+ writefile(['FAIL: Not able to start the language server'], 'results.txt')
+ qall!
+ endif
+
+ return serverStatus
+enddef
+
+# vim: shiftwidth=2 softtabstop=2 noexpandtab