Skip to content

Commit f112cd9

Browse files
committed
refactor: rename default fallback workspace key to '_default_'
1 parent d86eb84 commit f112cd9

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

R/languageserver.R

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#' An implementation of the Language Server Protocol for R
77
"_PACKAGE"
88

9+
DEFAULT_WORKSPACE <- "_default_"
10+
911
#' The language server
1012
#'
1113
#' Describe the language server and how it interacts with clients.
@@ -68,7 +70,7 @@ LanguageServer <- R6::R6Class("LanguageServer",
6870

6971
self$pending_replies <- collections::dict()
7072
self$workspaces <- collections::dict()
71-
self$workspaces$set("", Workspace$new(NULL))
73+
self$workspaces$set(DEFAULT_WORKSPACE, Workspace$new(NULL))
7274

7375
super$initialize()
7476
},
@@ -84,15 +86,15 @@ LanguageServer <- R6::R6Class("LanguageServer",
8486
}
8587
},
8688
add_workspace = function(uri) {
87-
key <- if (length(uri) == 0) "" else uri
89+
key <- if (length(uri) == 0) DEFAULT_WORKSPACE else uri
8890
if (!self$workspaces$has(key)) {
8991
path <- if (length(uri) == 0) NULL else path_from_uri(uri)
9092
new_workspace <- Workspace$new(path)
9193
self$workspaces$set(key, new_workspace)
92-
94+
9395
# Remove documents from the fallback workspace if they belong here
9496
if (!is.null(path)) {
95-
fallback <- self$workspaces$get("")
97+
fallback <- self$workspaces$get(DEFAULT_WORKSPACE)
9698
for (doc_uri in fallback$documents$keys()) {
9799
doc_path <- path_from_uri(doc_uri)
98100
if (path_has_parent(doc_path, path)) {
@@ -103,43 +105,54 @@ LanguageServer <- R6::R6Class("LanguageServer",
103105
}
104106
},
105107
remove_workspace = function(uri) {
106-
key <- if (length(uri) == 0) "" else uri
107-
if (key == "") return(invisible(NULL))
108+
key <- if (length(uri) == 0) DEFAULT_WORKSPACE else uri
109+
if (key == DEFAULT_WORKSPACE) {
110+
return(invisible(NULL))
111+
}
108112
if (self$workspaces$has(key)) {
109113
workspace <- self$workspaces$get(key)
110114
for (doc_uri in workspace$documents$keys()) {
111115
diagnostics_callback(self, doc_uri, NULL, list())
112116
doc <- workspace$documents$get(doc_uri)
113117
if (isTRUE(doc$is_open)) {
114-
self$workspaces$get("")$documents$set(doc_uri, doc)
118+
self$workspaces$get(DEFAULT_WORKSPACE)$documents$set(doc_uri, doc)
115119
}
116120
}
117121
self$workspaces$remove(key)
118122
}
119123
},
120124
get_workspace = function(uri) {
121-
workspaces_list <- self$workspaces$values()
125+
# Find the best matching workspace for a given URI.
126+
# If no match is found, or if URI is empty, return the default workspace.
122127

123-
default_key <- if (length(self$rootUri) == 0) "" else self$rootUri
124-
default_workspace <- self$workspaces$get(default_key, self$workspaces$get(""))
128+
# Determine the default workspace to use as a fallback
129+
default_key <- if (length(self$rootUri) == 0) DEFAULT_WORKSPACE else self$rootUri
130+
fallback <- self$workspaces$get(default_key, self$workspaces$get(DEFAULT_WORKSPACE))
125131

126-
if (length(uri) == 0) return(default_workspace)
132+
if (length(uri) == 0) {
133+
return(fallback)
134+
}
127135

128136
path <- path_from_uri(uri)
129137
best_match <- NULL
130138
max_len <- -1
131-
132-
for (workspace in workspaces_list) {
139+
140+
for (workspace in self$workspaces$values()) {
133141
root <- workspace$root
134142
if (length(root) > 0 && path_has_parent(path, root)) {
135-
if (nchar(root) > max_len) {
136-
max_len <- nchar(root)
143+
root_len <- nchar(root)
144+
if (root_len > max_len) {
145+
max_len <- root_len
137146
best_match <- workspace
138147
}
139148
}
140149
}
141-
142-
best_match %||% default_workspace
150+
151+
if (is.null(best_match)) {
152+
return(fallback)
153+
}
154+
155+
best_match
143156
},
144157
load_workspace = function(workspace) {
145158
if (!is_package(workspace$root)) {
@@ -163,8 +176,7 @@ LanguageServer <- R6::R6Class("LanguageServer",
163176
self$load_workspace(workspace)
164177
}
165178
},
166-
text_sync = function(
167-
uri, document, run_lintr = FALSE, parse = FALSE, delay = 0) {
179+
text_sync = function(uri, document, run_lintr = FALSE, parse = FALSE, delay = 0) {
168180
if (!self$pending_replies$has(uri)) {
169181
self$pending_replies$set(uri, list(
170182
`textDocument/documentSymbol` = collections::queue(),

0 commit comments

Comments
 (0)