Skip to content

Commit

Permalink
🚧 Support new format function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyen-Hoang-Nam committed May 30, 2022
1 parent 818877d commit 6c9bcdb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 67 deletions.
4 changes: 2 additions & 2 deletions lua/autocommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ local cmd = vim.api.nvim_command
local utils = require('utils.core')

-- Auto format
utils.autocomment_by_filetypes(
utils.autocommand_by_filetypes(
require('settings').autoformat.filetypes,
'BufWritePre',
[[lua require('format').format()]]
)

-- Code action
utils.autocomment_by_filetypes(
utils.autocommand_by_filetypes(
require('settings').codeaction.filetypes,
'CursorHold,CursorHoldI',
[[lua require('utils.lightbulb').code_action()]]
Expand Down
47 changes: 16 additions & 31 deletions lua/format.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
local utils = require('utils.core')
local M = {}

local settings = require('settings')
local setting_languages = require('languages.languages')
local settings = require("settings")
local setting_languages = require("languages.languages")
local filetypes = settings.efm.filetypes

local formatters = {}
Expand All @@ -22,13 +21,12 @@ M.choose_formatter = function()
local count = 0
for key, _ in pairs(all_formatters) do
count = count + 1
-- table.insert(store_formatters, key)
store_formatters[#store_formatters + 1] = key
print('[' .. count .. '] ' .. key)
print("[" .. count .. "] " .. key)
end

if count > 1 then
local option = vim.fn.input('Choose your formmatter: ')
local option = vim.fn.input("Choose your formmatter: ")

M.default_formatter[fileType] = store_formatters[tonumber(option)]
end
Expand All @@ -38,45 +36,32 @@ M.formatter_status = function()
local fileType = vim.bo.filetype
if M.default_formatter[fileType] then
local name = formatters[fileType][M.default_formatter[fileType]]
if name ~= '' then
return ' ' .. name
if name ~= "" then
return " " .. name
end
end

return ''
return ""
end

-- Credit https://github.com/terrortylor/neovim-environment/blob/main/lua/config/lsp/funcs.lua#L11
M.format = function()
local clients = vim.lsp.buf_get_clients(0)
local fileType = vim.bo.filetype
local code_formatter = M.default_formatter[fileType]

if utils.tablelength(clients) > 1 then
-- check if multiple clients, and if efm is setup
for _, c1 in pairs(clients) do
if c1.name == code_formatter then
c1.resolved_capabilities.document_formatting = true
-- if efm then disable others
for _, c2 in pairs(clients) do
-- print(c2.name, c2.resolved_capabilities.document_formatting)
if c2.name ~= code_formatter then
c2.resolved_capabilities.document_formatting = false
end
end
-- no need to contunue first loop
break
end
end
end
vim.lsp.buf.format({
filter = function(client)
return client.name == code_formatter
end,

vim.lsp.buf.formatting_sync(nil, 2000)
-- Ormolu (formatter of Haskell) is too slow
timeout_ms = 2000,
})
end

M.range_format = function()
local vim_mode = vim.api.nvim_eval('mode()')
local vim_mode = vim.api.nvim_eval("mode()")

if vim_mode == 'v' then
if vim_mode == "v" then
local start_position = vim.api.nvim_eval('getpos("v")')
local end_position = vim.api.nvim_eval('getpos(".")')

Expand Down
12 changes: 6 additions & 6 deletions lua/languages/elixir.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
local lsp = require('languages.lsp')
local lsp = require("languages.lsp")
local M = {}

M.efm = {
{
formatCommand = 'mix format -',
formatCommand = "mix format -",
formatStdin = true,
},
}

M.all_format = {
efm = 'Mix',
efm = "Mix",
}

M.default_format = 'efm'
M.default_format = "efm"

M.lsp_server = 'elixirls'
M.lsp_server = "elixirls"

M.lsp = {
capabilities = lsp.capabilities,
on_attach = lsp.on_attach,
cmd = { '/home/nguyenhoangnam/.local/share/lsp/elixir-ls/language_server.sh' },
cmd = { "/home/nguyenhoangnam/.local/share/tool/elixir-ls/language_server.sh" },
}

return M
21 changes: 8 additions & 13 deletions lua/languages/javascript.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
local lsp = require('languages.lsp')
local lsp = require("languages.lsp")
local M = {}

M.efm = {
{
formatCommand = 'prettier --tab-width=4 --use-tabs=false --stdin-filepath ${INPUT}',
formatCommand = "prettier --tab-width=4 --use-tabs=false --stdin-filepath ${INPUT}",
formatStdin = true,
},
{
-- lintCommand = 'eslint -f visualstudio --stdin --stdin-filename ${INPUT}',
lintCommand = 'eslint_d -f unix --stdin --stdin-filename ${INPUT}',
lintCommand = "eslint_d -f unix --stdin --stdin-filename ${INPUT}",
lintIgnoreExitCode = true,
lintStdin = true,
-- lintFormats = {
-- '%f(%l,%c): %tarning %m',
-- '%f(%l,%c): %rror %m',
-- },
lintFormats = { '%f:%l:%c: %m' },
lintFormats = { "%f:%l:%c: %m" },
},
}

M.all_format = {
efm = 'Prettier',
tsserver = 'Tssever',
efm = "Prettier",
tsserver = "Tssever",
}

M.default_format = 'efm'
M.default_format = "efm"

M.lsp_server = 'tsserver'
M.lsp_server = "tsserver"

M.lsp = {
capabilities = lsp.capabilities,
Expand Down
34 changes: 20 additions & 14 deletions lua/languages/lua.lua
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
local lsp = require('languages.lsp')
local lsp = require("languages.lsp")
local M = {}

M.efm = {
{
formatCommand = 'stylua - --config-path ~/.config/stylua/stylua.toml',
formatCommand = "stylua - --config-path ~/.config/stylua/stylua.toml",
formatStdin = true,
},
}

M.all_format = { efm = 'Stylua' }
M.all_format = { efm = "Stylua" }

M.default_format = 'efm'
M.default_format = "efm"

M.lsp_server = 'sumneko_lua'
M.lsp_server = "sumneko_lua"

local sumneko_root_path = '/home/nguyenhoangnam/.local/share/lsp/lua-language-server'
local sumneko_binary = sumneko_root_path .. '/bin/lua-language-server'
local sumneko_root_path = "/home/nguyenhoangnam/.local/share/tool/lua-language-server"
local sumneko_binary = sumneko_root_path .. "/bin/lua-language-server"

local runtime_path = vim.split(package.path, ';')
table.insert(runtime_path, 'lua/?.lua')
table.insert(runtime_path, 'lua/?/init.lua')
local runtime_path = vim.split(package.path, ";")
table.insert(runtime_path, "lua/?.lua")
table.insert(runtime_path, "lua/?/init.lua")

M.lsp = {
capabilities = lsp.capabilities,

on_attach = lsp.on_attach,
cmd = { sumneko_binary, '-E', sumneko_root_path .. '/main.lua' },

cmd = { sumneko_binary, "-E", sumneko_root_path .. "/main.lua" },

settings = {
Lua = {
runtime = {
version = 'LuaJIT',
version = "LuaJIT",
path = runtime_path,
},

diagnostics = {
globals = { 'vim' },
globals = { "vim" },
},

workspace = {
maxPreload = 2000,
preloadFileSize = 150,
library = vim.api.nvim_get_runtime_file('', true),
library = vim.api.nvim_get_runtime_file("", true),
},

telemetry = {
enable = false,
},
Expand Down
2 changes: 1 addition & 1 deletion lua/utils/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ M.map_filetype_filename = {
yaml = '*.yaml,*.yml',
}

function M.autocomment_by_filetypes(setting_filetypes, events, command)
function M.autocommand_by_filetypes(setting_filetypes, events, command)
local autocmd = 'autocmd ' .. events .. ' '
local i = 1
for _, filetype in pairs(setting_filetypes) do
Expand Down

0 comments on commit 6c9bcdb

Please sign in to comment.