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.
Returns the start and end byte offsets as a TextPosition.
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.
Requires the original source text to calculate line/column from byte offsets. 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.
Requires the original source text to calculate line/column from byte offsets. Line and column numbers are 1-indexed.
§Arguments
source_text- The original YAML 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(&mut self, text: &str) -> Result<(), CommentTextError>
pub fn set_text(&mut self, text: &str) -> Result<(), CommentTextError>
Replace the full comment text, including the leading #.
§Errors
Returns a CommentTextError, leaving the tree unchanged, if text
does not start with # or contains a line break: either would turn
the token into something other than a single comment.
Afterwards this handle refers to the new comment token.
§Example
use yaml_edit::Document;
use std::str::FromStr;
let doc = Document::from_str("key: value # teh value\n").unwrap();
let mut comment = doc.comments().next().unwrap();
comment.set_text("# the value").unwrap();
assert_eq!(doc.to_string(), "key: value # the value\n");Sourcepub fn set_content(&mut self, content: &str) -> Result<(), CommentTextError>
pub fn set_content(&mut self, content: &str) -> Result<(), CommentTextError>
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.
§Errors
Returns CommentTextError::LineBreak if content contains a line
break.
§Example
use yaml_edit::Document;
use std::str::FromStr;
let doc = Document::from_str("key: value # teh value\n").unwrap();
let mut comment = doc.comments().next().unwrap();
comment.set_content("the value").unwrap();
assert_eq!(doc.to_string(), "key: value # the value\n");Sourcepub fn syntax(&self) -> &SyntaxToken<Lang>
pub fn syntax(&self) -> &SyntaxToken<Lang>
Access the underlying syntax token.