MCPcopy Create free account
hub / github.com/astral-sh/ruff / add

Method add

crates/ruff_python_formatter/src/string/docstring.rs:698–737  ·  view source on GitHub ↗

Attempt to add an original line from a docstring to this code example. Based on the line and the internal state of whether a code example is currently being collected or not, this will push an "action" to the given queue for the caller to perform. The typical case is a "print" action, which instructs the caller to just print the line as though it were not part of a code snippet.

(
        &mut self,
        original: InputDocstringLine<'src>,
        queue: &mut VecDeque<CodeExampleAddAction<'src>>,
    )

Source from the content-addressed store, hash-verified

696 /// action, which instructs the caller to just print the line as though it
697 /// were not part of a code snippet.
698 fn add(
699 &mut self,
700 original: InputDocstringLine<'src>,
701 queue: &mut VecDeque<CodeExampleAddAction<'src>>,
702 ) {
703 match self.kind.take() {
704 // There's no existing code example being built, so we look for
705 // the start of one or otherwise tell the caller we couldn't find
706 // anything.
707 None => {
708 self.add_start(original, queue);
709 }
710 Some(CodeExampleKind::Doctest(doctest)) => {
711 let Some(doctest) = doctest.add_code_line(original, queue) else {
712 self.add_start(original, queue);
713 return;
714 };
715 self.kind = Some(CodeExampleKind::Doctest(doctest));
716 }
717 Some(CodeExampleKind::Rst(litblock)) => {
718 let Some(litblock) = litblock.add_code_line(original, queue) else {
719 self.add_start(original, queue);
720 return;
721 };
722 self.kind = Some(CodeExampleKind::Rst(litblock));
723 }
724 Some(CodeExampleKind::Markdown(fenced)) => {
725 let Some(fenced) = fenced.add_code_line(original, queue) else {
726 // For Markdown, the last line in a block should be printed
727 // as-is. Especially since the last line in many Markdown
728 // fenced code blocks is identical to the start of a code
729 // block. So if we try to start a new code block with
730 // the last line, we risk opening another Markdown block
731 // inappropriately.
732 return;
733 };
734 self.kind = Some(CodeExampleKind::Markdown(fenced));
735 }
736 }
737 }
738
739 /// Finish the code example by generating any final actions if applicable.
740 ///

Callers 1

add_oneMethod · 0.45

Calls 3

add_code_lineMethod · 0.80
takeMethod · 0.45
add_startMethod · 0.45

Tested by

no test coverage detected