summaryrefslogtreecommitdiffstats
path: root/modules/plugins/languages/scala.nix
blob: 36fa9048eaeeb62556ecc4e6bd75fcbd86a721b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{
  config,
  pkgs,
  lib,
  ...
}: let
  inherit (lib.generators) mkLuaInline;
  inherit (lib.modules) mkIf mkMerge;
  inherit (lib.nvim.dag) entryAfter;
  inherit (lib.nvim.lua) toLuaObject;
  inherit (lib.nvim.types) mkGrammarOption luaInline;
  inherit (lib.options) mkOption mkEnableOption mkPackageOption literalExpression;
  inherit (lib.strings) optionalString;
  inherit (lib.types) attrsOf anything bool;
  inherit (config.vim.lib) mkMappingOption;

  listCommandsAction =
    if config.vim.telescope.enable
    then ''require("telescope").extensions.metals.commands()''
    else ''require("metals").commands()'';

  cfg = config.vim.languages.scala;

  usingDap = config.vim.debugger.nvim-dap.enable && cfg.dap.enable;
  usingLualine = config.vim.statusline.lualine.enable;
in {
  options.vim.languages.scala = {
    enable = mkEnableOption "Scala language support";

    treesitter = {
      enable =
        mkEnableOption "Scala treesitter"
        // {
          default = config.vim.languages.enableTreesitter;
          defaultText = literalExpression "config.vim.languages.enableTreesitter";
        };
      package = mkGrammarOption pkgs "scala";
    };

    lsp = {
      enable =
        mkEnableOption "Scala LSP support (metals)"
        // {
          default = config.vim.lsp.enable;
          defaultText = literalExpression "config.vim.lsp.enable";
        };
      package = mkPackageOption pkgs "metals" {
        default = ["metals"];
      };

      extraMappings = {
        listCommands = mkMappingOption "List Metals commands" "<leader>lc";
      };

      extraSettings = mkOption {
        type = attrsOf anything;
        description = "Extra settings passed to the metals config. Check nvim-metals docs for available options";
        default = {
          showImplicitArguments = true;
          showImplicitConversionsAndClasses = true;
          showInferredType = true;
          excludedPackages = [
            "akka.actor.typed.javadsl"
            "com.github.swagger.akka.javadsl"
          ];
        };
      };
    };

    dap = {
      enable = mkEnableOption "Scala Debug Adapter support (metals)" // {default = config.vim.languages.enableDAP;};
      config = mkOption {
        description = "Lua configuration for dap";
        type = luaInline;
        default = mkLuaInline ''
          dap.configurations.scala = {
            {
              type = "scala",
              request = "launch",
              name = "RunOrTest",
              metals = {
                runType = "runOrTestFile",
                --args = { "firstArg", "secondArg", "thirdArg" }, -- here just as an example
              },
            },
            {
              type = "scala",
              request = "launch",
              name = "Test Target",
              metals = {
                runType = "testTarget",
              },
            },
          }
        '';
      };
    };

    fixShortmess = mkOption {
      type = bool;
      description = "Remove the 'F' flag from shortmess to allow messages to be shown. Without doing this, autocommands that deal with filetypes prohibit messages from being shown";
      default = true;
    };
  };

  config = mkIf cfg.enable (
    mkMerge [
      (mkIf cfg.treesitter.enable {
        vim.treesitter.enable = true;
        vim.treesitter.grammars = [cfg.treesitter.package];
      })
      (mkIf (cfg.lsp.enable || cfg.dap.enable) {
        vim = {
          startPlugins = ["nvim-metals"];
          pluginRC.nvim-metals = entryAfter ["lsp-setup"] ''
            local metals_caps = capabilities  -- from lsp-setup

            local attach_metals_keymaps = function(client, bufnr)
              attach_keymaps(client, bufnr)  -- from lsp-setup
              ${optionalString (cfg.lsp.extraMappings.listCommands != null) ''
              vim.api.nvim_buf_set_keymap(bufnr, 'n', '${cfg.lsp.extraMappings.listCommands}', '<cmd>lua ${listCommandsAction}<CR>', {noremap=true, silent=true, desc='Show all Metals commands'})
            ''}
            end

            metals_config = require('metals').bare_config()
            ${optionalString usingLualine "metals_config.init_options.statusBarProvider = 'on'"}

            metals_config.capabilities = metals_caps
            metals_config.on_attach = function(client, bufnr)
              ${optionalString usingDap "require('metals').setup_dap()"}
              attach_metals_keymaps(client, bufnr)
            end

            metals_config.settings = ${toLuaObject cfg.lsp.extraSettings}
            metals_config.settings.metalsBinaryPath = "${cfg.lsp.package}/bin/metals"

            metals_config.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(
              vim.lsp.diagnostic.on_publish_diagnostics, {
                virtual_text = {
                  prefix = '⚠',
                }
              }
            )

            ${optionalString cfg.fixShortmess ''vim.opt_global.shortmess:remove("F")''}

            local lsp_group = vim.api.nvim_create_augroup('lsp', { clear = true })

            vim.api.nvim_create_autocmd('FileType', {
                group = lsp_group,
                pattern = {'java', 'scala', 'sbt'},
                callback = function()
                    require('metals').initialize_or_attach(metals_config)
                end,
            })
          '';
        };
      })
    ]
  );
}