Add syntax highlighting with syntect.
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user