mirror of
				https://github.com/neovim/neovim.git
				synced 2025-11-04 01:34:25 +00:00 
			
		
		
		
	fix(lsp): set end_col in formatexpr (#19676)
The last line was excluded from formatting via formatexpr because the character in the params was set to 0 instead of the end of line.
This commit is contained in:
		
				
					committed by
					
						
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							2d5fce2cdb
						
					
				
				
					commit
					a46e6afb8b
				
			@@ -1982,29 +1982,32 @@ function lsp.formatexpr(opts)
 | 
				
			|||||||
    return 1
 | 
					    return 1
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  local start_line = vim.v.lnum
 | 
					  local start_lnum = vim.v.lnum
 | 
				
			||||||
  local end_line = start_line + vim.v.count - 1
 | 
					  local end_lnum = start_lnum + vim.v.count - 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if start_line > 0 and end_line > 0 then
 | 
					  if start_lnum <= 0 or end_lnum <= 0 then
 | 
				
			||||||
    local params = {
 | 
					    return 0
 | 
				
			||||||
      textDocument = util.make_text_document_params(),
 | 
					  end
 | 
				
			||||||
      range = {
 | 
					  local bufnr = api.nvim_get_current_buf()
 | 
				
			||||||
        start = { line = start_line - 1, character = 0 },
 | 
					  for _, client in pairs(lsp.get_active_clients({ bufnr = bufnr })) do
 | 
				
			||||||
        ['end'] = { line = end_line - 1, character = 0 },
 | 
					    if client.supports_method('textDocument/rangeFormatting') then
 | 
				
			||||||
      },
 | 
					      local params = util.make_formatting_params()
 | 
				
			||||||
    }
 | 
					      local end_line = vim.fn.getline(end_lnum)
 | 
				
			||||||
    params.options = util.make_formatting_params().options
 | 
					      local end_col = util._str_utfindex_enc(end_line, nil, client.offset_encoding)
 | 
				
			||||||
    local client_results =
 | 
					      params.range = {
 | 
				
			||||||
      vim.lsp.buf_request_sync(0, 'textDocument/rangeFormatting', params, timeout_ms)
 | 
					        start = {
 | 
				
			||||||
 | 
					          line = start_lnum - 1,
 | 
				
			||||||
    -- Apply the text edits from one and only one of the clients.
 | 
					          character = 0,
 | 
				
			||||||
    for client_id, response in pairs(client_results) do
 | 
					        },
 | 
				
			||||||
 | 
					        ['end'] = {
 | 
				
			||||||
 | 
					          line = end_lnum - 1,
 | 
				
			||||||
 | 
					          character = end_col,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      local response =
 | 
				
			||||||
 | 
					        client.request_sync('textDocument/rangeFormatting', params, timeout_ms, bufnr)
 | 
				
			||||||
      if response.result then
 | 
					      if response.result then
 | 
				
			||||||
        vim.lsp.util.apply_text_edits(
 | 
					        vim.lsp.util.apply_text_edits(response.result, 0, client.offset_encoding)
 | 
				
			||||||
          response.result,
 | 
					 | 
				
			||||||
          0,
 | 
					 | 
				
			||||||
          vim.lsp.get_client_by_id(client_id).offset_encoding
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
        return 0
 | 
					        return 0
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -113,7 +113,7 @@ end
 | 
				
			|||||||
--- Convert byte index to `encoding` index.
 | 
					--- Convert byte index to `encoding` index.
 | 
				
			||||||
--- Convenience wrapper around vim.str_utfindex
 | 
					--- Convenience wrapper around vim.str_utfindex
 | 
				
			||||||
---@param line string line to be indexed
 | 
					---@param line string line to be indexed
 | 
				
			||||||
---@param index number byte index (utf-8), or `nil` for length
 | 
					---@param index number|nil byte index (utf-8), or `nil` for length
 | 
				
			||||||
---@param encoding string utf-8|utf-16|utf-32|nil defaults to utf-16
 | 
					---@param encoding string utf-8|utf-16|utf-32|nil defaults to utf-16
 | 
				
			||||||
---@return number `encoding` index of `index` in `line`
 | 
					---@return number `encoding` index of `index` in `line`
 | 
				
			||||||
function M._str_utfindex_enc(line, index, encoding)
 | 
					function M._str_utfindex_enc(line, index, encoding)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user