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

Method trim_start_str

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

Trims at most `indent_len` indentation from the beginning of `line`. This is useful when one needs to trim some minimum level of indentation from a code snippet collected from a docstring before attempting to reformat it.

(self, line: &str)

Source from the content-addressed store, hash-verified

1817 /// level of indentation from a code snippet collected from a docstring before
1818 /// attempting to reformat it.
1819 fn trim_start_str(self, line: &str) -> &str {
1820 let mut seen_indent_len = 0;
1821 let mut trimmed = line;
1822 let indent_len = self.columns();
1823
1824 for char in line.chars() {
1825 if seen_indent_len >= indent_len {
1826 return trimmed;
1827 }
1828 if char == '\t' {
1829 // Pad to the next multiple of tab_width
1830 seen_indent_len += tab_offset(seen_indent_len, Self::TAB_INDENT_WIDTH);
1831 trimmed = &trimmed[1..];
1832 } else if char.is_whitespace() {
1833 seen_indent_len += char.len_utf8();
1834 trimmed = &trimmed[char.len_utf8()..];
1835 } else {
1836 break;
1837 }
1838 }
1839 trimmed
1840 }
1841
1842 const fn is_spaces_tabs(self) -> bool {
1843 matches!(self, Self::SpacesTabs { .. })

Callers 2

indented_codeMethod · 0.80
pushMethod · 0.80

Calls 4

tab_offsetFunction · 0.85
columnsMethod · 0.80
len_utf8Method · 0.80
charsMethod · 0.45

Tested by

no test coverage detected