diff --git a/CHANGELOG.md b/CHANGELOG.md index 994b462..6e4f441 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## [1.19.0](https://github.com/nvim-java/nvim-java/compare/v1.18.0...v1.19.0) (2024-07-14) + + +### Features + +* add clean workspace command ([8a1171c](https://github.com/nvim-java/nvim-java/commit/8a1171cc21aaae58f8a08759562814aea87e694d)) +* add toString code action ([4b1e1bd](https://github.com/nvim-java/nvim-java/commit/4b1e1bd5206174b8914858d085fe6809482b5575)) + ## [1.18.0](https://github.com/nvim-java/nvim-java/compare/v1.17.0...v1.18.0) (2024-07-14) diff --git a/README.md b/README.md index 7a47211..b15d417 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,10 @@ Just install and start writing `public static void main(String[] args)`. - :white_check_mark: Spring Boot Tools - :white_check_mark: Diagnostics & Auto Completion -- :white_check_mark: Automatic [DAP](https://github.com/mfussenegger/nvim-dap) - debug configuration +- :white_check_mark: Automatic Debug Configuration - :white_check_mark: Organize Imports & Code Formatting - :white_check_mark: Running Tests -- :white_check_mark: Run & Debug profiles +- :white_check_mark: Run & Debug Profiles - :white_check_mark: [Code Actions](https://github.com/nvim-java/nvim-java/wiki/Tips-&-Tricks#running-code-actions) ## :bulb: Why @@ -48,15 +47,12 @@ Just install and start writing `public static void main(String[] args)`. ### Starter Configs (Recommend for newbies) Following are forks of original repositories pre-configured for java. If you -don't know how to get started, use one of the following to get started. +don't know how to get started, use one of the following to get started. +You can click on **n commits ahead of** link to see the changes made on top of the original project - [LazyVim](https://github.com/nvim-java/starter-lazyvim) - [Kickstart](https://github.com/nvim-java/starter-kickstart) - -### Distribution Specific Instructions - -- [LazyVim](https://github.com/nvim-java/nvim-java/wiki/Lazyvim) -- [Kickstart](https://github.com/nvim-java/nvim-java/wiki/Kickstart) +- [AstroNvim](https://github.com/nvim-java/starter-astronvim) ### Custom Configuration Instructions diff --git a/doc/nvim-java.txt b/doc/nvim-java.txt index e47b886..545faef 100644 --- a/doc/nvim-java.txt +++ b/doc/nvim-java.txt @@ -37,11 +37,10 @@ FEATURES *nvim-java-features* - Spring Boot Tools - Diagnostics & Auto Completion -- Automatic DAP - debug configuration +- Automatic Debug Configuration - Organize Imports & Code Formatting - Running Tests -- Run & Debug profiles +- Run & Debug Profiles - Code Actions @@ -66,16 +65,13 @@ HOW TO INSTALL *nvim-java-how-to-install* STARTER CONFIGS (RECOMMEND FOR NEWBIES) ~ Following are forks of original repositories pre-configured for java. If you -don’t know how to get started, use one of the following to get started. +don’t know how to get started, use one of the following to get started. You +can click on **n commits ahead of** link to see the changes made on top of the +original project - LazyVim - Kickstart - - -DISTRIBUTION SPECIFIC INSTRUCTIONS ~ - -- LazyVim -- Kickstart +- AstroNvim CUSTOM CONFIGURATION INSTRUCTIONS ~ diff --git a/lua/java/api/build.lua b/lua/java/api/build.lua index 73de525..a32b4c9 100644 --- a/lua/java/api/build.lua +++ b/lua/java/api/build.lua @@ -1,5 +1,7 @@ -local async = require('java-core.utils.async').sync +local runner = require('async.runner') local get_error_handler = require('java.handlers.error') +local ui = require('java.utils.ui') +local jdtls = require('java.utils.jdtls2') local M = {} @@ -14,7 +16,7 @@ function M.full_build_workspace(is_full_build) is_full_build = type(is_full_build) == 'boolean' and is_full_build or true - return async(function() + return runner(function() JdtlsClient(jdtls()):java_build_workspace( is_full_build, buf_util.get_curr_buf() @@ -26,4 +28,25 @@ function M.full_build_workspace(is_full_build) .run() end +function M.clean_workspace() + runner(function() + local client = jdtls() + + local workpace_path = + vim.tbl_get(client, 'config', 'init_options', 'workspace') + + local prompt = string.format('Do you want to delete "%s"', workpace_path) + + local choice = ui.select(prompt, { 'Yes', 'No' }) + + if choice ~= 'Yes' then + return + end + + vim.fn.delete(workpace_path, 'rf') + end) + .catch(get_error_handler('Failed to clean up the workspace')) + .run() +end + return M diff --git a/lua/java/api/generate.lua b/lua/java/api/generate.lua index 244725e..1008857 100644 --- a/lua/java/api/generate.lua +++ b/lua/java/api/generate.lua @@ -1,15 +1,15 @@ +local runner = require('async.runner') + local M = {} ---@param params nvim.CodeActionParamsResponse function M.generate_constructor(params) - local JdtlsClient = require('java-core.ls.clients.jdtls-client') - local async = require('java-core.utils.async').sync - local get_client = require('java.utils.jdtls2') + local instance = require('java.utils.instance_factory') local get_error_handler = require('java.handlers.error') local ui = require('java.utils.ui') - return async(function() - local jdtls = JdtlsClient(get_client()) + return runner(function() + local jdtls = instance.jdtls_client() local status = jdtls:java_check_constructors_status(params.params) if not status or not status.constructors then @@ -52,4 +52,49 @@ function M.generate_constructor(params) .run() end +---@param params nvim.CodeActionParamsResponse +function M.generate_to_string(params) + local instance = require('java.utils.instance_factory') + local get_error_handler = require('java.handlers.error') + local ui = require('java.utils.ui') + + runner(function() + local jdtls = instance.jdtls_client() + local status = jdtls:java_check_to_string_status(params.params) + + if status.exists then + local prompt = string.format( + 'Method "toString()" already exists in the Class %s. Do you want to replace the implementation?', + status.type + ) + local choice = ui.select(prompt, { 'Replace', 'Cancel' }) + + if choice ~= 'Replace' then + return + end + end + + local fields = ui.multi_select( + 'Select the fields to include in the toString() method.', + status.fields, + function(field) + return field.name + end + ) + + if not fields then + return + end + + local edit = jdtls:java_generate_to_string({ + context = params.params, + fields = fields, + }) + + vim.lsp.util.apply_workspace_edit(edit, 'utf-8') + end) + .catch(get_error_handler('Generating to string failed')) + .run() +end + return M diff --git a/lua/java/commands/init.lua b/lua/java/commands/init.lua index 33485bf..df7b6e7 100644 --- a/lua/java/commands/init.lua +++ b/lua/java/commands/init.lua @@ -121,10 +121,18 @@ M.handlers = { require('java.api.generate').generate_constructor(params) end, + [M.commands.GENERATE_TOSTRING_PROMPT] = function(_, params) + require('java.api.generate').generate_to_string(params) + end, + ---@param is_full_build boolean [M.commands.COMPILE_WORKSPACE] = function(is_full_build) require('java.api.build').full_build_workspace(is_full_build) end, + + [M.commands.CLEAN_WORKSPACE] = function() + require('java.api.build').clean_workspace() + end, } local ignored_commands = { M.commands.REFRESH_BUNDLES_COMMAND } diff --git a/lua/java/utils/instance_factory.lua b/lua/java/utils/instance_factory.lua new file mode 100644 index 0000000..5bbfb75 --- /dev/null +++ b/lua/java/utils/instance_factory.lua @@ -0,0 +1,25 @@ +local class = require('java-core.utils.class') + +local M = class() + +---@private +---@return vim.lsp.Client +local function get_client() + local clients = vim.lsp.get_clients({ name = 'jdtls' }) + + if #clients < 1 then + local message = string.format('No jdtls client found to instantiate class') + require('java-core.utils.notify').error(message) + require('java.utils.log').error(message) + error(message) + end + + return clients[1] +end + +---@return java-core.JdtlsClient +function M.jdtls_client() + return require('java-core.ls.clients.jdtls-client')(get_client()) +end + +return M