Debian Codegraph @

src/utils/tabs.rs

raw symbols SCIP
1 use unicode_segmentationrust-analyzer cargo unicode-segmentation 1.12.0 crate/::UnicodeSegmentationrust-analyzer cargo unicode-segmentation 1.12.0 UnicodeSegmentation#;
2
3 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
4 pub struct TabCfgrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg# {
5 replacementrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg#replacement.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
6 }
7
8 impl TabCfgrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg# {
9 pub fn newrust-analyzer cargo git-delta 0.19.2 utils/tabs/impl#[TabCfg]new().(width: usize) -> Selfrust-analyzer cargo git-delta 0.19.2 utils/tabs/impl#[TabCfg] {
10 TabCfgrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg# {
11 replacementrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg#replacement.: " ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(width),
12 }
13 }
14 pub fn widthrust-analyzer cargo git-delta 0.19.2 utils/tabs/impl#[TabCfg]width().(&self) -> usize {
15 self.replacementrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg#replacement..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().()
16 }
17 pub fn replacerust-analyzer cargo git-delta 0.19.2 utils/tabs/impl#[TabCfg]replace().(&self) -> bool {
18 !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.replacementrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg#replacement..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().()
19 }
20 }
21
22 /// Expand tabs as spaces.
23 pub fn expandrust-analyzer cargo git-delta 0.19.2 utils/tabs/expand().(line: &str, tab_cfg: &TabCfgrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
24 if tab_cfg.replacerust-analyzer cargo git-delta 0.19.2 utils/tabs/impl#[TabCfg]replace().() && line.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().().containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&b'\t') {
25 itertoolsrust-analyzer cargo itertools 0.14.0 crate/::joinrust-analyzer cargo itertools 0.14.0 free/join().(line.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().('\t'), &tab_cfg.replacementrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg#replacement.)
26 } else {
27 line.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
28 }
29 }
30
31 /// Remove `prefix` chars from `line`, then call `tabs::expand()`.
32 pub fn remove_prefix_and_expandrust-analyzer cargo git-delta 0.19.2 utils/tabs/remove_prefix_and_expand().(prefix: usize, line: &str, tab_cfg: &TabCfgrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
33 let line_bytes = line.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().();
34 // The to-be-removed prefixes are almost always ascii +/- (or ++/ +/.. for merges) for
35 // which grapheme clusters are not required.
36 if line_bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().prefix && line_bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#prefix]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..is_asciirust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/ascii/impl#[`[u8]`]is_ascii().() {
37 // Safety: slicing into the utf-8 line-str is ok, upto `prefix` only ascii was present.
38 expandrust-analyzer cargo git-delta 0.19.2 utils/tabs/expand().(&line[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().prefix..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., tab_cfg)
39 } else {
40 let cut_line = line.graphemesrust-analyzer cargo unicode-segmentation 1.12.0 impl#[str][UnicodeSegmentation]graphemes().(true).skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(prefix).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>();
41 expandrust-analyzer cargo git-delta 0.19.2 utils/tabs/expand().(&cut_line, tab_cfg)
42 }
43 }
44
45 #[cfg(test)]
46 pub mod testsrust-analyzer cargo git-delta 0.19.2 utils/tabs/tests/ {
47 use superrust-analyzer cargo git-delta 0.19.2 utils/tabs/::*;
48
49 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
50 fn test_remove_prefix_and_expandrust-analyzer cargo git-delta 0.19.2 utils/tabs/tests/test_remove_prefix_and_expand().() {
51 let line = "+-foo\tbar";
52 let result = remove_prefix_and_expandrust-analyzer cargo git-delta 0.19.2 utils/tabs/remove_prefix_and_expand().(2, line, &TabCfgrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg#::newrust-analyzer cargo git-delta 0.19.2 utils/tabs/impl#[TabCfg]new().(3));
53 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result, "foo bar");
54 let result = remove_prefix_and_expandrust-analyzer cargo git-delta 0.19.2 utils/tabs/remove_prefix_and_expand().(2, line, &TabCfgrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg#::newrust-analyzer cargo git-delta 0.19.2 utils/tabs/impl#[TabCfg]new().(0));
55 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result, "foo\tbar");
56
57 let utf8_prefix = "-│-foo\tbar";
58 let n = 3;
59 let result = remove_prefix_and_expandrust-analyzer cargo git-delta 0.19.2 utils/tabs/remove_prefix_and_expand().(n, utf8_prefix, &TabCfgrust-analyzer cargo git-delta 0.19.2 utils/tabs/TabCfg#::newrust-analyzer cargo git-delta 0.19.2 utils/tabs/impl#[TabCfg]new().(1));
60 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result, "foo bar");
61 // ensure non-ascii chars were removed:
62 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(utf8_prefix.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). result.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().() >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). n);
63 }
64 }