fix: nil because i'm new on lua

master
Hung 2023-06-03 13:53:06 -07:00
parent edcceb144c
commit 2440a208d1
5 changed files with 55 additions and 27 deletions

View File

@ -54,11 +54,6 @@ local ts = require('tsql')
ts.t(ts.buf_match.any(), ts.ts_query.from_scm("string"), ts.sink_by.print()):do_nvim()
```
Here's the same thing in a one-liner
```lua
:lua local ts = require('tsql'); ts.t(ts.buf_match.any(), ts.ts_query.from_scm("string"), ts.sink_by.print()):do_nvim()
```
## Installation
Use your favorite package manager to install the plugin. For example, with [vim-plug](https://github.com/junegunn/vim-plug):

View File

@ -17,38 +17,37 @@ M.sink_by = {}
---@alias Format fun(nodes: QNode[]): string
M.format = {}
M.format.default = M.format.display
---@class Sink
---@field sink fun(self, nodes: QNode[])
M.Sink = {}
M.Sink.__index = M.Sink
---@return Sink
---@param func fun(self, nodes:QNode[])
---@type Ctor<fun(nodes: QNode[]), Sink>
function M.sink_by.pure_fn(func)
return setmetatable({
sink = func
local self = setmetatable({
sink = function(_, nodes)
return func(nodes)
end
}, M.Sink)
self.__index = M.Sink
return self
end
---@return Sink
function M.sink_by.highlight()
return setmetatable({
---@type fun(nodes: QNode[])
sink = function(nodes)
for _, node in ipairs(nodes) do
vim.highlight.range(
node.buf.bufnr,
M.config.nvim_ns,
M.config.nvim_hl_group,
{ node.start.row_0, node.start.col_0 },
{ node.end_ex_col.row_0, node.end_ex_col.col_0 },
{}
)
end
return M.sink_by.pure_fn(function(nodes)
for _, node in ipairs(nodes) do
vim.highlight.range(
node.buf.bufnr,
M.config.nvim_ns,
M.config.nvim_hl_group,
{ node.start.row_0, node.start.col_0 },
{ node.end_ex_col.row_0, node.end_ex_col.col_0 },
{}
)
end
}, M.Sink)
end)
end
---@type Format
@ -67,8 +66,14 @@ end
function M.format.display(nodes)
local texts = {}
for _, node in ipairs(nodes) do
local text = vim.api.nvim_buf_get_text(node.buf.bufnr, node.start.row_0, node.start.col_0, node.end_ex_col.row_0,
node.end_ex_col.col_0)
local text = vim.api.nvim_buf_get_text(
node.buf.bufnr,
node.start.row_0,
node.start.col_0,
node.end_ex_col.row_0,
node.end_ex_col.col_0,
{}
)
table.insert(texts, table.concat(text, '\n'))
end
return table.concat(texts, '\n\n')
@ -83,6 +88,7 @@ function M.format.dump(nodes)
end
M.format.debug = M.format.dump
M.format.default = M.format.display
---@param format Format | nil
---@return Sink
@ -214,6 +220,7 @@ end
function M.Store:new()
local o = { highlighting = {} }
setmetatable(o, self)
self.__index = self
return o
end
@ -233,6 +240,7 @@ function M.setup(config)
end, { nargs = "?", desc = "tsql DSL invocation" })
M.store = M.Store:new()
print("tsql v0.0.1")
end
return M

View File

@ -15,6 +15,7 @@ local M = {}
---from `vim.treesitter.get_parser(bufnr: number):lang() -> string [may fail]`
---@field is_loaded boolean whether it is loaded
M.QBuf = {}
M.QBuf.__index = M.QBuf
---@return string language
---NOTE: may fail with string
@ -52,7 +53,7 @@ function M.QBuf:new(bufnr, path, filetype, lang, is_loaded)
is_loaded = is_loaded
}
setmetatable(qbuf, self)
self.__index = self
qbuf.__index = self
return qbuf
end
@ -86,6 +87,7 @@ end
---NOTE: assigns `match_fn` private field
function M.BufMatch.new(match_fn)
local self = setmetatable({}, M.BufMatch)
self.__index = M.BufMatch
self.match_fn = match_fn
return self
end

10
lua/tsql/category.lua Normal file
View File

@ -0,0 +1,10 @@
-- Interface-only file for type-hints
--
---@generic T
---@generic R
---
---@alias Ctor<T, R> fun(arg: T): R
---A good practice is to only has a single argument per ctor/class method.
---If you need multiple arguments, having explicit names is much better than
---positional args, unless you want currying.

13
repl.md Normal file
View File

@ -0,0 +1,13 @@
# Simple location for me to use as a neovim repl
```lua
:lua local ts = require('tsql'); ts.t(ts.buf_match.any(), ts.ts_query.from_scm("string"), ts.sink_by.print()):do_nvim();
:lua local ts = require('tsql'); local tsql = ts.t(ts.buf_match.any(), ts.ts_query.from_scm("string"), ts.sink_by.print()); print(vim.inspect(tsql.buf_match));
-- print found bufs
:lua local t = require('tsql'); print(vim.inspect(t.nvim_get_qbufs()))
-- check that buf filter works
:lua local t = require('tsql'); print(vim.inspect(t.buf_match.any():filter_on(t.nvim_get_qbufs())))
```