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

Method add_first_line

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

Looks for the first line in a literal or code block. If a first line is found, then this returns true. Otherwise, an empty line has been found and the caller should pass it through to the docstring unchanged. (Empty lines are allowed to precede a block. And there must be at least one of them.) If the given line is invalid for a reStructuredText block (i.e., no empty lines seen between the openin

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

Source from the content-addressed store, hash-verified

1196 /// Callers must only call this when the first indentation has not yet been
1197 /// found. If it has, then this panics.
1198 fn add_first_line(
1199 mut self,
1200 original: InputDocstringLine<'src>,
1201 queue: &mut VecDeque<CodeExampleAddAction<'src>>,
1202 ) -> Option<CodeExampleRst<'src>> {
1203 assert!(self.min_indent.is_none());
1204
1205 // While the rst spec isn't completely clear on this point, through
1206 // experimentation, I found that multiple empty lines before the first
1207 // non-empty line are ignored.
1208 let (indent, rest) = indent_with_suffix(original.line);
1209 if rest.is_empty() {
1210 queue.push_back(CodeExampleAddAction::Print { original });
1211 return Some(self);
1212 }
1213 // Ignore parameters in field lists. These can only occur in
1214 // directives, not literal blocks.
1215 if self.is_directive && is_rst_option(rest) {
1216 queue.push_back(CodeExampleAddAction::Print { original });
1217 return Some(self);
1218 }
1219 let min_indent = Indentation::from_str(indent);
1220 // At this point, we found a non-empty line. The only thing we require
1221 // is that its indentation is strictly greater than the indentation of
1222 // the line containing the `::`. Otherwise, we treat this as an invalid
1223 // block and bail.
1224 if min_indent <= self.opening_indent {
1225 queue.push_back(self.into_reset_action());
1226 return None;
1227 }
1228 self.min_indent = Some(min_indent);
1229 self.push(original);
1230 queue.push_back(CodeExampleAddAction::Kept);
1231 Some(self)
1232 }
1233
1234 /// Pushes the given line as part of this code example.
1235 fn push(&mut self, original: InputDocstringLine<'src>) {

Callers 1

add_code_lineMethod · 0.80

Calls 5

indent_with_suffixFunction · 0.85
is_rst_optionFunction · 0.85
into_reset_actionMethod · 0.80
is_emptyMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected