-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improvements to loading project-local settings (#290)
- Loading branch information
Showing
12 changed files
with
203 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
local M = {} | ||
|
||
local function tbl_set(tbl, keys, value) | ||
local next = table.remove(keys, 1) | ||
if #keys > 0 then | ||
tbl[next] = tbl[next] or {} | ||
tbl_set(tbl[next], keys, value) | ||
else | ||
tbl[next] = value | ||
end | ||
end | ||
|
||
---@param tbl table | ||
---@param json_key string e.g. "rust-analyzer.check.overrideCommand" | ||
---@param json_value unknown | ||
local function override_tbl_values(tbl, json_key, json_value) | ||
local keys = vim.split(json_key, '%.') | ||
tbl_set(tbl, keys, json_value) | ||
end | ||
|
||
---@param json_content string | ||
---@return table | ||
function M.silent_decode(json_content) | ||
local ok, json_tbl = pcall(vim.json.decode, json_content) | ||
if not ok or type(json_tbl) ~= 'table' then | ||
return {} | ||
end | ||
return json_tbl | ||
end | ||
|
||
---@param tbl table | ||
---@param json_tbl { [string]: unknown } | ||
---@param key_predicate? fun(string): boolean | ||
function M.override_with_json_keys(tbl, json_tbl, key_predicate) | ||
for json_key, value in pairs(json_tbl) do | ||
if not key_predicate or key_predicate(json_key) then | ||
override_tbl_values(tbl, json_key, value) | ||
end | ||
end | ||
end | ||
|
||
---@param tbl table | ||
---@param json_tbl { [string]: unknown } | ||
function M.override_with_rust_analyzer_json_keys(tbl, json_tbl) | ||
M.override_with_json_keys(tbl, json_tbl, function(key) | ||
return vim.startswith(key, 'rust-analyzer') | ||
end) | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
local config = require('rustaceanvim.config.server') | ||
local compat = require('rustaceanvim.compat') | ||
describe('Decode rust-analyzer settings from json', function() | ||
local function expect_correct_loading(json_path) | ||
local settings = config.load_rust_analyzer_settings(json_path) | ||
assert.True(settings['rust-analyzer'].cargo.targetDir) | ||
assert.same({ 'leptosfmt', '--stdin', '--rustfmt' }, settings['rust-analyzer'].rustfmt.overrideCommand) | ||
assert.True(settings['rust-analyzer'].checkOnSave) | ||
end | ||
it('Fixture: rust-analyzer key', function() | ||
local json_path = compat.joinpath(vim.fn.getcwd(), 'spec', 'fixtures', 'rust-analyzer-json') | ||
expect_correct_loading(json_path) | ||
end) | ||
it('Fixture: top-level settings', function() | ||
local json_path = compat.joinpath(vim.fn.getcwd(), 'spec', 'fixtures', 'rust-analyzer-json-top-level') | ||
expect_correct_loading(json_path) | ||
end) | ||
end) |
5 changes: 5 additions & 0 deletions
5
spec/fixtures/rust-analyzer-json-top-level/rust-analyzer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"cargo.targetDir": true, | ||
"rustfmt.overrideCommand": ["leptosfmt", "--stdin", "--rustfmt"], | ||
"checkOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rust-analyzer.cargo.targetDir": true, | ||
"rust-analyzer.rustfmt.overrideCommand": ["leptosfmt", "--stdin", "--rustfmt"], | ||
"rust-analyzer.checkOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
local json = require('rustaceanvim.config.json') | ||
describe('Decode rust-analyzer settings from json', function() | ||
it('can extract rust-analyzer key from index', function() | ||
local json_content = [[ | ||
{ | ||
"rust-analyzer.check.overrideCommand": [ | ||
"cargo", | ||
"check", | ||
"-p", | ||
"service_b", | ||
"--message-format=json" | ||
], | ||
"rust-analyzer.foo.enable": true, | ||
"rust-analyzer.foo.bar.enable": true, | ||
"rust-analyzer.foo.bar.baz.bat": "something deeply nested", | ||
"some-other-key.foo.bar.baz.bat": "should not be included" | ||
} | ||
]] | ||
local tbl = {} | ||
local json_tbl = json.silent_decode(json_content) | ||
json.override_with_rust_analyzer_json_keys(tbl, json_tbl) | ||
assert.same({ | ||
['rust-analyzer'] = { | ||
check = { | ||
overrideCommand = { | ||
'cargo', | ||
'check', | ||
'-p', | ||
'service_b', | ||
'--message-format=json', | ||
}, | ||
}, | ||
foo = { | ||
enable = true, | ||
bar = { | ||
enable = true, | ||
baz = { | ||
bat = 'something deeply nested', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, tbl) | ||
end) | ||
end) |