Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds option to reverse path display with highlighting #3010

Conversation

alycklama
Copy link
Contributor

@alycklama alycklama commented Mar 24, 2024

Description

Long paths make it sometimes difficult to see the file name. You can create a custom formatter, but I thought it would be great if we can all have a format similar to Intelij, where the file name is in front.

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

I've tested my code with the following configurations:

Simple config:

path_display = {
  filename_first = {
    reverse_directories = false
  }
},

Screenshots

Find Files

image

Live Grep

image

Quickfix

image

Find Word

image

Configuration:

  • Neovim version (nvim --version): NVIM v0.9.5 | Build type: Release | LuaJIT 2.1.1710088188
  • Operating system and version: MacOs 13.6.4 (22G513)

Checklist:

  • [ x ] My code follows the style guidelines of this project (stylua)
  • [ x ] I have performed a self-review of my own code
  • [ x ] I have commented my code, particularly in hard-to-understand areas
  • [ x ] I have made corresponding changes to the documentation (lua annotations)

lua/telescope/utils.lua Outdated Show resolved Hide resolved
@alycklama
Copy link
Contributor Author

I've added the highlighting code of @chrisgrieser, as it was the most simple and straightforward solution for highlighting:
#2014 (comment)

I tried the code posted here, put I guess it has not yet been implemented:
#2014 (comment)

@alycklama alycklama changed the title Adds option to reverse path display Adds option to reverse path display with highlighting Mar 24, 2024
@alycklama
Copy link
Contributor Author

alycklama commented Mar 25, 2024

I think I found a nice way to do highlighting in a flexible way. I'm lacking a bit of time right now, so I made a very simple proof-of-concept:

make_entry.lua

    mt_file_entry.display = function(entry)
      local hl_group, icon
      local display = utils.transform_path(opts, entry.value)
     -- local display, path_style = utils.transform_path(opts, entry.value)

      display, hl_group, icon = utils.transform_devicons(entry.value, display, disable_devicons)
       
     -- path_style would be returned from utils.transform_path (see commented code above)
      local path_style = { { 10, 20, }, hl_group }
      local style = { { { 0, #icon, }, hl_group }, path_style }

      if hl_group then
        return display, style
      else
        return display
      end
    end

When utils.transform_path returns both the transformed_path, as well as the path_style, you can easily merge the styles, resulting in the image below. The 10th up until the 20th character take the color of the icon. This is now statically done in the example above, but imagine every transform_path to decide for itself what styling to return. It would make it quite powerful and flexible.

image

What do you think? I'll work on a real example this week so you can see how it would look like.

@alycklama
Copy link
Contributor Author

I've pushed a new commit as a proof-of-concept:
106ac89

image

I've a hardcoded value (4) in there that I want to get rid of, but I need to learn more about unpacking tables dynamically in Lua. That way I could return a relative style position in the functions and shift it to the right absolute position where I need it.

@alycklama
Copy link
Contributor Author

I've removed the hardcoded value (4) with a function to shift the offset dynamically:
ac33dd0

@alycklama
Copy link
Contributor Author

Hereby an example how this could work for other places where the transform_path function is called:

image

@alycklama
Copy link
Contributor Author

So far, it seems to be working quite well. Does this align with what you envision? @jamestrew

Find Files

image

LSP definitions

image

Live Grep

image

Quickfix

image

@jamestrew
Copy link
Contributor

Thanks @alycklama
At first glance, this all looks pretty good. Give me a few days to give it a proper look through.

@jamestrew jamestrew linked an issue Mar 28, 2024 that may be closed by this pull request
@alycklama alycklama force-pushed the feature/adds-option-to-reverse-path-display branch from b3167da to f702ad2 Compare March 28, 2024 18:42
@alycklama
Copy link
Contributor Author

Thanks @alycklama At first glance, this all looks pretty good. Give me a few days to give it a proper look through.

Take all the time you need!

In the meantime I'm trying to fix the tests, but I'm having some issues running them. Any idea what the easiest way would be? I've created a local Docker container to install Ubuntu 22.04. However, I'm not able to run make test:

nvim --headless --noplugin -u scripts/minimal_init.vim -c "PlenaryBustedDirectory lua/tests/automated/ { minimal_init = './scripts/minimal_init.vim' }"
Error detected while processing command line:
E492: Not an editor command: PlenaryBustedDirectory lua/tests/automated/ { minimal_init = './scripts/minimal_init.vim' }

To workaround this, I've manually created a ~/.config/nvim/init.lua file with Lazy to install plenary. This works, because I can now use the PlenaryBustedDirectory Ex Command from the command line. However, it still looks like plenary.path is missing.

image

Any tips regarding testing?

@jamestrew
Copy link
Contributor

Yeah I have another PR to improve the CI and local testing workflow better.
In the meantime, probably easiest thing is to temporarily edit the minimal_init.vim file like this

set rtp+=.
set rtp+=../plenary.nvim/ " changed this to your plenary installation path
" set rtp+=../tree-sitter-lua/ not needed for testing

runtime! plugin/plenary.vim
runtime! plugin/telescope.lua

" I think you can comment this out as well
" outputs unnecessary warnings
" runtime! plugin/ts_lua.vim

let g:telescope_test_delay = 100

@alycklama alycklama marked this pull request as draft March 29, 2024 09:49
@alycklama alycklama force-pushed the feature/adds-option-to-reverse-path-display branch from f702ad2 to 883b238 Compare March 29, 2024 10:08
@alycklama alycklama marked this pull request as ready for review March 29, 2024 20:09
@alycklama
Copy link
Contributor Author

@jamestrew I've updated the initial post, fixed the tests. However, the nightly OSX build seems to fail in the preparation phase. Any idea what could be wrong? If that's fixed then this PR is ready for review.

Comment on lines 165 to 177
local function get_offset()
local offset = 0

if hl_group then
offset = #icon + 1
end

return offset
end

if hl_group then
return display, { { { 0, #icon }, hl_group } }
local style = { { { 0, get_offset() }, hl_group } }
style = utils.merge_styles(style, path_style, get_offset())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this get_offset function necessary? Seems redundant to me. Something similar to what been done to gen_from_vimgrep should work no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. I cleaned it up and pushed my changes.

@jamestrew
Copy link
Contributor

@jamestrew I've updated the initial post, fixed the tests. However, the nightly OSX build seems to fail in the preparation phase. Any idea what could be wrong? If that's fixed then this PR is ready for review.

Yeah don't worry about that. I think I'm pretty good with these changes. It's slightly unfortunate that entry makers using entry_display.create won't be able to utilize the highlighting but I think there's plans to rework the whole entry_display stuff so I think we'll handle the remaining entry makers in a follow up.

@alycklama
Copy link
Contributor Author

@jamestrew I've updated the initial post, fixed the tests. However, the nightly OSX build seems to fail in the preparation phase. Any idea what could be wrong? If that's fixed then this PR is ready for review.

Yeah don't worry about that. I think I'm pretty good with these changes. It's slightly unfortunate that entry makers using entry_display.create won't be able to utilize the highlighting but I think there's plans to rework the whole entry_display stuff so I think we'll handle the remaining entry makers in a follow up.

Ok, that's great to hear! If there are discussions going on regarding the plans around entry_display, I'd be more than happy to jump in and read through them.

Copy link

@lkhphuc lkhphuc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on neovim nightly and worked great. Thanks.

@alycklama alycklama force-pushed the feature/adds-option-to-reverse-path-display branch from 1199f9b to 10ac537 Compare April 13, 2024 17:11
@alycklama
Copy link
Contributor Author

Tested on neovim nightly and worked great. Thanks.

Good to hear, thanks!

I've rebased the PR and resolved the conflict.

@lkhphuc
Copy link

lkhphuc commented Apr 18, 2024

I found a case where the path is not properly highlight (applied the Comment hlgroup?) when run: Telescope buffers
All other pickers (find files, git files, oldfiles, grep word, etc) are highlighted correctly though.

Screenshot 2024-04-18 at 16 33 48

@alycklama
Copy link
Contributor Author

I found a case where the path is not properly highlight (applied the Comment hlgroup?) when run: Telescope buffers All other pickers (find files, git files, oldfiles, grep word, etc) are highlighted correctly though.

I've pushed a commit fixing this issue. Can you check again?

image

@lkhphuc
Copy link

lkhphuc commented Apr 19, 2024

That worked. Thank you.
On that note, do you think this would be nicer / possible to add the cursor position right after the file's name, instead of after the path i.e invariant.md:1 docs/typeclasses in your screenshot above?
I think that would make more sense, and the highlight color would match too.

@jamestrew jamestrew force-pushed the feature/adds-option-to-reverse-path-display branch from 99cbf40 to 3489b5c Compare April 20, 2024 04:12
@jamestrew
Copy link
Contributor

I fixed some formatting (and rebased to try something with our docgen CI but didn't work, whatever 🤷).

I'll need to think deeper about some of the loose ends like entry_display.create and line number stuff but I think we have a good starting point, enough to merge this.

Thanks!

@jamestrew jamestrew merged commit a4432df into nvim-telescope:master Apr 20, 2024
8 of 9 checks passed
fusillicode added a commit to fusillicode/dotfiles that referenced this pull request Apr 20, 2024
…vim#3010 <3  (#79)

* Love ya Telescope <3

* Fix inlay_hint old API deprecation
@Tronikelis
Copy link

Tronikelis commented Apr 20, 2024

Hi, was testing these changes and I saw that with truncate parameter, it truncates the filename and not the filepath, see image below of find files picker

Image

image

Config:

				path_display = {
					"truncate",
					filename_first = {
						reverse_directories = false,
					},
				},

This is probably not the expected behavior right?

@alycklama
Copy link
Contributor Author

alycklama commented Apr 20, 2024

Hi, was testing these changes and I saw that with truncate parameter, it truncates the filename and not the filepath, see image below of find files picker

Config:

				path_display = {
					"truncate",
					filename_first = {
						reverse_directories = false,
					},
				},

This is probably not the expected behavior right?

The path_display options are mutually exclusive (as far as I understand). You need to remove "truncate" and only use the "filename_first" option. Does that solve your issue?

@alycklama
Copy link
Contributor Author

I didn't realize the options are composable. Straight from the documentation:

        path_display can be set to an array with a combination of:
        - "hidden"          hide file names
        - "tail"            only display the file name, and not the path
        - "absolute"        display absolute paths
        - "smart"           remove as much from the path as possible to only show
                            the difference between the displayed paths.
                            Warning: The nature of the algorithm might have a negative
                            performance impact!
        - "shorten"         only display the first character of each directory in
                            the path
        - "truncate"        truncates the start of the path when the whole path will
                            not fit. To increase the gap between the path and the edge,
                            set truncate to number `truncate = 3`
        - "filename_first"  shows filenames first and then the directories

I don't think filename_first is going to be composable in any way. The reason for that is it's the only option with the filename in front.

What do you guys want to do with this? I can document that this option is not composable with any of the other options for now. If we want it to be make it composable, then the internals need to be refactored. You can't do any logic on a string. So maybe returning a tuple instead (filename, other directories) could be an option.

@Tronikelis
Copy link

What do you guys want to do with this?

What vscode does and what I expected to happen

image

image

But it would be awesome if the current truncate behavior was not changed, i.e. the path end is visible, not the start

Basically, I would want it to be truncated like it currently is:

[truncate start] Ideally

image

[truncate end] how vscode does it, but not my ideal solution

image

@jamestrew
Copy link
Contributor

jamestrew commented Apr 20, 2024

In terms of general composability, I think moving the filename_first logic to the end of the transform_path function could help in most cases I believe.

For flipping which side of the path (start vs end) is truncated, we would need to tweak the truncate logic to check for whether filename_first exists in path_display. The actual truncate function has a direction param which I'm guessing here controls the start vs end.

edit: nvm I misinterpreted, maybe we just keep truncating at the start.

@alycklama
Copy link
Contributor Author

alycklama commented Apr 20, 2024

@jamestrew I created a new PR fixing this issue:
#3065

Thanks for reporting! @Tronikelis

@Tronikelis
Copy link

@alycklama awesome 🚀

@Leiyi548
Copy link

@alycklama Telescope git_files not useful

@alycklama
Copy link
Contributor Author

alycklama commented Apr 21, 2024

Telescope git_files not useful

@Leiyi548 Could you be a bit more specific? What is not useful and why is it not useful?

A quick check on my side makes me believe everything works as intended:

image

@Leiyi548
Copy link

Telescope git_files not useful

@Leiyi548 Could you be a bit more specific? What is not useful and why is it not useful?

A quick check on my side makes me believe everything works as intended:

image

This is my config

image
image

@alycklama
Copy link
Contributor Author

@Leiyi548 Would you be able to post a screenshot of what you see as well?

@Leiyi548
Copy link

@Leiyi548 Would you be able to post a screenshot of what you see as well?

image

@alycklama
Copy link
Contributor Author

@Leiyi548 I can't reproduce it. Are you sure you are on the latest commit? Please check and report back.

@Leiyi548
Copy link

@Leiyi548 I can't reproduce it. Are you sure you are on the latest commit? Please check and report back.

maybe I use neovim in windows11?

@alycklama
Copy link
Contributor Author

maybe I use neovim in windows11?

From your screenshot it seems you're not using the latest code, hence it'll never show correctly. I asked you earlier if you could check which commit you are at. If you can't answer this question, then unfortunately, I won't be able to help you further.

@Leiyi548
Copy link

maybe I use neovim in windows11?

From your screenshot it seems you're not using the latest code, hence it'll never show correctly. I asked you earlier if you could check which commit you are at. If you can't answer this question, then unfortunately, I won't be able to help you further.

image

@jamestrew
Copy link
Contributor

@Leiyi548 please open a bug report for this and provide info there.

scottmckendry added a commit to scottmckendry/Windots that referenced this pull request May 5, 2024
thanks to the merge of nvim-telescope/telescope.nvim#3010 I was able to
simplify my telescope config quite a bit without sacrificing on looks :)
scottmckendry added a commit to scottmckendry/dots that referenced this pull request May 5, 2024
thanks to the merge of nvim-telescope/telescope.nvim#3010 I was able to
simplify my telescope config quite a bit without sacrificing on looks :)
[skip ci]
Automated sync from scottmckendry/Windots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to make file name more visible in file path
5 participants