Skip to content

Commit

Permalink
Merge pull request #300 from renkun-ken/try-parse-string
Browse files Browse the repository at this point in the history
Try parsing string in link and color
  • Loading branch information
renkun-ken authored Jul 3, 2020
2 parents 556e31a + a5a9f24 commit 8794cc4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
45 changes: 22 additions & 23 deletions R/color.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +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 <- as.character(parse(text = str_expr, keep.source = FALSE))
str_texts <- tryCatch(as.character(parse(text = str_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
Expand Down
47 changes: 23 additions & 24 deletions R/link.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +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 <- as.character(parse(text = str_expr, keep.source = FALSE))
str_texts <- tryCatch(as.character(parse(text = str_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)
}

0 comments on commit 8794cc4

Please sign in to comment.