diff --git a/index/map_index_nodes.go b/index/map_index_nodes.go index 02711d0..cdf6260 100644 --- a/index/map_index_nodes.go +++ b/index/map_index_nodes.go @@ -18,20 +18,20 @@ type nodeMap struct { // of files a node has come from. type NodeOrigin struct { // Node is the node in question - Node *yaml.Node + Node *yaml.Node `json:"-"` // Line is yhe original line of where the node was found in the original file - Line int + Line int `json:"line" yaml:"line"` // Column is the original column of where the node was found in the original file - Column int + Column int `json:"column" yaml:"column"` // AbsoluteLocation is the absolute path to the reference was extracted from. // This can either be an absolute path to a file, or a URL. - AbsoluteLocation string + AbsoluteLocation string `json:"absolute_location" yaml:"absolute_location"` // Index is the index that contains the node that was located in. - Index *SpecIndex + Index *SpecIndex `json:"-" yaml:"-"` } // GetNode returns a node from the spec based on a line and column. The second return var bool is true @@ -70,42 +70,6 @@ func (index *SpecIndex) MapNodes(rootNode *yaml.Node) { close(index.nodeMapCompleted) } -// FindNodeOrigin searches this index for a matching node. If the node is found, a NodeOrigin -// is returned, otherwise nil is returned. -func (index *SpecIndex) FindNodeOrigin(node *yaml.Node) *NodeOrigin { - if node != nil { - if index.nodeMap[node.Line] != nil { - if index.nodeMap[node.Line][node.Column] != nil { - foundNode := index.nodeMap[node.Line][node.Column] - if foundNode.Kind == yaml.DocumentNode { - foundNode = foundNode.Content[0] - } - match := true - if foundNode.Value != node.Value || foundNode.Kind != node.Kind || foundNode.Tag != node.Tag { - match = false - } - if len(foundNode.Content) == len(node.Content) { - for i := range foundNode.Content { - if foundNode.Content[i].Value != node.Content[i].Value { - match = false - } - } - } - if match { - return &NodeOrigin{ - Node: foundNode, - Line: node.Line, - Column: node.Column, - AbsoluteLocation: index.specAbsolutePath, - Index: index, - } - } - } - } - } - return nil -} - func enjoyALuxuryCruise(node *yaml.Node, nodeChan chan *nodeMap, root bool) { if len(node.Content) > 0 { for _, child := range node.Content { diff --git a/index/search_rolodex.go b/index/search_rolodex.go index a59bf84..9178f27 100644 --- a/index/search_rolodex.go +++ b/index/search_rolodex.go @@ -32,5 +32,41 @@ func (r *Rolodex) FindNodeOrigin(node *yaml.Node) *NodeOrigin { searched++ } } + return r.GetRootIndex().FindNodeOrigin(node) +} + +// FindNodeOrigin searches this index for a matching node. If the node is found, a NodeOrigin +// is returned, otherwise nil is returned. +func (index *SpecIndex) FindNodeOrigin(node *yaml.Node) *NodeOrigin { + if node != nil { + if index.nodeMap[node.Line] != nil { + if index.nodeMap[node.Line][node.Column] != nil { + foundNode := index.nodeMap[node.Line][node.Column] + if foundNode.Kind == yaml.DocumentNode { + foundNode = foundNode.Content[0] + } + match := true + if foundNode.Value != node.Value || foundNode.Kind != node.Kind || foundNode.Tag != node.Tag { + match = false + } + if len(foundNode.Content) == len(node.Content) { + for i := range foundNode.Content { + if foundNode.Content[i].Value != node.Content[i].Value { + match = false + } + } + } + if match { + return &NodeOrigin{ + Node: foundNode, + Line: node.Line, + Column: node.Column, + AbsoluteLocation: index.specAbsolutePath, + Index: index, + } + } + } + } + } return nil }