Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit08dd8a2

Browse files
committed
Add initial multiline support
1 parent32def5a commit08dd8a2

File tree

3 files changed

+152
-35
lines changed

3 files changed

+152
-35
lines changed

‎examples/format.rs‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@ fn main() {
2929
source,
3030
line_start:Some(51),
3131
origin:Some("src/format.rs"),
32-
annotations:vec![SourceAnnotation{
33-
label:"expected `Option<String>` because of return type",
34-
annotation_type:AnnotationType::Warning,
35-
range:(5,19),
36-
}],
32+
annotations:vec![
33+
SourceAnnotation{
34+
label:"expected `Option<String>` because of return type",
35+
annotation_type:AnnotationType::Warning,
36+
range:(5,19),
37+
},
38+
SourceAnnotation{
39+
label:"expected enum `std::option::Option`",
40+
annotation_type:AnnotationType::Error,
41+
range:(23,704),
42+
},
43+
],
3744
};
3845
println!("{}", slice);
3946
}

‎src/display_list/line.rs‎

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,37 @@ use super::annotation::Annotation;
77
pubenumDisplayLine<'d>{
88
Source{
99
lineno:Option<usize>,
10+
inline_marks:Vec<DisplayMark>,
1011
line:DisplaySourceLine<'d>,
1112
},
1213
Raw(DisplayRawLine<'d>),
1314
}
1415

1516
impl<'d>DisplayLine<'d>{
16-
pubfnfmt(&self,f:&mut fmt::Formatter<'_>,lineno_max:Option<usize>) -> fmt::Result{
17+
pubfnfmt(
18+
&self,
19+
f:&mut fmt::Formatter<'_>,
20+
lineno_max:Option<usize>,
21+
inline_marks_width:usize,
22+
) -> fmt::Result{
1723
let lineno_max = lineno_max.unwrap_or(1);
1824
matchself{
19-
Self::Source{ lineno, line} =>{
25+
Self::Source{
26+
lineno,
27+
inline_marks,
28+
line,
29+
} =>{
2030
ifletSome(lineno) = lineno{
2131
write!(f,"{:>1$}", lineno, lineno_max)?;
22-
writeln!(f," | {}", line)
2332
}else{
2433
write!(f,"{:>1$}","", lineno_max)?;
25-
writeln!(f," | {}", line)
2634
}
35+
f.write_str(" |")?;
36+
write!(f,"{:>1$}","", inline_marks_width - inline_marks.len())?;
37+
for markin inline_marks{
38+
write!(f,"{}", mark)?;
39+
}
40+
writeln!(f,"{}", line)
2741
}
2842
Self::Raw(dl) => dl.fmt(f, lineno_max),
2943
}
@@ -45,13 +59,21 @@ pub enum DisplaySourceLine<'d> {
4559
impl<'d> fmt::DisplayforDisplaySourceLine<'d>{
4660
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
4761
matchself{
48-
Self::Content{ text} => f.write_str(text),
62+
Self::Content{ text} =>{
63+
f.write_char(' ')?;
64+
f.write_str(text)
65+
}
4966
Self::Annotation{
5067
annotation,
5168
range:(start, end),
5269
} =>{
53-
write!(f,"{:>1$}","", start)?;
54-
write!(f,"{:->1$} ","", end - start)?;
70+
let indent =if start ==&0{0}else{ start +1};
71+
write!(f,"{:>1$}","", indent)?;
72+
write!(f,"{:->1$}","", end - start)?;
73+
if start ==&0{
74+
f.write_str("^")?;
75+
}
76+
f.write_char(' ')?;
5577
annotation.fmt(f)
5678
}
5779
Self::Empty =>Ok(()),
@@ -81,3 +103,34 @@ impl<'d> DisplayRawLine<'d> {
81103
}
82104
}
83105
}
106+
107+
#[derive(Debug,Clone)]
108+
pubstructDisplayMark{
109+
pubmark_type:DisplayMarkType,
110+
pubannotation_type:DisplayAnnotationType,
111+
}
112+
113+
impl fmt::DisplayforDisplayMark{
114+
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
115+
matchself.mark_type{
116+
DisplayMarkType::AnnotationStart => f.write_char('/'),
117+
DisplayMarkType::AnnotationThrough => f.write_char('|'),
118+
}
119+
}
120+
}
121+
122+
#[derive(Debug,Clone)]
123+
pubenumDisplayMarkType{
124+
AnnotationThrough,
125+
AnnotationStart,
126+
}
127+
128+
#[derive(Debug,Clone)]
129+
pubenumDisplayAnnotationType{
130+
None,
131+
Error,
132+
Warning,
133+
Info,
134+
Note,
135+
Help,
136+
}

‎src/display_list/list.rs‎

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
usesuper::annotation::Annotation;
2-
usesuper::line::{DisplayLine,DisplayRawLine,DisplaySourceLine};
3-
usecrate::slice::Slice;
2+
usesuper::line::{
3+
DisplayAnnotationType,DisplayLine,DisplayMark,DisplayMarkType,DisplayRawLine,
4+
DisplaySourceLine,
5+
};
6+
usecrate::slice::{Slice,SourceAnnotation};
7+
use std::cmp;
48
use std::fmt;
59

610
#[derive(Debug,Clone)]
@@ -26,46 +30,95 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
2630

2731
body.push(DisplayLine::Source{
2832
lineno:None,
33+
inline_marks:vec![],
2934
line:DisplaySourceLine::Empty,
3035
});
3136

32-
letmut annotations= slice.annotations.iter();
37+
letmut annotations:Vec<&SourceAnnotation>= slice.annotations.iter().collect();
3338

34-
letmut current_annotation = annotations.next();
39+
//let mut current_annotation = annotations.next();
3540
letmut line_start_pos =0;
3641

3742
letmut i = slice.line_start.unwrap_or(1);
3843
for linein slice.source.lines(){
3944
let line_length = line.chars().count();
45+
46+
letmut current_annotations =vec![];
47+
letmut inline_marks =vec![];
48+
49+
annotations.retain(|ann|{
50+
if ann.range.0 >= line_start_pos && ann.range.1 <= line_start_pos + line_length{
51+
// Annotation in this line
52+
current_annotations.push(*ann);
53+
false
54+
}elseif ann.range.0 >= line_start_pos
55+
&& ann.range.0 <= line_start_pos + line_length
56+
{
57+
// Annotation starts in this line
58+
inline_marks.push(DisplayMark{
59+
mark_type:DisplayMarkType::AnnotationStart,
60+
annotation_type:DisplayAnnotationType::Error,
61+
});
62+
true
63+
}elseif ann.range.0 < line_start_pos && ann.range.1 > line_start_pos + line_length
64+
{
65+
// Annotation goes through this line
66+
inline_marks.push(DisplayMark{
67+
mark_type:DisplayMarkType::AnnotationThrough,
68+
annotation_type:DisplayAnnotationType::Error,
69+
});
70+
true
71+
}elseif ann.range.0 < line_start_pos
72+
&& ann.range.1 >= line_start_pos
73+
&& ann.range.1 <= line_start_pos + line_length
74+
{
75+
// Annotation ends on this line
76+
inline_marks.push(DisplayMark{
77+
mark_type:DisplayMarkType::AnnotationThrough,
78+
annotation_type:DisplayAnnotationType::Error,
79+
});
80+
current_annotations.push(*ann);
81+
false
82+
}else{
83+
true
84+
}
85+
});
86+
4087
body.push(DisplayLine::Source{
4188
lineno:Some(i),
89+
inline_marks,
4290
line:DisplaySourceLine::Content{text: line},
4391
});
44-
ifletSome(annotation) = current_annotation{
45-
if annotation.range.0 >= line_start_pos
46-
&& annotation.range.1 <= line_start_pos + line_length
47-
{
48-
body.push(DisplayLine::Source{
49-
lineno:None,
50-
line:DisplaySourceLine::Annotation{
51-
annotation:Annotation{
52-
label: annotation.label,
53-
},
54-
range:(
55-
annotation.range.0 - line_start_pos,
56-
annotation.range.1 - line_start_pos,
57-
),
58-
},
59-
});
60-
current_annotation = annotations.next();
61-
}
92+
for annin current_annotations{
93+
let start =if ann.range.0 >= line_start_pos{
94+
ann.range.0 - line_start_pos
95+
}else{
96+
0
97+
};
98+
let inline_marks =if ann.range.0 < line_start_pos{
99+
vec![DisplayMark{
100+
mark_type:DisplayMarkType::AnnotationThrough,
101+
annotation_type:DisplayAnnotationType::Error,
102+
}]
103+
}else{
104+
vec![]
105+
};
106+
body.push(DisplayLine::Source{
107+
lineno:None,
108+
inline_marks,
109+
line:DisplaySourceLine::Annotation{
110+
annotation:Annotation{label: ann.label},
111+
range:(start, ann.range.1 - line_start_pos),
112+
},
113+
});
62114
}
63115
line_start_pos += line_length;
64116
i +=1;
65117
}
66118

67119
body.push(DisplayLine::Source{
68120
lineno:None,
121+
inline_marks:vec![],
69122
line:DisplaySourceLine::Empty,
70123
});
71124

@@ -96,8 +149,12 @@ impl<'d> fmt::Display for DisplayList<'d> {
96149
None
97150
}
98151
});
152+
let inline_marks_width =self.body.iter().fold(0, |max, line|match line{
153+
DisplayLine::Source{ inline_marks, ..} => cmp::max(inline_marks.len(), max),
154+
_ => max,
155+
});
99156
for linein&self.body{
100-
line.fmt(f, lineno_max)?
157+
line.fmt(f, lineno_max, inline_marks_width)?
101158
}
102159
Ok(())
103160
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp