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

Method from_str

crates/ruff_python_formatter/src/string/docstring.rs:1656–1705  ·  view source on GitHub ↗
(s: &str)

Source from the content-addressed store, hash-verified

1654 const TAB_INDENT_WIDTH: usize = 8;
1655
1656 fn from_str(s: &str) -> Self {
1657 let mut iter = s.chars().peekable();
1658
1659 let spaces = iter.peeking_take_while(|c| *c == ' ').count();
1660 let tabs = iter.peeking_take_while(|c| *c == '\t').count();
1661
1662 if tabs == 0 {
1663 // No indent, or spaces only indent
1664 return Self::Spaces(spaces);
1665 }
1666
1667 let align_spaces = iter.peeking_take_while(|c| *c == ' ').count();
1668
1669 if spaces == 0 {
1670 if align_spaces == 0 {
1671 return Self::Tabs(tabs);
1672 }
1673
1674 // At this point it's either a smart tab (tabs followed by spaces) or a wild mix of tabs and spaces.
1675 if iter.peek().copied() != Some('\t') {
1676 return Self::TabSpaces {
1677 tabs,
1678 spaces: align_spaces,
1679 };
1680 }
1681 } else if align_spaces == 0 {
1682 return Self::SpacesTabs { spaces, tabs };
1683 }
1684
1685 // Sequence of spaces.. tabs, spaces, tabs...
1686 let mut width = spaces + tabs * Self::TAB_INDENT_WIDTH + align_spaces;
1687 // SAFETY: Safe because Ruff doesn't support files larger than 4GB.
1688 let mut len = TextSize::try_from(spaces + tabs + align_spaces).unwrap();
1689
1690 for char in iter {
1691 if char == '\t' {
1692 // Pad to the next multiple of tab_width
1693 width += tab_offset(width, Self::TAB_INDENT_WIDTH);
1694 len += '\t'.text_len();
1695 } else if char.is_whitespace() {
1696 width += char.len_utf8();
1697 len += char.text_len();
1698 } else {
1699 break;
1700 }
1701 }
1702
1703 // Mixed tabs and spaces
1704 Self::Mixed { width, len }
1705 }
1706
1707 /// Returns the indentation's visual width in columns/spaces.
1708 ///

Callers

nothing calls this directly

Calls 6

tab_offsetFunction · 0.85
len_utf8Method · 0.80
charsMethod · 0.45
countMethod · 0.45
peekMethod · 0.45
text_lenMethod · 0.45

Tested by

no test coverage detected