repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/lua/bugabinga/java.lua

Raw
local puk = require 'std.puk'
local project = require 'std.project'
local auto = require 'std.auto'
local map = require 'std.map'
local dbg = require 'std.dbg'
local localrc = require 'std.localrc'
local table = require 'std.table'

local jdtls_ss_id = nil

local create_config = function(syntax_server_mode, path)
  local data_path = vim.fn.stdpath 'data'
  if type(data_path) == 'table' then
    data_path = data_path[1]
  end
  local home = vim.fs.abspath '~'
  local tools_path = vim.fs.joinpath(home, 'Tools')
  local jdtls_jre_path = vim.fs.find(function(name) return name:match 'graalvm%-community%-openjdk%-24.*' end,
    { path = tools_path, type = 'directory' })[1]
  local jdtls_install_path = vim.fs.find(function(name) return name:match 'jdt%-language%-server%-1%.47.*' end,
    { path = tools_path, type = 'directory' })[1]
  local plugins_path = vim.fs.joinpath(jdtls_install_path, 'plugins')
  local launcher_path = vim.fs.find(function(name) return name:match 'org%.eclipse%.equinox%.launcher_.*' end,
    { path = plugins_path, type = 'file' })[1]

  local project_local_settings = localrc('.jdtls.lua', 'table') or {}
  project_local_settings.jdtls = project_local_settings.jdtls or {}

  local find_runtimes = function()
    local runtimes = {}

    table.insert(runtimes, {
      name = 'JavaSE-24',
      path = jdtls_jre_path,
      sources = vim.fs.joinpath(jdtls_jre_path, 'lib', 'src.zip'),
      default = true,
    })

    if (project_local_settings.jdtls.runtimes) then
      return table.deep_extend('force', runtimes, project_local_settings.jdlts.runtimes)
    else
      return runtimes
    end
  end
  local root_dir = project.find_java_project_root(path)
  local jdlts_config_dir
  if syntax_server_mode then
    jdlts_config_dir = vim.fn.has 'win32' and 'config_ss_win' or 'config_ss_linux'
  else
    jdlts_config_dir = vim.fn.has 'win32' and 'config_win' or 'config_linux'
  end
  local config_dir = vim.fs.joinpath(jdtls_install_path, jdlts_config_dir)
  local workspace_dir = vim.fs.joinpath(data_path, 'jdtls-workspace', vim.fs.basename(root_dir))
  local compiler_options_path = vim.fs.joinpath(home, '.config', 'jdlts', 'compiler_options.properties')

  local cmd = {
    vim.fs.joinpath(jdtls_jre_path, 'bin', 'java'),
    '-Declipse.application=org.eclipse.jdt.ls.core.id1',
    '-Dosgi.bundles.defaultStartLevel=4',
    '-Declipse.product=org.eclipse.jdt.ls.core.product',
    '-Dlog.level=ERROR',
    '-Xmx8G',
    '--add-modules=ALL-SYSTEM',
    '--add-opens', 'java.base/java.util=ALL-UNNAMED',
    '--add-opens', 'java.base/java.lang=ALL-UNNAMED',
    '--add-modules', 'jdk.incubator.vector',
    '--enable-native-access=ALL-UNNAMED',
    '-jar', launcher_path,
    '-configuration', config_dir,
    '-data', workspace_dir,
  }

  if syntax_server_mode then
    table.insert(cmd, 2, '-Dsyntaxserver=true')
  end

  local runtimes = find_runtimes()
  local config = {
    name = syntax_server_mode and 'jdtls_ss' or 'jdtls',
    cmd = cmd,
    root_dir = root_dir,
    settings = {
      java = {
        autobuild               = { enabled = true },
        cleanup                 = {
          actionsOnSave = {
            'qualifyMembers',
            'qualifyStaticMembers',
            'addOverride',
            'addDeprecated',
            'stringConcatToTextBlock',
            'invertEquals',
            'addFinalModifier',
            'instanceofPatternMatch',
            'switchExpression',
          },
        },
        codeGeneration          = {
          generateComments = false,
          hashCodeEquals = { useJava7Objects = true },
          toString = {
            template = '${object.className}{${member.name()}=${member.value()}, ${otherMembers}}',
          },
          useBlocks = true,
        },
        codeAction              = { sortMembers = { avoidVolatileChanges = true } },
        completion              = {
          enabled = true,
          favortiteStaticMembers = {
            'org.junit.Assert.*',
            'org.junit.Assume.*',
            'org.junit.jupiter.api.Assertions.*',
            'org.junit.jupiter.api.Assumptions.*',
            'org.junit.jupiter.api.DynamicContainer.*',
            'org.junit.jupiter.api.DynamicTest.*',
            'org.assertj.core.api.Assertions.*',
            'org.assertj.core.api.Assumptions.*',
            'java.util.Objects.requireNonNull',
            'java.util.Objects.requireNonNullElse',
            'java.time.temporal.ChronoUnit.*',
            'java.lang.System.Logger',
            'java.lang.System.Logger.Level.*',
          },
          filteredTypes = {
            -- Swing and AWT
            'javax.swing.*',
            'java.awt.*',
            -- CORBA
            'org.omg.*',
            -- Applet
            'java.applet.*',
            -- Legacy JAF
            'javax.activation.*',
            -- Legacy Security
            'java.security.acl.*',
            'javax.security.auth.callback.*',
            -- Legacy XML/SOAP
            'javax.xml.soap.*',
            'javax.xml.rpc.*',
            -- RMI
            'java.rmi.*',
            -- Legacy Collections
            'java.util.Vector',
            'java.util.Hashtable',
            'java.util.Stack',
            'java.util.Dictionary',
            -- Deprecated Classes
            'java.util.Date',
            'java.util.Calendar',
            'java.util.Observable',
            'java.util.Observer',
            -- Niche Packages
            'javax.annotation.*',
            'javax.jws.*',
            'java.beans.*',
            'javax.management.*',
          },
          guessMethodArguments = false,
          importOrder = {
            'net',
            'de',
            'org',
            'com',
            'javax',
            'java',
          },
          matchCase = 'FIRSTLETTER',
          maxResults = 5,
          overwrite = true,
          postfix = { enabled = true },
        },
        configuration           = {
          runtimes = runtimes,
          updateBuildConfiguration = 'interactive',
        },
        contentProvider         = { preferred = nil },
        eclipse                 = { downloadSources = true },
        errors                  = { incompleteClassPath = { severity = 'error' } },
        executeCommand          = { enabled = true },
        foldingRange            = { enabled = false },
        format                  = { enabled = true },
        home                    = jdtls_jre_path,
        implementationsCodeLens = { enabled = true },
        import                  = {
          maven = { enabled = true },
          gradle = {
            enabled = true,
            annotationProcessing = true,
            wrapper = { enabled = true },
          },
          exclusions = {
            '**/output/**',
            '**/target/**',
            '**/node_modules/**',
            '**/.metadata/**',
            '**/archetype-resources/**',
            '**/META-INF/maven/**',
          },
        },
        inlayHints              = { parameterNames = { enabled = 'all' } },
        jdt                     = {
          ls = {
            androidSupport = false,
            lombokSupport = false,
            protofBufSupport = true,
          },
        },
        maven                   = { notCoveredPluginExecutionSeverity = 'error' },
        maxConcurrentBuilds     = 16,
        project                 = {
          encoding = 'SETDEFAULT',
          outputPath = project_local_settings.jdtls.outputPath,
          referencedLibraries = project_local_settings.jdtls.referencedLibraries,
          resourceFilters = project_local_settings.jdtls.resourceFilters,
          sourcePaths = project_local_settings.jdtls.sourcePaths,
        },
        referencesCodeLens      = { enabled = true },
        references              = {
          includeAccessors = true,
          includeDecompiledSources = true,
        },
        rename                  = { enabled = true },
        saveActions             = { origanizeImports = true },
        selectionRange          = { enabled = true },
        settings                = {
          url = compiler_options_path,
        },
        signatureHelp           = { enabled = true },
        sources                 = {
          organizeImports = {
            starThreshold = 9999,
            staticStarThreshold = 9999,
          },
        },
        symbols                 = { includeSourceMethodDeclarations = true },
        trace                   = { server = dbg.get() and 'verbose' or 'off' },
        edit                    = { validateAllOpenBuffersOnChanges = false },
      },
    },
    handlers = {
      ['language/status'] = function(_, status)
        if status.message == 'ServiceReady' and jdtls_ss_id ~= nil then
          vim.notify('Stopping jdtls_ss: ' .. jdtls_ss_id)
          vim.lsp.stop_client(jdtls_ss_id, true)
        end
      end,

    },
  }
  return config
end

auto 'start_jdtls' {
  pattern = 'java',
  events = 'FileType',
  description = 'Starts jdlts (with syntax mode initially)',
  command = function()
    local path = vim.api.nvim_buf_get_name(0)
    -- FIXME: often, jdtls_ss stays open...
    jdtls_ss_id = vim.lsp.start(create_config(true, path))
    -- puk 'jdtls'.start_or_attach(create_config(false, path))
  end,
}