aboutsummaryrefslogtreecommitdiffstats
path: root/lua/alpha/utils.lua
blob: ee80a1c3861aebcbf78ea9ae0aca80373059484e (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
local M = {}

M.save_and_exec = function()
  -- local source_commands = {
  --   lua = "luafi %",
  --   vim = "source %",
  -- }
  --
  -- local ft = vim.api.nvim_buf_get_option(0, "filetype")
  -- local command = source_commands[ft]
  -- if command == nil then
  --   vim.notify("This type cant not be sourced", vim.log.levels.ERROR, { title = "Alpha Developer" })
  --   return
  -- end

  -- Save and source
  vim.api.nvim_command "silent w"
  vim.api.nvim_command "so"

  local current_file_name = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":t")
  vim.notify(string.format("File %s Sourced", current_file_name), vim.log.levels.INFO, { title = "Alpha Developer" })
end

return M
.open_floating_preview = function(contents, syntax, opts, ...) opts = opts or {} opts.border = opts.border or "single" return orig_open_floating_preview(contents, syntax, opts, ...) end for server, options in pairs(ops.servers) do if options.enable == true then local ok, definition = pcall(require, "pi.lsp.servers." .. server) local server_options if not ok then server_options = ops.default_options(options) else server_options = definition(options) end -- Setup the server with native LSP local client_config = vim.tbl_extend("force", { name = server, cmd = server_options.cmd or ops.get_server_cmd(server), root_dir = server_options.root_dir or ops.get_default_root_dir(), settings = server_options.settings, init_options = server_options.init_options, on_attach = server_options.on_attach, capabilities = server_options.capabilities, filetypes = server_options.filetypes, }, server_options) -- Auto-start LSP for appropriate filetypes if client_config.filetypes then vim.api.nvim_create_autocmd("FileType", { pattern = client_config.filetypes, callback = function(ev) -- Only start if no client of this type is already attached local clients = vim.lsp.get_clients({ bufnr = ev.buf, name = server }) if #clients == 0 then -- Create a copy of config for this buffer local buf_config = vim.deepcopy(client_config) if buf_config.root_dir and type(buf_config.root_dir) == "function" then buf_config.root_dir = buf_config.root_dir(vim.api.nvim_buf_get_name(ev.buf)) end vim.lsp.start(buf_config, { bufnr = ev.buf, }) end end, }) end end end -- TODO: implement null ls with options require "pi.lsp.null-ls" local servers = {} for server, options in pairs(ops.servers) do table.insert(servers, string.format("%s <%s>", server, options.enable)) end -- TODO: create an auto command that on write of the configuration, calls again the setup and restart the server if it's up. -- can get the client by name of the file vim.api.nvim_create_user_command("PiLSP", function() vim.ui.select(servers, { prompt = "LSP Servers" }, function(server) if server == nil then return end server = string.match(server, "^(.+) <") local path = string.format("%s/lua/pi/lsp/servers/%s.lua", vim.fn.stdpath "config", server) if vim.fn.filereadable(path) == 0 then local create = vim.fn.confirm("Server Does Not exist. Create new server?", "&Yes\n&No\n&Cancel") if create ~= 1 then return end end local uri = vim.uri_from_fname(path) local buffer = vim.uri_to_bufnr(uri) vim.api.nvim_win_set_buf(0, buffer) end) end, {}) end, }