diff options
author | Canop <cano.petrole@gmail.com> | 2022-01-21 22:37:33 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2022-01-21 22:37:33 +0100 |
commit | d75f7580fd8cd5ce7f2564b555f161ecbfdc1dcf (patch) | |
tree | 514f432c484df4f94f40445ea70ca932af74ed3c /src | |
parent | fcaf7b5cff5f038e4583d67cbe26e6f196ef7338 (diff) |
Fix a crash on a syntect panic on svelte file
fall back to unstyled when syntect crashes.
This uses a temporary fork, which hopefully will disappear
as soon as syntect 5 is usable.
Diffstat (limited to 'src')
-rw-r--r-- | src/display/displayable_tree.rs | 3 | ||||
-rw-r--r-- | src/errors.rs | 1 | ||||
-rw-r--r-- | src/filesystems/filesystems_state.rs | 4 | ||||
-rw-r--r-- | src/keys.rs | 2 | ||||
-rw-r--r-- | src/preview/preview.rs | 28 | ||||
-rw-r--r-- | src/syntactic/syntactic_view.rs | 7 |
6 files changed, 35 insertions, 10 deletions
diff --git a/src/display/displayable_tree.rs b/src/display/displayable_tree.rs index 8a27aa6..be5f2db 100644 --- a/src/display/displayable_tree.rs +++ b/src/display/displayable_tree.rs @@ -106,7 +106,8 @@ impl<'a, 's, 't> DisplayableTree<'a, 's, 't> { ) -> Result<usize, termimad::Error> { Ok(if let Some(s) = line.sum { cond_bg!(count_style, self, selected, self.skin.count); - cw.queue_g_string(count_style, format!("{:>width$}", s.to_count(), width=count_len))?; + let s = s.to_count(); + cw.queue_g_string(count_style, format!("{s:>count_len$}"))?; 1 } else { count_len + 1 diff --git a/src/errors.rs b/src/errors.rs index bf76509..749889d 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -29,6 +29,7 @@ custom_error! {pub ProgramError ZeroLenFile = "File seems empty", UnmappableFile = "File can't be mapped", UnprintableFile = "File can't be printed", // has characters that can't be printed without escaping + SyntectCrashed { details: String } = "Syntect crashed on {details:?}", } custom_error! {pub TreeBuildError diff --git a/src/filesystems/filesystems_state.rs b/src/filesystems/filesystems_state.rs index 41ed07f..fd83857 100644 --- a/src/filesystems/filesystems_state.rs +++ b/src/filesystems/filesystems_state.rs @@ -314,14 +314,14 @@ impl PanelState for FilesystemState { //- titles w.queue(cursor::MoveTo(area.left, area.top))?; let mut cw = CropWriter::new(w, width); - cw.queue_g_string(&styles.default, format!("{:width$}", "filesystem", width = wc_fs))?; + cw.queue_g_string(&styles.default, format!("{:wc_fs$}", "filesystem"))?; cw.queue_char(border_style, '│')?; if e_dsk { cw.queue_g_string(&styles.default, "disk ".to_string())?; cw.queue_char(border_style, '│')?; } if e_type { - cw.queue_g_string(&styles.default, format!("{:^width$}", "type", width = w_type))?; + cw.queue_g_string(&styles.default, format!("{:^w_type$}", "type"))?; cw.queue_char(border_style, '│')?; } if e_use { diff --git a/src/keys.rs b/src/keys.rs index 4019c21..8868733 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -72,7 +72,7 @@ pub fn key_event_desc(key: KeyEvent) -> String { s.push(c); } F(u) => { - s.push_str(&format!("F{}", u)); + s.push_str(&format!("F{u}")); } _ => { s.push_str(&format!("{:?}", key.code)); // FIXME check diff --git a/src/preview/preview.rs b/src/preview/preview.rs index d38d8a6..0e716b4 100644 --- a/src/preview/preview.rs +++ b/src/preview/preview.rs @@ -64,7 +64,7 @@ impl Preview { } PreviewMode::Text => { Ok( - SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con) + SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con, false) .transpose() .expect("syntactic view without pattern shouldn't be none") .map(Self::Syntactic)?, @@ -89,18 +89,38 @@ impl Preview { path: &Path, con: &AppContext, ) -> Self { - match SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con) { + match SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con, false) { Ok(Some(sv)) => Self::Syntactic(sv), Err(ProgramError::ZeroLenFile | ProgramError::UnmappableFile) => { debug!("zero len or unmappable file - check if system file"); Self::ZeroLen(ZeroLenFileView::new(path.to_path_buf())) } + Err(ProgramError::SyntectCrashed { details }) => { + warn!("syntect crashed with message : {details:?}"); + Self::unstyled_text(path, con) + } // not previewable as UTF8 text // we'll try reading it as binary Err(ProgramError::UnprintableFile) => Self::hex(path), _ => Self::hex(path), } } + /// build a text preview with no syntax highlighting, if possible + pub fn unstyled_text( + path: &Path, + con: &AppContext, + ) -> Self { + match SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con, true) { + Ok(Some(sv)) => Self::Syntactic(sv), + Err(ProgramError::ZeroLenFile | ProgramError::UnmappableFile) => { + debug!("zero len or unmappable file - check if system file"); + Self::ZeroLen(ZeroLenFileView::new(path.to_path_buf())) + } + // not previewable as UTF8 text - we'll try reading it as binary + Err(ProgramError::UnprintableFile) => Self::hex(path), + _ => Self::hex(path), + } + } /// try to build a filtered text view. Will return None if /// the dam gets an event before it's built pub fn filtered( @@ -112,7 +132,7 @@ impl Preview { ) -> Option<Self> { match self { Self::Syntactic(_) => { - match SyntacticView::new(path, pattern, dam, con) { + match SyntacticView::new(path, pattern, dam, con, false) { // normal finished loading Ok(Some(sv)) => Some(Self::Syntactic(sv)), @@ -122,7 +142,7 @@ impl Preview { // not previewable as UTF8 text // we'll try reading it as binary - Err(_) => Some(Self::hex(path)), + Err(_) => Some(Self::hex(path)), // FIXME try as unstyled if syntect crashed } } _ => None, // not filterable diff --git a/src/syntactic/syntactic_view.rs b/src/syntactic/syntactic_view.rs index 4ba6560..3d685dc 100644 --- a/src/syntactic/syntactic_view.rs +++ b/src/syntactic/syntactic_view.rs @@ -79,6 +79,7 @@ impl SyntacticView { pattern: InputPattern, dam: &mut Dam, con: &AppContext, + no_style: bool, ) -> Result<Option<Self>, ProgramError> { let mut sv = Self { path: path.to_path_buf(), @@ -89,7 +90,7 @@ impl SyntacticView { selection_idx: None, total_lines_count: 0, }; - if sv.read_lines(dam, con)? { + if sv.read_lines(dam, con, no_style)? { sv.select_first(); Ok(Some(sv)) } else { @@ -102,6 +103,7 @@ impl SyntacticView { &mut self, dam: &mut Dam, con: &AppContext, + no_style: bool, ) -> Result<bool, ProgramError> { let f = File::open(&self.path)?; { @@ -116,7 +118,7 @@ impl SyntacticView { if md.len() == 0 { return Err(ProgramError::ZeroLenFile); } - let with_style = md.len() < MAX_SIZE_FOR_STYLING; + let with_style = !no_style && md.len() < MAX_SIZE_FOR_STYLING; let mut reader = BufReader::new(f); self.lines.clear(); let mut line = String::new(); @@ -149,6 +151,7 @@ impl SyntacticView { let regions = if let Some(highlighter) = highlighter.as_mut() { highlighter .highlight(&line, &SYNTAXER.syntax_set) + .map_err(|e| ProgramError::SyntectCrashed { details: e.to_string() })? .iter() .map(Region::from_syntect) .collect() |