Trims the indent of `rhs` by `self`. Returns `None` if `self` is not a prefix of `rhs` or either `self` or `rhs` use mixed indentation.
(self, rhs: Self)
| 1747 | /// |
| 1748 | /// Returns `None` if `self` is not a prefix of `rhs` or either `self` or `rhs` use mixed indentation. |
| 1749 | fn trim_start(self, rhs: Self) -> Option<Self> { |
| 1750 | let (left_tabs, left_spaces) = match self { |
| 1751 | Self::Spaces(spaces) => (0usize, spaces), |
| 1752 | Self::Tabs(tabs) => (tabs, 0usize), |
| 1753 | Self::TabSpaces { tabs, spaces } => (tabs, spaces), |
| 1754 | // Handle spaces here because it is the only indent where the spaces come before the tabs. |
| 1755 | Self::SpacesTabs { |
| 1756 | spaces: left_spaces, |
| 1757 | tabs: left_tabs, |
| 1758 | } => { |
| 1759 | return match rhs { |
| 1760 | Self::Spaces(right_spaces) => { |
| 1761 | left_spaces.checked_sub(right_spaces).map(|spaces| { |
| 1762 | if spaces == 0 { |
| 1763 | Self::Tabs(left_tabs) |
| 1764 | } else { |
| 1765 | Self::SpacesTabs { |
| 1766 | tabs: left_tabs, |
| 1767 | spaces, |
| 1768 | } |
| 1769 | } |
| 1770 | }) |
| 1771 | } |
| 1772 | Self::SpacesTabs { |
| 1773 | spaces: right_spaces, |
| 1774 | tabs: right_tabs, |
| 1775 | } => left_spaces.checked_sub(right_spaces).and_then(|spaces| { |
| 1776 | let tabs = left_tabs.checked_sub(right_tabs)?; |
| 1777 | |
| 1778 | Some(if spaces == 0 { |
| 1779 | if tabs == 0 { |
| 1780 | Self::Spaces(0) |
| 1781 | } else { |
| 1782 | Self::Tabs(tabs) |
| 1783 | } |
| 1784 | } else { |
| 1785 | Self::SpacesTabs { spaces, tabs } |
| 1786 | }) |
| 1787 | }), |
| 1788 | |
| 1789 | _ => None, |
| 1790 | }; |
| 1791 | } |
| 1792 | Self::Mixed { .. } => return None, |
| 1793 | }; |
| 1794 | |
| 1795 | let (right_tabs, right_spaces) = match rhs { |
| 1796 | Self::Spaces(spaces) => (0usize, spaces), |
| 1797 | Self::Tabs(tabs) => (tabs, 0usize), |
| 1798 | Self::TabSpaces { tabs, spaces } => (tabs, spaces), |
| 1799 | Self::SpacesTabs { .. } | Self::Mixed { .. } => return None, |
| 1800 | }; |
| 1801 | |
| 1802 | let tabs = left_tabs.checked_sub(right_tabs)?; |
| 1803 | let spaces = left_spaces.checked_sub(right_spaces)?; |
| 1804 | |
| 1805 | Some(if tabs == 0 { |
| 1806 | Self::Spaces(spaces) |