clang.lua 4.4 KB

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