pub struct Comment(/* private fields */);Expand description
A single comment token in a YAML document.
Comments are trivia: they are preserved in the syntax tree but do not affect
the parsed value. Use YamlFile::comments or
Document::comments to iterate over them.
Implementations§
Source§impl Comment
impl Comment
Sourcepub fn text(&self) -> &str
pub fn text(&self) -> &str
The full comment text, including the leading #.
For # hello this returns "# hello".
Sourcepub fn content(&self) -> &str
pub fn content(&self) -> &str
The comment content with the leading # and a single optional space removed.
For # hello this returns "hello"; for #hello it returns "hello".
Trailing whitespace is left intact.
Sourcepub fn byte_range(&self) -> TextPosition
pub fn byte_range(&self) -> TextPosition
Get the byte offset range of this comment in the source text.
Sourcepub fn text_range(&self) -> TextRange
pub fn text_range(&self) -> TextRange
Get the text range of this comment as a rowan TextRange.
The range spans the full comment text, including the leading #.
Sourcepub fn set_text(&self, text: &str)
pub fn set_text(&self, text: &str)
Replace the full comment text, including the leading #.
The replacement must be a single valid comment token starting with #
and containing no newline; otherwise this panics, since writing it back
would corrupt the tree.
§Example
use yaml_edit::Document;
use std::str::FromStr;
let doc = Document::from_str("key: value # teh value\n").unwrap();
let comment = doc.comments().next().unwrap();
comment.set_text("# the value");
assert_eq!(doc.to_string(), "key: value # the value\n");Sourcepub fn set_content(&self, content: &str)
pub fn set_content(&self, content: &str)
Replace the comment content, preserving the leading # marker.
This is the counterpart to content: it sets the text
after the # and a single space. Use set_text when
you need full control over the marker and spacing.
§Example
use yaml_edit::Document;
use std::str::FromStr;
let doc = Document::from_str("key: value # teh value\n").unwrap();
let comment = doc.comments().next().unwrap();
comment.set_content("the value");
assert_eq!(doc.to_string(), "key: value # the value\n");Sourcepub fn start_position(&self, source_text: &str) -> LineColumn
pub fn start_position(&self, source_text: &str) -> LineColumn
Get the line and column where this comment starts.
Line and column numbers are 1-indexed.
§Arguments
source_text- The original YAML source text
Sourcepub fn end_position(&self, source_text: &str) -> LineColumn
pub fn end_position(&self, source_text: &str) -> LineColumn
Get the line and column where this comment ends.
Line and column numbers are 1-indexed.
§Arguments
source_text- The original YAML source text
Sourcepub fn syntax(&self) -> &SyntaxToken<Lang>
pub fn syntax(&self) -> &SyntaxToken<Lang>
Access the underlying syntax token.