-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- rewrite tests to fix new structure
- Loading branch information
Showing
17 changed files
with
741 additions
and
377 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: Format | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
format: | ||
name: Stylua | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- run: date +%W > weekly | ||
|
||
- name: Restore cache | ||
id: cache | ||
uses: actions/cache@v4 | ||
with: | ||
path: | | ||
~/.cargo/bin | ||
key: ${{ runner.os }}-cargo-${{ hashFiles('weekly') }} | ||
|
||
- name: Install | ||
if: steps.cache.outputs.cache-hit != 'true' | ||
run: cargo install stylua | ||
|
||
- name: Format | ||
run: stylua --check lua/ --config-path=.stylua.toml |
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 @@ | ||
name: Lint | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
lint: | ||
name: Luacheck | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install luarocks | ||
sudo luarocks install luacheck | ||
- name: Lint | ||
run: luacheck lua/ --globals vim |
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,206 @@ | ||
local M = {} | ||
|
||
local ppath = require("plenary.path") | ||
local utils = require("telescope._extensions.utils") | ||
|
||
M.create_entry = function(filename, matches, iter_query, bufnr, capture_name) | ||
local entry = {} | ||
entry.filename = filename | ||
entry.bufnr = bufnr | ||
|
||
for i, _ in pairs(matches) do | ||
local curr_capture_name = iter_query.captures[i] | ||
local lnum, from, torow, to = matches[i]:range() | ||
local line_text = | ||
vim.api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, false)[1] | ||
|
||
-- if multiline and its the capture line just include the line | ||
if to ~= torow and curr_capture_name == "agrolens.scope" then | ||
from = 0 | ||
to = -1 | ||
end | ||
|
||
if not entry[curr_capture_name] then | ||
entry[curr_capture_name] = {} | ||
end | ||
|
||
entry[curr_capture_name].match = string.sub(line_text, from + 1, to) | ||
entry[curr_capture_name].capture = capture_name | ||
|
||
entry.lnum = lnum + 1 | ||
entry.col = from | ||
+ (string.len(line_text) - string.len(utils.ltrim(line_text))) | ||
entry.line = line_text | ||
end | ||
return entry | ||
end | ||
|
||
M.does_match = function(opts, entry) | ||
-- no matches defined | ||
if not opts.matches then | ||
return true | ||
end | ||
|
||
for _, match in ipairs(opts.matches) do | ||
local n = "agrolens." .. match.name | ||
if entry[n] then | ||
-- only one has to match (or and not and) | ||
if match.word == entry[n].match then | ||
return true | ||
end | ||
end | ||
end | ||
|
||
return false | ||
end | ||
|
||
M.add_entries = function( | ||
opts, | ||
entries, | ||
capture_names, | ||
bufnr, | ||
filename, | ||
filetype | ||
) | ||
local ts = vim.treesitter | ||
local dublicates = {} | ||
local ok, tsparser = pcall(ts.get_parser, bufnr, filetype) | ||
if ok and tsparser and type(tsparser) ~= "string" then | ||
local trees = tsparser:parse() | ||
local root = trees[1]:root() | ||
|
||
for _, capture_name in ipairs(capture_names) do | ||
local iter_query = | ||
vim.treesitter.query.get(filetype, "agrolens." .. capture_name) | ||
if iter_query then | ||
for _, matches, _ in iter_query:iter_matches(root, bufnr) do | ||
local entry = M.create_entry( | ||
filename, | ||
matches, | ||
iter_query, | ||
bufnr, | ||
capture_name | ||
) | ||
|
||
if opts.disable_indentation then | ||
entry.line = utils.all_trim(entry.line) | ||
end | ||
|
||
local formated_entry = utils.format_entry(entry) | ||
if not dublicates[formated_entry] then | ||
dublicates[formated_entry] = true | ||
if M.does_match(opts, entry) then | ||
table.insert(entries, entry) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
return entries | ||
end | ||
|
||
M.get_captures = function(opts) | ||
local entries = {} | ||
|
||
for _, bufnr in ipairs(opts.bufids) do | ||
local buffilename = vim.api.nvim_buf_get_name(bufnr) | ||
local relpath = ppath:new(buffilename):make_relative(opts.cwd) | ||
local filetype = vim.filetype.match({ buf = bufnr }) | ||
|
||
if filetype and filetype ~= "" then | ||
entries = M.add_entries( | ||
opts, | ||
entries, | ||
opts.queries, | ||
bufnr, | ||
relpath, | ||
filetype | ||
) | ||
end | ||
end | ||
return entries | ||
end | ||
|
||
M.generate_jump_list = function(opts) | ||
local entries = {} | ||
local ts = vim.treesitter | ||
local capture_names = opts.queries | ||
local bufnr = 0 | ||
local filetype = vim.filetype.match({ buf = bufnr }) | ||
if filetype and filetype ~= "" then | ||
local ok, tsparser = pcall(ts.get_parser, bufnr, filetype) | ||
if ok and tsparser and type(tsparser) ~= "string" then | ||
local trees = tsparser:parse() | ||
local root = trees[1]:root() | ||
|
||
for _, capture_name in ipairs(capture_names) do | ||
local iter_query = vim.treesitter.query.get( | ||
filetype, | ||
"agrolens." .. capture_name | ||
) | ||
if iter_query then | ||
for _, matches, _ in iter_query:iter_matches(root, bufnr) do | ||
for i, _ in pairs(matches) do | ||
local curr_capture_name = iter_query.captures[i] | ||
local lnum, _, _, _ = matches[i]:range() | ||
if curr_capture_name == "agrolens.scope" then | ||
entries[lnum + 1] = true | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
return utils.hash_keys_to_list(entries) | ||
end | ||
|
||
M.sanitize_opts = function(opts, telescope_opts, telescope_config) | ||
opts.queries = {} | ||
|
||
opts = vim.tbl_deep_extend( | ||
"force", | ||
telescope_opts, | ||
opts or {}, | ||
telescope_config | ||
) | ||
|
||
opts.query = opts.aliases[opts.query] or opts.query | ||
|
||
if opts.query then | ||
for query in opts.query:gmatch("[^,%s]+") do | ||
table.insert(opts.queries, query) | ||
end | ||
end | ||
|
||
local matches = {} | ||
if opts.match then | ||
local current_word = utils.get_word_at_cursor() | ||
|
||
for match in opts.match:gmatch("[^,%s]+") do | ||
local elements = utils.split(match, "=") | ||
|
||
if vim.tbl_count(elements) == 1 then | ||
table.insert(elements, current_word) | ||
end | ||
|
||
local entry = {} | ||
entry.name = elements[1] | ||
entry.word = elements[2] | ||
|
||
table.insert(matches, entry) | ||
end | ||
end | ||
|
||
opts.disable_indentation = telescope_config.disable_indentation or false | ||
|
||
if not vim.tbl_isempty(matches) then | ||
opts.matches = matches | ||
end | ||
|
||
return opts | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,77 @@ | ||
describe("c", function() | ||
|
||
local lens = nil | ||
local core = require("agrolens.core") | ||
local buffers = nil | ||
local eq = assert.equals | ||
|
||
it("load", function() | ||
vim.cmd.edit("tests/c/test.c") | ||
buffers = vim.api.nvim_list_bufs() | ||
assert.equal(#buffers, 1) | ||
eq(#buffers, 1) | ||
|
||
local content = vim.api.nvim_buf_get_lines(buffers[1], 0, -1, false) | ||
|
||
lens = require("telescope._extensions.agrolenslib") | ||
lens._get_captures({queries={"functions"}, bufids=buffers}) | ||
-- make sure buffer has content | ||
eq(string.match(content[1], "include"), "include") | ||
|
||
core.get_captures({ queries = { "functions" }, bufids = buffers }) | ||
end) | ||
|
||
it("functions", function() | ||
local entries = lens._get_captures({queries={"functions"}, bufids=buffers}) | ||
local entries = | ||
core.get_captures({ queries = { "functions" }, bufids = buffers }) | ||
|
||
assert.equals(#entries, 5) | ||
eq(#entries, 5) | ||
|
||
assert.equals('tests/c/test.c:16:0:int days_since_birth(struct Person *p) {', entries[1]) | ||
assert.equals('tests/c/test.c:45:0:int basic_calc(struct Person *p) {', entries[2]) | ||
assert.equals('tests/c/test.c:55:0:void print_person(struct Person *p, format_func func) {', entries[3]) | ||
assert.equals('tests/c/test.c:65:0:char **do_nothing(char **argv) {', entries[4]) | ||
assert.equals('tests/c/test.c:69:0:int main(int argc, char **argv) {', entries[5]) | ||
eq(entries[1].filename, "tests/c/test.c") | ||
eq(entries[1].lnum, 16) | ||
eq(entries[1].col, 0) | ||
eq(entries[1].line, "int days_since_birth(struct Person *p) {") | ||
eq(entries[2].line, "int basic_calc(struct Person *p) {") | ||
eq( | ||
entries[3].line, | ||
"void print_person(struct Person *p, format_func func) {" | ||
) | ||
eq(entries[4].line, "char **do_nothing(char **argv) {") | ||
eq(entries[5].line, "int main(int argc, char **argv) {") | ||
end) | ||
it("callings", function() | ||
local entries = lens._get_captures({queries={"callings"}, bufids=buffers}) | ||
assert.equals(#entries, 10) | ||
|
||
assert.equals('tests/c/test.c:23:2: if (sscanf(p->born, "%d-%d-%d", &day, &month, &year) != EOF) {', entries[1]) | ||
assert.equals('tests/c/test.c:25:4: now = time(0);', entries[2]) | ||
assert.equals('tests/c/test.c:29:4: parsedDate = localtime(&now);', entries[3]) | ||
assert.equals('tests/c/test.c:34:4: time_t born = mktime(parsedDate);', entries[4]) | ||
assert.equals('tests/c/test.c:49:2: return days_since_birth(p);', entries[5]) | ||
assert.equals('tests/c/test.c:60:2: int age = (*func)(p);', entries[6]) | ||
assert.equals('tests/c/test.c:62:2: printf("%s is %d days old\\n", p->name, (*func)(p));', entries[7]) | ||
assert.equals('tests/c/test.c:73:2: strncpy(donald.name, "Donald Duck", MAXNAME);', entries[8]) | ||
assert.equals('tests/c/test.c:74:2: strncpy(donald.born, "07-09-1934", MAXDATE);', entries[9]) | ||
assert.equals('tests/c/test.c:77:2: print_person(&donald, basic_calc);', entries[10]) | ||
local entries = | ||
core.get_captures({ queries = { "callings" }, bufids = buffers }) | ||
eq(#entries, 10) | ||
|
||
eq(entries[1].filename, "tests/c/test.c") | ||
eq(entries[1].lnum, 23) | ||
eq(entries[1].col, 2) | ||
|
||
eq( | ||
entries[1].line, | ||
' if (sscanf(p->born, "%d-%d-%d", &day, &month, &year) != EOF) {' | ||
) | ||
eq(entries[2].line, " now = time(0);") | ||
eq(entries[3].line, " parsedDate = localtime(&now);") | ||
eq(entries[4].line, " time_t born = mktime(parsedDate);") | ||
eq(entries[5].line, " return days_since_birth(p);") | ||
eq(entries[6].line, " int age = (*func)(p);") | ||
eq( | ||
entries[7].line, | ||
' printf("%s is %d days old\\n", p->name, (*func)(p));' | ||
) | ||
eq(entries[8].line, ' strncpy(donald.name, "Donald Duck", MAXNAME);') | ||
eq(entries[9].line, ' strncpy(donald.born, "07-09-1934", MAXDATE);') | ||
eq(entries[10].line, " print_person(&donald, basic_calc);") | ||
end) | ||
it("comments", function() | ||
local entries = lens._get_captures({queries={"comments"}, bufids=buffers}) | ||
assert.equals(#entries, 4) | ||
local entries = | ||
core.get_captures({ queries = { "comments" }, bufids = buffers }) | ||
eq(#entries, 4) | ||
|
||
assert.equals('tests/c/test.c:52:0:/*', entries[1]) | ||
assert.equals('tests/c/test.c:59:2: // not used', entries[2]) | ||
assert.equals('tests/c/test.c:71:2: // Create Donald', entries[3]) | ||
assert.equals('tests/c/test.c:76:2: // Print Donald', entries[4]) | ||
end) | ||
eq(entries[1].filename, "tests/c/test.c") | ||
eq(entries[1].lnum, 52) | ||
eq(entries[1].col, 0) | ||
|
||
eq(entries[1].line, "/*") | ||
eq(entries[2].line, " // not used") | ||
eq(entries[3].line, " // Create Donald") | ||
eq(entries[4].line, " // Print Donald") | ||
end) | ||
end) |
Oops, something went wrong.