From 1d4a8096254374b1f223e06f2f8e505b093dceb4 Mon Sep 17 00:00:00 2001 From: Doug Kelkhoff <18220321+dgkf@users.noreply.github.com> Date: Thu, 29 Aug 2024 03:06:48 -0400 Subject: [PATCH] Avoid re-calculation of xml parse data for lsp use (#21) --- R/ast.R | 20 +++++++++++++++++--- R/reshape.R | 5 ++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/R/ast.R b/R/ast.R index 190fde7..4eb40dc 100644 --- a/R/ast.R +++ b/R/ast.R @@ -8,19 +8,33 @@ parse_xml <- function(info) { read_xml(xml_text) } -parse_info <- function(file = "", text = NULL, lines = NULL) { +parse_info <- function(file = "", text = NULL, lines = NULL, xml = NULL) { if (!is_null(text)) { lines <- as_lines(text) } list( file = file, - lines = lines + lines = lines, + xml = xml ) } +parse_info_complete <- function(info) { + if (is.null(info$lines)) { + info$lines <- readLines(info$file) + } + + if (is.null(info$xml)) { + info$xml <- parse_xml(info) + } + + info +} + is_info <- function(x) { - is.list(x) && all(c("file", "lines") %in% names(x)) + is.list(x) && all(c("file", "lines", "xml") %in% names(x)) } + check_info <- function(info, arg = caller_arg(info), call = caller_env()) { diff --git a/R/reshape.R b/R/reshape.R index 76ad324..9f0ca12 100644 --- a/R/reshape.R +++ b/R/reshape.R @@ -26,9 +26,8 @@ can_reshape <- function(data) { } reshape_info <- function(line, col, ..., info, to = NULL) { - xml <- parse_xml(info) - - call <- find_function_call(line, col, data = xml) + info <- parse_info_complete(info) + call <- find_function_call(line, col, data = info$xml) if (is_null(call)) { return() }