Skip to content

Commit 32b5981

Browse files
authoredFeb 6, 2025
Merge pull request #3053 from tomlau10/feat/workspaceFolder_var_sub
feat: add variable substitution support for vscode's `${workspaceFolder:x}`
2 parents 15a57e7 + c762646 commit 32b5981

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed
 

‎changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
cfgs[2] = {} -- only warns missing `b`
3232
```
3333
This enables the previous missing field check behavior before [#2970](https://github.com/LuaLS/lua-language-server/issues/2970)
34+
* `NEW` Added variable substitution support for vscode's `${workspaceFolder:x}` when resolving path placeholders [#2987](https://github.com/LuaLS/lua-language-server/issues/2987)
3435
* `NEW` Added `--check_format=json|pretty` for use with `--check` to output diagnostics in a human readable format.
3536

3637
## 3.13.5

‎script/files.lua

+11
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,17 @@ function m.resolvePathPlaceholders(path)
945945
elseif key:sub(1, 4) == "env:" then
946946
local env = os.getenv(key:sub(5))
947947
return env
948+
elseif key == "workspaceFolder" then
949+
local ws = require 'workspace'
950+
return ws.rootUri and furi.decode(ws.rootUri)
951+
elseif key:match("^workspaceFolder:.+$") then
952+
local folderName = key:match("^workspaceFolder:(.+)$")
953+
for _, scp in ipairs(scope.folders) do
954+
if scp:getFolderName() == folderName then
955+
return scp.uri and furi.decode(scp.uri)
956+
end
957+
end
958+
log.warn(('variable ${%s} cannot be resolved when processing path: %s'):format(key, path))
948959
end
949960
end)
950961

‎script/provider/provider.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ m.register 'initialize' {
119119

120120
if params.workspaceFolders then
121121
for _, folder in ipairs(params.workspaceFolders) do
122-
workspace.create(files.getRealUri(folder.uri))
122+
workspace.create(files.getRealUri(folder.uri), folder.name)
123123
end
124124
elseif params.rootUri then
125125
workspace.create(files.getRealUri(params.rootUri))

‎script/workspace/scope.lua

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local m = {}
88
---@class scope
99
---@field type scope.type
1010
---@field uri? uri
11+
---@field folderName? string
1112
---@field _links table<uri, boolean>
1213
---@field _data table<string, any>
1314
---@field _gc gc
@@ -134,6 +135,11 @@ function mt:getName()
134135
return self.uri or ('<' .. self.type .. '>')
135136
end
136137

138+
---@return string?
139+
function mt:getFolderName()
140+
return self.folderName
141+
end
142+
137143
function mt:gc(obj)
138144
self._gc:add(obj)
139145
end
@@ -187,10 +193,12 @@ end
187193
m.reset()
188194

189195
---@param uri uri
196+
---@param folderName? string
190197
---@return scope
191-
function m.createFolder(uri)
198+
function m.createFolder(uri, folderName)
192199
local scope = createScope 'folder'
193200
scope.uri = uri
201+
scope.folderName = folderName
194202

195203
local inserted = false
196204
for i, otherScope in ipairs(m.folders) do

‎script/workspace/workspace.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ function m.initRoot(uri)
4444
end
4545

4646
--- 初始化工作区
47-
function m.create(uri)
47+
function m.create(uri, folderName)
4848
log.info('Workspace create: ', uri)
49-
local scp = scope.createFolder(uri)
49+
local scp = scope.createFolder(uri, folderName)
5050
m.folders[#m.folders+1] = scp
5151
if uri == furi.encode '/'
5252
or uri == furi.encode(os.getenv 'HOME' or '') then

0 commit comments

Comments
 (0)