A TCP client for corresponding with R’s language server enabling interactive debugging.
pak::pkg_install("milesmcbain/tcplspclient")
In R session 1 (client):
- Start the client. (It’s technically the server. Good one Microsoft.)
library(tcplspclient)
client <- TCPLanguageClient$new(host = "localhost", port = 8888)
In R session 2 (server):
devtools::load_all()
- Add
browser()
statements or usedebugonce()
/debug()
etc tolanguageserver
functions. - Connect the language server to the client:
run(port = 8888)
In R session 1 (client)
- Do handshake.
# TCP client connected to localhost:8888
server_capabilities <- client$handshake()
server_capabilities
# $textDocumentSync
# $textDocumentSync$openClose
# [1] TRUE
# $textDocumentSync$change
# [1] 1
# $textDocumentSync$willSave
# [1] FALSE
# ...
- Send messages to trigger server actions to debug in session 2.
- note see this file for protocol wrapper functions.
doc_path <- "~/code/r/blogdown_test/content/post/test2.R"
client$send_notification(
method = "textDocument/didSave",
params = list(textDocument = list(
uri = languageserver:::path_to_uri(doc_path)),
text = paste0(stringi::stri_read_lines(doc_path), collapse = "\n")
)
)
response <- client$send_message(
method = "textDocument/documentSymbol",
params = list(
textDocument = list(uri =
languageserver:::path_to_uri("~/code/r/blogdown_test/content/post/test2.R"))
)
)
Now debug interactively in R session 1. Eventually get the response you expected:
response
# response
# [[1]]
# [[1]]$name
# [1] "f"
# [[1]]$kind
# [1] 12
# [[1]]$location
# [[1]]$location$uri
# [1] "file:///home/miles/code/r/blogdown_test/content/post/test2.R"
# [[1]]$location$range
# [[1]]$location$range$start
# [[1]]$location$range$start$line
# [1] 0
# [[1]]$location$range$start$character
# [1] 0
Code in this project is substantially copied from the {languageserver}. Code files carry approriate licenses and attribution in header comments.