Add syntax highlighting with syntect.

This commit is contained in:
2025-03-31 22:32:17 -04:00
parent 365940267f
commit 9b86553513
4 changed files with 273 additions and 16 deletions

View File

@@ -41,9 +41,12 @@ pub mod qobject {
use cxx_qt_lib::{QModelIndex, QString};
use dirs;
use log::warn;
use std::fs;
use std::fs::FileType;
use log::warn;
use syntect::highlighting::{Style, ThemeSet};
use syntect::parsing::SyntaxSet;
use syntect::html::highlighted_html_for_file;
// TODO: Impleent a provider for QFileSystemModel::setIconProvider for icons.
pub struct FileSystemImpl {
@@ -66,16 +69,19 @@ impl qobject::FileSystem {
if path.is_empty() {
return QString::default();
}
// TODO: Use syntect for syntax highlighting?
// https://github.com/trishume/syntect/blob/master/examples/syncat.rs
if fs::metadata(path.to_string())
.expect(format!("Failed to get file metadata {}", path).as_str())
.is_file()
{
QString::from(
fs::read_to_string(path.to_string())
.expect(format!("Failed to read file {}", path).as_str()),
)
let ps = SyntaxSet::load_defaults_nonewlines();
let ts = ThemeSet::load_defaults();
if let Ok(result) = highlighted_html_for_file(std::path::Path::new(&path.to_string()), &ps, &ts.themes["base16-ocean.dark"]) {
QString::from(result)
} else {
warn!("Failed to read file {}", path);
QString::default()
}
} else {
warn!("Attempted to open file {} that is not a valid file", path);
QString::default()