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

yaml-path: show full path of a specific line number #143

Open
fr3nd opened this issue Sep 28, 2021 · 4 comments
Open

yaml-path: show full path of a specific line number #143

fr3nd opened this issue Sep 28, 2021 · 4 comments

Comments

@fr3nd
Copy link

fr3nd commented Sep 28, 2021

Is your feature request related to a problem? Please describe.
When editing large YAML files, it's sometimes difficult to determine the full path of the line you're editing. In some cases it's very common to be editing a value from a key that's not the one you want.

Describe the solution you'd like
Using the yaml-paths command, I'd like to be able to pass a specific line number instead of a search expression. The output should be the YAML path of this specific line.

Describe alternatives you've considered
I've considered using search expressions looking for the content of the line, but this solution is prone to fail.

Additional context
My intention is to use this feature with nvim to always show the YAML path of the current line being edited.

@wwkimball
Copy link
Owner

This is an interesting idea that's not trivial to implement. While yamlpath itself doesn't perceive the YAML data as a text file and therefore has no direct concept of line numbers -- it operates on an object model (DOM) of the data -- the underlying ruamel.yaml, which produces the DOM, may. I'll poke around the ruamel.yaml DOM and see whether line numbers are available for each data node. If so, I'll incorporate this idea.

@kronn
Copy link

kronn commented Sep 29, 2022

@wwkimball could you find a way to generate or output such a mapping? I am basically interested in the same thing as @fr3nd :-)

@wwkimball
Copy link
Owner

This is still a very interesting enhancement. When I last looked into it, ruamel.yaml (the underpinning library on which the yamlpath library is based) simply did not reliably provide line numbers in the DOM objects. I did find ln objects inconsistently added to various elements of the DOM, but they had two major issues. First, they weren't everywhere. I could get the line number and column of a comment or the outermost dict container, but not the data element a comment was attached to (and since comments in ruamel.yaml only follow the preceding data node and include all white-space including the leading new-line, this was simply not reliable). Second, there's no built-in mechanism to update them when the DOM is updated. Once the data is loaded, those line/column numbers are static. So, if a user were to load a document, make a change, then request the YAML Path of data at a particular line, the answer would be wrong.

I'm keeping this ticket open because other ruamel.yaml users are asking this same question elsewhere, too. Anthon, the author of ruamel.yaml has offered bits of custom code to address very specific use-cases for these, but nothing that auto-updates and everything I've so far seen is extremely specific to individual versions of ruamel.yaml. I try to support as many versions as I can. So, being restricted to just one version and worse, being on the hook to update that custom code to match every change to each ruamel.yaml release, is just not a good position to be in.

I check for ruamel.yaml progress on this once in a while. Should I ever see Anthon add reliable, self-updating line numbers, I will gladly add this enhancement.

@kronn
Copy link

kronn commented Sep 30, 2022

Thanks for the update. And I understand completely, going from code to AST has been done so often, that it's easy. The other way around is less common (historically) and not always easy.

If the line-numbers were updating, it would already be a benefit to have some paths available for visual annotations.

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

No branches or pull requests

3 participants