summaryrefslogtreecommitdiffstats
path: root/modules/plugins/lsp/presets/roslyn-ls.nix
blob: 0f5eb6e1c5e943b5fb6a36c43b2d17fa60159323 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
{
  config,
  lib,
  pkgs,
  ...
}: let
  inherit (lib.modules) mkIf;
  inherit (lib.nvim.types) mkLspPresetEnableOption;
  inherit (lib.meta) getExe;
  inherit (lib.generators) mkLuaInline;

  cfg = config.vim.lsp.presets.roslyn-ls;
in {
  options.vim.lsp.presets.roslyn-ls = {
    enable = mkLspPresetEnableOption {
      option = "roslyn-ls";
      display = "Roslyn";
    };
  };

  config = mkIf cfg.enable {
    vim.lsp.servers.roslyn-ls = {
      cmd = mkLuaInline ''
        {
          '${getExe pkgs.roslyn-ls}',
          '--logLevel',
          'Information',
          '--extensionLogDirectory',
          vim.fs.joinpath(vim.uv.os_tmpdir(), 'roslyn_ls/logs'),
          '--stdio',
        }
      '';
      cmd_env = mkLuaInline ''
        {
          -- Fixes LSP navigation in decompiled files for systems with symlinked TMPDIR (macOS)
          TMPDIR = vim.env.TMPDIR and vim.env.TMPDIR ~= "" and vim.fn.resolve(vim.env.TMPDIR) or nil,
        }
      '';
      handlers = {
        "workspace/projectInitializationComplete" = mkLuaInline ''
          function(_, _, ctx)
              vim.notify('Roslyn project initialization complete', vim.log.levels.INFO, { title = 'roslyn_ls' })
              local client = assert(vim.lsp.get_client_by_id(ctx.client_id))

              local function refresh_diagnostics(client)
                for buf, _ in pairs(vim.lsp.get_client_by_id(client.id).attached_buffers) do
                  if vim.api.nvim_buf_is_loaded(buf) then
                    client:request(
                      vim.lsp.protocol.Methods.textDocument_diagnostic,
                      { textDocument = vim.lsp.util.make_text_document_params(buf) },
                      nil,
                      buf
                    )
                  end
                end
              end

              refresh_diagnostics(client)
              return vim.NIL
            end
        '';
        "workspace/_roslyn_projectNeedsRestore" = mkLuaInline ''
          function(_, result, ctx)
            local client = assert(vim.lsp.get_client_by_id(ctx.client_id))

            ---@diagnostic disable-next-line: param-type-mismatch
            client:request('workspace/_roslyn_restore', result, function(err, response)
              if err then
                vim.notify(err.message, vim.log.levels.ERROR, { title = 'roslyn_ls' })
              end
              if response then
                for _, v in ipairs(response) do
                  vim.notify(v.message, vim.log.levels.INFO, { title = 'roslyn_ls' })
                end
              end
            end)

            return vim.NIL
          end
        '';
        "razor/provideDynamicFileInfo" = mkLuaInline ''
          function(_, _, _)
            vim.notify(
              'Razor is not supported.\nPlease use https://github.com/tris203/rzls.nvim',
              vim.log.levels.WARN,
              { title = 'roslyn_ls' }
            )
            return vim.NIL
          end
        '';
      };
      commands = {
        "roslyn.client.completionComplexEdit" = mkLuaInline ''
          function(command, ctx)
            local client = assert(vim.lsp.get_client_by_id(ctx.client_id))
            local args = command.arguments or {}
            local uri, edit = args[1], args[2]

            ---@diagnostic disable: undefined-field
            if uri and edit and edit.newText and edit.range then
              local workspace_edit = {
                changes = {
                  [uri.uri] = {
                    {
                      range = edit.range,
                      newText = edit.newText,
                    },
                  },
                },
              }
              vim.lsp.util.apply_workspace_edit(workspace_edit, client.offset_encoding)
            ---@diagnostic enable: undefined-field
            else
              vim.notify('roslyn_ls: completionComplexEdit args not understood: ' .. vim.inspect(args), vim.log.levels.WARN)
            end
          end
        '';

        "roslyn.client.nestedCodeAction" = mkLuaInline ''
          function(command, ctx)
            local client = assert(vim.lsp.get_client_by_id(ctx.client_id))
            local arg = command.arguments and command.arguments[1]

            if type(arg) ~= 'table' then
              vim.notify('roslyn_ls: invalid nestedCodeAction arguments', vim.log.levels.ERROR)
              return
            end

            local function handle(action)
              if not action then
                return
              end

              if action.data and not action.edit and not action.command then
                client:request('codeAction/resolve', action, function(err, resolved)
                  if err then
                    vim.notify(err.message or tostring(err), vim.log.levels.ERROR)
                    return
                  end
                  if resolved then
                    handle(resolved)
                  end
                end, ctx.bufnr)
                return
              end

              local nested = vim.islist(action) and action or action.NestedCodeActions
              if type(nested) ~= 'table' or vim.tbl_isempty(nested) then
                local function apply_action(client, action)
                  if action.edit then
                    vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding)
                  end
                  if action.command then
                    client:exec_cmd(action.command)
                  end
                end
                apply_action(client, action)
                return
              end

              if #nested == 1 then
                handle(nested[1])
                return
              end

              vim.ui.select(nested, {
                prompt = action.title or 'Select code action',
                format_item = function(item)
                  return item.title or (item.command and item.command.title) or 'Unnamed action'
                end,
              }, function(choice)
                if choice then
                  handle(choice)
                end
              end)
            end

            handle(arg)
          end
        '';

        "roslyn.client.fixAllCodeAction" = mkLuaInline ''
          function(command, ctx)
            local client = assert(vim.lsp.get_client_by_id(ctx.client_id))

            local function handle_fix_all_action(client, command, bufnr)
              local arg = command.arguments and command.arguments[1]
              if type(arg) ~= 'table' then
                vim.notify('roslyn_ls: invalid fixAllCodeAction arguments', vim.log.levels.ERROR)
                return
              end

              local flavors = arg.FixAllFlavors
              if type(flavors) ~= 'table' or vim.tbl_isempty(flavors) then
                vim.notify('roslyn_ls: fixAllCodeAction has no FixAllFlavors', vim.log.levels.WARN)
                return
              end

              vim.ui.select(flavors, {
                prompt = 'Fix All Scope:',
              }, function(chosen_scope)
                if not chosen_scope then
                  return
                end

                client:request('codeAction/resolveFixAll', {
                  title = command.title,
                  data = arg,
                  scope = chosen_scope,
                }, function(err, resolved)
                  if err then
                    vim.notify(
                      'roslyn_ls: fixAllCodeAction resolve error: ' .. (err.message or tostring(err)),
                      vim.log.levels.ERROR
                    )
                    return
                  end
                  if resolved then
                    local function apply_action(client, action)
                      if action.edit then
                        vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding)
                      end
                      if action.command then
                        client:exec_cmd(action.command)
                      end
                    end
                    apply_action(client, resolved)
                  end
                end, bufnr)
              end)
            end

            handle_fix_all_action(client, command, ctx.bufnr)
          end
        '';
      };
      root_dir = mkLuaInline ''
        function(bufnr, cb)
            local bufname = vim.api.nvim_buf_get_name(bufnr)

            local function is_decompiled(bufname)
              local _, endpos = bufname:find('[/\\]MetadataAsSource[/\\]')
              if endpos == nil then
                return false
              end
              return vim.fn.finddir(bufname:sub(1, endpos), vim.uv.os_tmpdir()) ~= ""
            end

            -- don't try to find sln or csproj for files from libraries
            -- outside of the project
            if not is_decompiled(bufname) then
              -- try find solutions root first
              local root_dir = vim.fs.root(bufnr, function(fname, _)
                return fname:match('%.sln[x]?$') ~= nil
              end)

              if not root_dir then
                -- try find projects root
                root_dir = vim.fs.root(bufnr, function(fname, _)
                  return fname:match('%.csproj$') ~= nil
                end)
              end

              if root_dir then
                cb(root_dir)
              end
            else
              -- Decompiled code (example: "/tmp/MetadataAsSource/f2bfba/DecompilationMetadataAsSourceFileProvider/d5782a/Console.cs")
              local prev_buf = vim.fn.bufnr('#')
              local client = vim.lsp.get_clients({
                name = 'roslyn_ls',
                bufnr = prev_buf ~= 1 and prev_buf or nil,
              })[1]
              if client then
                cb(client.config.root_dir)
              end
            end
          end
      '';
      on_init = [
        (mkLuaInline
          ''
            function(client)
              local root_dir = client.config.root_dir

              local function on_init_sln(client, target)
                vim.notify('Initializing: ' .. target, vim.log.levels.TRACE, { title = 'roslyn_ls' })
                ---@diagnostic disable-next-line: param-type-mismatch
                client:notify('solution/open', {
                  solution = vim.uri_from_fname(target),
                })
              end


              local function on_init_project(client, project_files)
                vim.notify('Initializing: projects', vim.log.levels.TRACE, { title = 'roslyn_ls' })
                ---@diagnostic disable-next-line: param-type-mismatch
                client:notify('project/open', {
                  projects = vim.tbl_map(function(file)
                    return vim.uri_from_fname(file)
                  end, project_files),
                })
              end

              -- try load first solution we find
              for entry, type in vim.fs.dir(root_dir) do
                if type == 'file' and (vim.endswith(entry, '.sln') or vim.endswith(entry, '.slnx')) then
                  on_init_sln(client, vim.fs.joinpath(root_dir, entry))
                  return
                end
              end

              -- if no solution is found load project
              for entry, type in vim.fs.dir(root_dir) do
                if type == 'file' and vim.endswith(entry, '.csproj') then
                  on_init_project(client, { vim.fs.joinpath(root_dir, entry) })
                end
              end
            end
          '')
      ];
      on_attach = mkLuaInline ''
        function(client, bufnr)
            -- avoid duplicate autocmds for same buffer
            if vim.api.nvim_get_autocmds({ buffer = bufnr, group = group })[1] then
              return
            end

            local function refresh_diagnostics(client)
              for buf, _ in pairs(vim.lsp.get_client_by_id(client.id).attached_buffers) do
                if vim.api.nvim_buf_is_loaded(buf) then
                  client:request(
                    vim.lsp.protocol.Methods.textDocument_diagnostic,
                    { textDocument = vim.lsp.util.make_text_document_params(buf) },
                    nil,
                    buf
                  )
                end
              end
            end

            vim.api.nvim_create_autocmd({ 'BufWritePost', 'InsertLeave' }, {
              group = group,
              buffer = bufnr,
              callback = function()
                local function refresh_diagnostics(client)
                  for buf, _ in pairs(vim.lsp.get_client_by_id(client.id).attached_buffers) do
                    if vim.api.nvim_buf_is_loaded(buf) then
                      client:request(
                        vim.lsp.protocol.Methods.textDocument_diagnostic,
                        { textDocument = vim.lsp.util.make_text_document_params(buf) },
                        nil,
                        buf
                      )
                    end
                  end
                end
                refresh_diagnostics(client)
              end,
              desc = 'roslyn_ls: refresh diagnostics',
            })
          end
      '';
      capabilities = {
        # HACK: Doesn't show any diagnostics if we do not set this to true
        textDocument = {
          diagnostic = {
            dynamicRegistration = true;
          };
        };
      };
      settings = {
        "csharp|background_analysis" = {
          dotnet_analyzer_diagnostics_scope = "fullSolution";
          dotnet_compiler_diagnostics_scope = "fullSolution";
        };
        "csharp|inlay_hints" = {
          csharp_enable_inlay_hints_for_implicit_object_creation = true;
          csharp_enable_inlay_hints_for_implicit_variable_types = true;
          csharp_enable_inlay_hints_for_lambda_parameter_types = true;
          csharp_enable_inlay_hints_for_types = true;
          dotnet_enable_inlay_hints_for_indexer_parameters = true;
          dotnet_enable_inlay_hints_for_literal_parameters = true;
          dotnet_enable_inlay_hints_for_object_creation_parameters = true;
          dotnet_enable_inlay_hints_for_other_parameters = true;
          dotnet_enable_inlay_hints_for_parameters = true;
          dotnet_suppress_inlay_hints_for_parameters_that_differ_only_by_suffix = true;
          dotnet_suppress_inlay_hints_for_parameters_that_match_argument_name = true;
          dotnet_suppress_inlay_hints_for_parameters_that_match_method_intent = true;
        };
        "csharp|symbol_search" = {
          dotnet_search_reference_assemblies = true;
        };
        "csharp|completion" = {
          dotnet_show_name_completion_suggestions = true;
          dotnet_show_completion_items_from_unimported_namespaces = true;
          dotnet_provide_regex_completions = true;
        };
        "csharp|code_lens" = {
          dotnet_enable_references_code_lens = true;
        };
      };
    };
  };
}