Neovim 下Java开发的环境配置

2023-10-30 20:52
文章标签 java 配置 开发 环境 neovim

本文主要是介绍Neovim 下Java开发的环境配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


tags: Java Vim
categories: Java

写在前面

秋招基本上结束了, C++算是告一段落, 但是学习之路才刚刚开始.

下面写一下 Nvim 上 Java 开发的一些基本配置, 还是延续了以往的轻量级开发环境搭建方法, Nvim 的配置可以看我之前的文章.

光会 C++不行, 后端语言还得看 Java/Go

下面的配置主要针对 Java 开发的代码补全, 代码格式化等操作, 用到的插件是

  • clang-format(没错, 这个万能插件可以格式化 Java)
  • nvim-jdtls(相当于是对 eclipse-jdtls 的一层封装, 比较好用的, 之所以不用 java-language-server 是因为这个插件的维护还是差点意思)

参考了 GitHub 的一些文档:

  • mfussenegger/nvim-jdtls: Extensions for the built-in LSP support in Neovim for eclipse.jdt.ls;
  • eclipse-jdtls/eclipse.jdt.ls: Java language server;

安装 jdtls

不能通过 Mason 安装 jdtls, 只能自己下载压缩包, 因为通过 mason 安装的 jdtls 只有可执行文件而没有 jar 等配置包.

首先下载压缩包, 这里就下载最新版了:

  • Project download area | The Eclipse Foundation;

然后解压, 随便找一个目录

我这里的目录在:

 ==> pwd
/Users/xxx/code/java_code/tools/jdtls √  ~/code/java_code/tools/jdtls==> ls
bin                 config_mac          config_ss_linux_arm config_ss_win       log_data
config_linux        config_mac_arm      config_ss_mac       config_win          plugins
config_linux_arm    config_ss_linux     config_ss_mac_arm   features

然后就是配置代码检查插件了, 这里有几个坑点:

  • 主要修改的几个路径

    – 💀 标记出来的

    必须用绝对路径, 使用$HOME/都不行

  • data 路径可以指定在 jdtls 的安装路径下, 但是像这样的缓存最好每一个项目独立一份比较好, 我这里是在安装路径下 mkdir 了log_data目录

代码检查插件

配置:

plugins:

lang["mfussenegger/nvim-jdtls"] = {lazy = true,ft = "java",config = require("lang.nvim-jdtls"),
}

config:

return function()-- See `:help vim.lsp.start_client` for an overview of the supported `config` options.local config = {-- The command that starts the language server-- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-linecmd = {-- 💀"java", -- or '/path/to/java17_or_newer/bin/java'-- depends on if `java` is in your $PATH env variable and if it points to the right version."-Declipse.application=org.eclipse.jdt.ls.core.id1","-Dosgi.bundles.defaultStartLevel=4","-Declipse.product=org.eclipse.jdt.ls.core.product","-Dlog.protocol=true","-Dlog.level=ALL","-Xmx1g","--add-modules=ALL-SYSTEM","--add-opens","java.base/java.util=ALL-UNNAMED","--add-opens","java.base/java.lang=ALL-UNNAMED",-- 💀"-jar","/Users/xxx/code/java_code/tools/jdtls/plugins/org.eclipse.equinox.launcher_1.6.500.v20230717-2134.jar",-- "/path/to/jdtls_install_location/plugins/org.eclipse.equinox.launcher_VERSION_NUMBER.jar",-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                       ^^^^^^^^^^^^^^-- Must point to the                                                     Change this to-- eclipse.jdt.ls installation                                           the actual version-- 💀"-configuration","/Users/xxx/code/java_code/tools/jdtls/config_mac",-- "/path/to/jdtls_install_location/config_SYSTEM",-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        ^^^^^^-- Must point to the                      Change to one of `linux`, `win` or `mac`-- eclipse.jdt.ls installation            Depending on your system.-- 💀-- See `data directory configuration` section in the README"-data","/Users/xxx/code/java_code/tools/jdtls/log_data/",-- "/path/to/unique/per/project/workspace/folder",},-- 💀-- This is the default if not provided, you can remove it. Or adjust as needed.-- One dedicated LSP server & client will be started per unique root_dirroot_dir = require("jdtls.setup").find_root({ ".git", "mvnw", "gradlew" }),-- Here you can configure eclipse.jdt.ls specific settings-- See https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request-- for a list of optionssettings = {java = {},},-- Language server `initializationOptions`-- You need to extend the `bundles` with paths to jar files-- if you want to use additional eclipse.jdt.ls plugins.---- See https://github.com/mfussenegger/nvim-jdtls#java-debug-installation---- If you don't plan on using the debugger or other eclipse.jdt.ls plugins you can remove thisinit_options = {bundles = {},},}-- This starts a new client & server,-- or attaches to an existing client & server depending on the `root_dir`.require("jdtls").start_or_attach(config)
end

格式化插件

改一下clang-format 的配置, 加上支持 Java 即可, 注意 config 也有相应改动

return function()local null_ls = require("null-ls")local btns = null_ls.builtins-- Please set additional flags for the supported servers here-- Don't specify any config here if you are using the default one.local sources = {btns.formatting.clang_format.with({filetypes = { "c", "cpp", "java" }, -- change thisextra_args = require("completion.formatters.clang_format"),}),

针对单项目设置: .clang-format 文件

BasedOnStyle: Google
---
Language: Java
IndentWidth: 4
ColumnLimit: 100
BreakStringLiterals: true
BreakAfterJavaFieldAnnotations: false
BraceWrapping:AfterCaseLabel: trueAfterClass: trueAfterControlStatement: trueAfterEnum: trueAfterFunction: trueAfterNamespace: trueAfterObjCDeclaration: trueAfterStruct: trueAfterUnion: trueAfterExternBlock: trueBeforeCatch: trueBeforeElse: trueIndentBraces: trueSplitEmptyFunction: falseSplitEmptyRecord: falseSplitEmptyNamespace: false

这篇关于Neovim 下Java开发的环境配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/310526

相关文章

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时