mfussenegger / nvim-jdtls

Extensions for the built-in LSP support in Neovim for eclipse.jdt.ls
GNU General Public License v3.0
1.08k stars 62 forks source link

Formatting on save #533

Closed sirstanislav closed 1 year ago

sirstanislav commented 1 year ago

LSP client configuration

local home = os.getenv "HOME"
local jdtls = require "jdtls"
local opts = { noremap = true, silent = true }
local root_markers = { "gradlew", "mvnw", ".git", "pom.xml", "build.gradle" }
local root_dir = require("jdtls.setup").find_root(root_markers)
local workspace_folder = home .. "/.local/share/eclipse/" .. vim.fn.fnamemodify(root_dir, ":p:h:t")
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})

function nnoremap(rhs, lhs, bufopts, desc)
  bufopts.desc = desc
  vim.keymap.set("n", rhs, lhs, bufopts)
end

local on_attach = function(client, bufnr)
  local _, _ = pcall(vim.lsp.codelens.refresh)
  local bufopts = { noremap = true, silent = true, buffer = bufnr }

  require("jdtls").setup_dap { hotcodereplace = "auto" }
  require("jdtls.setup").add_commands()
  require("jdtls.dap").setup_dap_main_class_configs()
  require("telescope").load_extension "ui-select"

  --Auto formatting on save
  if client.supports_method "textDocument/formatting" then
    vim.api.nvim_clear_autocmds { group = augroup, buffer = bufnr }
    vim.api.nvim_create_autocmd("BufWritePre", {
      group = augroup,
      buffer = bufnr,
      callback = function()
        vim.lsp.buf.format { bufnr = bufnr }
      end,
    })
  end

  nnoremap("<C-o>", jdtls.organize_imports, bufopts, "Organize imports")
  nnoremap("<space>df", jdtls.test_class, bufopts, "Test class")
  nnoremap("<space>dn", jdtls.test_nearest_method, bufopts, "Test method")
  nnoremap("<space>ec", jdtls.extract_constant, bufopts, "Extract constant")
  nnoremap("<space>ev", jdtls.extract_variable, bufopts, "Extract variable")
  vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>lc", "<cmd>lua vim.lsp.buf.code_action()<CR>", opts)
  vim.keymap.set(
    "v",
    "<space>em",
    [[<ESC><CMD>lua require('jdtls').extract_method(true)<CR>]],
    { noremap = true, silent = true, buffer = bufnr, desc = "Extract method" }
  )
end

local config = {
  flags = {
    debounce_text_changes = 80,
  },
  on_attach = on_attach,
  root_dir = root_dir,
  cmd = {
    "java",
    "-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",
    "-Xmx4g",
    "--add-modules=ALL-SYSTEM",
    "--add-opens",
    "java.base/java.util=ALL-UNNAMED",
    "--add-opens",
    "java.base/java.lang=ALL-UNNAMED",
    "-jar",
    vim.fn.glob "/opt/homebrew/Cellar/jdtls/1.25.0/libexec/plugins/org.eclipse.equinox.launcher_*.jar",
    "-configuration",
    "/opt/homebrew/Cellar/jdtls/1.25.0/libexec/config_mac",
    "-data",
    workspace_folder,
  },
  settings = {
    java = {
      format = {
        enabled = true,
        settings = {
          url = "~/.local/share/eclipse/eclipse-java-google-style.xml",
        },
      },
      signatureHelp = { enabled = true },
      contentProvider = { preferred = "fernflower" }, -- Use fernflower to decompile library code
      completion = {
        favoriteStaticMembers = {
          "org.hamcrest.MatcherAssert.assertThat",
          "org.hamcrest.Matchers.*",
          "org.hamcrest.CoreMatchers.*",
          "org.junit.jupiter.api.Assertions.*",
          "java.util.Objects.requireNonNull",
          "java.util.Objects.requireNonNullElse",
          "org.mockito.Mockito.*",
        },
        filteredTypes = {
          "com.sun.*",
          "io.micrometer.shaded.*",
          "java.awt.*",
          "jdk.*",
          "sun.*",
        },
      },
      sources = {
        organizeImports = {
          starThreshold = 9999,
          staticStarThreshold = 9999,
        },
      },
      codeGeneration = {
        toString = {
          template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}",
        },
        hashCodeEquals = {
          useJava7Objects = true,
        },
        useBlocks = true,
      },
      configuration = {
        runtimes = {
          {
            name = "JavaSE-20",
            path = "/Users/ctacbarada/Library/Java/JavaVirtualMachines/openjdk-20.0.1/Contents/Home",
          },
          {
            name = "JavaSE-17",
            path = "/Users/ctacbarada/Library/Java/JavaVirtualMachines/corretto-17.0.7/Contents/Home",
          },
          {
            name = "JavaSE-11",
            path = "/Users/ctacbarada/Library/Java/JavaVirtualMachines/corretto-11.0.19/Contents/Home",
          },
          {
            name = "JavaSE-1.8",
            path = "/Users/ctacbarada/Library/Java/JavaVirtualMachines/corretto-1.8.0_372/Contents/Home",
          },
        },
      },
      implementationsCodeLens = {
        enabled = true,
      },
      referencesCodeLens = {
        enabled = true,
      },
      references = {
        includeDecompiledSources = true,
      },
    },
  },
}

vim.api.nvim_create_autocmd({ "BufWritePost" }, {
  pattern = { "*.java" },
  callback = function()
    local _, _ = pcall(vim.lsp.codelens.refresh)
  end,
})

vim.opt.tabstop = 4
vim.opt.shiftwidth = 4

local bundles = {
  vim.fn.glob(
    "/Users/ctacbarada/Storage/GitHub/Java/Debugging/Java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-*.jar",
    1
  ),
}

vim.list_extend(
  bundles,
  vim.split(vim.fn.glob("/Users/ctacbarada/Storage/GitHub/Java/Debugging/vscode-java-test/server/*.jar", 1), "\n")
)

config["init_options"] = {
  bundles = bundles,
}

jdtls.start_or_attach(config)

Eclipse.jdt.ls version

1.25.0

Steps to Reproduce

Creting project with Maven cli:

 mvn -B archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4

Formatting doesn't work in opened file App.java for example, after opens another file/class Users.java in new buffer where formatting is works. If i close previous file App.java where formating doesn't work and reopen it, formating start working on save, but finished work in opened Users.java.

image image

LspInfo in file where formatting on save stop working after opening another file.

I think the problem in this function

--Auto formatting on save
  if client.supports_method "textDocument/formatting" then
    vim.api.nvim_clear_autocmds { group = augroup, buffer = bufnr }
    vim.api.nvim_create_autocmd("BufWritePre", {
      group = augroup,
      buffer = bufnr,
      callback = function()
        vim.lsp.buf.format { bufnr = bufnr }
      end,
    })
  end

I have copied from null ls config where it works fine for other formatters.

Expected Result

Make formatting on save working in every file. Thank you for jdtls and any suggestions. Here is full repo with all configs

Actual Result

Clean logs from this startup and try to formatting first two files. I'm understand what there is some other errors, not related maybe for formatting. But anyway.

:JdtShowLogs

.log


!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:37.351
!MESSAGE >> shutdown

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:42.012
!MESSAGE Parent process stopped running, forcing server exit

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:42.016
!MESSAGE >> exit

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:42.024
!MESSAGE Shutdown received... waking up main thread

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:42.055
!MESSAGE class org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin is stopping:
!SESSION 2023-07-18 17:44:48.128 -----------------------------------------------
eclipse.buildId=unknown
java.version=20.0.1
java.vendor=Oracle Corporation
BootLoader constants: OS=macosx, ARCH=aarch64, WS=cocoa, NL=en_IT
Command-line arguments:  -data /Users/ctacbarada/.local/share/eclipse/UsersApp

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:48.977
!MESSAGE class org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin is started

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.063
!MESSAGE Main thread is waiting

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.083
!MESSAGE >> initialize

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.084
!MESSAGE Initializing Java Language Server 1.25.0.202306291518

!ENTRY org.eclipse.jdt.ls.core 4 0 2023-07-18 17:44:49.091
!MESSAGE Failed to load extension bundles 
!STACK 1
org.eclipse.core.runtime.CoreException: Load bundle list
    at org.eclipse.jdt.ls.core.internal.handlers.BundleUtils.loadBundles(BundleUtils.java:169)
    at org.eclipse.jdt.ls.core.internal.handlers.InitHandler.handleInitializationOptions(InitHandler.java:100)
    at org.eclipse.jdt.ls.core.internal.handlers.BaseInitHandler.initialize(BaseInitHandler.java:64)
    at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.initialize(JDTLanguageServer.java:275)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint.lambda$recursiveFindRpcMethods$0(GenericEndpoint.java:65)
    at org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint.request(GenericEndpoint.java:120)
    at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.handleRequest(RemoteEndpoint.java:261)
    at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.consume(RemoteEndpoint.java:190)
    at org.eclipse.jdt.ls.core.internal.ParentProcessWatcher.lambda$1(ParentProcessWatcher.java:144)
    at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.handleMessage(StreamMessageProducer.java:194)
    at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.listen(StreamMessageProducer.java:94)
    at org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor.run(ConcurrentMessageProcessor.java:113)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:577)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
    at java.base/java.lang.Thread.run(Thread.java:1623)
Contains: Failed to get bundleInfo for bundle from /Users/ctacbarada/Storage/GitHub/Java/Debugging/vscode-java-test/server/com.microsoft.java.test.runner-jar-with-dependencies.jar
!SUBENTRY 1 org.eclipse.jdt.ls.core 4 0 2023-07-18 17:44:49.092
!MESSAGE Load bundle list
!SUBENTRY 2 org.eclipse.jdt.ls.core 4 0 2023-07-18 17:44:49.092
!MESSAGE Failed to get bundleInfo for bundle from /Users/ctacbarada/Storage/GitHub/Java/Debugging/vscode-java-test/server/com.microsoft.java.test.runner-jar-with-dependencies.jar

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.108
!MESSAGE Static Commands: []

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.108
!MESSAGE Non-Static Commands: [java.edit.organizeImports, java.project.refreshDiagnostics, java.project.import, java.navigate.openTypeHierarchy, java.project.removeFromSourcePath, java.project.listSourcePaths, java.project.resolveStackTraceLocation, java.project.getAll, java.reloadBundles, java.project.isTestFile, java.edit.handlePasteEvent, java.project.getClasspaths, java.navigate.resolveTypeHierarchy, java.edit.stringFormatting, java.project.getSettings, java.project.updateSourceAttachment, java.project.resolveWorkspaceSymbol, java.project.upgradeGradle, java.decompile, java.project.createModuleInfo, java.protobuf.generateSources, java.project.resolveSourceAttachment, java.project.addToSourcePath, java.completion.onDidSelect]

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.108
!MESSAGE Static Commands: []

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.109
!MESSAGE Non-Static Commands: [vscode.java.checkProjectSettings, vscode.java.isOnClasspath, vscode.java.fetchUsageData, vscode.java.validateLaunchConfig, vscode.java.resolveInlineVariables, vscode.java.resolveClassFilters, vscode.java.resolveMainMethod, vscode.java.resolveClasspath, vscode.java.resolveBuildFiles, vscode.java.resolveMainClass, vscode.java.updateDebugSettings, vscode.java.resolveSourceUri, vscode.java.fetchPlatformSettings, vscode.java.buildWorkspace, vscode.java.startDebugSession, vscode.java.inferLaunchCommandLength, vscode.java.resolveElementAtSelection, vscode.java.resolveJavaExecutable]

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.109
!MESSAGE Static Commands: []

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.109
!MESSAGE Non-Static Commands: [vscode.java.test.findJavaProjects, vscode.java.test.findTestPackagesAndTypes, vscode.java.test.findTestTypesAndMethods, vscode.java.test.resolvePath, vscode.java.test.findTestLocation, vscode.java.test.get.testpath, vscode.java.test.findDirectTestChildrenForClass, vscode.java.test.navigateToTestOrTarget, vscode.java.test.junit.argument, vscode.java.test.generateTests]

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.112
!MESSAGE Started org.eclipse.m2e.core 0ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.113
!MESSAGE ProjectRegistryRefreshJob finished 1ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.124
!MESSAGE Started org.eclipse.buildship.core 11ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.136
!MESSAGE >> initialized

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.530
!MESSAGE RepositoryRegistryUpdateJob finished 397ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.744
!MESSAGE Importing Maven project(s)

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.782
!MESSAGE Workspace initialized in 649ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.802
!MESSAGE >> initialization job finished

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:49.804
!MESSAGE >> registerWatchers'

!ENTRY org.eclipse.jdt.ls.core 4 0 2023-07-18 17:44:49.915
!MESSAGE Command _java.reloadBundles.command not supported on client

!ENTRY java-debug 1 0 2023-07-18 17:44:49.919
!MESSAGE Starting com.microsoft.java.debug.plugin

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:50.274
!MESSAGE >> registerWatchers'

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:50.275
!MESSAGE >> build jobs finished

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:50.661
!MESSAGE Reconciled 1. Took 3 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:51.315
!MESSAGE begin problem for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:51.315
!MESSAGE 1 problems reported for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:51.316
!MESSAGE Validated 1. Took 53 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:53.329
!MESSAGE Reconciled 1. Took 0 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:53.752
!MESSAGE begin problem for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:53.753
!MESSAGE 0 problems reported for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:53.763
!MESSAGE begin problem for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:53.764
!MESSAGE 1 problems reported for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:53.765
!MESSAGE Validated 2. Took 28 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.036
!MESSAGE Completion request completed

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.040
!MESSAGE Reconciled 1. Took 2 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.115
!MESSAGE Completion request completed

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.122
!MESSAGE Reconciled 1. Took 0 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.260
!MESSAGE Completion request completed

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.266
!MESSAGE Reconciled 1. Took 1 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.454
!MESSAGE Completion request completed

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.460
!MESSAGE Reconciled 1. Took 1 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.877
!MESSAGE begin problem for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.878
!MESSAGE 0 problems reported for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.889
!MESSAGE begin problem for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.890
!MESSAGE 1 problems reported for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:57.891
!MESSAGE Validated 2. Took 25 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:58.235
!MESSAGE Reconciled 1. Took 0 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:58.646
!MESSAGE begin problem for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:58.647
!MESSAGE 0 problems reported for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:58.658
!MESSAGE begin problem for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:58.658
!MESSAGE 1 problems reported for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:44:58.659
!MESSAGE Validated 2. Took 22 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:00.257
!MESSAGE Reconciled 1. Took 0 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:00.672
!MESSAGE begin problem for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:00.673
!MESSAGE 0 problems reported for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:00.683
!MESSAGE begin problem for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:00.684
!MESSAGE 1 problems reported for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:00.685
!MESSAGE Validated 2. Took 22 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:02.663
!MESSAGE Reconciled 1. Took 2 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:03.076
!MESSAGE begin problem for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:03.077
!MESSAGE 0 problems reported for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:03.088
!MESSAGE begin problem for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:03.089
!MESSAGE 1 problems reported for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:03.090
!MESSAGE Validated 2. Took 25 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:04.801
!MESSAGE Reconciled 1. Took 2 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:05.215
!MESSAGE begin problem for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:05.217
!MESSAGE 0 problems reported for /Users.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:05.226
!MESSAGE begin problem for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:05.227
!MESSAGE 1 problems reported for /App.java

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:05.228
!MESSAGE Validated 2. Took 21 ms

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:11.562
!MESSAGE Parent process stopped running, forcing server exit

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:11.568
!MESSAGE >> exit

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:11.574
!MESSAGE Shutdown received... waking up main thread

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-07-18 17:45:11.591
!MESSAGE class org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin is stopping:

lsp.log

[START][2023-07-18 17:44:48] LSP logging initiated
[ERROR][2023-07-18 17:44:48] .../vim/lsp/rpc.lua:734    "rpc"   "java"  "stderr"    "WARNING: Using incubator modules: jdk.incubator.concurrent, jdk.incubator.vector\n"
[ERROR][2023-07-18 17:44:48] .../vim/lsp/rpc.lua:734    "rpc"   "java"  "stderr"    "SLF4J: No SLF4J providers were found.\nSLF4J: Defaulting to no-operation (NOP) logger implementation\nSLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.\n"
[ERROR][2023-07-18 17:44:49] ...lsp/handlers.lua:535    "Jul 18, 2023, 5:44:49 PM Failed to load extension bundles \nLoad bundle list\norg.eclipse.core.runtime.CoreException: Load bundle list\n\tat org.eclipse.jdt.ls.core.internal.handlers.BundleUtils.loadBundles(BundleUtils.java:169)\n\tat org.eclipse.jdt.ls.core.internal.handlers.InitHandler.handleInitializationOptions(InitHandler.java:100)\n\tat org.eclipse.jdt.ls.core.internal.handlers.BaseInitHandler.initialize(BaseInitHandler.java:64)\n\tat org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.initialize(JDTLanguageServer.java:275)\n\tat java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:578)\n\tat org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint.lambda$recursiveFindRpcMethods$0(GenericEndpoint.java:65)\n\tat org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint.request(GenericEndpoint.java:120)\n\tat org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.handleRequest(RemoteEndpoint.java:261)\n\tat org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.consume(RemoteEndpoint.java:190)\n\tat org.eclipse.jdt.ls.core.internal.ParentProcessWatcher.lambda$1(ParentProcessWatcher.java:144)\n\tat org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.handleMessage(StreamMessageProducer.java:194)\n\tat org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.listen(StreamMessageProducer.java:94)\n\tat org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor.run(ConcurrentMessageProcessor.java:113)\n\tat java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:577)\n\tat java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)\n\tat java.base/java.lang.Thread.run(Thread.java:1623)\nContains: Failed to get bundleInfo for bundle from /Users/ctacbarada/Storage/GitHub/Java/Debugging/vscode-java-test/server/com.microsoft.java.test.runner-jar-with-dependencies.jar\n"
[WARN][2023-07-18 17:44:49] ...lsp/handlers.lua:137 "The language server jdtls triggers a registerCapability handler despite dynamicRegistration set to false. Report upstream, this warning is harmless"
[ERROR][2023-07-18 17:44:49] ...lsp/handlers.lua:535    "Jul 18, 2023, 5:44:49 PM Command _java.reloadBundles.command not supported on client"
ghost commented 1 year ago

i had the same problem after adding formatting, when opening a new file and running :au LspFormatting only the new buffer was present( that's why in your case when App.java is reopened, formating start working but it does not work in other files anymore), I solved it by adding clear = false to nvim_create_augroup:

local augroup = vim.api.nvim_create_augroup("LspFormatting", { clear = false })

and now, all buffers are present when :au LspFormatting is executed.

2023-07-19_17-26-55

i don't know why it's not necessary with null-ls, i'd rather stay with the doubt.

My current config (ftplugin/java.lua):

local lsp = require("plugins.lsp.lsp-servers")
local augroup = vim.api.nvim_create_augroup("LspFormatting", { clear = false })
local on_attach = function(client, bufnr)
    lsp.on_attach(client, bufnr)
    if client.supports_method("textDocument/formatting") then
        vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
        vim.api.nvim_create_autocmd("BufWritePre", {
            group = augroup,
            buffer = bufnr,
            callback = function()
                vim.lsp.buf.format({ async = false })
            end,
        })
    end
end
local javaconfig = {
    cmd = {
        "/home/levioneyh/.local/share/nvim/mason/bin/jdtls",
        "-data",
        "/home/levioneyh/.cache/jdtls/workspace",
        "-configuration",
        "/home/levioneyh/.cache/jdtls/config",
    },
    root_dir = vim.fs.dirname(vim.fs.find({ "gradlew", ".git", "mvnw" }, { upward = true })[1]),
    on_attach = on_attach,
    capabilities = lsp.default_capabilities,
}

require("jdtls").start_or_attach(javaconfig)
mfussenegger commented 1 year ago

on_attach is called per buffer. If you use the same group name without clear = false it will override the previous autocmd definition.

You can either:

sirstanislav commented 1 year ago

Thank you guys. Now it's works