ref: Code refactoring

- Try to not expose as much commands and code via neogen
- Use minidoc to generate docgen
This commit is contained in:
danymat
2022-01-26 11:28:03 +01:00
parent c575186ff9
commit 57e76c6102
19 changed files with 297 additions and 129 deletions

View File

@@ -1,3 +1,7 @@
local extractors = require("neogen.utilities.extractors")
local nodes_utils = require("neogen.utilities.nodes")
local default_locator = require("neogen.locators.default")
local c_params = {
retrieve = "first",
node_type = "parameter_list",
@@ -64,12 +68,12 @@ local c_function_extractor = function(node)
c_params,
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
if nodes.function_declarator then
local subnodes = neogen.utilities.nodes:matching_nodes_from(nodes.function_declarator[1]:parent(), tree)
local subres = neogen.utilities.extractors:extract_from_matched(subnodes)
local subnodes = nodes_utils:matching_nodes_from(nodes.function_declarator[1]:parent(), tree)
local subres = extractors:extract_from_matched(subnodes)
res = vim.tbl_deep_extend("keep", res, subres)
end
@@ -139,7 +143,7 @@ local c_config = {
},
locator = function(node_info, nodes_to_match)
local result = neogen.default_locator(node_info, nodes_to_match)
local result = default_locator(node_info, nodes_to_match)
if not result then
return nil
end
@@ -198,8 +202,8 @@ local cpp_config = {
local tree = {
{ retrieve = "first", node_type = "type_identifier", extract = true },
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
return res
end,
},

View File

@@ -1,3 +1,6 @@
local extractors = require("neogen.utilities.extractors")
local nodes_utils = require("neogen.utilities.nodes")
return {
parent = {
func = {
@@ -42,8 +45,8 @@ return {
},
},
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
res.identifier = res["_"]
return res
end,
@@ -97,8 +100,8 @@ return {
},
},
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
return res
end,
},

View File

@@ -1,3 +1,6 @@
local extractors = require("neogen.utilities.extractors")
local nodes_utils = require("neogen.utilities.nodes")
local function_tree = {
{
retrieve = "first",
@@ -58,8 +61,8 @@ return {
local results = {}
local tree = function_tree
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
if res.throws then
results.throw_statement = res.throws
@@ -77,8 +80,8 @@ return {
local results = {}
local tree = function_tree
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
results.parameters = res.identifier
if res.throws then
@@ -98,8 +101,8 @@ return {
extract = function(node)
local results = {}
local tree = { { retrieve = "all", node_type = "identifier", extract = true } }
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
results.class_name = res.identifier
return results

View File

@@ -1,3 +1,6 @@
local extractors = require("neogen.utilities.extractors")
local nodes_utils = require("neogen.utilities.nodes")
local function_tree = {
{
retrieve = "first",
@@ -37,8 +40,8 @@ return {
extract = function(node)
local results = {}
local tree = function_tree
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
results.parameters = res.identifier
results.return_statement = res.return_statement
@@ -51,8 +54,8 @@ return {
extract = function(node)
local results = {}
local tree = { { retrieve = "all", node_type = "function", subtree = function_tree } }
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
results.parameters = res.identifier
results.return_statement = res.return_statement
@@ -65,8 +68,8 @@ return {
extract = function(node)
local results = {}
local tree = { { retrieve = "all", node_type = "arrow_function", subtree = function_tree } }
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
results.parameters = res.identifier
results.return_statement = res.return_statement

View File

@@ -1,3 +1,6 @@
local extractors = require("neogen.utilities.extractors")
local nodes_utils = require("neogen.utilities.nodes")
local function_extractor = function(node, type)
if not vim.tbl_contains({ "local", "function" }, type) then
error("Incorrect call for function_extractor")
@@ -26,8 +29,8 @@ local function_extractor = function(node, type)
}
end
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
return {
parameters = res.identifier,
@@ -56,7 +59,7 @@ local extract_from_var = function(node)
extract = true,
},
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local nodes = nodes_utils:matching_nodes_from(node, tree)
return nodes
end
@@ -93,7 +96,7 @@ return {
["0"] = {
extract = function(node)
local nodes = extract_from_var(node)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local res = extractors:extract_from_matched(nodes)
return {
class_name = res.identifier,
}
@@ -109,7 +112,7 @@ return {
result.type = {}
local nodes = extract_from_var(node)
local res = neogen.utilities.extractors:extract_from_matched(nodes, { type = true })
local res = extractors:extract_from_matched(nodes, { type = true })
-- We asked the extract_from_var function to find the type node at right assignment.
-- We check if it found it, or else will put `any` in the type

View File

@@ -1,3 +1,6 @@
local extractors = require("neogen.utilities.extractors")
local nodes_utils = require("neogen.utilities.nodes")
return {
parent = {
type = { "property_declaration", "const_declaration", "foreach_statement" },
@@ -12,8 +15,8 @@ return {
local tree = {
{ node_type = "property_element", retrieve = "all", extract = true },
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
return res
end,
},
@@ -54,8 +57,8 @@ return {
},
},
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
return res
end,
},

View File

@@ -1,4 +1,6 @@
local ts_utils = require("nvim-treesitter.ts_utils")
local nodes_utils = require("neogen.utilities.nodes")
local extractors = require("neogen.utilities.extractors")
return {
-- Search for these nodes
@@ -74,7 +76,7 @@ return {
extract = true,
},
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local nodes = nodes_utils:matching_nodes_from(node, tree)
if nodes["typed_parameter"] then
results["typed_parameters"] = {}
for _, n in pairs(nodes["typed_parameter"]) do
@@ -82,15 +84,15 @@ return {
{ retrieve = "all", node_type = "identifier", extract = true },
{ retrieve = "all", node_type = "type", extract = true },
}
local typed_parameters = neogen.utilities.nodes:matching_nodes_from(n, type_subtree)
typed_parameters = neogen.utilities.extractors:extract_from_matched(typed_parameters)
local typed_parameters = nodes_utils:matching_nodes_from(n, type_subtree)
typed_parameters = extractors:extract_from_matched(typed_parameters)
table.insert(results["typed_parameters"], typed_parameters)
end
end
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local res = extractors:extract_from_matched(nodes)
if nodes["anonymous_return"] then
local _res = neogen.utilities.extractors:extract_from_matched(nodes, { type = true })
local _res = extractors:extract_from_matched(nodes, { type = true })
results.anonymous_return = _res.anonymous_return
end
@@ -133,7 +135,7 @@ return {
},
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local nodes = nodes_utils:matching_nodes_from(node, tree)
if vim.tbl_isempty(nodes) then
return {}

View File

@@ -1,3 +1,6 @@
local extractors = require("neogen.utilities.extractors")
local nodes_utils = require("neogen.utilities.nodes")
return {
parent = {
func = { "function_item", "function_signature_item" },
@@ -29,8 +32,8 @@ return {
},
},
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
return res
end,
},
@@ -64,8 +67,8 @@ return {
},
},
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
return res
end,
},

View File

@@ -1,3 +1,6 @@
local extractors = require("neogen.utilities.extractors")
local nodes_utils = require("neogen.utilities.nodes")
local construct_type_annotation = function(parameters)
local results = parameters and {} or nil
if parameters then
@@ -6,8 +9,8 @@ local construct_type_annotation = function(parameters)
{ retrieve = "all", node_type = "identifier", extract = true },
{ retrieve = "all", node_type = "type_identifier", recursive = true, extract = true },
}
local typed_parameters = neogen.utilities.nodes:matching_nodes_from(param, subtree)
typed_parameters = neogen.utilities.extractors:extract_from_matched(typed_parameters)
local typed_parameters = nodes_utils:matching_nodes_from(param, subtree)
typed_parameters = extractors:extract_from_matched(typed_parameters)
table.insert(results, typed_parameters)
end
end
@@ -56,8 +59,8 @@ return {
extract = function(node)
local results = {}
local tree = function_tree
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
results.type = construct_type_annotation(nodes.required_parameter)
results.opt_type = construct_type_annotation(nodes.optional_parameter)
@@ -72,8 +75,8 @@ return {
extract = function(node)
local results = {}
local tree = { { retrieve = "all", node_type = "function", subtree = function_tree } }
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
results.type = construct_type_annotation(nodes.type_annotation)
results.opt_type = construct_type_annotation(nodes.optional_parameter)
@@ -88,8 +91,8 @@ return {
extract = function(node)
local results = {}
local tree = { { retrieve = "all", node_type = "arrow_function", subtree = function_tree } }
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
results.type = construct_type_annotation(nodes.type_annotation)
results.opt_type = construct_type_annotation(nodes.optional_parameter)
@@ -124,8 +127,8 @@ return {
extract = true,
},
}
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local results = neogen.utilities.extractors:extract_from_matched(nodes)
local nodes = nodes_utils:matching_nodes_from(node, tree)
local results = extractors:extract_from_matched(nodes)
res.type = results.identifier
return res

View File

@@ -1,4 +1,5 @@
local ts_utils = require("nvim-treesitter.ts_utils")
local nodes = require("neogen.utilities.nodes")
--- Generates the prefix according to `template` options.
--- Prefix is generated with an offset (currently spaces) repetition of `n` times.
@@ -49,7 +50,7 @@ end
--- @param template table a template from the configuration
--- @param required_type string
--- @return table { line, content, opts }, with line being the line to append the content
neogen.default_generator = function(parent, data, template, required_type)
return function(parent, data, template, required_type)
local row_to_place, col_to_place
-- You can use a custom position placement
if template.position and type(template.position) == "function" then
@@ -73,7 +74,7 @@ neogen.default_generator = function(parent, data, template, required_type)
local append = template.append or {}
if append.position == "after" then
local child_node = neogen.utilities.nodes:matching_child_nodes(parent, append.child_name)[1]
local child_node = nodes:matching_child_nodes(parent, append.child_name)[1]
if child_node ~= nil then
row_to_place, col_to_place, _, _ = child_node:range()
end

View File

@@ -3,7 +3,7 @@ local ts_utils = require("nvim-treesitter.ts_utils")
--- Tries to use the configuration to find all required content nodes from the parent node
--- @param parent_node userdata the node found by the locator
--- @param node_data table the data from configurations[lang].data
neogen.default_granulator = function(parent_node, node_data)
return function(parent_node, node_data)
local result = {}
for parent_type, child_data in pairs(node_data) do

View File

@@ -1,22 +1,143 @@
local ok, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
assert(ok, "neogen requires nvim-treesitter to operate :(")
neogen = {}
--- What is Neogen ?
---
--- # Abstract~
---
--- Neogen is an extensible and extremely configurable annotation generator for your favorite languages
---
--- Want to know what the supported languages are ?
--- Check out the up-to-date readme section: https://github.com/danymat/neogen#supported-languages
---
--- # Concept~
---
--- - Create annotations with one keybind, and jump your cursor in the inserted annotation
--- - Defaults for multiple languages and annotation conventions
--- - Extremely customizable and extensible
--- - Written in lua (and uses Tree-sitter)
---@tag neogen
-- Require utilities
neogen.utilities = {}
require("neogen.utilities.extractors")
require("neogen.utilities.nodes")
require("neogen.utilities.cursor")
require("neogen.utilities.helpers")
-- Requires ===================================================================
-- Require defaults
require("neogen.locators.default")
require("neogen.granulators.default")
require("neogen.generators.default")
local neogen = {}
local notify = neogen.utilities.helpers.notify
local helpers = require("neogen.utilities.helpers")
local notify = helpers.notify
local cursor = require("neogen.utilities.cursor")
local default_locator = require("neogen.locators.default")
local default_granulator = require("neogen.granulators.default")
local default_generator = require("neogen.generators.default")
-- Module definition ==========================================================
--- Module setup
---
---@param opts table Config table (see |neogen.configuration|)
---
---@usage `require('neogen').setup({})` (replace `{}` with your `config` table)
neogen.setup = function(opts)
neogen.configuration = vim.tbl_deep_extend("keep", opts or {}, neogen.configuration)
if neogen.configuration.enabled == true then
neogen.generate_command()
end
-- Export module
_G.neogen = neogen
end
--- Neogen Usage
---
--- Neogen will use Treesitter parsing to properly generate annotations.
---
--- The basic idea is that Neogen will generate annotation to the type you're in.
--- For example, if you have a csharp function like (note the cursor position):
---
--- >
--- public class HelloWorld
--- {
--- public static void Main(string[] args)
--- {
--- # CURSOR HERE
--- Console.WriteLine("Hello world!");
--- return true;
--- }
---
--- public int someMethod(string str, ref int nm, void* ptr) { return 1; }
---
--- }
--- <
---
--- and you call `:Neogen class`, it will generate the annotation for the upper class:
---
--- >
--- /// <summary>
--- /// ...
--- /// </summary>
--- public class HelloWorld
--- {
--- <
---
--- Currently supported types are `func`, `class`, `type`, `file`.
--- Check out the up-to-date readme section: https://github.com/danymat/neogen#supported-languages
--- To know the supported types for a certain language
---
--- NOTE: calling `:Neogen` without any type is the same as `:Neogen func`
---@tag neogen.usage
--- # Basic configurations~
---
--- Neogen provides those defaults, and you can change them to suit your needs
---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
---@test # Notes~
---
--- - To disable a language, you can do: `languages["java"] = nil`
--- - `jump_text` is widely used and will certainly break most language templates.
--- I'm thinking of removing it from defaults so that it can't be modified
neogen.configuration = {
-- Enables Neogen capabilities
enabled = true,
-- Go to annotation after insertion, and change to insert mode
input_after_comment = true,
-- Symbol to find for jumping cursor in template
jump_text = "$1",
-- Configuration for default languages
languages = {
--minidoc_replace_start lua = { -- overwrite the defaults here }
lua = require("neogen.configurations.lua"),
--minidoc_replace_end
python = require("neogen.configurations.python"),
javascript = require("neogen.configurations.javascript"),
typescript = require("neogen.configurations.typescript"),
c = require("neogen.configurations.c").c_config,
cpp = require("neogen.configurations.c").cpp_config,
go = require("neogen.configurations.go"),
java = require("neogen.configurations.java"),
rust = require("neogen.configurations.rust"),
cs = require("neogen.configurations.csharp"),
php = require("neogen.configurations.php"),
},
}
--minidoc_afterlines_end
-- Basic API ===================================================================
--- The only function required to use Neogen.
---
--- It'll try to find the first parent that matches a certain type.
--- For example, if you are inside a function, and called `generate({ type = "func" })`,
--- Neogen will go until the start of the function and start annotating for you.
---
---@param opts table Options to change default behaviour of generation.
--- - {opts.type} `(string?, default: "func")` Which type we are trying to use for generating annotations.
--- Currently supported: `func`, `class`, `type`, `file`
---@tag test.generator
neogen.generate = function(opts)
opts = opts or {}
opts.type = (opts.type == nil or opts.type == "") and "func" or opts.type -- Default type
@@ -42,9 +163,9 @@ neogen.generate = function(opts)
return
end
language.locator = language.locator or neogen.default_locator
language.granulator = language.granulator or neogen.default_granulator
language.generator = language.generator or neogen.default_generator
language.locator = language.locator or default_locator
language.granulator = language.granulator or default_granulator
language.generator = language.generator or default_generator
if not language.parent[opts.type] or not language.data[opts.type] then
notify("Type `" .. opts.type .. "` not supported", vim.log.levels.WARN)
@@ -74,7 +195,7 @@ neogen.generate = function(opts)
)
if #content ~= 0 then
neogen.utilities.cursor.del_extmarks() -- Delete previous extmarks before setting any new ones
cursor.del_extmarks() -- Delete previous extmarks before setting any new ones
local jump_text = language.jump_text or neogen.configuration.jump_text
@@ -107,7 +228,7 @@ neogen.generate = function(opts)
-- First and last extmarks are needed to know the range of inserted content
if neogen.configuration.input_after_comment == true then
-- Creates extmark for the beggining of the content
neogen.utilities.cursor.create(to_place + 1, start_column)
cursor.create(to_place + 1, start_column)
-- Creates extmarks for the content
for i, value in pairs(content_with_marks) do
local start = 0
@@ -117,7 +238,7 @@ neogen.generate = function(opts)
if not start then
break
end
neogen.utilities.cursor.create(to_place + i, start - count * #jump_text)
cursor.create(to_place + i, start - count * #jump_text)
count = count + 1
end
end
@@ -131,72 +252,65 @@ neogen.generate = function(opts)
col = pos[2] + 1
end
neogen.utilities.cursor.create(pos[1], col)
cursor.create(pos[1], col)
-- Creates extmark for the end of the content
neogen.utilities.cursor.create(to_place + #content + 1, 0)
cursor.create(to_place + #content + 1, 0)
neogen.utilities.cursor.jump({ first_time = true })
cursor.jump({ first_time = true })
end
end
end
end
-- Expose more API ============================================================
-- Expose match_commands for `:Neogen` completion
neogen.match_commands = helpers.match_commands
-- Required for use with completion engine =====================================
--- Jumps to the next cursor template position
---@private
function neogen.jump_next()
neogen.utilities.cursor.jump()
cursor.jump()
end
--- Jumps to the next cursor template position
---@private
function neogen.jump_prev()
neogen.utilities.cursor.jump_prev()
cursor.jump_prev()
end
--- Checks if the cursor can jump backwards or forwards
--- @param reverse number? if `-1`, will try to see if can be jumped backwards
---@private
function neogen.jumpable(reverse)
return neogen.utilities.cursor.jumpable(reverse)
return cursor.jumpable(reverse)
end
function neogen.match_commands()
if vim.bo.filetype == "" then
return {}
end
local language = neogen.configuration.languages[vim.bo.filetype]
if not language or not language.parent then
return {}
end
return vim.tbl_keys(language.parent)
end
-- Command generation ==========================================================
--- Generates the `:Neogen` command, which calls `neogen.generate()`
---@private
function neogen.generate_command()
vim.api.nvim_command(
'command! -nargs=? -complete=customlist,v:lua.neogen.match_commands -range -bar Neogen lua require("neogen").generate({ type = <q-args>})'
)
end
neogen.setup = function(opts)
neogen.configuration = vim.tbl_deep_extend("keep", opts or {}, {
input_after_comment = true, -- bool, If you want to jump with the cursor after annotation
jump_text = "$1", -- symbol to find for jumping cursor in template
-- DEFAULT CONFIGURATION
languages = {
lua = require("neogen.configurations.lua"),
python = require("neogen.configurations.python"),
javascript = require("neogen.configurations.javascript"),
typescript = require("neogen.configurations.typescript"),
c = require("neogen.configurations.c").c_config,
cpp = require("neogen.configurations.c").cpp_config,
go = require("neogen.configurations.go"),
java = require("neogen.configurations.java"),
rust = require("neogen.configurations.rust"),
cs = require("neogen.configurations.csharp"),
php = require("neogen.configurations.php"),
},
})
if neogen.configuration.enabled == true then
neogen.generate_command()
end
end
--- Contribute to Neogen
---
--- * Want to add a new language?
--- 1. Using the defaults to generate a new language support:
--- https://github.com/danymat/neogen/blob/main/docs/adding-languages.md
--- 2. (advanced) Only if the defaults aren't enough, please see here:
--- https://github.com/danymat/neogen/blob/main/docs/advanced-integration.md
--- * Want to contribute to an existing language?
--- I guess you can still read the previous links, as they have some valuable knowledge inside.
--- You can go and directly open/edit the configuration file relative to the language you want to contribute to.
---
---Feel free to submit a PR, I will be happy to help you !
---@tag neogen.develop
return neogen

View File

@@ -7,7 +7,7 @@
--- @param node_info node_info a node informations
--- @param nodes_to_match table a list of parent nodes to match
--- @return userdata node one of the nodes to match directly above the given node
neogen.default_locator = function(node_info, nodes_to_match)
return function(node_info, nodes_to_match)
if not node_info.current then
if vim.tbl_contains(nodes_to_match, node_info.root:type()) then
return node_info.root

View File

@@ -1,4 +1,5 @@
local ts_utils = require("nvim-treesitter.ts_utils")
local default_locator = require("neogen.locators.default")
return function(node_info, nodes_to_match)
-- We're dealing with a lua comment and we need to escape its grasp
@@ -8,7 +9,7 @@ return function(node_info, nodes_to_match)
node_info.current = ts_utils.get_node_at_cursor()
end
local found_node = neogen.default_locator(node_info, nodes_to_match)
local found_node = default_locator(node_info, nodes_to_match)
-- for functions like "local x = function ()...end" the assignment_statement
-- will be fetched just before the variable_declaration

View File

@@ -1,4 +1,4 @@
neogen.utilities.cursor = {}
cursor = {}
local neogen_ns = vim.api.nvim_create_namespace("neogen")
local neogen_virt_text_ns = vim.api.nvim_create_namespace("neogen_virt_text")
@@ -8,7 +8,7 @@ local current_position = 1
--- @param line number
--- @param col number
--- @return number
neogen.utilities.cursor.create = function(line, col)
cursor.create = function(line, col)
current_position = 1
local new_col = col == 0 and 0 or col - 1
return vim.api.nvim_buf_set_extmark(0, neogen_ns, line - 1, new_col, {})
@@ -17,7 +17,7 @@ end
--- Find next created extmark and goes to it.
--- It removes the extmark afterwards.
--- First jumpable extmark is the one after the extmarks responsible of start/end of annotation
neogen.utilities.cursor.go_next_extmark = function()
cursor.go_next_extmark = function()
local extm_list = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {})
local position = current_position + 1
table.sort(extm_list, function(a, b)
@@ -37,7 +37,7 @@ end
--- Goes to next extmark and start insert mode.
--- If `opts.first_time` is supplied, will try to go to normal mode before going to extmark
--- @param opts table
neogen.utilities.cursor.jump = function(opts)
cursor.jump = function(opts)
opts = opts or {}
-- This is weird, the first time nvim goes to insert is not the same as when i'm already on insert mode
@@ -46,12 +46,12 @@ neogen.utilities.cursor.jump = function(opts)
vim.api.nvim_command("startinsert")
end
if neogen.utilities.cursor.go_next_extmark() then
if cursor.go_next_extmark() then
vim.api.nvim_command("startinsert")
end
end
neogen.utilities.cursor.jump_prev = function()
cursor.jump_prev = function()
local marks = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {})
table.sort(marks, function(a, b)
return a[1] < b[1]
@@ -69,7 +69,7 @@ neogen.utilities.cursor.jump_prev = function()
end
--- Delete all active extmarks
neogen.utilities.cursor.del_extmarks = function()
cursor.del_extmarks = function()
local extmarks = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {})
for _, v in pairs(extmarks) do
vim.api.nvim_buf_del_extmark(0, neogen_ns, v[1])
@@ -78,7 +78,7 @@ end
--- Checks if there are still possible jump positions to perform
--- Verifies if the cursor is in the last annotated part
neogen.utilities.cursor.jumpable = function(reverse)
cursor.jumpable = function(reverse)
local extm_list = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {})
if #extm_list == 0 then
return false
@@ -106,3 +106,5 @@ neogen.utilities.cursor.jumpable = function(reverse)
return false
end
end
return cursor

View File

@@ -1,6 +1,6 @@
local ts_utils = require("nvim-treesitter.ts_utils")
neogen.utilities.extractors = {
return {
--- Extract the content from each node from data
--- @param _ any self
--- @param data table a list of k,v values where k is the node_type and v a table of nodes

View File

@@ -1,5 +1,21 @@
neogen.utilities.helpers = {
return {
notify = function(msg, log_level)
vim.notify(msg, log_level, { title = "Neogen" })
end,
--- Generates a list of possible types in the current language
---@private
match_commands = function()
if vim.bo.filetype == "" then
return {}
end
local language = neogen.configuration.languages[vim.bo.filetype]
if not language or not language.parent then
return {}
end
return vim.tbl_keys(language.parent)
end,
}

View File

@@ -1,4 +1,4 @@
neogen.utilities.nodes = {
return {
--- Get a list of child nodes that match the provided node name
--- @param _ any
--- @param parent userdata the parent's node