clang.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. local M = {}
  2. M.config = function()
  3. O.lang.clang = {
  4. diagnostics = {
  5. virtual_text = { spacing = 0, prefix = "" },
  6. signs = true,
  7. underline = true,
  8. },
  9. cross_file_rename = true,
  10. header_insertion = "never",
  11. filetypes = { "c", "cpp", "objc" },
  12. formatter = {
  13. exe = "clang-format",
  14. args = {},
  15. },
  16. debug = {
  17. adapter = {
  18. command = "/usr/bin/lldb-vscode",
  19. },
  20. stop_on_entry = false,
  21. },
  22. }
  23. end
  24. M.format = function()
  25. local shared_config = {
  26. function()
  27. return {
  28. exe = O.lang.clang.formatter.exe,
  29. args = O.lang.clang.formatter.args,
  30. stdin = not (O.lang.clang.formatter.stdin ~= nil),
  31. cwd = vim.fn.expand "%:h:p",
  32. }
  33. end,
  34. }
  35. O.formatters.filetype["c"] = shared_config
  36. O.formatters.filetype["cpp"] = shared_config
  37. O.formatters.filetype["objc"] = shared_config
  38. require("formatter.config").set_defaults {
  39. logging = false,
  40. filetype = O.formatters.filetype,
  41. }
  42. end
  43. M.lint = function()
  44. -- TODO: implement linters (if applicable)
  45. return "No linters configured!"
  46. end
  47. M.lsp = function()
  48. if require("lv-utils").check_lsp_client_active "clangd" then
  49. return
  50. end
  51. local clangd_flags = { "--background-index" }
  52. if O.lang.clang.cross_file_rename then
  53. table.insert(clangd_flags, "--cross-file-rename")
  54. end
  55. table.insert(clangd_flags, "--header-insertion=" .. O.lang.clang.header_insertion)
  56. require("lspconfig").clangd.setup {
  57. cmd = { DATA_PATH .. "/lspinstall/cpp/clangd/bin/clangd", unpack(clangd_flags) },
  58. on_attach = require("lsp").common_on_attach,
  59. handlers = {
  60. ["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
  61. virtual_text = O.lang.clang.diagnostics.virtual_text,
  62. signs = O.lang.clang.diagnostics.signs,
  63. underline = O.lang.clang.diagnostics.underline,
  64. update_in_insert = true,
  65. }),
  66. },
  67. }
  68. end
  69. M.dap = function()
  70. if O.plugin.dap.active then
  71. local dap_install = require "dap-install"
  72. local dap = require "dap"
  73. dap_install.config("ccppr_vsc_dbg", {})
  74. dap.adapters.lldb = {
  75. type = "executable",
  76. command = O.lang.clang.debug.adapter.command,
  77. name = "lldb",
  78. }
  79. local shared_dap_config = {
  80. {
  81. name = "Launch",
  82. type = "lldb",
  83. request = "launch",
  84. program = function()
  85. return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file")
  86. end,
  87. cwd = "${workspaceFolder}",
  88. stopOnEntry = O.lang.clang.debug.stop_on_entry,
  89. args = {},
  90. env = function()
  91. local variables = {}
  92. for k, v in pairs(vim.fn.environ()) do
  93. table.insert(variables, string.format("%s=%s", k, v))
  94. end
  95. return variables
  96. end,
  97. runInTerminal = false,
  98. },
  99. {
  100. -- If you get an "Operation not permitted" error using this, try disabling YAMA:
  101. -- echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
  102. name = "Attach to process",
  103. type = "lldb", -- Adjust this to match your adapter name (`dap.adapters.<name>`)
  104. request = "attach",
  105. pid = function()
  106. local output = vim.fn.system { "ps", "a" }
  107. local lines = vim.split(output, "\n")
  108. local procs = {}
  109. for _, line in pairs(lines) do
  110. -- output format
  111. -- " 107021 pts/4 Ss 0:00 /bin/zsh <args>"
  112. local parts = vim.fn.split(vim.fn.trim(line), " \\+")
  113. local pid = parts[1]
  114. local name = table.concat({ unpack(parts, 5) }, " ")
  115. if pid and pid ~= "PID" then
  116. pid = tonumber(pid)
  117. if pid ~= vim.fn.getpid() then
  118. table.insert(procs, { pid = tonumber(pid), name = name })
  119. end
  120. end
  121. end
  122. local choices = { "Select process" }
  123. for i, proc in ipairs(procs) do
  124. table.insert(choices, string.format("%d: pid=%d name=%s", i, proc.pid, proc.name))
  125. end
  126. local choice = vim.fn.inputlist(choices)
  127. if choice < 1 or choice > #procs then
  128. return nil
  129. end
  130. return procs[choice].pid
  131. end,
  132. args = {},
  133. },
  134. }
  135. dap.configurations.c = shared_dap_config
  136. dap.configurations.cpp = shared_dap_config
  137. end
  138. end
  139. return M