wwkimball / yamlpath

YAML/JSON/EYAML/Compatible get/set/merge/validate/scan/convert/diff processors using powerful, intuitive, command-line friendly syntax.
https://github.com/wwkimball/yamlpath/wiki
ISC License
120 stars 23 forks source link

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

Open fr3nd opened 3 years ago

fr3nd commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

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

wwkimball commented 2 years ago

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 commented 2 years ago

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.