Skip to content

Commit

Permalink
implements file extension based config
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdsk committed Jul 21, 2021
1 parent 0604b91 commit 25ec193
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
12 changes: 9 additions & 3 deletions lua/prosesitter.lua
Expand Up @@ -2,15 +2,21 @@ local log = require("prosesitter/log")
local shared = require("prosesitter/shared")
local on_event = require("prosesitter/on_event/on_event")
local api = vim.api
local buf_cfg = shared.cfg.by_buf

local M = {}
M.hover = require("prosesitter/hover") -- exposed for keybindings

local attached = {}
function M.attach()
local bufnr = api.nvim_get_current_buf()
if not attached[bufnr] then
attached[bufnr] = true
if buf_cfg[bufnr] == nil then
local extension = vim.fn.expand('%:e')
local cfg = shared.cfg.by_ext[extension]
if cfg == nil then
cfg = shared.cfg.default
end

buf_cfg[bufnr] = cfg
on_event.on_win(bufnr)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/prosesitter/on_event/check/check.lua
Expand Up @@ -7,7 +7,6 @@ M.schedualled = false
M.lint_req = nil
local callback = nil
local job = nil
local cfg = nil

local function do_check()
M.schedualled = false
Expand Down Expand Up @@ -43,6 +42,7 @@ local function closest_smaller(target, array)
return prev
end

local cfg = nil
-- iterator that returns a span and highlight group
function M.hl_iter(results, meta_array)
local problems = vim.fn.json_decode(results)["stdin.md"]
Expand Down
22 changes: 9 additions & 13 deletions lua/prosesitter/on_event/on_event.lua
Expand Up @@ -6,7 +6,6 @@ local query = require("vim.treesitter.query")
local disabled = false -- weather the plugin has been disabled
local get_parser = vim.treesitter.get_parser
local api = vim.api
local cfg = nil
local M = {}

local function postprocess(results, meta_array)
Expand All @@ -15,6 +14,7 @@ local function postprocess(results, meta_array)
end
end

local cfg = nil
local hl_queries = {}
local function comments(bufnr, start_l, end_l)
local parser = get_parser(bufnr)
Expand All @@ -31,16 +31,18 @@ local function comments(bufnr, start_l, end_l)
return
end
for id, node in hl_query:iter_captures(root_node, bufnr, start_l, end_l) do
if vim.tbl_contains(cfg.captures, hl_query.captures[id]) then
nodes[#nodes+1] = node
if vim.tbl_contains(cfg[bufnr].captures, hl_query.captures[id]) then
nodes[#nodes + 1] = node
end
end
end)
return nodes
end

function M.on_lines(_, buf, _, first_changed, last_changed, last_updated, byte_count, _, _)
if disabled then return true end -- stop calling on lines if the plugin was just disabled
function M.on_lines(_, buf, _, first_changed, last_changed, last_updated, _, _, _)
if disabled then
return true
end -- stop calling on lines if the plugin was just disabled

local lines_removed = first_changed == last_updated
if lines_removed then
Expand All @@ -54,7 +56,7 @@ function M.on_lines(_, buf, _, first_changed, last_changed, last_updated, byte_c
if start_row == end_row then
check.lint_req:add(buf, start_row, start_col, end_col)
else
for row=start_row,end_row-1 do
for row = start_row, end_row - 1 do
check.lint_req:add(buf, row, start_col, 0)
start_col = 0 -- only relevent for first line of comment
end
Expand All @@ -72,11 +74,6 @@ function M.on_win(bufnr)
return false
end

if vim.tbl_isempty(cfg.captures) then
return false
end

get_parser(bufnr)
local ok, parser = pcall(get_parser, bufnr)
if not ok then
return false
Expand All @@ -102,8 +99,7 @@ end
function M.setup(shared)
check:setup(shared, postprocess)
marks.setup(shared)

cfg = shared.cfg
cfg = shared.cfg.by_buf
end

function M.enable()
Expand Down
7 changes: 5 additions & 2 deletions lua/prosesitter/shared.lua
@@ -1,12 +1,15 @@
local M = {}

M.cfg = {
captures = { "comment" },
by_buf = {},
by_ext = {},
default = { captures = { "comment" } },
vale_to_hl = { error = "SpellBad", warning = "SpellRare", suggestion = "SpellCap" },
}

M.mark_to_hover = {}
M.ns_placeholders = nil
M.ns_marks = nil
M.mark_to_hover = {}

function M:setup()
M.ns_marks = vim.api.nvim_create_namespace("prosesitter_marks")
Expand Down

0 comments on commit 25ec193

Please sign in to comment.