From 486085b3ab7b26ff6e7003e0f79a545ac2ea4141 Mon Sep 17 00:00:00 2001 From: Kevin Manca Date: Tue, 7 Jan 2025 08:57:44 +0100 Subject: [PATCH] chore: various improvements and merge w/ nvim branch --- after/ftplugin/java.lua | 74 +----------------- lua/lib/gradle.lua | 116 +++++++++++++++++++++++++++++ lua/lib/notes.lua | 3 +- lua/lib/ui/statusline.lua | 3 +- lua/plugins/editor/completion.lua | 1 + lua/plugins/init.lua | 2 +- lua/plugins/lsp/handlers.lua | 2 +- lua/plugins/lsp/init.lua | 6 +- lua/plugins/utils/markdown.lua | 4 +- snippets/general/java/javadoc.json | 14 ++-- snippets/general/lua/luadoc.json | 53 ++++--------- 11 files changed, 151 insertions(+), 127 deletions(-) create mode 100644 lua/lib/gradle.lua diff --git a/after/ftplugin/java.lua b/after/ftplugin/java.lua index af41363..9b33874 100644 --- a/after/ftplugin/java.lua +++ b/after/ftplugin/java.lua @@ -21,9 +21,10 @@ local extendedClientCapabilities = require("jdtls").extendedClientCapabilities extendedClientCapabilities.resolveAdditionalTextEditsSupport = true extendedClientCapabilities.document_formatting = false -local root_dir = vim.fs.root(0, { ".git", "mvnw", "gradlew", "pom.xml" }) or vim.fs.dirname(vim.api.nvim_buf_get_name(0)) +local root_dir = vim.fs.root(0, { ".git", "mvnw", "gradlew", "pom.xml" }) + or vim.fs.dirname(vim.api.nvim_buf_get_name(0)) local cache_dir = vim.fn.stdpath "cache" -local project_name = vim.fs.basename(root_dir) +local project_name = vim.fs.basename(root_dir or vim.uv.cwd()) local workspace_dir = string.format("%s/java/wksp/%s", cache_dir, project_name) local launcher_path = vim.fn.glob(data_path .. "/mason/packages/jdtls/plugins/org.eclipse.equinox.launcher_*.jar", true) @@ -296,71 +297,4 @@ vim.api.nvim_create_autocmd({ "BufWritePost" }, { end, }) -if vim.fn.filereadable(root_dir .. "/gradlew") then - local gradlew = root_dir .. "/gradlew" - - local function get_gradle_tasks() - if vim.g["gradle_" .. root_dir] ~= nil then - return vim.g["gradle_" .. root_dir] - end - - local taskList = {} - local out = vim.system({ gradlew, "tasks", "--all" }, { text = true }):wait() - if out.code == 0 then - for line in out.stdout:gmatch "[^\r\n]+" do - if line:find " - " then - local taskName = line:match "^(.-)%s+-" --line:match("^(.-) -") - if taskName then - table.insert(taskList, taskName) - end - end - end - vim.g["gradle_" .. root_dir] = taskList - else - vim.notify("Error executing command: " .. out.stderr, vim.log.levels.ERROR, { text = "Gradle (tasks)" }) - end - return taskList - end - - local function select_gradle_task(tasks, callback) - vim.ui.select(tasks, { - prompt = "⟩ Gradle task: ", - }, function(choice) - if not choice then - return - end - callback(choice) - end) - end - - local function run_gradle_task(task) - local msg = string.format(" running: < gradlew %s >", task) - vim.notify(msg, vim.log.levels.INFO, { title = "Gradle" }) - vim.system({ gradlew, task }, { text = true }, function(obj) - if obj.code ~= 0 then - vim.notify(obj.stderr, vim.log.levels.WARN) - else - vim.print(obj.stdout) - vim.schedule(function() - vim.fn.setqflist({}, "r", { title = "Gradle Output", lines = vim.split(obj.stdout, "\n") }) - vim.cmd.copen() - end) - end - end) - end - - vim.api.nvim_create_user_command("Gradle", function(input) - local task = nil - if input.args ~= "" then - run_gradle_task(task) - else - local tasks = get_gradle_tasks() - select_gradle_task(tasks, function(selected_task) - run_gradle_task(selected_task) - end) - end - end, { - nargs = "?", - complete = get_gradle_tasks, - }) -end \ No newline at end of file +require("lib.gradle").setup { root_dir = root_dir } diff --git a/lua/lib/gradle.lua b/lua/lib/gradle.lua new file mode 100644 index 0000000..d380342 --- /dev/null +++ b/lua/lib/gradle.lua @@ -0,0 +1,116 @@ +------------------------------------- +-- File : gradle.lua +-- Description : gradle utils functions +-- Author : Kevin +-- Last Modified: 06/01/2025 - 12:19 +------------------------------------- + +local M = {} + +---Get Gradle tasks for the project +---@param gradlew string the path for the gradle +---@param root_dir string the root directory of the project +local function get_gradle_tasks(gradlew, root_dir) + if vim.g["gradle_" .. root_dir] ~= nil then + return vim.g["gradle_" .. root_dir] + end + + local taskList = {} + local out = vim.system({ gradlew, "tasks", "--all" }, { text = true }):wait() + if out.code == 0 then + for line in out.stdout:gmatch "[^\r\n]+" do + if line:find " - " then + local taskName = line:match "^(.-)%s+-" --line:match("^(.-) -") + if taskName then + table.insert(taskList, taskName) + end + end + end + vim.g["gradle_" .. root_dir] = taskList + else + vim.notify("Error executing command: " .. out.stderr, vim.log.levels.ERROR, { text = "Gradle (tasks)" }) + end + return taskList +end + +local function run_gradle_task(gradlew, task) + local msg = string.format(" running: < gradlew %s >", task) + -- vim.notify(msg, 2, { title = "Gradle" }) + print("Gradle⟩ " .. msg) + vim.system({ gradlew, task }, { text = true }, function(obj) + local out = (obj.code ~= 0) and obj.stderr or obj.stdout + + vim.schedule(function() + local text = string.format(" OUTPUT⟩ gradle %s\n\n%s", task, out) + local lines = vim.split(text, "\n") + + local buf = vim.api.nvim_create_buf(false, true) + + vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) + local height = math.floor(vim.o.lines * 0.25) + local win = vim.api.nvim_open_win(buf, true, { + height = height, + win = -1, + split = "below", + }) + vim.api.nvim_set_option_value("buftype", "nofile", { buf = buf }) + vim.api.nvim_set_option_value("swapfile", false, { buf = buf }) + vim.api.nvim_set_option_value("bufhidden", "wipe", { buf = buf }) + vim.api.nvim_set_option_value("filetype", "sh", { buf = buf }) + vim.api.nvim_set_option_value("number", false, { win = win }) + vim.api.nvim_set_option_value("relativenumber", false, { win = win }) + + vim.keymap.set("n", "q", "close", { buffer = buf }) + vim.keymap.set("n", "", "close", { buffer = buf }) + end) + end) +end + +local function select_and_run_gradle_task(gradlew, tasks) + vim.ui.select(tasks, { + prompt = "⟩ Gradle task: ", + }, function(choice) + if not choice then + return + end + run_gradle_task(gradlew, choice) + end) +end + +---Setup Gradle module +---@param opts table +function M.setup(opts) + local root_dir = opts.root_dir or nil + + if not root_dir then + vim.notify("root_dir not passed!", vim.log.levels.ERROR, { title = "Gradle" }) + return + end + + if not vim.fn.filereadable(root_dir .. "/gradlew") then + vim.notify("No gradlew bin found for the path\n" .. root_dir, vim.log.levels.WARN, { title = "Gradle" }) + return + end + + local gradlew = root_dir .. "/gradlew" + + vim.api.nvim_create_user_command("Gradle", function(input) + local task = nil + if input.args ~= "" then + run_gradle_task(gradlew, task) + else + local tasks = get_gradle_tasks(gradlew, root_dir) + select_and_run_gradle_task(gradlew, tasks) + end + end, { + nargs = "?", + complete = get_gradle_tasks, + }) + + vim.keymap.set("n", "dg", function() + local tasks = get_gradle_tasks(gradlew, root_dir) + select_and_run_gradle_task(gradlew, tasks) + end, { desc = "Gradle", buffer = true }) +end + +return M \ No newline at end of file diff --git a/lua/lib/notes.lua b/lua/lib/notes.lua index 0aba4b1..6bfcfb3 100644 --- a/lua/lib/notes.lua +++ b/lua/lib/notes.lua @@ -8,7 +8,6 @@ local note = {} ---Get available notes ----@private ---@return table local function get_notes() local notes = {} @@ -75,4 +74,4 @@ function note.open_note() end end -return note \ No newline at end of file +return note diff --git a/lua/lib/ui/statusline.lua b/lua/lib/ui/statusline.lua index a9397c3..6b90971 100644 --- a/lua/lib/ui/statusline.lua +++ b/lua/lib/ui/statusline.lua @@ -92,6 +92,7 @@ local sl = { dapui_hover = "󰃤", dapui_watches = "󰃤", default = "", + gradle = "", }, { __index = function(t, k) local has_icons, icons = pcall(require, "mini.icons") @@ -366,7 +367,7 @@ end ---Get lsp status and if active get names of server running ---@return string lsp_status local function get_lsp_info() - return #vim.lsp.get_clients() ~= 0 and string.format("%s• ", sl.colors.name) + return #vim.lsp.get_clients { bufnr = 0 } ~= 0 and string.format("%s• ", sl.colors.name) or string.format("%s• ", sl.colors.lspnoactive) end diff --git a/lua/plugins/editor/completion.lua b/lua/plugins/editor/completion.lua index 8ea6b15..d321572 100644 --- a/lua/plugins/editor/completion.lua +++ b/lua/plugins/editor/completion.lua @@ -28,6 +28,7 @@ return { [""] = { "show", "hide" }, [""] = { "cancel" }, }, + ghost_text = { enabled = true }, }, appearance = { nerd_font_variant = "mono", diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index bcb9e7a..a59c434 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -179,7 +179,7 @@ local M = { ---LaTeX { "lervag/vimtex", - ft = { "tex", "plaintex" }, + ft = { "tex", "plaintex", "bib" }, config = function() vim.g.vimtex_view_method = "sioyek" vim.g.vimtex_quickfix_mode = 0 -- don't open qflist on compile errors diff --git a/lua/plugins/lsp/handlers.lua b/lua/plugins/lsp/handlers.lua index c36c6bd..fe8a7e4 100644 --- a/lua/plugins/lsp/handlers.lua +++ b/lua/plugins/lsp/handlers.lua @@ -91,4 +91,4 @@ return { }, }) end, -} \ No newline at end of file +} diff --git a/lua/plugins/lsp/init.lua b/lua/plugins/lsp/init.lua index dc88da3..1ab0f69 100644 --- a/lua/plugins/lsp/init.lua +++ b/lua/plugins/lsp/init.lua @@ -29,7 +29,7 @@ local function set_buf_keymaps(client, bufnr) local _, tele_builtin = pcall(require, "telescope.builtin") local function nmap(tbl) - vim.keymap.set("n", tbl[1], tbl[2], { buffer = bufnr, desc = "LSP❭ " .. tbl[3] }) + vim.keymap.set("n", tbl[1], tbl[2], { buffer = bufnr, desc = "Lsp❭ " .. tbl[3] }) end nmap { @@ -66,9 +66,9 @@ local function set_buf_keymaps(client, bufnr) end if client.supports_method "textDocument/signatureHelp" then - vim.keymap.set("s", "", vim.lsp.buf.signature_help, { + vim.keymap.set("s", "", vim.lsp.buf.signature_help, { buffer = bufnr, - desc = "LSP❭ SignatureHelp", + desc = "Lsp❭ SignatureHelp", }) end diff --git a/lua/plugins/utils/markdown.lua b/lua/plugins/utils/markdown.lua index f6475f9..e3b6e77 100644 --- a/lua/plugins/utils/markdown.lua +++ b/lua/plugins/utils/markdown.lua @@ -64,6 +64,6 @@ return { vim.keymap.set("n", "r", function() require("render-markdown").toggle() - end, { desc = "Render Markdown" }) + end, { desc = "Render Markdown", buffer = true }) end, -} +} \ No newline at end of file diff --git a/snippets/general/java/javadoc.json b/snippets/general/java/javadoc.json index 0371ad1..8ffe5b0 100644 --- a/snippets/general/java/javadoc.json +++ b/snippets/general/java/javadoc.json @@ -7,13 +7,9 @@ " *", " * ${2:Description.}$0", " *", - " * @param ${4:name} ${5:Type and description of the parameter.}", - " * @return ${6:Type and description of the returned object.}", + " * @param ${4:name} ${5:Description of the parameter.}", + " * @return ${6:Description of the returned object.}", " *", - " * @example", - " * ```", - " * ${7:Write me later}", - " * ```", " */" ], "description": "A Java comment block including short summary, description, param, return, and examples." @@ -25,12 +21,12 @@ }, "@param": { "prefix": "@param", - "body": ["@param ${1:name} ${2:Type and description of the parameter.}$0"], + "body": ["@param ${1:name} ${2:Description of the parameter.}$0"], "description": "Documents a parameter." }, "@return": { "prefix": "@return", - "body": ["@param ${1:name} ${2:Type and description of the parameter.}$0"], + "body": ["@param ${1:name} ${2:Description of the parameter.}$0"], "description": "Documents a the returned object." }, "@example": { @@ -96,4 +92,4 @@ ], "description": "The @deprecated description in the first sentence should at least tell the user when the API was deprecated and what to use as a replacement. Only the first sentence will appear in the summary section and index. Subsequent sentences can also explain why it has been deprecated." } -} \ No newline at end of file +} diff --git a/snippets/general/lua/luadoc.json b/snippets/general/lua/luadoc.json index 564d9d1..aa2b8a1 100644 --- a/snippets/general/lua/luadoc.json +++ b/snippets/general/lua/luadoc.json @@ -3,89 +3,66 @@ "prefix": "---", "body": [ "--- ${1:A one-line summary.}", - "-- ${2:Description.}$0", - "-- @param ${5:name} ${6:type} ${7:Parameter description.}", - "-- @return ${3:type} ${4: Description of the returned object.}", - "-- @usage ${8:Example about how to use it.}" + "---${2:Description.}$0", + "---@param ${5:name} ${6:type} ${7:Parameter description.}", + "---@return ${3:type} ${4: Description of the returned object.}", + "---@usage ${8:Example about how to use it.}" ], "description": "A lua comment with short summary, description, parameters, return, and example." }, "comment_simple": { "prefix": "--", - "body": [ - "--- ${1:A one-line summary.}", - "-- ${2:Description.}$0" - ], + "body": ["--- ${1:A one-line summary.}", "-- ${2:Description.}$0"], "description": "A simple lua comment with short summary and description. Useful when you prefer to add the tags manually on functions." }, "@author": { "prefix": "@author", - "body": [ - "@author ${1:text.}$0" - ], + "body": ["@author ${1:text.}$0"], "description": "An author of the module or file." }, "@copyright": { "prefix": "@copyright", - "body": [ - "@copyright ${1:text.}$0" - ], + "body": ["@copyright ${1:text.}$0"], "description": "The copyright notice of the module or file. LuaDoc adds a © sign between the label (Copyright) and the given text (e.g. 2004-2007 Kepler Project)." }, "@field": { "prefix": "@field", - "body": [ - "@field ${1:description.}$0" - ], + "body": ["@field ${1:description.}$0"], "description": "Describe a table field definition." }, "@param": { "prefix": "@param", - "body": [ - "@param ${1:name} ${2:type} ${3:Parameter description.}$0" - ], + "body": ["@param ${1:name} ${2:type} ${3:Parameter description.}$0"], "description": "Describe function parameters. It requires the name of the parameter and its description." }, "@release": { "prefix": "@release", - "body": [ - "@release ${1:text}$0" - ], + "body": ["@release ${1:text}$0"], "description": "Free format string to describe the module or file release." }, "@return": { "prefix": "@return", - "body": [ - "@return ${1:type} ${2:Description of the returned object.}$0" - ], + "body": ["@return ${1:type} ${2:Description of the returned object.}$0"], "description": "Describe a returning value of the function. Since Lua can return multiple values, this tag should appear more than once." }, "@see": { "prefix": "@see", - "body": [ - "@see ${1:name of a function or table}$0" - ], + "body": ["@see ${1:name of a function or table}$0"], "description": "Refers to other descriptions of functions or tables." }, "@class": { "prefix": "@class", - "body": [ - "@class ${1:name}$0" - ], + "body": ["@class ${1:name}$0"], "description": "If LuaDoc cannot infer the type of documentation (function, table or module definition), the programmer can specify it explicitly." }, "@description": { "prefix": "@description", - "body": [ - "@description ${1:text}$0" - ], + "body": ["@description ${1:text}$0"], "description": "The description of the function or table. This is usually infered automatically." }, "@name": { "prefix": "@name", - "body": [ - "@name ${1:text}$0" - ], + "body": ["@name ${1:text}$0"], "description": "The name of the function or table definition. This is usually infered from the code analysis, and the programmer does not need to define it. If LuaDoc can infer the name of the function automatically it's even not recomended to define the name explicitly, to avoid redundancy." } }