Skip to content

initial commit for yaml support#7

Open
oneubauer wants to merge 1 commit into
phelipetls:mainfrom
oneubauer:oneubauer/add-yaml-support
Open

initial commit for yaml support#7
oneubauer wants to merge 1 commit into
phelipetls:mainfrom
oneubauer:oneubauer/add-yaml-support

Conversation

@oneubauer

@oneubauer oneubauer commented May 2, 2023

Copy link
Copy Markdown

First attempt at YAML support.
Choice of yaml or json parsing is made based on filetype which can be explicitly set, if needed:

:set filetype=yaml

Related: #1

@oneubauer

Copy link
Copy Markdown
Author

Also, it might be necessary to add yaml to treesitter:

require 'nvim-treesitter.configs'.setup {
  ensure_installed = { "yaml"},
 }

@phelipetls phelipetls left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Just tested this against the YAMl document at https://learnxinyminutes.com/docs/yaml/ and I think it works well enough.

It just doesn't work for:

  • sets (not sure how to get a value out of a set with yq either).
  • complex keys (probably a wontfix due to complexity).

But I think it'd be nice if it'd work for JSON inside YAML.

Comment thread lua/jsonpath.lua
Comment on lines +107 to +111
if node:type() == "block_mapping_pair" then
local key_node = unpack(node:field("key"))
local key = get_node_text(key_node)

if key and starts_with_number(key) or contains_special_characters(key) then

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Currently, this doesn't handle JSON inside YAML.

For instance, with my cursor on

json_map: { |"key": "value" }

It returns json_map. I'd expect it to return json_map.key.

I could make it work by changing also handle the node type flow_pair

Suggested change
if node:type() == "block_mapping_pair" then
local key_node = unpack(node:field("key"))
local key = get_node_text(key_node)
if key and starts_with_number(key) or contains_special_characters(key) then
if node:type() == "block_mapping_pair" or node:type() == "flow_pair" then
local key_node = unpack(node:field("key"))
local key = get_node_text(key_node)
if key and starts_with_number(key) or contains_special_characters(key) then

But then the result would be also unexpected, json_map[""key""] instead of json_map.key. So we should handle the node double_scalar_key separately.

The node string_scalar seems to work fine, for instance in this line:

and quotes are optional: { |key: [ 3, 2, 1, takeoff ] }

Comment thread lua/jsonpath.lua
end
end

if node:type() == "block_sequence" then

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

It could improve this here by also handling the nodes of type flow_sequence. I didn't find something unexpected with the following change:

Suggested change
if node:type() == "block_sequence" then
if node:type() == "block_sequence" or node:type() == "flow_sequence" then

For instance, in this line:

json_seq: [ 3, 2, 1, "takeoff" ]

@joshzcold

Copy link
Copy Markdown

If this gets too complicated I opened a PR for similar functionality to enable winbar to yaml.nvim
cuducos/yaml.nvim#25

I don't know how good yaml.nvim is when it comes to parsing, but it seems to work for my use case.
If you wanted to keep to just json logic here and point people to yaml.nvim.

@phelipetls

Copy link
Copy Markdown
Owner

Thanks @joshzcold, that's a good recommendation.

YAML is much more complicated and I think the current implementation is hacky enough for JSON, so I'm not 100% comfortable extending support to YAML.

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.

3 participants