From 6d05a27fb965245cefedc12df3b15dc19fe9988f Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Fri, 3 Jul 2020 11:03:49 +0800 Subject: [PATCH 1/4] Try parsing string in link and color --- R/color.R | 4 +++- R/link.R | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/R/color.R b/R/color.R index ed18440f..600c4e41 100644 --- a/R/color.R +++ b/R/color.R @@ -17,7 +17,9 @@ document_color_reply <- function(id, uri, workspace, document) { str_col1 <- as.integer(xml_attr(str_tokens, "col1")) str_col2 <- as.integer(xml_attr(str_tokens, "col2")) str_expr <- substr(document$content[str_line1], str_col1, str_col2) - str_texts <- as.character(parse(text = str_expr, keep.source = FALSE)) + str_texts <- vapply(str_expr, function(expr) { + tryCatch(parse(expr, keep.source = FALSE), error = function(e) "") + }, character(1)) is_color <- !grepl("^[rR]", str_expr) & (grepl("^#[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$", str_texts) | diff --git a/R/link.R b/R/link.R index 6b0e6640..6b87f4fe 100644 --- a/R/link.R +++ b/R/link.R @@ -17,7 +17,9 @@ document_link_reply <- function(id, uri, workspace, document, rootPath) { str_col1 <- as.integer(xml_attr(str_tokens, "col1")) str_col2 <- as.integer(xml_attr(str_tokens, "col2")) str_expr <- substr(document$content[str_line1], str_col1, str_col2) - str_texts <- as.character(parse(text = str_expr, keep.source = FALSE)) + str_texts <- vapply(str_expr, function(expr) { + tryCatch(parse(expr, keep.source = FALSE), error = function(e) "") + }, character(1)) paths <- fs::path_abs(str_texts, rootPath) From 587188340b3dfaa6325a4e51b9ccd9811d0c7828 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Fri, 3 Jul 2020 11:12:30 +0800 Subject: [PATCH 2/4] Update link and color --- R/color.R | 47 ++++++++++++++++++++++------------------------- R/link.R | 49 +++++++++++++++++++++++-------------------------- 2 files changed, 45 insertions(+), 51 deletions(-) diff --git a/R/color.R b/R/color.R index 600c4e41..5cc1385b 100644 --- a/R/color.R +++ b/R/color.R @@ -17,35 +17,32 @@ document_color_reply <- function(id, uri, workspace, document) { str_col1 <- as.integer(xml_attr(str_tokens, "col1")) str_col2 <- as.integer(xml_attr(str_tokens, "col2")) str_expr <- substr(document$content[str_line1], str_col1, str_col2) - str_texts <- vapply(str_expr, function(expr) { - tryCatch(parse(expr, keep.source = FALSE), error = function(e) "") - }, character(1)) + str_texts <- tryCatch(as.character(parse(expr, keep.source = FALSE)), + error = function(e) NULL) - is_color <- !grepl("^[rR]", str_expr) & - (grepl("^#[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$", str_texts) | - str_texts %in% grDevices::colors()) - color_texts <- str_texts[is_color] - color_line1 <- str_line1[is_color] - color_col1 <- str_col1[is_color] - color_col2 <- str_col2[is_color] - color_rgb <- grDevices::col2rgb(color_texts, alpha = TRUE) / 255 + if (length(str_texts)) { + is_color <- !grepl("^[rR]", str_expr) & + (grepl("^#[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$", str_texts) | + str_texts %in% grDevices::colors()) + color_texts <- str_texts[is_color] + color_line1 <- str_line1[is_color] + color_col1 <- str_col1[is_color] + color_col2 <- str_col2[is_color] + color_rgb <- grDevices::col2rgb(color_texts, alpha = TRUE) / 255 - result <- .mapply(function(line, col1, col2, i) { - list( - range = range( - start = document$to_lsp_position(line - 1, col1), - end = document$to_lsp_position(line - 1, col2 - 1) - ), - color = as.list(color_rgb[, i]) - ) - }, list(color_line1, color_col1, color_col2, seq_along(color_texts)), NULL) + result <- .mapply(function(line, col1, col2, i) { + list( + range = range( + start = document$to_lsp_position(line - 1, col1), + end = document$to_lsp_position(line - 1, col2 - 1) + ), + color = as.list(color_rgb[, i]) + ) + }, list(color_line1, color_col1, color_col2, seq_along(color_texts)), NULL) + } } - if (is.null(result)) { - Response$new(id) - } else { - Response$new(id, result = result) - } + Response$new(id, result = result) } #' The response to a textDocument/colorPresentation Request diff --git a/R/link.R b/R/link.R index 6b87f4fe..f4941450 100644 --- a/R/link.R +++ b/R/link.R @@ -17,35 +17,32 @@ document_link_reply <- function(id, uri, workspace, document, rootPath) { str_col1 <- as.integer(xml_attr(str_tokens, "col1")) str_col2 <- as.integer(xml_attr(str_tokens, "col2")) str_expr <- substr(document$content[str_line1], str_col1, str_col2) - str_texts <- vapply(str_expr, function(expr) { - tryCatch(parse(expr, keep.source = FALSE), error = function(e) "") - }, character(1)) + str_texts <- as.character(tryCatch(parse(expr, keep.source = FALSE), + error = function(e) NULL)) - paths <- fs::path_abs(str_texts, rootPath) + if (length(str_texts)) { + paths <- fs::path_abs(str_texts, rootPath) - is_link <- file.exists(paths) & !dir.exists(paths) - link_paths <- path.expand(paths[is_link]) - link_expr <- str_expr[is_link] - is_raw_string <- grepl("^[rR]", link_expr) - link_line1 <- str_line1[is_link] - link_col1 <- str_col1[is_link] + is_raw_string - link_col2 <- str_col2[is_link] - uris <- vapply(link_paths, path_to_uri, character(1)) + is_link <- file.exists(paths) & !dir.exists(paths) + link_paths <- path.expand(paths[is_link]) + link_expr <- str_expr[is_link] + is_raw_string <- grepl("^[rR]", link_expr) + link_line1 <- str_line1[is_link] + link_col1 <- str_col1[is_link] + is_raw_string + link_col2 <- str_col2[is_link] + uris <- vapply(link_paths, path_to_uri, character(1)) - result <- .mapply(function(line, col1, col2, uri) { - list( - range = range( - start = document$to_lsp_position(line - 1, col1), - end = document$to_lsp_position(line - 1, col2 - 1) - ), - target = uri - ) - }, list(link_line1, link_col1, link_col2, uris), NULL) + result <- .mapply(function(line, col1, col2, uri) { + list( + range = range( + start = document$to_lsp_position(line - 1, col1), + end = document$to_lsp_position(line - 1, col2 - 1) + ), + target = uri + ) + }, list(link_line1, link_col1, link_col2, uris), NULL) + } } - if (is.null(result)) { - Response$new(id) - } else { - Response$new(id, result = result) - } + Response$new(id, result = result) } From ae54fcdc3140dadd1126c248a977088a2e63e0b9 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Fri, 3 Jul 2020 11:13:03 +0800 Subject: [PATCH 3/4] Fix typo --- R/color.R | 2 +- R/link.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/color.R b/R/color.R index 5cc1385b..331a5035 100644 --- a/R/color.R +++ b/R/color.R @@ -17,7 +17,7 @@ document_color_reply <- function(id, uri, workspace, document) { str_col1 <- as.integer(xml_attr(str_tokens, "col1")) str_col2 <- as.integer(xml_attr(str_tokens, "col2")) str_expr <- substr(document$content[str_line1], str_col1, str_col2) - str_texts <- tryCatch(as.character(parse(expr, keep.source = FALSE)), + str_texts <- tryCatch(as.character(parse(str_expr, keep.source = FALSE)), error = function(e) NULL) if (length(str_texts)) { diff --git a/R/link.R b/R/link.R index f4941450..d0b97be1 100644 --- a/R/link.R +++ b/R/link.R @@ -17,7 +17,7 @@ document_link_reply <- function(id, uri, workspace, document, rootPath) { str_col1 <- as.integer(xml_attr(str_tokens, "col1")) str_col2 <- as.integer(xml_attr(str_tokens, "col2")) str_expr <- substr(document$content[str_line1], str_col1, str_col2) - str_texts <- as.character(tryCatch(parse(expr, keep.source = FALSE), + str_texts <- as.character(tryCatch(parse(str_expr, keep.source = FALSE), error = function(e) NULL)) if (length(str_texts)) { From a5a9f247afe870fc1983af25598db7a00aaa85c2 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Fri, 3 Jul 2020 11:18:52 +0800 Subject: [PATCH 4/4] Fix parse text --- R/color.R | 2 +- R/link.R | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/color.R b/R/color.R index 331a5035..3fc9b95c 100644 --- a/R/color.R +++ b/R/color.R @@ -17,7 +17,7 @@ document_color_reply <- function(id, uri, workspace, document) { str_col1 <- as.integer(xml_attr(str_tokens, "col1")) str_col2 <- as.integer(xml_attr(str_tokens, "col2")) str_expr <- substr(document$content[str_line1], str_col1, str_col2) - str_texts <- tryCatch(as.character(parse(str_expr, keep.source = FALSE)), + str_texts <- tryCatch(as.character(parse(text = str_expr, keep.source = FALSE)), error = function(e) NULL) if (length(str_texts)) { diff --git a/R/link.R b/R/link.R index d0b97be1..76559e2e 100644 --- a/R/link.R +++ b/R/link.R @@ -17,8 +17,8 @@ document_link_reply <- function(id, uri, workspace, document, rootPath) { str_col1 <- as.integer(xml_attr(str_tokens, "col1")) str_col2 <- as.integer(xml_attr(str_tokens, "col2")) str_expr <- substr(document$content[str_line1], str_col1, str_col2) - str_texts <- as.character(tryCatch(parse(str_expr, keep.source = FALSE), - error = function(e) NULL)) + str_texts <- tryCatch(as.character(parse(text = str_expr, keep.source = FALSE)), + error = function(e) NULL) if (length(str_texts)) { paths <- fs::path_abs(str_texts, rootPath)