SmiteshP / nvim-gps

Simple statusline component that shows what scope you are working inside
Apache License 2.0
478 stars 46 forks source link

Support show on winbar #99

Open leaxoy opened 2 years ago

leaxoy commented 2 years ago

https://github.com/neovim/neovim/pull/17336 winbar ui has been merged into nightly, it's general to show the gps component, there is vscode example:

image

and neovim example: image

wookayin commented 2 years ago

You can simply configure winbar to use nvim-gps. nvim-gps is a statusline component, so how to use it for winbar, statusline, tabline, etc., is up to users or other plugins.

For example:

_G.gps_location = function()
  local gps = require "nvim-gps"
  return gps.is_available() and gps.get_location() or ""
end
vim.opt.winbar = "%{%v:lua.gps_location()%}"

Or via your favorite statusline/tabline plugin.

rockyzhang24 commented 2 years ago

@wookayin Thanks for the code. However, winbar won't be updated automatically like what statusline does when we move cursor, so it seems we need to use autocmd CursorMoved * to update it.

wookayin commented 2 years ago

However, winbar won't be updated automatically as statusline when we move cursor, so it seems we need to use autocmd CursorMoved * to update it.

@rockyzhang24 No, that's not correct and the above code works as you'd expect. Did you try them out?%{...%} expression means that the function will be called whenever needed (including after cursor update). Read the documentation::help statusline

rockyzhang24 commented 2 years ago

@wookayin Yes, I know it works as expected and no bugs for statusline. I've been using this plugin for a long time.

Confirmed that this is a bug of the newly added winbar(see this https://github.com/neovim/neovim/pull/17336#issuecomment-1132085879). I already opened an issue in the neovim repo (https://github.com/neovim/neovim/issues/18645)

So far, until this winbar bug is fixed, we should use a piece of code like this:

_G.gps_location = function()
  local gps = require "nvim-gps"
  return gps.is_available() and gps.get_location() or ""
end

vim.opt.winbar = "%{%v:lua.gps_location()%}"

vim.api.nvim_create_autocmd ("CursorMoved", {
  pattern = '*',
  command = "set winbar=%{%v:lua.gps_location()%}",
})
leaxoy commented 2 years ago

Corner case, the winbar always shows even gps is not available.

Correct case

image

Wrong case

image
rockyzhang24 commented 2 years ago

@leaxoy Use my code above and it works well currently. Because winbar won't be updated when you move your cursor (it's a bug and the PR solving it will be merged soon), thus you need the autocmd to refresh it. When that mentioned issue gets fixed, you can remove the autocmd part.

emuel-vassallo commented 2 years ago

@rockyzhang24 This causes Telescope to turn from looking like the first image to the second. Other than that, it works really great! image image

rockyzhang24 commented 2 years ago

@emuel-vassallo Using the autocmd is just a temporary workaround to solve the winbar update issue I mentioned above and it causes this telescope side effect. Waiting for the fix (https://github.com/neovim/neovim/pull/18646) to be merged, and then the autocmd will not be needed.

rockyzhang24 commented 2 years ago

@emuel-vassallo Try to change the autocmd event to CursorHold to avoid it disrupting telescope windows.

emuel-vassallo commented 2 years ago

@emuel-vassallo Try to change the autocmd event to CursorHold to avoid it disrupting telescope windows.

@rockyzhang24 That works, thank you!

SmiteshP commented 2 years ago

Will add instructions in README on how to setup in winbar 👍🏽

rockyzhang24 commented 2 years ago

@SmiteshP Great! You can wait until that PR fixing the update bug gets merged.

max397574 commented 2 years ago

it's merged

rockyzhang24 commented 2 years ago

Great. Then adding gps to winbar is the same with adding it to statusline. It's very handy. And this issue can be closed now.

nghialm269 commented 2 years ago

Now that https://github.com/neovim/neovim/pull/18650 is merged, is it possible to add click support to each part of gps to jump to it?

max397574 commented 2 years ago

perhaps open a new issue for that @nghialm269

siduck commented 2 years ago

https://github.com/fgheng/winbar.nvim