Skip to content

Commit

Permalink
feat(nvim): Do not update LSP format statusline component for markdow…
Browse files Browse the repository at this point in the history
…n previews
  • Loading branch information
mrjones2014 committed Aug 10, 2023
1 parent 279fee9 commit ef9faef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 4 additions & 2 deletions nvim/lua/my/configure/heirline/statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ M.OnePassword = {

M.LspFormatToggle = {
provider = function()
if require('my.lsp.utils').is_formatting_supported() then
local buf = vim.b.mdpreview_session and vim.b.mdpreview_session.source_buf or 0 ---@diagnostic disable-line
if require('my.lsp.utils').is_formatting_supported(buf) then
return ''
else
return ''
Expand All @@ -219,7 +220,8 @@ M.LspFormatToggle = {
},
{
provider = function()
local name = require('my.lsp.utils').get_formatter_name()
local buf = vim.b.mdpreview_session and vim.b.mdpreview_session.source_buf or 0 ---@diagnostic disable-line
local name = require('my.lsp.utils').get_formatter_name(buf)
if name then
return string.format(' (%s) ', name)
end
Expand Down
16 changes: 11 additions & 5 deletions nvim/lua/my/lsp/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,15 @@ function M.toggle_formatting_enabled(enable)
end
end

function M.get_formatter_name()
local clients = vim.lsp.get_active_clients({ bufnr = vim.api.nvim_get_current_buf() })
---@param buf number|nil defaults to 0 (current buffer)
---@return string|nil
function M.get_formatter_name(buf)
buf = buf or tonumber(vim.g.actual_curbuf or vim.api.nvim_get_current_buf())
local clients = vim.lsp.get_active_clients({ bufnr = buf })
for _, client in ipairs(clients) do
if client.supports_method('textDocument/formatting') then
if client.name == 'efm' then
local ft_config = require('my.lsp.filetypes').config[vim.bo[tonumber(vim.g.actual_curbuf or 0)].filetype]
local ft_config = require('my.lsp.filetypes').config[vim.bo[buf].filetype]
if ft_config then
if type(ft_config.formatter) == 'table' then
return ft_config.formatter[1]
Expand All @@ -133,12 +136,15 @@ function M.get_formatter_name()
return nil
end

function M.is_formatting_supported()
---@param buf number|nil defaults to 0 (current buffer)
---@return boolean
function M.is_formatting_supported(buf)
buf = buf or vim.api.nvim_get_current_buf()
if not formatting_enabled then
return false
end

local clients = vim.lsp.get_active_clients({ bufnr = vim.api.nvim_get_current_buf() })
local clients = vim.lsp.get_active_clients({ bufnr = buf })
for _, client in ipairs(clients) do
if client.supports_method('textDocument/formatting') then
return true
Expand Down

0 comments on commit ef9faef

Please sign in to comment.