Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
vhakulinen committed Feb 2, 2019
1 parent b4732fe commit 430c8f6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 41 deletions.
6 changes: 4 additions & 2 deletions examples/build-syntect-pack.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
extern crate syntect;

use syntect::parsing::SyntaxSetBuilder;
use syntect::dumps::*;
use syntect::parsing::SyntaxSetBuilder;

fn main() {
let mut builder = SyntaxSetBuilder::new();
builder.add_plain_text_syntax();
builder.add_from_folder("./sublime-syntaxes/syntaxes/", true).unwrap();
builder
.add_from_folder("./sublime-syntaxes/syntaxes/", true)
.unwrap();
let ss = builder.build();
dump_to_file(&ss, "./sublime-syntaxes/all.pack").unwrap();
}
6 changes: 1 addition & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ fn build(app: &gtk::Application, opts: &Options) {
nvim.command(format!("tabe {}", file).as_str()).unwrap();
}

let ui = ui::UI::init(
app,
rx,
Arc::new(Mutex::new(nvim)),
);
let ui = ui::UI::init(app, rx, Arc::new(Mutex::new(nvim)));
ui.start();
}

Expand Down
58 changes: 35 additions & 23 deletions src/ui/cursor_tooltip.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::borrow::Cow;
use std::cell::RefCell;
use std::fs;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs;
use std::rc::Rc;

use gtk;
Expand All @@ -14,11 +14,11 @@ use webkit2gtk::{SettingsExt, UserContentManagerExt, WebViewExt};
use ammonia;
use pulldown_cmark as md;

use syntect::parsing::SyntaxSet;
use syntect::parsing::Scope;
use syntect::dumps::from_binary;
use syntect::highlighting::{Color as SyntectColor, ThemeSet};
use syntect::html::highlighted_html_for_string;
use syntect::dumps::from_binary;
use syntect::parsing::Scope;
use syntect::parsing::SyntaxSet;

use thread_guard::ThreadGuard;
use ui::color::Color;
Expand Down Expand Up @@ -118,8 +118,8 @@ impl CursorTooltip {
*a = alloc.clone();
});

let syntax_set: SyntaxSet = from_binary(
include_bytes!("../../sublime-syntaxes/all.pack"));
let syntax_set: SyntaxSet =
from_binary(include_bytes!("../../sublime-syntaxes/all.pack"));
let theme_set = ThemeSet::load_defaults();

CursorTooltip {
Expand Down Expand Up @@ -161,7 +161,6 @@ impl CursorTooltip {
}

fn parse_events<'a>(&self, parser: md::Parser<'a>) -> Vec<md::Event<'a>> {

let theme = &self.theme_set.themes["base16-ocean.dark"];
let mut syntax = self.syntax_set.find_syntax_plain_text();

Expand All @@ -172,7 +171,8 @@ impl CursorTooltip {
for event in parser {
match event {
md::Event::Start(md::Tag::CodeBlock(lang)) => {
syntax = self.syntax_set
syntax = self
.syntax_set
// Try to find the syntax by token.
.find_syntax_by_token(&lang)
.unwrap_or({
Expand All @@ -182,18 +182,27 @@ impl CursorTooltip {
.iter()
.rev()
.find(|&syntax| {
syntax.name.to_lowercase().contains(&lang.to_string())
syntax
.name
.to_lowercase()
.contains(&lang.to_string())
})
// And if not still found, use the plain text one.
.unwrap_or(self.syntax_set.find_syntax_plain_text())
// And if not still found, use the plain text one.
.unwrap_or(
self.syntax_set.find_syntax_plain_text(),
)
});

in_code_block = true;
}
md::Event::End(md::Tag::CodeBlock(_)) => {
if in_code_block {
let html = syntect::html::highlighted_html_for_string(
&to_highlight, &self.syntax_set, &syntax, &theme);
&to_highlight,
&self.syntax_set,
&syntax,
&theme,
);
events.push(md::Event::Html(Cow::Owned(html)));
}
in_code_block = false;
Expand Down Expand Up @@ -394,22 +403,24 @@ fn get_preferred_vertical_position(
return (y, height);
}

fn attribute_filter<'u>(element: &str, attribute: &str, value: &'u str) -> Option<Cow<'u, str>> {
fn attribute_filter<'u>(
element: &str,
attribute: &str,
value: &'u str,
) -> Option<Cow<'u, str>> {
match (element, attribute) {
("span", "style") => {
let mut allowed_fixed = HashMap::new();
allowed_fixed.insert("text-decorator", [ "underline" ]);
allowed_fixed.insert("font-weight", [ "bold" ]);
allowed_fixed.insert("font-style", [ "italic" ]);
let allowed_color = [
"color",
"background-color",
];
allowed_fixed.insert("text-decorator", ["underline"]);
allowed_fixed.insert("font-weight", ["bold"]);
allowed_fixed.insert("font-style", ["italic"]);
let allowed_color = ["color", "background-color"];

let mut new = String::new();

for attrs in value.split(";") {
if let [prop, val] = attrs.split(":").collect::<Vec<&str>>()[..] {
if let [prop, val] = attrs.split(":").collect::<Vec<&str>>()[..]
{
if let Some(vals) = allowed_fixed.get(&prop) {
if vals.contains(&val) {
new.push_str(&prop);
Expand All @@ -418,7 +429,9 @@ fn attribute_filter<'u>(element: &str, attribute: &str, value: &'u str) -> Optio
new.push_str(";");
}
} else if allowed_color.contains(&prop) {
if let Ok(color) = Color::from_hex_string(val.to_string()) {
if let Ok(color) =
Color::from_hex_string(val.to_string())
{
new.push_str(&prop);
new.push_str(":#");
new.push_str(&color.to_hex());
Expand All @@ -428,7 +441,6 @@ fn attribute_filter<'u>(element: &str, attribute: &str, value: &'u str) -> Optio
}
}


Some(new.into())
}
_ => None,
Expand Down
22 changes: 11 additions & 11 deletions src/ui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,17 +392,17 @@ fn handle_gnvim_event(
GnvimEvent::HideHover => state.cursor_tooltip.hide(),
GnvimEvent::SetCursorTooltipStyle(style) => {
//if let Err(reason) = state.cursor_tooltip.set_style(style.clone()) {
//let mut nvim = nvim.lock().unwrap();
//nvim.command("echohl ErrorMsg").unwrap();
//nvim.command(
//format!(
//"echo \"Failed to set cursor tooltip style: {}\"",
//reason
//)
//.as_str(),
//)
//.unwrap();
//nvim.command("echohl None").unwrap();
//let mut nvim = nvim.lock().unwrap();
//nvim.command("echohl ErrorMsg").unwrap();
//nvim.command(
//format!(
//"echo \"Failed to set cursor tooltip style: {}\"",
//reason
//)
//.as_str(),
//)
//.unwrap();
//nvim.command("echohl None").unwrap();
//}
}
GnvimEvent::Unknown(msg) => {
Expand Down

0 comments on commit 430c8f6

Please sign in to comment.