nvim-lualine / lualine.nvim

A blazing fast and easy to configure neovim statusline plugin written in pure lua.
MIT License
5.72k stars 455 forks source link

fix: avoid crashing when nvim_win_set_option received invalid status line text #1190

Open kiddos opened 5 months ago

kiddos commented 5 months ago

For some programming language there are some string formatting functions like String.format("%d", a) or printf("%d", a"). If lualine is combined with nvim_treesitter#statusline like the following:

lualine_x = {
  {
    'nvim_treesitter#statusline',
    type = 'vim_fun'
  },
  'encoding',
  'fileformat',
  'filetype'
},

It will sometimes crash when the code has % in it.

For example,

package samples.quickstart.service.pojo;

import java.util.HashMap;

public class StockQuoteService {
    private HashMap map = new HashMap();

    public double getPrice(String symbol) {
        Double price = (Double) map.get(symbol);
        if(price != null){
            return price.doubleValue();
        }
        return 42.00;
    }

    private void writeLog(String symbol, double price) {
        map.put(symbol, new Double(price));
        try {
            Path p = Paths.get("/home", "user", "Document", "templogs", "folde" String.format("%d.log", System.currentTimeMillis()));
        }
    }
}

the %d should be %%d in status line text, but when the line gets too long nvim_treesitter#statusline will try to trim the output text and replace with ..., and something the extra % get trimmed off, which may look something like the following

...%d.log", System.currentTimeMillis())) -> "folde" String.format("%%d.log", System.currentTimeMillis())

and this would throw. (this trigger may differ for different screen widths)

one cheap solution is to wrap vim.api.nvim_win_set_option with pcall. another solution would be to make sure vim.api.nvim_win_set_option is never called with a single %.