efm-langserver
efm-langserver copied to clipboard
Passing dynamic data to commands?
I'm using https://github.com/mhartington/formatter.nvim to format & in some formatters I pass textwidth dynamically.
lua = {
function()
return {
exe = "luafmt",
args = {
"--indent-count",
2,
"-l",
vim.bo.textwidth, -- this is dynamic per buffer & also it will react to textwidth changes.
"--stdin"
},
stdin = true
}
end
}
So I was wondering how can I replicate this behavior?
A simple solution is to configure using nvim-lspconfig, and pass the value as a formatted string In that case, you would have to get the value before executing the command. I believe it is not difficult to do such a thing.
local f = string.format -- I set it to f, for ease of use and also to look like Python 😄🐍
local luafmt = {
formatCommand = f('luafmt --indent-count 2 -l %s --stdin -', vim.bo.textwidth),
formatStdin = true,
}
Unfortunately this value is set to 0, this would be solved by making this setting start after the file is loaded or something like that.
A simple solution is to configure using nvim-lspconfig
@AndreAugustoAAQ not sure I understand this part, can you elaborate more?
@AndreAugustoAAQ não tenho certeza se entendi essa parte, você pode explicar mais?
@ahmedelgabri nvim-lspconfig is a configuration helper, for built-in lsp One of these settings is for efm-langserver I believe that with the correct settings it is possible to pass the dynamic value to the command using the lua configuration
Here is a basic configuration of efm-langserver in lspconfig:
local lspconfig = require('lspconfig')
local f = string.format
local luafmt = {
formatCommand = f('luafmt --indent-count 2 -l %s --stdin -', vim.bo.textwidth),
formatStdin = true,
}
-- Languages efm-langserver --
local efm_languages = {
lua = { luafmt },
}
-- Filetypes efm-langserver --
local efm_filetypes = {'lua'}
local home = os.getenv('HOME')
-- Settings efm-langserver --
lspconfig.efm.setup{
cmd = {'efm-langserver', '-logfile', home .. '/.config/efm-langserver/efm.log', '-loglevel', '10'},
filetypes = efm_filetypes,
on_attach = on_attach,
init_options = {documentFormatting = true},
root_dir = function() return vim.fn.getcwd() end;
settings = {
rootMarkers = {'.git/', vim.fn.getcwd()},
languages = efm_languages,
}
}
Now explaining what each thing does, maybe unnecessary if you already understand about
Here I require the lspconfig plugin, just need to install it before using it
lspconfig local = require('lspconfig')
I set f to string.format to help when writing commands, I find this way more efficient than concatenating with '..'
local f = string.format
In this variable I defined the command to be executed when formatting the file of a certain type, at this point is where the dynamic value
local luafmt = {
formatCommand = f('luafmt --indent-count 2 -l %s --stdin -', vim.bo.textwidth),
formatStdin = true,
}
This variable stores the languages that will be supported, and which tools will be used in each language.
local efm_languages = {
lua = { luafmt },
}
Here it is defined which file types will be supported For multiple types, enclose in quotes and separate by comma Example: {'lua','html','python'}
location efm_filetypes = {'moon'}
A variable to store the user's directory
local home = os.getenv('HOME')
And finally here the settings defined previously are used
lspconfig.efm.setup{
-- Note that in the cmd key it is not possible to use the formatted string, or if possible I could not use it correctly
cmd = {'efm-langserver', '-logfile', home .. '/.config/efm-langserver/efm.log', '-loglevel', '10'},
filetypes = efm_filetypes,
-- on_attach is set to do something to attach to file
on_attach = on_attach,
init_options = {documentFormatting = true},
root_dir = function() return vim.fn.getcwd() end;
settings = {
rootMarkers = {'.git/', vim.fn.getcwd()},
languages = efm_languages,
}
}
@AndreAugustoAAQ não tenho certeza se entendi essa parte, você pode explicar mais?
Last time I checked I didn't speak Portuguese 😄
Thanks for the reply, I already tried doing that but it was not working properly still, or at least it was not behaving the way I expected it to be.
For example here https://github.com/ahmedelgabri/dotfiles/blob/6cb514c943240426f5f683cdd82cc4416b3db1b8/config/.vim/lua//config/lsp/efm.lua#L1-L23 & here https://github.com/ahmedelgabri/dotfiles/blob/6cb514c943240426f5f683cdd82cc4416b3db1b8/config/.vim/lua//config/lsp/efm.lua#L77-L83
This will set the command correctly the first time the buffer loads, but will not react to changing the textwidth or if I open a new buffer with a different textwidth value. (unless I was doing something wrong)
Last time I checked I didn't speak Portuguese 😄
Oh sorry, I'm Brazilian and I must have let the translation of the text pass to the answer 😅
Thanks for the reply, I already tried doing that but it was not working properly still, or at least it was not behaving the way I expected it to be.
For example here ahmedelgabri/dotfiles@
6cb514c/config/.vim/lua/_/config/lsp/efm.lua#L1-L23 & here ahmedelgabri/dotfiles@6cb514c/config/.vim/lua/_/config/lsp/efm.lua#L77-L83This will set the command correctly the first time the buffer loads, but will not react to changing the
textwidthor if I open a new buffer with a differenttextwidthvalue. (unless I was doing something wrong)
From what I read in this configuration you showed, in the final configuration there is a line for new buffers Maybe this line may be missing in your configuration https://github.com/ahmedelgabri/dotfiles/blob/6cb514c943240426f5f683cdd82cc4416b3db1b8/config/.vim/lua/_/config/lsp/efm.lua#L156 It could be that some configuration of the upper files is making a difference in the final result
My recommendation would be to clone this repo and test if it works as you need it, and if so, try to find the correct configuration, or the error that is preventing you from working correctly.