-
Notifications
You must be signed in to change notification settings - Fork 12
initial commit for yaml support #7
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
Open
oneubauer
wants to merge
1
commit into
phelipetls:main
Choose a base branch
from
oneubauer:oneubauer/add-yaml-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -25,7 +25,7 @@ local contains_special_characters = function(str) | |||||
| return str:match("[^a-zA-Z0-9_]") | ||||||
| end | ||||||
|
|
||||||
| M.get = function() | ||||||
| local get_json_path = function() | ||||||
| if not parsers.has_parser() then | ||||||
| return "" | ||||||
| end | ||||||
|
|
@@ -88,4 +88,80 @@ M.get = function() | |||||
| return path | ||||||
| end | ||||||
|
|
||||||
| local function get_yaml_path() | ||||||
| if not parsers.has_parser() then | ||||||
| return "" | ||||||
| end | ||||||
|
|
||||||
| local current_node = ts_utils.get_node_at_cursor() | ||||||
| if not current_node then | ||||||
| return "" | ||||||
| end | ||||||
|
|
||||||
| local accessors = {} | ||||||
| local node = current_node | ||||||
|
|
||||||
| while node do | ||||||
| local accessor = "" | ||||||
|
|
||||||
| 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 | ||||||
| accessor = string.format('["%s"]', key) | ||||||
| else | ||||||
| accessor = string.format("%s", key) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| if node:type() == "block_sequence" then | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could improve this here by also handling the nodes of type
Suggested change
For instance, in this line: json_seq: [ 3, 2, 1, "takeoff" ] |
||||||
| accessor = "[]" | ||||||
|
|
||||||
| for i, child in ipairs(ts_utils.get_named_children(node)) do | ||||||
| if ts_utils.is_parent(child, current_node) then | ||||||
| accessor = string.format("[%d]", i - 1) | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| if accessor ~= "" then | ||||||
| table.insert(accessors, 1, accessor) | ||||||
| end | ||||||
|
|
||||||
| node = node:parent() | ||||||
| end | ||||||
|
|
||||||
| if #accessors == 0 then | ||||||
| return "." | ||||||
| end | ||||||
|
|
||||||
| local path = "" | ||||||
|
|
||||||
| for i, accessor in ipairs(accessors) do | ||||||
| if i == 1 then | ||||||
| path = path .. "." .. accessor | ||||||
| elseif vim.startswith(accessor, "[") then | ||||||
| path = path .. accessor | ||||||
| else | ||||||
| path = path .. "." .. accessor | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| return path | ||||||
| end | ||||||
|
|
||||||
| M.get = function() | ||||||
| local filetype = vim.bo.filetype | ||||||
|
|
||||||
| if filetype == "json" then | ||||||
| return get_json_path() | ||||||
| elseif filetype == "yaml" or filetype == "yml" then | ||||||
| return get_yaml_path() | ||||||
| else | ||||||
| return "" | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| return M | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
It returns
json_map. I'd expect it to returnjson_map.key.I could make it work by changing also handle the node type
flow_pairBut then the result would be also unexpected,
json_map[""key""]instead ofjson_map.key. So we should handle the nodedouble_scalar_keyseparately.The node
string_scalarseems to work fine, for instance in this line: