annotate-snippets-rs icon indicating copy to clipboard operation
annotate-snippets-rs copied to clipboard

Annotate range cannot match `\t`

Open tisonkun opened this issue 2 years ago • 1 comments

use std::fs;
use std::path::PathBuf;

use annotate_snippets::display_list::{DisplayList, FormatOptions};
use annotate_snippets::snippet::{AnnotationType, Slice, Snippet, SourceAnnotation};

fn main() {
    let sql = fs::read_to_string(PathBuf::from("21.sql")).unwrap();
    let source = sql.as_str();

    println!("source: {:?}", source);
    println!("capture: {}", &source[51..52]);

    let snippet = Snippet {
        title: None,
        footer: vec![],
        slices: vec![Slice {
            source,
            line_start: 1,
            origin: None,
            annotations: vec![SourceAnnotation {
                range: (51, 52),
                label: "",
                annotation_type: AnnotationType::Error,
            }],
            fold: false,
        }],
        opt: FormatOptions {
            color: true,
            ..Default::default()
        },
    };

    println!("{}", DisplayList::from(snippet));
}

... output:

source: "-- using default substitutions\r\n\r\n\r\nselect\r\n\ts_name,\r\n\tcount(*) as numwait\r\nfrom\r\n\tsupplier,\r\n\tlineitem l1,\r\n\torders,\r\n\tnation\r\nwhere\r\n\ts_suppkey = l1.l_suppkey\r\n\tand o_orderkey = l1.l_orderkey\r\n\tand o_orderstatus = 'F'\r\n\tand l1.l_receiptdate > l1.l_commitdate\r\n\tand exists (\r\n\t\tselect\r\n\t\t\t*\r\n\t\tfrom\r\n\t\t\tlineitem l2\r\n\t\twhere\r\n\t\t\tl2.l_orderkey = l1.l_orderkey\r\n\t\t\tand l2.l_suppkey <> l1.l_suppkey\r\n\t)\r\n\tand not exists (\r\n\t\tselect\r\n\t\t\t*\r\n\t\tfrom\r\n\t\t\tlineitem l3\r\n\t\twhere\r\n\t\t\tl3.l_orderkey = l1.l_orderkey\r\n\t\t\tand l3.l_suppkey <> l1.l_suppkey\r\n\t\t\tand l3.l_receiptdate > l3.l_commitdate\r\n\t)\r\n\tand s_nationkey = n_nationkey\r\n\tand n_name = 'SAUDI ARABIA'\r\ngroup by\r\n\ts_name\r\norder by\r\n\tnumwait desc,\r\n\ts_name;\r\n"
capture: ,
   |
 1 | -- using default substitutions
 2 | 
 3 | 
 4 | select
 5 |    s_name,
   |       ^
 6 |    count(*) as numwait
 7 | from
 8 |    supplier,
 9 |    lineitem l1,
10 |    orders,
11 |    nation
12 | where
13 |    s_suppkey = l1.l_suppkey
14 |    and o_orderkey = l1.l_orderkey
15 |    and o_orderstatus = 'F'
16 |    and l1.l_receiptdate > l1.l_commitdate
17 |    and exists (
18 |            select
19 |                    *
20 |            from
21 |                    lineitem l2
22 |            where
23 |                    l2.l_orderkey = l1.l_orderkey
24 |                    and l2.l_suppkey <> l1.l_suppkey
25 |    )
26 |    and not exists (
27 |            select
28 |                    *
29 |            from
30 |                    lineitem l3
31 |            where
32 |                    l3.l_orderkey = l1.l_orderkey
33 |                    and l3.l_suppkey <> l1.l_suppkey
34 |                    and l3.l_receiptdate > l3.l_commitdate
35 |    )
36 |    and s_nationkey = n_nationkey
37 |    and n_name = 'SAUDI ARABIA'
38 | group by
39 |    s_name
40 | order by
41 |    numwait desc,
42 |    s_name;

~~If I change all \r\n to \n, it can correctly point at ,.~~

If I change all \t to spaces and correct count the span (I have a outer fn to count chars, here it becomes 54..55), it can correctly point at ,

  • Env: macOS
  • Lib Version: 0.9.1

tisonkun avatar Jun 25 '23 13:06 tisonkun

No. The problem is not about \r\n, it's about \t.

tisonkun avatar Jun 25 '23 15:06 tisonkun

Closing in favor of #25

epage avatar Mar 13 '24 15:03 epage