Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make theme configurable #15

Merged
merged 5 commits into from
Feb 6, 2020
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
3 changes: 3 additions & 0 deletions assets/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ indent_width = 4
# Set true if you are running in legacy terminal which has no true color
ansi_color = false
tabnine = ["TabNine"]
# Set theme
# You can set either theme name which is bundled by `syntect` and file path for .tmTheme
theme = "Solarized (dark)"

# Configure for *.rs files
[file.rs]
Expand Down
15 changes: 11 additions & 4 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<'a, B: CoreBuffer> Buffer<'a, B> {
syntax_parent: &'a syntax::SyntaxParent,
config: &'a config::ConfigWithDefault,
) -> Self {
let syntax = syntax_parent.load_syntax_or_txt("txt");
let syntax = syntax_parent.load_syntax_or_txt("txt", None);

let mut res = Self {
storage: None,
Expand All @@ -113,6 +113,7 @@ impl<'a, B: CoreBuffer> Buffer<'a, B> {
};
res.restart_completer();
res.reset_snippet();
res.reset_syntax();
res
}

Expand All @@ -137,8 +138,9 @@ impl<'a, B: CoreBuffer> Buffer<'a, B> {
}

pub fn extend_cache_duration(&mut self, duration: std::time::Duration) {
let highlighter = syntect::highlighting::Highlighter::new(&self.syntax.theme);
self.cache
.extend_cache_duration(self.core.core_buffer(), duration);
.extend_cache_duration(self.core.core_buffer(), duration, &highlighter);
}

pub fn indent_width(&self) -> usize {
Expand Down Expand Up @@ -173,7 +175,10 @@ impl<'a, B: CoreBuffer> Buffer<'a, B> {
})
})
.unwrap_or_default();
self.syntax = self.syntax_parent.load_syntax_or_txt(&syntax_extension);
self.syntax = self.syntax_parent.load_syntax_or_txt(
&syntax_extension,
self.get_config::<keys::Theme>().map(|s| s.as_str()),
);
self.cache = DrawCache::new(&self.syntax);
}

Expand Down Expand Up @@ -393,6 +398,7 @@ impl<'a, B: CoreBuffer> Buffer<'a, B> {
}
ShowCursor::None => {}
}
let highlighter = syntect::highlighting::Highlighter::new(&self.syntax.theme);
self.show_cursor_on_draw = ShowCursor::None;
view.bg = self.syntax.theme.settings.background.map(Into::into);
let v = Vec::new();
Expand All @@ -415,7 +421,8 @@ impl<'a, B: CoreBuffer> Buffer<'a, B> {
}

'outer: for i in self.row_offset..self.core.core_buffer().len_lines() {
self.cache.cache_line(self.core.core_buffer(), i);
self.cache
.cache_line(self.core.core_buffer(), i, &highlighter);
let line_ref = self.cache.get_line(i).unwrap();
let mut line = Cow::Borrowed(line_ref);

Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct LanguageConfigToml {
compiler: Option<CompilerConfig>,
test_command: Option<Vec<String>>,
tabnine: Option<Vec<String>>,
theme: Option<String>,
}

pub struct LanguageConfig(typemap::TypeMap);
Expand Down Expand Up @@ -105,6 +106,8 @@ impl Into<LanguageConfig> for LanguageConfigToml {
.and_then(Command::new),
);

language_config.insert_option::<keys::Theme>(self.theme);

language_config
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,9 @@ pub mod keys {
impl Key for TabNineCommand {
type Value = Command;
}

pub struct Theme;
impl Key for Theme {
type Value = String;
}
}
41 changes: 26 additions & 15 deletions src/draw_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ impl DrawState {
pub struct DrawCache<'a> {
syntax: &'a syntect::parsing::SyntaxReference,
syntax_set: &'a syntect::parsing::SyntaxSet,
highlighter: Highlighter<'a>,
bg: Color,
state_cache: Vec<DrawState>,
draw_cache: HashMap<usize, Vec<(char, CharStyle)>>,
Expand All @@ -166,31 +165,34 @@ impl<'a> DrawCache<'a> {
const CACHE_WIDTH: usize = 100;

pub fn new(syntax: &syntax::Syntax<'a>) -> Self {
let highlighter = Highlighter::new(syntax.theme);
let bg = syntax.theme.settings.background.unwrap().into();
Self {
syntax: syntax.syntax,
syntax_set: syntax.syntax_set,
highlighter,
state_cache: Vec::new(),
draw_cache: HashMap::new(),
draw_cache_pseudo: HashMap::new(),
bg,
}
}

fn start_state(&self) -> DrawState {
DrawState::new(self.syntax, &self.highlighter)
fn start_state(&self, highlighter: &syntect::highlighting::Highlighter) -> DrawState {
DrawState::new(self.syntax, highlighter)
}

pub fn extend_cache_duration<B: CoreBuffer>(&mut self, buffer: &B, duration: Duration) {
pub fn extend_cache_duration<B: CoreBuffer>(
&mut self,
buffer: &B,
duration: Duration,
highlighter: &syntect::highlighting::Highlighter,
) {
let start = Instant::now();
while self.state_cache.len() < buffer.len_lines() / Self::CACHE_WIDTH {
let mut state = self
.state_cache
.last()
.cloned()
.unwrap_or_else(|| self.start_state());
.unwrap_or_else(|| self.start_state(&highlighter));

for line in self.state_cache.len() * Self::CACHE_WIDTH
..(self.state_cache.len() + 1) * Self::CACHE_WIDTH
Expand All @@ -206,7 +208,7 @@ impl<'a> DrawCache<'a> {
)
.as_str(),
self.syntax_set,
&self.highlighter,
&highlighter,
);
}

Expand All @@ -218,17 +220,26 @@ impl<'a> DrawCache<'a> {
}
}

fn near_state(&mut self, i: usize) -> Option<DrawState> {
fn near_state(
&mut self,
i: usize,
highlighter: &syntect::highlighting::Highlighter,
) -> Option<DrawState> {
if i / Self::CACHE_WIDTH == 0 {
return Some(self.start_state());
return Some(self.start_state(highlighter));
}

self.state_cache.get(i / Self::CACHE_WIDTH - 1).cloned()
}

pub fn cache_line<B: CoreBuffer>(&mut self, buffer: &B, i: usize) {
pub fn cache_line<B: CoreBuffer>(
&mut self,
buffer: &B,
i: usize,
highlighter: &syntect::highlighting::Highlighter,
) {
if !self.draw_cache.contains_key(&i) {
if let Some(mut state) = self.near_state(i) {
if let Some(mut state) = self.near_state(i, highlighter) {
for i in i - (i % Self::CACHE_WIDTH)
..min(
buffer.len_lines(),
Expand All @@ -245,7 +256,7 @@ impl<'a> DrawCache<'a> {
)
.as_str(),
self.syntax_set,
&self.highlighter,
&highlighter,
self.bg,
);

Expand All @@ -255,7 +266,7 @@ impl<'a> DrawCache<'a> {
}

if !self.draw_cache.contains_key(&i) && !self.draw_cache_pseudo.contains_key(&i) {
let mut state = self.start_state();
let mut state = self.start_state(&highlighter);
for i in i - (i % Self::CACHE_WIDTH)
..min(
buffer.len_lines(),
Expand All @@ -272,7 +283,7 @@ impl<'a> DrawCache<'a> {
)
.as_str(),
self.syntax_set,
&self.highlighter,
&highlighter,
self.bg,
);

Expand Down
26 changes: 18 additions & 8 deletions src/syntax.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io::BufReader;
use syntect;
use syntect::easy::HighlightLines;
use syntect::highlighting::ThemeSet;
Expand All @@ -7,7 +8,7 @@ use syntect::parsing::SyntaxSet;
pub struct Syntax<'a> {
pub syntax_set: &'a syntect::parsing::SyntaxSet,
pub syntax: &'a syntect::parsing::SyntaxReference,
pub theme: &'a syntect::highlighting::Theme,
pub theme: syntect::highlighting::Theme,
}

pub struct SyntaxParent {
Expand Down Expand Up @@ -52,24 +53,33 @@ impl Default for SyntaxParent {
}

impl SyntaxParent {
pub fn load_syntax(&self, extension: &str) -> Option<Syntax> {
pub fn load_syntax(&self, extension: &str, theme: Option<&str>) -> Option<Syntax> {
let syntax = self.syntax_set.find_syntax_by_extension(extension)?;

let theme = theme
.and_then(|s| {
self.theme_set.themes.get(s).cloned().or_else(|| {
let file = std::fs::File::open(s).ok()?;
ThemeSet::load_from_reader(&mut BufReader::new(file)).ok()
})
})
.unwrap_or_else(|| self.theme_set.themes["Solarized (dark)"].clone());
// let theme = ThemeSet::load_from_reader(&mut Cursor::new(theme::ONE_DARK.as_bytes())).unwrap();
Some(Syntax {
syntax_set: &self.syntax_set,
syntax,
theme: &self.theme_set.themes["Solarized (dark)"],
theme,
})
}

pub fn load_syntax_or_txt(&self, extension: &str) -> Syntax {
self.load_syntax(extension)
.unwrap_or_else(|| self.load_syntax("txt").unwrap())
pub fn load_syntax_or_txt(&self, extension: &str, theme: Option<&str>) -> Syntax {
self.load_syntax(extension, theme)
.unwrap_or_else(|| self.load_syntax("txt", theme).unwrap())
}
}

impl<'a> Syntax<'a> {
pub fn highlight_lines(&self) -> HighlightLines<'a> {
HighlightLines::new(self.syntax, self.theme)
pub fn highlight_lines(&'a self) -> HighlightLines<'a> {
HighlightLines::new(self.syntax, &self.theme)
}
}