clang.lua 4.5 KB

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