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

Add line:col numbers of end of node to API v3 #626

Open
mathieucaroff opened this issue Jun 12, 2020 · 2 comments
Open

Add line:col numbers of end of node to API v3 #626

mathieucaroff opened this issue Jun 12, 2020 · 2 comments

Comments

@mathieucaroff
Copy link

@mathieucaroff mathieucaroff commented Jun 12, 2020

The yaml library for JS supports giving the position of the node end in the file:

https://eemeli.org/yaml/#cst-nodes

class Range {
  start: number,        // offset of first character
  end: number,          // offset after last character
  ...
}

I need that feature for the project I'm working on.

Here's what the code of API v3 currently look like:

yaml.go

type Node struct {
    ...
    // Line and Column hold the node position in the decoded YAML text.
    // These fields are not respected when encoding the node.
    Line   int
    Column int
}

I'd like to add to fields, LineEnd and ColumnEnd, that would capture the position of the end of the YAML node. I plan on working on this and making a Pull Request.

I'm interested in ideas and thoughts.

@mathieucaroff
Copy link
Author

@mathieucaroff mathieucaroff commented Jun 12, 2020

Okay, so the change in the code is ludicrously simple:

yaml.go

	// Line and Column hold the node position in the decoded YAML text.
	// LineEnd and ColumnEnd hold the position of the end of the node.
	// These fields are not respected when encoding the node.
	Line      int
	Column    int
	LineEnd   int
	ColumnEnd int

decode.go

	n.Line = p.event.start_mark.line + 1
	n.Column = p.event.start_mark.column + 1
	n.LineEnd = p.event.end_mark.line + 1
	n.ColumnEnd = p.event.end_mark.column + 1

but the change in the tests looks to be a bit of a pain:

$ grep -R "\b(Line|Column): " node_test.go | wc -l
628

Here's the helper regex I wrote for me:

^(\s*)Line:( *)(\d+),\n(\s*)Column:( *)(\d+),\n
$1Line:$2$3,\n$4Column:$5$6,\n$1LineEnd:$2$3,\n$4ColumnEnd:$5$6,\n

-- now I just have to fix the line and column numbers everywhere ;-;

mathieucaroff added a commit to mathieucaroff/yaml that referenced this issue Jun 12, 2020
@mathieucaroff
Copy link
Author

@mathieucaroff mathieucaroff commented Jun 12, 2020

It appears the p.event.end_mark from https://github.com/yaml/libyaml is unreliable. I checked their repository, and they don't have tests for start_mark and end_mark :/.

The end_mark is not completely useless, but it's just not always accurate. I'm tempted to hack the test function to make it ignore the value of end_mark, since it's inherited from an external library. -- The test should not be done in https://github.com/go-yaml/yaml, but rather in https://github.com/yaml/libyaml, since this is where the code of the parser actually lives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.