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

Add option to anonymize line numbers#3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
oli-obk merged 2 commits intorust-lang:masterfromphansch:anonymized_line_numbers
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletionexamples/expected_type.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -37,6 +37,6 @@ fn main() {
};

let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true);
let dlf = DisplayListFormatter::new(true, false);
println!("{}", dlf.format(&dl));
}
2 changes: 1 addition & 1 deletionexamples/footer.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,6 +34,6 @@ fn main() {
};

let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true);
let dlf = DisplayListFormatter::new(true, false);
println!("{}", dlf.format(&dl));
}
2 changes: 1 addition & 1 deletionexamples/format.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,6 +55,6 @@ fn main() {
};

let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true);
let dlf = DisplayListFormatter::new(true, false);
println!("{}", dlf.format(&dl));
}
2 changes: 1 addition & 1 deletionexamples/multislice.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,6 +31,6 @@ fn main() {
};

let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true);
let dlf = DisplayListFormatter::new(true, false);
println!("{}", dlf.format(&dl));
}
8 changes: 4 additions & 4 deletionssrc/display_list/structs.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -131,7 +131,7 @@ pub enum DisplayMarkType {
/// use annotate_snippets::display_list::*;
/// use annotate_snippets::formatter::DisplayListFormatter;
///
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
///
/// let dl = DisplayList {
/// body: vec![
Expand DownExpand Up@@ -161,7 +161,7 @@ pub enum DisplayMarkType {
/// use annotate_snippets::display_list::*;
/// use annotate_snippets::formatter::DisplayListFormatter;
///
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
///
/// let dl = DisplayList {
/// body: vec![
Expand DownExpand Up@@ -214,7 +214,7 @@ pub enum DisplayHeaderType {
/// use annotate_snippets::display_list::*;
/// use annotate_snippets::formatter::DisplayListFormatter;
///
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
///
/// let dl = DisplayList {
/// body: vec![
Expand All@@ -236,7 +236,7 @@ pub enum DisplayHeaderType {
/// use annotate_snippets::display_list::*;
/// use annotate_snippets::formatter::DisplayListFormatter;
///
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
///
/// let dl = DisplayList {
/// body: vec![
Expand Down
42 changes: 32 additions & 10 deletionssrc/formatter/mod.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,17 +20,19 @@ fn repeat_char(c: char, n: usize) -> String {
s.repeat(n)
}

/// DisplayListFormatter' constructor accepts a single argument which
/// allows the formatter to optionally apply colors and emphasis
/// using `ansi_term` crate.
/// DisplayListFormatter' constructor accepts two arguments:
///
/// * `color` allows the formatter to optionally apply colors and emphasis
/// using the `ansi_term` crate.
/// * `anonymized_line_numbers` will replace line numbers in the left column with the text `LL`.
///
/// Example:
///
/// ```
/// use annotate_snippets::formatter::DisplayListFormatter;
/// use annotate_snippets::display_list::{DisplayList, DisplayLine, DisplaySourceLine};
///
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors, Don't anonymize line numbers
///
/// let dl = DisplayList {
/// body: vec![
Expand All@@ -48,23 +50,33 @@ fn repeat_char(c: char, n: usize) -> String {
/// ```
pub struct DisplayListFormatter {
stylesheet: Box<dyn Stylesheet>,
anonymized_line_numbers: bool,
}

impl DisplayListFormatter {
/// Constructor for the struct. The argument `color` selects
/// the stylesheet depending on the user preferences and `ansi_term`
/// crate availability.
pub fn new(color: bool) -> Self {
const ANONYMIZED_LINE_NUM: &'static str = "LL";

/// Constructor for the struct.
///
/// The argument `color` selects the stylesheet depending on the user preferences and
/// `ansi_term` crate availability.
///
/// The argument `anonymized_line_numbers` will replace line numbers in the left column with
/// the text `LL`. This can be useful to enable when running UI tests, such as in the Rust
/// test suite.
pub fn new(color: bool, anonymized_line_numbers: bool) -> Self {
if color {
Self {
#[cfg(feature = "ansi_term")]
stylesheet: Box::new(AnsiTermStylesheet {}),
#[cfg(not(feature = "ansi_term"))]
stylesheet: Box::new(NoColorStylesheet {}),
anonymized_line_numbers,
}
} else {
Self {
stylesheet: Box::new(NoColorStylesheet {}),
anonymized_line_numbers,
}
}
}
Expand All@@ -75,7 +87,13 @@ impl DisplayListFormatter {
DisplayLine::Source {
lineno: Some(lineno),
..
} => cmp::max(lineno.to_string().len(), max),
} => {
if self.anonymized_line_numbers {
Self::ANONYMIZED_LINE_NUM.len()
} else {
cmp::max(lineno.to_string().len(), max)
}
},
_ => max,
});
let inline_marks_width = dl.body.iter().fold(0, |max, line| match line {
Expand DownExpand Up@@ -285,7 +303,11 @@ impl DisplayListFormatter {
inline_marks,
line,
} => {
let lineno = self.format_lineno(*lineno, lineno_width);
let lineno = if self.anonymized_line_numbers {
Self::ANONYMIZED_LINE_NUM.to_string()
} else {
self.format_lineno(*lineno, lineno_width)
};
let marks = self.format_inline_marks(inline_marks, inline_marks_width);
let lf = self.format_source_line(line);
let lineno_color = self.stylesheet.get_style(StyleClass::LineNo);
Expand Down
2 changes: 1 addition & 1 deletiontests/fixtures.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,7 @@ fn test_fixtures() {
let expected_out = read_file(&path_out).expect("Failed to read file");

let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true);
let dlf = DisplayListFormatter::new(true, false);
let actual_out = dlf.format(&dl);
println!("{}", expected_out);
println!("{}", actual_out.trim_end());
Expand Down
73 changes: 58 additions & 15 deletionstests/formatter.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,7 +11,7 @@ fn test_source_empty() {
line: DisplaySourceLine::Empty,
}]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), " |");
}
Expand All@@ -37,7 +37,7 @@ fn test_source_content() {
},
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
Expand DownExpand Up@@ -65,7 +65,7 @@ fn test_source_annotation_standalone_singleline() {
},
}]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), " | ^^^^^ Example string");
}
Expand DownExpand Up@@ -109,7 +109,7 @@ fn test_source_annotation_standalone_multiline() {
},
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
Expand DownExpand Up@@ -241,7 +241,7 @@ fn test_source_annotation_standalone_multi_annotation() {
},
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), " | ----- info: Example string\n | Second line\n | warning: This is a note\n | Second line of the warning\n | ----- info: This is an info\n | ----- help: This is help\n | This is an annotation of type none");
}
Expand DownExpand Up@@ -270,7 +270,7 @@ fn test_fold_line() {
},
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
Expand All@@ -286,7 +286,7 @@ fn test_raw_origin_initial_nopos() {
header_type: DisplayHeaderType::Initial,
})]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), "--> src/test.rs");
}
Expand All@@ -299,7 +299,7 @@ fn test_raw_origin_initial_pos() {
header_type: DisplayHeaderType::Initial,
})]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), "--> src/test.rs:23:15");
}
Expand All@@ -312,7 +312,7 @@ fn test_raw_origin_continuation() {
header_type: DisplayHeaderType::Continuation,
})]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), "::: src/test.rs:23:15");
}
Expand All@@ -332,7 +332,7 @@ fn test_raw_annotation_unaligned() {
continuation: false,
})]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), "error[E0001]: This is an error");
}
Expand DownExpand Up@@ -366,7 +366,7 @@ fn test_raw_annotation_unaligned_multiline() {
}),
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
Expand All@@ -389,7 +389,7 @@ fn test_raw_annotation_aligned() {
continuation: false,
})]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), " = error[E0001]: This is an error");
}
Expand DownExpand Up@@ -423,7 +423,7 @@ fn test_raw_annotation_aligned_multiline() {
}),
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
Expand DownExpand Up@@ -472,7 +472,7 @@ fn test_different_annotation_types() {
}),
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
Expand All@@ -491,7 +491,50 @@ fn test_inline_marks_empty_line() {
line: DisplaySourceLine::Empty,
}]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), " | |",);
}

#[test]
fn test_anon_lines() {
let dl = DisplayList::from(vec![
DisplayLine::Source {
lineno: Some(56),
inline_marks: vec![],
line: DisplaySourceLine::Content {
text: "This is an example".to_string(),
range: (0, 19),
},
},
DisplayLine::Source {
lineno: Some(57),
inline_marks: vec![],
line: DisplaySourceLine::Content {
text: "of content lines".to_string(),
range: (0, 19),
},
},
]);

let dlf = DisplayListFormatter::new(false, true);

assert_eq!(
dlf.format(&dl),
"LL | This is an example\nLL | of content lines"
);
}

#[test]
fn test_raw_origin_initial_pos_anon_lines() {
let dl = DisplayList::from(vec![DisplayLine::Raw(DisplayRawLine::Origin {
path: "src/test.rs".to_string(),
pos: Some((23, 15)),
header_type: DisplayHeaderType::Initial,
})]);

let dlf = DisplayListFormatter::new(false, true);

// Using anonymized_line_numbers should not affect the inital position
assert_eq!(dlf.format(&dl), "--> src/test.rs:23:15");
}

[8]ページ先頭

©2009-2025 Movatter.jp