Skip to content

Commit

Permalink
Merge pull request #3053 from tomlau10/feat/workspaceFolder_var_sub
Browse files Browse the repository at this point in the history
feat: add variable substitution support for vscode's `${workspaceFolder:x}`
  • Loading branch information
sumneko authored Feb 6, 2025
2 parents 15a57e7 + c762646 commit 32b5981
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
cfgs[2] = {} -- only warns missing `b`
```
This enables the previous missing field check behavior before [#2970](https://github.com/LuaLS/lua-language-server/issues/2970)
* `NEW` Added variable substitution support for vscode's `${workspaceFolder:x}` when resolving path placeholders [#2987](https://github.com/LuaLS/lua-language-server/issues/2987)
* `NEW` Added `--check_format=json|pretty` for use with `--check` to output diagnostics in a human readable format.

## 3.13.5
Expand Down
11 changes: 11 additions & 0 deletions script/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,17 @@ function m.resolvePathPlaceholders(path)
elseif key:sub(1, 4) == "env:" then
local env = os.getenv(key:sub(5))
return env
elseif key == "workspaceFolder" then
local ws = require 'workspace'
return ws.rootUri and furi.decode(ws.rootUri)
elseif key:match("^workspaceFolder:.+$") then
local folderName = key:match("^workspaceFolder:(.+)$")
for _, scp in ipairs(scope.folders) do
if scp:getFolderName() == folderName then
return scp.uri and furi.decode(scp.uri)
end
end
log.warn(('variable ${%s} cannot be resolved when processing path: %s'):format(key, path))
end
end)

Expand Down
2 changes: 1 addition & 1 deletion script/provider/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ m.register 'initialize' {

if params.workspaceFolders then
for _, folder in ipairs(params.workspaceFolders) do
workspace.create(files.getRealUri(folder.uri))
workspace.create(files.getRealUri(folder.uri), folder.name)
end
elseif params.rootUri then
workspace.create(files.getRealUri(params.rootUri))
Expand Down
10 changes: 9 additions & 1 deletion script/workspace/scope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local m = {}
---@class scope
---@field type scope.type
---@field uri? uri
---@field folderName? string
---@field _links table<uri, boolean>
---@field _data table<string, any>
---@field _gc gc
Expand Down Expand Up @@ -134,6 +135,11 @@ function mt:getName()
return self.uri or ('<' .. self.type .. '>')
end

---@return string?
function mt:getFolderName()
return self.folderName
end

function mt:gc(obj)
self._gc:add(obj)
end
Expand Down Expand Up @@ -187,10 +193,12 @@ end
m.reset()

---@param uri uri
---@param folderName? string
---@return scope
function m.createFolder(uri)
function m.createFolder(uri, folderName)
local scope = createScope 'folder'
scope.uri = uri
scope.folderName = folderName

local inserted = false
for i, otherScope in ipairs(m.folders) do
Expand Down
4 changes: 2 additions & 2 deletions script/workspace/workspace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ function m.initRoot(uri)
end

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

0 comments on commit 32b5981

Please sign in to comment.