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

Method add_code_line

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

Looks for a valid doctest PS2 prompt in the line given. If one is found, it is added to this code example and ownership of the example is returned to the caller. In this case, callers should continue trying to add PS2 prompt lines. But if one isn't found, then the given line is not part of the code example and ownership of this example is not returned. In either case, relevant actions will be ad

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

Source from the content-addressed store, hash-verified

906 /// In either case, relevant actions will be added to the given queue to
907 /// process.
908 fn add_code_line(
909 mut self,
910 original: InputDocstringLine<'src>,
911 queue: &mut VecDeque<CodeExampleAddAction<'src>>,
912 ) -> Option<CodeExampleDoctest<'src>> {
913 let Some((ps2_indent, ps2_after)) = original.line.split_once("...") else {
914 queue.push_back(self.into_format_action());
915 return None;
916 };
917 // PS2 prompts must have the same indentation as their
918 // corresponding PS1 prompt.[1] While the 'doctest' Python
919 // module will error in this case, we just treat this line as a
920 // non-doctest line.
921 //
922 // [1]: https://github.com/python/cpython/blob/0ff6368519ed7542ad8b443de01108690102420a/Lib/doctest.py#L733
923 if self.ps1_indent != ps2_indent {
924 queue.push_back(self.into_format_action());
925 return None;
926 }
927 // PS2 prompts must be followed by an ASCII space character unless
928 // it's an otherwise empty line[1].
929 //
930 // [1]: https://github.com/python/cpython/blob/0ff6368519ed7542ad8b443de01108690102420a/Lib/doctest.py#L809-L812
931 let code = match ps2_after.strip_prefix(' ') {
932 None if ps2_after.is_empty() => "",
933 None => {
934 queue.push_back(self.into_format_action());
935 return None;
936 }
937 Some(code) => code,
938 };
939 self.lines.push(CodeExampleLine { original, code });
940 queue.push_back(CodeExampleAddAction::Kept);
941 Some(self)
942 }
943
944 /// Consume this doctest and turn it into a formatting action.
945 fn into_format_action(self) -> CodeExampleAddAction<'src> {

Callers 1

addMethod · 0.80

Calls 10

indent_with_suffixFunction · 0.85
into_format_actionMethod · 0.80
strip_prefixMethod · 0.80
add_first_lineMethod · 0.80
push_format_actionMethod · 0.80
into_reset_actionMethod · 0.80
trim_whitespaceMethod · 0.80
is_emptyMethod · 0.45
pushMethod · 0.45
is_endMethod · 0.45

Tested by

no test coverage detected