Skip to content

Commit 2564649

Browse files
authored
Merge pull request #413 from renkun-ken/faster-completion
Faster completion
2 parents f9b218c + 6155b1d commit 2564649

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

R/completion.R

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ completion_reply <- function(id, uri, workspace, document, point, capabilities)
455455
items = list()
456456
)))
457457
}
458+
459+
t0 <- Sys.time()
458460
snippet_support <- isTRUE(capabilities$completionItem$snippetSupport) &&
459461
getOption("languageserver.snippet_support", TRUE)
460462

@@ -499,10 +501,32 @@ completion_reply <- function(id, uri, workspace, document, point, capabilities)
499501
)
500502
}
501503

504+
init_count <- length(completions)
505+
nmax <- getOption("languageserver.max_completions", 200)
506+
507+
if (init_count > nmax) {
508+
isIncomplete <- TRUE
509+
label_text <- vapply(completions, "[[", character(1), "label")
510+
sort_text <- vapply(completions, "[[", character(1), "sortText")
511+
order <- order(!startsWith(label_text, token), sort_text)
512+
completions <- completions[order][seq_len(nmax)]
513+
} else {
514+
isIncomplete <- FALSE
515+
}
516+
517+
t1 <- Sys.time()
518+
519+
logger$info("completions: ", list(
520+
init_count = init_count,
521+
final_count = length(completions),
522+
time = as.numeric(t1 - t0),
523+
isIncomplete = isIncomplete
524+
))
525+
502526
Response$new(
503527
id,
504528
result = list(
505-
isIncomplete = FALSE,
529+
isIncomplete = isIncomplete,
506530
items = completions
507531
)
508532
)

R/utils.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ check_scope <- function(uri, document, point) {
180180
}
181181
}
182182

183-
match_with <- function(x, pattern) {
183+
match_with <- function(x, token) {
184+
pattern <- gsub(".", "\\.", token, fixed = TRUE)
184185
grepl(pattern, x, ignore.case = TRUE)
185186
}
186187

0 commit comments

Comments
 (0)