Skip to content

Implement renameProvider #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion R/capabilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ DocumentLinkOptions <- list(
resolveProvider = FALSE
)

RenameOptions <- list(
prepareProvider = TRUE
)

ExecuteCommandOptions <- list(
commands = NULL
)
Expand All @@ -59,11 +63,22 @@ ServerCapabilities <- list(
documentFormattingProvider = TRUE,
documentRangeFormattingProvider = TRUE,
documentOnTypeFormattingProvider = DocumentOnTypeFormattingOptions,
# renameProvider = FALSE,
renameProvider = TRUE,
documentLinkProvider = DocumentLinkOptions,
colorProvider = TRUE,
foldingRangeProvider = TRUE
# selectionRangeProvider = FALSE,
# executeCommandProvider = ExecuteCommandOptions,
# workspace = list()
)

update_server_capabilities <- function(server_capabilities, client_capabilities) {
# RenameOptions may only be specified if the client states that
# it supports prepareSupport in its initial initialize request
if (isTRUE(server_capabilities$renameProvider) &&
isTRUE(client_capabilities$textDocument$rename$prepareSupport)) {
server_capabilities$renameProvider <- RenameOptions
}

server_capabilities
}
9 changes: 6 additions & 3 deletions R/handlers-general.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ on_initialize <- function(self, id, params) {
self$workspace <- Workspace$new(self$rootPath)
self$initializationOptions <- params$initializationOptions
self$ClientCapabilities <- params$capabilities
ServerCapabilities <- merge_list(
ServerCapabilities,
server_capabilities <- update_server_capabilities(
ServerCapabilities, self$ClientCapabilities)
server_capabilities <- merge_list(
server_capabilities,
getOption("languageserver.server_capabilities"))
self$deliver(Response$new(id = id, result = list(capabilities = ServerCapabilities)))
self$ServerCapabilities <- server_capabilities
self$deliver(Response$new(id = id, result = list(capabilities = server_capabilities)))
}

#' `initialized` handler
Expand Down
15 changes: 12 additions & 3 deletions R/handlers-langfeatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,25 @@ text_document_on_type_formatting <- function(self, id, params) {
#'
#' Handler to the `textDocument/rename` [Request].
#' @keywords internal
text_document_rename <- function(self, id, params) {

text_document_rename <- function(self, id, params) {
textDocument <- params$textDocument
uri <- uri_escape_unicode(textDocument$uri)
document <- self$workspace$documents$get(uri)
point <- document$from_lsp_position(params$position)
newName <- params$newName
self$deliver(rename_reply(id, uri, self$workspace, document, point, newName))
}

#' `textDocument/prepareRename` request handler
#'
#' Handler to the `textDocument/prepareRename` [Request].
#' @keywords internal
text_document_prepare_rename <- function(self, id, params) {

textDocument <- params$textDocument
uri <- uri_escape_unicode(textDocument$uri)
document <- self$workspace$documents$get(uri)
point <- document$from_lsp_position(params$position)
self$deliver(prepare_rename_reply(id, uri, self$workspace, document, point))
}

#' `textDocument/foldingRange` request handler
Expand Down
36 changes: 25 additions & 11 deletions R/languagebase.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,31 @@ LanguageBase <- R6::R6Class("LanguageBase",
self$request_callbacks$pop(as.character(id)),
error = function(e) NULL
)
if ("error" %in% names(response)) {
logger$info("internal error:", response$error)
} else if (!is.null(callback)) {
logger$info("calling callback")
if (self$catch_callback_error) {
tryCatchStack(
callback(self, response$result),
error = function(e) logger$info("callback error: ", e)
)
} else {
callback(self, response$result)
if (is.null(response$error)) {
if (!is.null(callback)) {
logger$info("calling callback")
if (self$catch_callback_error) {
tryCatchStack(
callback(self, result = response$result),
error = function(e) logger$info("callback error: ", e)
)
} else {
callback(self, result = response$result)
}
}
} else {
logger$info("error:", response$error)
# only call callback if it handles error
if (!is.null(callback) && "error" %in% names(formals(callback))) {
logger$info("calling callback")
if (self$catch_callback_error) {
tryCatchStack(
callback(self, result = response$result, error = response$error),
error = function(e) logger$info("callback error: ", e)
)
} else {
callback(self, result = response$result, error = response$error)
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions R/languageserver.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ LanguageServer <- R6::R6Class("LanguageServer",
rootPath = NULL,
initializationOptions = NULL,
ClientCapabilities = NULL,
ServerCapabilities = NULL,

diagnostics_task_manager = NULL,
parse_task_manager = NULL,
Expand Down Expand Up @@ -209,6 +210,8 @@ LanguageServer$set("public", "register_handlers", function() {
`textDocument/colorPresentation` = text_document_color_presentation,
`textDocument/foldingRange` = text_document_folding_range,
`textDocument/references` = text_document_references,
`textDocument/rename` = text_document_rename,
`textDocument/prepareRename` = text_document_prepare_rename,
`workspace/symbol` = workspace_symbol
)

Expand Down
57 changes: 57 additions & 0 deletions R/rename.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
prepare_rename_reply <- function(id, uri, workspace, document, point) {
token <- document$detect_token(point)
defn <- definition_reply(NULL, uri, workspace, document, point)

logger$info("prepare_rename_reply: ", list(
token = token,
defn = defn$result
))

if (length(defn$result)) {
Response$new(
id,
result = range(
start = document$to_lsp_position(
row = token$range$start$row,
col = token$range$start$col),
end = document$to_lsp_position(
row = token$range$end$row,
col = token$range$end$col)
)
)
} else {
ResponseErrorMessage$new(
id,
errortype = "RequestCancelled",
message = "Cannot rename the symbol"
)
}
}

#' @keywords internal
rename_reply <- function(id, uri, workspace, document, point, newName) {
refs <- references_reply(NULL, uri, workspace, document, point)
result <- list()

for (ref in refs$result) {
result[[ref$uri]] <- c(result[[ref$uri]], list(text_edit(
range = ref$range,
new_text = newName
)))
}

logger$info("rename_reply: ", result)

if (length(result)) {
Response$new(
id,
result = list(
changes = result
)
)
} else {
Response$new(
id
)
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ These editors are supported by installing the corresponding package.
- [x] [documentFormattingProvider](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_formatting)
- [x] [documentRangeFormattingProvider](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rangeFormatting)
- [x] [documentOnTypeFormattingProvider](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_onTypeFormatting)
- [ ] [renameProvider](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename)
- [x] [renameProvider](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename)
- [x] [prepareRenameProvider](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_prepareRename)
- [x] [documentLinkProvider](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentLink)
- [x] [colorProvider](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentColor)
- [x] [colorPresentation](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_colorPresentation)
Expand Down
43 changes: 37 additions & 6 deletions tests/testthat/helper-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ did_save <- function(client, path) {
client,
"textDocument/didSave",
params)
Sys.sleep(0.5)
invisible(client)
}


respond <- function(client, method, params, timeout, retry = TRUE,
retry_when = function(result) length(result) == 0) {
respond <- function(client, method, params, timeout, allow_error = FALSE,
retry = TRUE, retry_when = function(result) length(result) == 0) {
if (missing(timeout)) {
if (Sys.getenv("R_COVR", "") == "true") {
# we give more time to covr
Expand All @@ -109,9 +110,14 @@ respond <- function(client, method, params, timeout, retry = TRUE,
}
}
storage <- new.env(parent = .GlobalEnv)
cb <- function(self, result) {
storage$done <- TRUE
storage$result <- result
cb <- function(self, result, error = NULL) {
if (is.null(error)) {
storage$done <- TRUE
storage$result <- result
} else if (allow_error) {
storage$done <- TRUE
storage$result <- error
}
}

start_time <- Sys.time()
Expand All @@ -134,7 +140,7 @@ respond <- function(client, method, params, timeout, retry = TRUE,
return(NULL)
}
Sys.sleep(0.2)
return(Recall(client, method, params, remaining, retry, retry_when))
return(Recall(client, method, params, remaining, allow_error, retry, retry_when))
}
return(result)
}
Expand Down Expand Up @@ -209,6 +215,31 @@ respond_references <- function(client, path, pos, ...) {
)
}

respond_rename <- function(client, path, pos, newName, ...) {
respond(
client,
"textDocument/rename",
list(
textDocument = list(uri = path_to_uri(path)),
position = list(line = pos[1], character = pos[2]),
newName = newName
),
...
)
}

respond_prepare_rename <- function(client, path, pos, ...) {
respond(
client,
"textDocument/prepareRename",
list(
textDocument = list(uri = path_to_uri(path)),
position = list(line = pos[1], character = pos[2])
),
...
)
}


respond_formatting <- function(client, path, ...) {
respond(
Expand Down
Loading