feat: Only load language configs for current language
- I subscribed to the BufEnter autocmd to switch language configs. Only previously opened languages are kept inside the languages table
This commit is contained in:
@@ -190,49 +190,5 @@ local c_config = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
local cpp_config = {
|
|
||||||
parent = {
|
|
||||||
class = { "class_specifier" },
|
|
||||||
},
|
|
||||||
data = {
|
|
||||||
class = {
|
|
||||||
["class_specifier"] = {
|
|
||||||
["0"] = {
|
|
||||||
extract = function(node)
|
|
||||||
local tree = {
|
|
||||||
{ retrieve = "first", node_type = "type_identifier", extract = true },
|
|
||||||
}
|
|
||||||
local nodes = nodes_utils:matching_nodes_from(node, tree)
|
|
||||||
local res = extractors:extract_from_matched(nodes)
|
|
||||||
return res
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
template = {
|
|
||||||
|
|
||||||
doxygen = {
|
return c_config
|
||||||
{ nil, "/**", { no_results = true, type = { "func", "file", "class" } } },
|
|
||||||
{ nil, " * @file", { no_results = true, type = { "file" } } },
|
|
||||||
{ nil, " * @brief $1", { no_results = true, type = { "func", "file", "class" } } },
|
|
||||||
{ nil, " */", { no_results = true, type = { "func", "file", "class" } } },
|
|
||||||
{ nil, "", { no_results = true, type = { "file" } } },
|
|
||||||
|
|
||||||
{ nil, "/**", { type = { "func", "class" } } },
|
|
||||||
{ "type_identifier", " * @class %s", { type = { "class" } } },
|
|
||||||
{ nil, " * @brief $1", { type = { "func", "class" } } },
|
|
||||||
{ nil, " *", { type = { "func", "class" } } },
|
|
||||||
{ "tparam", " * @tparam %s $1" },
|
|
||||||
{ "parameters", " * @param %s $1" },
|
|
||||||
{ "return_statement", " * @return $1" },
|
|
||||||
{ nil, " */", { type = { "func", "class" } } },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cpp_config = vim.tbl_deep_extend("force", c_config, cpp_config)
|
|
||||||
|
|
||||||
return {
|
|
||||||
c_config = c_config,
|
|
||||||
cpp_config = cpp_config,
|
|
||||||
}
|
|
||||||
|
|||||||
48
lua/neogen/configurations/cpp.lua
Normal file
48
lua/neogen/configurations/cpp.lua
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
local c_config = require("neogen.configurations.c")
|
||||||
|
local extractors = require("neogen.utilities.extractors")
|
||||||
|
local nodes_utils = require("neogen.utilities.nodes")
|
||||||
|
|
||||||
|
local cpp_config = {
|
||||||
|
parent = {
|
||||||
|
class = { "class_specifier" },
|
||||||
|
},
|
||||||
|
data = {
|
||||||
|
class = {
|
||||||
|
["class_specifier"] = {
|
||||||
|
["0"] = {
|
||||||
|
extract = function(node)
|
||||||
|
local tree = {
|
||||||
|
{ retrieve = "first", node_type = "type_identifier", extract = true },
|
||||||
|
}
|
||||||
|
local nodes = nodes_utils:matching_nodes_from(node, tree)
|
||||||
|
local res = extractors:extract_from_matched(nodes)
|
||||||
|
return res
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
template = {
|
||||||
|
|
||||||
|
doxygen = {
|
||||||
|
{ nil, "/**", { no_results = true, type = { "func", "file", "class" } } },
|
||||||
|
{ nil, " * @file", { no_results = true, type = { "file" } } },
|
||||||
|
{ nil, " * @brief $1", { no_results = true, type = { "func", "file", "class" } } },
|
||||||
|
{ nil, " */", { no_results = true, type = { "func", "file", "class" } } },
|
||||||
|
{ nil, "", { no_results = true, type = { "file" } } },
|
||||||
|
|
||||||
|
{ nil, "/**", { type = { "func", "class" } } },
|
||||||
|
{ "type_identifier", " * @class %s", { type = { "class" } } },
|
||||||
|
{ nil, " * @brief $1", { type = { "func", "class" } } },
|
||||||
|
{ nil, " *", { type = { "func", "class" } } },
|
||||||
|
{ "tparam", " * @tparam %s $1" },
|
||||||
|
{ "parameters", " * @param %s $1" },
|
||||||
|
{ "return_statement", " * @return $1" },
|
||||||
|
{ nil, " */", { type = { "func", "class" } } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cpp_config = vim.tbl_deep_extend("force", c_config, cpp_config)
|
||||||
|
|
||||||
|
return cpp_config
|
||||||
@@ -31,6 +31,8 @@ local default_locator = require("neogen.locators.default")
|
|||||||
local default_granulator = require("neogen.granulators.default")
|
local default_granulator = require("neogen.granulators.default")
|
||||||
local default_generator = require("neogen.generators.default")
|
local default_generator = require("neogen.generators.default")
|
||||||
|
|
||||||
|
local autocmd = require("neogen.utilities.autocmd")
|
||||||
|
|
||||||
-- Module definition ==========================================================
|
-- Module definition ==========================================================
|
||||||
|
|
||||||
--- Module setup
|
--- Module setup
|
||||||
@@ -39,7 +41,10 @@ local default_generator = require("neogen.generators.default")
|
|||||||
---
|
---
|
||||||
---@usage `require('neogen').setup({})` (replace `{}` with your `config` table)
|
---@usage `require('neogen').setup({})` (replace `{}` with your `config` table)
|
||||||
neogen.setup = function(opts)
|
neogen.setup = function(opts)
|
||||||
neogen.configuration = vim.tbl_deep_extend("keep", opts or {}, neogen.configuration)
|
-- Stores the user configuration globally so that we keep his configs when switching languages
|
||||||
|
neogen.user_configuration = opts or {}
|
||||||
|
|
||||||
|
neogen.configuration = vim.tbl_deep_extend("keep", neogen.user_configuration, neogen.configuration)
|
||||||
|
|
||||||
if neogen.configuration.enabled == true then
|
if neogen.configuration.enabled == true then
|
||||||
neogen.generate_command()
|
neogen.generate_command()
|
||||||
@@ -94,7 +99,14 @@ end
|
|||||||
---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
|
---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
|
||||||
---@test # Notes~
|
---@test # Notes~
|
||||||
---
|
---
|
||||||
--- - To disable a language, you can do: `languages["java"] = nil`
|
--- - to configure a language, just add your configurations in the `languages` table
|
||||||
|
--- For example, for the `lua` lang:
|
||||||
|
--- >
|
||||||
|
--- languages = {
|
||||||
|
--- lua = { -- Configuration here }
|
||||||
|
--- }
|
||||||
|
--- <
|
||||||
|
---
|
||||||
--- - `jump_text` is widely used and will certainly break most language templates.
|
--- - `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
|
--- I'm thinking of removing it from defaults so that it can't be modified
|
||||||
neogen.configuration = {
|
neogen.configuration = {
|
||||||
@@ -108,21 +120,7 @@ neogen.configuration = {
|
|||||||
jump_text = "$1",
|
jump_text = "$1",
|
||||||
|
|
||||||
-- Configuration for default languages
|
-- Configuration for default languages
|
||||||
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
|
--minidoc_afterlines_end
|
||||||
|
|
||||||
@@ -289,7 +287,7 @@ function neogen.jumpable(reverse)
|
|||||||
return cursor.jumpable(reverse)
|
return cursor.jumpable(reverse)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Command generation ==========================================================
|
-- Command and autocommands ====================================================
|
||||||
|
|
||||||
--- Generates the `:Neogen` command, which calls `neogen.generate()`
|
--- Generates the `:Neogen` command, which calls `neogen.generate()`
|
||||||
---@private
|
---@private
|
||||||
@@ -299,6 +297,17 @@ function neogen.generate_command()
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
autocmd.subscribe("BufEnter", function()
|
||||||
|
helpers.switch_language()
|
||||||
|
end)
|
||||||
|
|
||||||
|
vim.cmd([[
|
||||||
|
augroup ___neogen___
|
||||||
|
autocmd!
|
||||||
|
autocmd BufEnter * lua require'neogen.utilities.autocmd'.emit('BufEnter')
|
||||||
|
augroup END
|
||||||
|
]])
|
||||||
|
|
||||||
--- Contribute to Neogen
|
--- Contribute to Neogen
|
||||||
---
|
---
|
||||||
--- * Want to add a new language?
|
--- * Want to add a new language?
|
||||||
|
|||||||
52
lua/neogen/utilities/autocmd.lua
Normal file
52
lua/neogen/utilities/autocmd.lua
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
-- MIT License
|
||||||
|
--
|
||||||
|
-- Copyright (c) 2021 hrsh7th
|
||||||
|
--
|
||||||
|
-- Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
-- of this software and associated documentation files (the "Software"), to deal
|
||||||
|
-- in the Software without restriction, including without limitation the rights
|
||||||
|
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
-- copies of the Software, and to permit persons to whom the Software is
|
||||||
|
-- furnished to do so, subject to the following conditions:
|
||||||
|
--
|
||||||
|
-- The above copyright notice and this permission notice shall be included in all
|
||||||
|
-- copies or substantial portions of the Software.
|
||||||
|
--
|
||||||
|
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
-- SOFTWARE.
|
||||||
|
local autocmd = {}
|
||||||
|
|
||||||
|
autocmd.events = {}
|
||||||
|
|
||||||
|
---Subscribe autocmd
|
||||||
|
---@param event string
|
||||||
|
---@param callback function
|
||||||
|
---@return function
|
||||||
|
autocmd.subscribe = function(event, callback)
|
||||||
|
autocmd.events[event] = autocmd.events[event] or {}
|
||||||
|
table.insert(autocmd.events[event], callback)
|
||||||
|
return function()
|
||||||
|
for i, callback_ in ipairs(autocmd.events[event]) do
|
||||||
|
if callback_ == callback then
|
||||||
|
table.remove(autocmd.events[event], i)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---Emit autocmd
|
||||||
|
---@param event string
|
||||||
|
autocmd.emit = function(event)
|
||||||
|
autocmd.events[event] = autocmd.events[event] or {}
|
||||||
|
for _, callback in ipairs(autocmd.events[event]) do
|
||||||
|
callback()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return autocmd
|
||||||
@@ -18,4 +18,19 @@ return {
|
|||||||
|
|
||||||
return vim.tbl_keys(language.parent)
|
return vim.tbl_keys(language.parent)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
switch_language = function()
|
||||||
|
local filetype = vim.bo.filetype
|
||||||
|
local ok, ft_configuration = pcall(require, "neogen.configurations." .. filetype)
|
||||||
|
|
||||||
|
if not ok then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
neogen.configuration.languages[filetype] = vim.tbl_deep_extend(
|
||||||
|
"keep",
|
||||||
|
neogen.user_configuration.languages and neogen.user_configuration.languages[filetype] or {},
|
||||||
|
ft_configuration
|
||||||
|
)
|
||||||
|
end,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user