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

相关文章

Java计算经纬度距离的示例代码

《Java计算经纬度距离的示例代码》在Java中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下... 目录1. Haversine公式(中等精度,推荐通用场景)2. 球面余弦定理(简单但精度较低)3. Vincenty公式(高精度,

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Java实现视频格式转换的完整指南

《Java实现视频格式转换的完整指南》在Java中实现视频格式的转换,通常需要借助第三方工具或库,因为视频的编解码操作复杂且性能需求较高,以下是实现视频格式转换的常用方法和步骤,需要的朋友可以参考下... 目录核心思路方法一:通过调用 FFmpeg 命令步骤示例代码说明优点方法二:使用 Jaffree(FF

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

Java如何用乘号来重复字符串的功能

《Java如何用乘号来重复字符串的功能》:本文主要介绍Java使用乘号来重复字符串的功能,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java乘号来重复字符串的功能1、利用循环2、使用StringBuilder3、采用 Java 11 引入的String.rep

SpringBoot中HTTP连接池的配置与优化

《SpringBoot中HTTP连接池的配置与优化》这篇文章主要为大家详细介绍了SpringBoot中HTTP连接池的配置与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、HTTP连接池的核心价值二、Spring Boot集成方案方案1:Apache HttpCl

Spring Boot项目打包和运行的操作方法

《SpringBoot项目打包和运行的操作方法》SpringBoot应用内嵌了Web服务器,所以基于SpringBoot开发的web应用也可以独立运行,无须部署到其他Web服务器中,下面以打包dem... 目录一、打包为JAR包并运行1.打包为可执行的 JAR 包2.运行 JAR 包二、打包为WAR包并运行

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

Spring Boot 常用注解整理(最全收藏版)

《SpringBoot常用注解整理(最全收藏版)》本文系统整理了常用的Spring/SpringBoot注解,按照功能分类进行介绍,每个注解都会涵盖其含义、提供来源、应用场景以及代码示例,帮助开发... 目录Spring & Spring Boot 常用注解整理一、Spring Boot 核心注解二、Spr

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R