From 2440a208d16d648062fcfe7a14a542c47d284aeb Mon Sep 17 00:00:00 2001 From: Hung Date: Sat, 3 Jun 2023 13:53:06 -0700 Subject: [PATCH] fix: nil because i'm new on lua --- README.md | 5 ----- lua/tsql.lua | 50 ++++++++++++++++++++++++----------------- lua/tsql/buf_select.lua | 4 +++- lua/tsql/category.lua | 10 +++++++++ repl.md | 13 +++++++++++ 5 files changed, 55 insertions(+), 27 deletions(-) create mode 100644 lua/tsql/category.lua create mode 100644 repl.md diff --git a/README.md b/README.md index e960cdb..9a996b0 100644 --- a/README.md +++ b/README.md @@ -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): diff --git a/lua/tsql.lua b/lua/tsql.lua index 6502e88..caa63ce 100644 --- a/lua/tsql.lua +++ b/lua/tsql.lua @@ -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 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 diff --git a/lua/tsql/buf_select.lua b/lua/tsql/buf_select.lua index 43dd5c7..e3812da 100644 --- a/lua/tsql/buf_select.lua +++ b/lua/tsql/buf_select.lua @@ -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 diff --git a/lua/tsql/category.lua b/lua/tsql/category.lua new file mode 100644 index 0000000..cdfa136 --- /dev/null +++ b/lua/tsql/category.lua @@ -0,0 +1,10 @@ +-- Interface-only file for type-hints +-- + +---@generic T +---@generic R +--- +---@alias Ctor 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. diff --git a/repl.md b/repl.md new file mode 100644 index 0000000..5068a1c --- /dev/null +++ b/repl.md @@ -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()))) +```