clang.lua 4.4 KB

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