Skip to content

Commit 338dc2f

Browse files
Improve syntax detection (#2524)
* Improve syntax detection Currently, gitui prioritizes file extensions for syntax detection. When a file lacks an extension (e.g. Makefile), or the extension isn't tied to a specific format (e.g. .lock), it disables syntax highlighting. Gitui will now try to detect the syntax from the entire filename and the first line of the file using find_syntax_for_file(). --------- Co-authored-by: extrawurst <[email protected]>
1 parent cb4294a commit 338dc2f

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## Unreleased
99

1010
### Changed
11+
* improve syntax highlighting file detection [[@acuteenvy](https://github.com/acuteenvy)] ([#2524](https://github.com/extrawurst/gitui/pull/2524))
1112
* After commit: jump back to unstaged area [[@tommady](https://github.com/tommady)] ([#2476](https://github.com/extrawurst/gitui/issues/2476))
1213

1314
## [0.27.0] - 2024-01-14

Diff for: src/ui/syntax_text.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use once_cell::sync::Lazy;
66
use ratatui::text::{Line, Span};
77
use scopetime::scope_time;
88
use std::{
9-
ffi::OsStr,
109
ops::Range,
1110
path::{Path, PathBuf},
1211
sync::{Arc, Mutex},
@@ -73,24 +72,20 @@ impl SyntaxText {
7372
params: &RunParams<AsyncAppNotification, ProgressPercent>,
7473
) -> asyncgit::Result<Self> {
7574
scope_time!("syntax_highlighting");
76-
7775
let mut state = {
7876
scope_time!("syntax_highlighting.0");
79-
let syntax = file_path
80-
.extension()
81-
.and_then(OsStr::to_str)
82-
.map_or_else(
83-
|| {
84-
SYNTAX_SET.find_syntax_by_path(
85-
file_path.to_str().unwrap_or_default(),
86-
)
87-
},
88-
|ext| SYNTAX_SET.find_syntax_by_extension(ext),
89-
);
90-
91-
ParseState::new(syntax.unwrap_or_else(|| {
92-
SYNTAX_SET.find_syntax_plain_text()
93-
}))
77+
let plain_text = || SYNTAX_SET.find_syntax_plain_text();
78+
let syntax = SYNTAX_SET
79+
.find_syntax_for_file(
80+
file_path.to_str().unwrap_or_default(),
81+
)
82+
.unwrap_or_else(|e| {
83+
log::error!("Could not read the file to detect its syntax: {e}");
84+
Some(plain_text())
85+
})
86+
.unwrap_or_else(plain_text);
87+
88+
ParseState::new(syntax)
9489
};
9590

9691
let highlighter = Highlighter::new(

0 commit comments

Comments
 (0)