Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

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

## [0.27.0] - 2024-01-14
Expand Down
29 changes: 12 additions & 17 deletions src/ui/syntax_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use once_cell::sync::Lazy;
use ratatui::text::{Line, Span};
use scopetime::scope_time;
use std::{
ffi::OsStr,
ops::Range,
path::{Path, PathBuf},
sync::{Arc, Mutex},
Expand Down Expand Up @@ -73,24 +72,20 @@ impl SyntaxText {
params: &RunParams<AsyncAppNotification, ProgressPercent>,
) -> asyncgit::Result<Self> {
scope_time!("syntax_highlighting");

let mut state = {
scope_time!("syntax_highlighting.0");
let syntax = file_path
.extension()
.and_then(OsStr::to_str)
.map_or_else(
|| {
SYNTAX_SET.find_syntax_by_path(
file_path.to_str().unwrap_or_default(),
)
},
|ext| SYNTAX_SET.find_syntax_by_extension(ext),
);

ParseState::new(syntax.unwrap_or_else(|| {
SYNTAX_SET.find_syntax_plain_text()
}))
let plain_text = || SYNTAX_SET.find_syntax_plain_text();
let syntax = SYNTAX_SET
.find_syntax_for_file(
file_path.to_str().unwrap_or_default(),
)
.unwrap_or_else(|e| {
log::error!("Could not read the file to detect its syntax: {e}");
Some(plain_text())
})
.unwrap_or_else(plain_text);

ParseState::new(syntax)
};

let highlighter = Highlighter::new(
Expand Down