-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
256 additions
and
211 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
local helpers = require('nvim-test.helpers') | ||
local clear = helpers.clear | ||
local exec_lua = helpers.exec_lua | ||
local cmd = helpers.api.nvim_command | ||
local feed = helpers.feed | ||
local api = helpers.api | ||
local fn = helpers.fn | ||
|
||
local tc_helpers = require('test.helpers') | ||
local get_langs = tc_helpers.get_langs | ||
|
||
---@param line string | ||
---@return string? | ||
local function parse_directive(line) | ||
--- @type string? | ||
local directive = line:match('{{([A-Z]+)}}') | ||
return directive | ||
end | ||
|
||
--- @param filename string | ||
--- @return table<integer, integer[]>? contexts | ||
local function parse_directives(filename) | ||
local f = io.open(filename, 'r') | ||
if not f then | ||
return | ||
end | ||
|
||
local context = {} --- @type table<integer,integer[]> | ||
local contexts = {} --- @type table<integer,integer[]> | ||
|
||
local i = 0 | ||
for l in f:lines() do | ||
local directive = parse_directive(l) | ||
if directive then | ||
if directive == 'TEST' then | ||
context = {} | ||
elseif directive == 'CURSOR' then | ||
contexts[i] = vim.deepcopy(context) | ||
elseif directive == 'CONTEXT' then | ||
table.insert(context, i) | ||
elseif directive == 'POPCONTEXT' then | ||
table.remove(context, #context) | ||
end | ||
end | ||
i = i + 1 | ||
end | ||
f:close() | ||
|
||
for _, c in pairs(contexts) do | ||
table.sort(c) | ||
end | ||
|
||
return contexts | ||
end | ||
|
||
for _, lang in ipairs(get_langs()) do | ||
describe('contexts (' .. lang .. '):', function() | ||
setup(function() | ||
clear() | ||
cmd([[set runtimepath+=.,./nvim-treesitter]]) | ||
|
||
-- Required to load custom predicates | ||
exec_lua([[require'nvim-treesitter'.setup()]]) | ||
|
||
cmd([[let $XDG_CACHE_HOME='scratch/cache']]) | ||
tc_helpers.install_langs(lang) | ||
end) | ||
|
||
local test_file = 'test/lang/test.' .. lang | ||
if not vim.uv.fs_stat(test_file) then | ||
pending('No test file') | ||
return | ||
end | ||
|
||
local contexts = parse_directives(test_file) | ||
|
||
if not contexts or not next(contexts) then | ||
pending('No tests') | ||
return | ||
end | ||
|
||
for cursor_row, context_rows in pairs(contexts) do | ||
it(('line %s in %s'):format(cursor_row, test_file), function() | ||
cmd('edit ' .. test_file) | ||
local bufnr = api.nvim_get_current_buf() | ||
local winid = api.nvim_get_current_win() | ||
api.nvim_win_set_cursor(winid, { cursor_row + 1, 0 }) | ||
assert(fn.getline('.'):match('{{CURSOR}}')) | ||
feed(string.format('zt%d<C-y>', #context_rows + 2)) | ||
|
||
--- @type [integer,integer,integer,integer][] | ||
local ranges = exec_lua( | ||
[[ | ||
return require('treesitter-context.context').get(...) | ||
]], | ||
bufnr, | ||
winid | ||
) | ||
|
||
local act_context_rows = {} --- @type integer[] | ||
for _, r in ipairs(ranges) do | ||
table.insert(act_context_rows, r[1]) | ||
end | ||
|
||
helpers.eq( | ||
context_rows, | ||
act_context_rows, | ||
string.format('test for cursor %d failed', cursor_row) | ||
) | ||
end) | ||
end | ||
end) | ||
end |
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,63 @@ | ||
local helpers = require('nvim-test.helpers') | ||
local exec_lua = helpers.exec_lua | ||
|
||
local M = {} | ||
|
||
function M.install_langs(langs) | ||
if type(langs) == 'string' then | ||
langs = { langs } | ||
end | ||
exec_lua( | ||
[[ | ||
local langs = ... | ||
require'nvim-treesitter.configs'.setup { | ||
ensure_installed = langs, | ||
sync_install = true, | ||
} | ||
-- Clear the message "<lang> has been installed". | ||
print(' ') | ||
]], | ||
langs | ||
) | ||
end | ||
|
||
local langs --- @type string[]? | ||
|
||
local lang_alias = { | ||
janet_simple = 'janet' | ||
} | ||
|
||
function M.get_langs() | ||
if langs then | ||
return langs | ||
end | ||
|
||
langs = {} | ||
local f = assert(io.open('README.md', 'r')) | ||
local readme_langs = {} --- @type table<string,true> | ||
for l in f:lines() do | ||
--- @type string? | ||
local lang = l:match('%- %[x%] `([^`]+)`') | ||
if lang then | ||
readme_langs[lang] = true | ||
end | ||
end | ||
f:close() | ||
|
||
f = assert(io.open('nvim-treesitter/lockfile.json', 'r')) | ||
|
||
for k in pairs(vim.json.decode(f:read('*a'))) do | ||
k = lang_alias[k] or k | ||
if readme_langs[k] then | ||
langs[#langs + 1] = k | ||
readme_langs[k] = nil | ||
end | ||
end | ||
if next(readme_langs) then | ||
print('Invalid languages:', table.concat(vim.tbl_keys(readme_langs), ', ')) | ||
end | ||
return langs | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- Test the query for each language is valid and update the README. | ||
local helpers = require('nvim-test.helpers') | ||
local exec_lua = helpers.exec_lua | ||
|
||
local tc_helpers = require('test.helpers') | ||
local install_langs = tc_helpers.install_langs | ||
local get_langs = tc_helpers.get_langs | ||
|
||
describe('query:', function() | ||
local readme_lines = {} --- @type string[] | ||
|
||
setup(function() | ||
local f = assert(io.open('README.md', 'r')) | ||
for l in f:lines() do | ||
readme_lines[#readme_lines + 1] = l | ||
end | ||
f:close() | ||
end) | ||
|
||
for _, lang in ipairs(get_langs()) do | ||
it(lang, function() | ||
install_langs(lang) | ||
|
||
local index --- @type integer | ||
local line_orig --- @type string | ||
|
||
-- Find the line in the README for this lang | ||
for i, l in pairs(readme_lines) do | ||
--- @type string? | ||
local lang1 = l:match('%- %[x%] `([^`]+)`') | ||
if lang1 == lang then | ||
index, line_orig = i, l | ||
l = l:gsub(' %(broken%)', '') | ||
-- Mark as broken now, unmark later. | ||
readme_lines[i] = l .. ' (broken)' | ||
break | ||
end | ||
end | ||
|
||
assert(index) | ||
|
||
exec_lua([[vim.treesitter.query.get(...)]], lang, 'context') | ||
|
||
readme_lines[index] = line_orig:gsub(' %(broken%)', '') | ||
end) | ||
end | ||
|
||
teardown(function() | ||
-- Update the README. | ||
local f = assert(io.open('README.md', 'w')) | ||
for _, l in ipairs(readme_lines) do | ||
f:write(l) | ||
f:write('\n') | ||
end | ||
f:close() | ||
end) | ||
end) |
Oops, something went wrong.