Respect --error-limit when parsing

Next step for #472
pull/481/head
Wilfred Hughes 2023-02-04 22:09:05 +07:00
parent 7ec0c2a956
commit 7d5afd78dc
5 changed files with 44 additions and 149 deletions

@ -50,7 +50,7 @@ sample_files/hack_before.php sample_files/hack_after.php
83d4a92c596b5d465ff024aa1b30be92 -
sample_files/hare_before.ha sample_files/hare_after.ha
2af3b9897290b3652d7aef626d5787e6 -
eb25ef369343f49ded00cb0d149b2e54 -
sample_files/haskell_before.hs sample_files/haskell_after.hs
5a2c0c5d4a04f79e2f8f32299e6cd364 -
@ -107,7 +107,7 @@ sample_files/lua_before.lua sample_files/lua_after.lua
9886d61f459cdf566be9c42f7fa61a12 -
sample_files/makefile_before.mk sample_files/makefile_after.mk
43524233cc128cbd5c23673874095251 -
3ee27ed4ee27354222e065185131f99c -
sample_files/metadata_before.clj sample_files/metadata_after.clj
e8e76c97731accc5572708664bb61910 -
@ -188,13 +188,13 @@ sample_files/swift_before.swift sample_files/swift_after.swift
4285db52158468d58d54115b6cb8f29b -
sample_files/syntax_error_before.js sample_files/syntax_error_after.js
867bfe1d9a53c85832334c8c229044e8 -
aad79ca060f77fb066eea0deb6aed1d2 -
sample_files/tab_before.c sample_files/tab_after.c
b652d15f3a05b82a7d871cfeca2f453f -
sample_files/tailwind_before.css sample_files/tailwind_after.css
2004624b277da0ca8f249b7787b264b5 -
b79bce6e053fd1d57217ad6b2f8067a9 -
sample_files/text_before.txt sample_files/text_after.txt
5fbdac2d1156ed8bb6b098e87f30d319 -

@ -53,65 +53,3 @@ function limitstringmime(mime::MIME, x)
end
return String(take!(buf))
end
for mime in ipy_mime
@eval begin
function display(d::InlineDisplay, ::MIME{Symbol($mime)}, x)
send_ipython(publish[],
msg_pub(execute_msg, "display_data",
Dict(
"metadata" => metadata(x), # optional
"data" => Dict($mime => limitstringmime(MIME($mime), x)))))
end
displayable(d::InlineDisplay, ::MIME{Symbol($mime)}) = true
end
end
# deal with annoying application/x-latex == text/latex synonyms
display(d::InlineDisplay, m::MIME"application/x-latex", x) = display(d, MIME("text/latex"), limitstringmime(m, x))
# deal with annoying text/javascript == application/javascript synonyms
display(d::InlineDisplay, m::MIME"text/javascript", x) = display(d, MIME("application/javascript"), limitstringmime(m, x))
# If the user explicitly calls display("foo/bar", x), we send
# the display message, also sending text/plain for text data.
displayable(d::InlineDisplay, M::MIME) = istextmime(M)
function display(d::InlineDisplay, M::MIME, x)
sx = limitstringmime(M, x)
d = Dict(string(M) => sx)
if istextmime(M)
d["text/plain"] = sx # directly show text data, e.g. text/csv
end
send_ipython(publish[],
msg_pub(execute_msg, "display_data",
Dict("metadata" => metadata(x), # optional
"data" => d)))
end
# override display to send IPython a dictionary of all supported
# output types, so that IPython can choose what to display.
function display(d::InlineDisplay, x)
undisplay(x) # dequeue previous redisplay(x)
send_ipython(publish[],
msg_pub(execute_msg, "display_data",
Dict("metadata" => metadata(x), # optional
"data" => display_dict(x))))
end
# we overload redisplay(d, x) to add x to a queue of objects to display,
# with the actual display occuring when display() is called or when
# an input cell has finished executing.
function redisplay(d::InlineDisplay, x)
if !in(x,displayqueue)
push!(displayqueue, x)
end
end
function display()
q = copy(displayqueue)
empty!(displayqueue) # so that undisplay in display(x) is no-op
for x in q
display(x)
end
end

@ -47,65 +47,3 @@ function limitstringmime(mime::MIME, x)
end
return String(take!(buf))
end
for mime in ipy_mime
@eval begin
function display(d::InlineDisplay, ::MIME{Symbol($mime)}, x)
send_ipython(publish[],
msg_pub(execute_msg, "display_data",
Dict(
"metadata" => metadata(x), # optional
"data" => Dict($mime => limitstringmime(MIME($mime), x)))))
end
displayable(d::InlineDisplay, ::MIME{Symbol($mime)}) = true
end
end
# deal with annoying application/x-latex == text/latex synonyms
display(d::InlineDisplay, m::MIME"application/x-latex", x) = display(d, MIME("text/latex"), limitstringmime(m, x))
# deal with annoying text/javascript == application/javascript synonyms
display(d::InlineDisplay, m::MIME"text/javascript", x) = display(d, MIME("application/javascript"), limitstringmime(m, x))
# If the user explicitly calls display("foo/bar", x), we send
# the display message, also sending text/plain for text data.
displayable(d::InlineDisplay, M::MIME) = istextmime(M)
function display(d::InlineDisplay, M::MIME, x)
sx = limitstringmime(M, x)
d = Dict(string(M) => sx)
if istextmime(M)
d["text/plain"] = sx # directly show text data, e.g. text/csv
end
send_ipython(publish[],
msg_pub(execute_msg, "display_data",
Dict("metadata" => metadata(x), # optional
"data" => d)))
end
# override display to send IPython a dictionary of all supported
# output types, so that IPython can choose what to display.
function display(d::InlineDisplay, x)
undisplay(x) # dequeue previous redisplay(x)
send_ipython(publish[],
msg_pub(execute_msg, "display_data",
Dict("metadata" => metadata(x), # optional
"data" => display_dict(x))))
end
# we overload redisplay(d, x) to add x to a queue of objects to display,
# with the actual display occuring when display() is called or when
# an input cell has finished executing.
function redisplay(d::InlineDisplay, x)
if !in(x,displayqueue)
push!(displayqueue, x)
end
end
function display()
q = copy(displayqueue)
empty!(displayqueue) # so that undisplay in display(x) is no-op
for x in q
display(x)
end
end

@ -369,26 +369,26 @@ fn diff_file_content(
let rhs_tree = tsp::to_tree(&rhs_src, &ts_lang);
let arena = Arena::new();
let (lhs, _lhs_err_count) = tsp::to_syntax(
let (lhs, lhs_err_count) = tsp::to_syntax(
&lhs_tree,
&lhs_src,
&arena,
&ts_lang,
diff_options.ignore_comments,
);
let (rhs, _rhs_err_count) = tsp::to_syntax(
let (rhs, rhs_err_count) = tsp::to_syntax(
&rhs_tree,
&rhs_src,
&arena,
&ts_lang,
diff_options.ignore_comments,
);
dbg!(_lhs_err_count, _rhs_err_count);
init_all_info(&lhs, &rhs);
if diff_options.check_only {
let lang_name = language.map(|l| language_name(l).into());
// TODO: respect syntax limit.
let has_syntactic_changes = lhs != rhs;
language_used = language;
@ -442,6 +442,23 @@ fn diff_file_content(
lhs_positions,
rhs_positions,
)
} else if lhs_err_count + rhs_err_count > diff_options.syntax_error_limit {
// TODO: doing a syntactic diff here is wasteful.
let lhs_positions = line_parser::change_positions(&lhs_src, &rhs_src);
let rhs_positions = line_parser::change_positions(&rhs_src, &lhs_src);
(
Some(format!(
"Text ({} error{}, exceeded DFT_ERROR_LIMIT)",
(lhs_err_count + rhs_err_count),
if (lhs_err_count + rhs_err_count) == 1 {
""
} else {
"s"
}
)),
lhs_positions,
rhs_positions,
)
} else {
language_used = language;
// TODO: Make this .expect() unnecessary.

@ -15,6 +15,8 @@ pub const DEFAULT_BYTE_LIMIT: usize = 1_000_000;
// files (the highest is slow_before/after.rs at 1.3M nodes), but
// small enough to terminate in ~5 seconds like the test file in #306.
pub const DEFAULT_GRAPH_LIMIT: usize = 3_000_000;
pub const DEFAULT_SYNTAX_ERROR_LIMIT: usize = 0;
pub const DEFAULT_TAB_WIDTH: usize = 8;
const USAGE: &str = concat!(env!("CARGO_BIN_NAME"), " [OPTIONS] OLD-PATH NEW-PATH");
@ -59,7 +61,7 @@ impl Default for DisplayOptions {
pub struct DiffOptions {
pub graph_limit: usize,
pub byte_limit: usize,
// pub error_limit: usize,
pub syntax_error_limit: usize,
pub check_only: bool,
pub ignore_comments: bool,
}
@ -69,7 +71,7 @@ impl Default for DiffOptions {
Self {
graph_limit: DEFAULT_GRAPH_LIMIT,
byte_limit: DEFAULT_BYTE_LIMIT,
// error_limit: 0,
syntax_error_limit: DEFAULT_SYNTAX_ERROR_LIMIT,
check_only: false,
ignore_comments: false,
}
@ -230,16 +232,16 @@ fn app() -> clap::Command<'static> {
.validator(|s| s.parse::<usize>())
.required(false),
)
// .arg(
// Arg::new("error-limit").long("error-limit")
// .takes_value(true)
// .value_name("LIMIT")
// .help(concat!("Use a text diff if the number of parse errors exceeds this number."))
// .default_value("0")
// .env("DFT_ERROR_LIMIT")
// .validator(|s| s.parse::<usize>())
// .required(false),
// )
.arg(
Arg::new("error-limit").long("error-limit")
.takes_value(true)
.value_name("LIMIT")
.help("Use a text diff if the number of parse errors exceeds this value.")
.default_value(formatcp!("{}", DEFAULT_SYNTAX_ERROR_LIMIT))
.env("DFT_ERROR_LIMIT")
.validator(|s| s.parse::<usize>())
.required(false),
)
.arg(
Arg::new("paths")
.value_name("PATHS")
@ -490,11 +492,11 @@ pub fn parse_args() -> Mode {
.parse::<usize>()
.expect("Value already validated by clap");
// let error_limit = matches
// .value_of("error-limit")
// .expect("Always present as we've given clap a default")
// .parse::<usize>()
// .expect("Value already validated by clap");
let syntax_error_limit = matches
.value_of("error-limit")
.expect("Always present as we've given clap a default")
.parse::<usize>()
.expect("Value already validated by clap");
let tab_width = matches
.value_of("tab-width")
@ -533,7 +535,7 @@ pub fn parse_args() -> Mode {
let diff_options = DiffOptions {
graph_limit,
byte_limit,
// error_limit,
syntax_error_limit,
check_only,
ignore_comments,
};