Skip to content

Commit

Permalink
Merge pull request #40 from desdic/20240223refactor
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
desdic committed Feb 25, 2024
2 parents 773374d + b45e272 commit 337f680
Show file tree
Hide file tree
Showing 25 changed files with 864 additions and 702 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:
manager: sudo apt-get
packages: -y make lua-check
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: date +%F > todays-date
- name: Restore from todays cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: _neovim
key: ${{ runner.os }}-${{ matrix.url }}-${{ hashFiles('todays-date') }}
Expand Down Expand Up @@ -56,9 +56,9 @@ jobs:
export VIM="${PWD}/_neovim/share/nvim/runtime"
nvim --headless -c "lua require'nvim-treesitter.install'.prefer_git=false" -c "TSInstallSync lua c cpp rust python go make yaml markdown perl glsl toml php ruby" -c "q"
# - name: Run tests
# run: |
# export PATH="${PWD}/_neovim/bin:${PATH}"
# export VIM="${PWD}/_neovim/share/nvim/runtime"
# nvim --version
# make test
- name: Run tests
run: |
export PATH="${PWD}/_neovim/bin:${PATH}"
export VIM="${PWD}/_neovim/share/nvim/runtime"
nvim --version
make test
26 changes: 26 additions & 0 deletions .github/workflows/format.yml
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
18 changes: 18 additions & 0 deletions .github/workflows/lint.yml
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ fmt:
stylua lua/ --config-path=.stylua.toml

lint:
luacheck lua/telescope
luacheck lua/ --globals vim

deps:
@mkdir -p deps
git clone --depth 1 https://github.com/echasnovski/mini.nvim deps/mini.nvim

localsetup:
ln -s ~/.local/share/nvim/lazy/nvim-treesitter ~/.local/share/nvim/site/pack/vendor/start/nvim-treesitter.git

documentation:
nvim --headless --noplugin -u ./scripts/minimal_init_doc.lua -c "lua require('mini.doc').generate()" -c "qa!"

Expand Down
2 changes: 1 addition & 1 deletion doc/agrolens.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Usage ~
Default telescope options:
>
agrolens.telescope_opts = {
-- Enable/Disable debug messages
-- Enable/Disable debug messages (is put in ~/.cache/nvim/agrolens.log)
debug = false,
-- Some tree-sitter plugins uses hidden buffers
Expand Down
206 changes: 206 additions & 0 deletions lua/agrolens/core.lua
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
14 changes: 5 additions & 9 deletions lua/agrolens/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ agrolens.create_entry = function(node, level)
end

agrolens.traverse = function(results, node, cur_node, level)
local entry = {}
if is_same(node, cur_node) then
entry = agrolens.create_entry(node, level)
local entry = agrolens.create_entry(node, level)

table.insert(results, entry)

Expand All @@ -131,7 +130,7 @@ agrolens.traverse = function(results, node, cur_node, level)

if node ~= nil then
if node:named() then
entry = agrolens.create_entry(node, level)
local entry = agrolens.create_entry(node, level)
table.insert(results, entry)

for next_node, _ in node:iter_children() do
Expand Down Expand Up @@ -238,22 +237,19 @@ agrolens.match_line = function(block, line, captures, capindex)
end

agrolens.create_content = function(block, node_match, captures, capindex)
local content = ""

if block.field_name then
content = string.rep(" ", block.level)
local content = string.rep(" ", block.level)
.. block.field_name
.. ":("
.. block.node_type

if is_same(block.node, node_match) then
captures[capindex] = "@agrolens.name"
end
else
content = string.rep(" ", block.level) .. "(" .. block.node_type
return content
end

return content
return string.rep(" ", block.level) .. "(" .. block.node_type
end

--- Generate treesitter query
Expand Down
14 changes: 9 additions & 5 deletions lua/telescope/_extensions/agrolens.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ end
local agrolens = require("telescope._extensions.agrolenslib")

local setup = function(ext_config, _)
local config = vim.F.if_nil(ext_config, { debug = false })
agrolens.telescope_config = ext_config

-- redefine log if debug enabled
if vim.F.if_nil(config.debug, false) then
agrolens.log = plenary.log.new({ plugin = "agrolens", level = "debug" })
local level = "info"
if ext_config.debug == true then
level = "debug"
end

agrolens.telescope_config = ext_config
agrolens.log = plenary.log.new({
plugin = "agrolens",
level = level,
})

if agrolens.telescope_config.disable_devicons ~= false then
local has_devicons
has_devicons, agrolens.devicons = pcall(require, "nvim-web-devicons")
if has_devicons then
if not agrolens.devicons.has_loaded() then
agrolens.log.debug("loading devicons")
agrolens.devicons.setup()
end
end
Expand Down
Loading

0 comments on commit 337f680

Please sign in to comment.