Don't allocate while accessing svgs

This commit is contained in:
Dániel Buga 2022-05-12 22:11:54 +02:00
parent 4a7275dc71
commit 00be1af344
1 changed files with 74 additions and 47 deletions

View File

@ -1,4 +1,4 @@
use std::{collections::HashMap, path::Path, str::FromStr, sync::Arc}; use std::{collections::HashMap, ffi::OsStr, path::Path, str::FromStr, sync::Arc};
use druid::{piet::Svg, Color}; use druid::{piet::Svg, Color};
use include_dir::{include_dir, Dir}; use include_dir::{include_dir, Dir};
@ -50,52 +50,67 @@ pub fn get_svg(name: impl AsRef<str>) -> Option<Svg> {
} }
pub fn file_svg_new(path: &Path) -> Svg { pub fn file_svg_new(path: &Path) -> Svg {
let extension = path let file_type = if path.file_name().and_then(OsStr::to_str) == Some("LICENSE") {
.extension() "file_type_license.svg"
.and_then(|s| s.to_str()) } else {
.unwrap_or("") path.extension()
.to_lowercase(); .and_then(OsStr::to_str)
let file_type = match path.file_name().and_then(|f| f.to_str()).unwrap_or("") { .and_then(|extension| {
"LICENSE" => "license", const TYPES: &[(&[&str], &str)] = &[
_ => match extension.as_str() { (&["c"], "file_type_c.svg"),
"rs" => "rust", (&["cxx", "cc", "c++", "cpp"], "file_type_cpp.svg"),
"md" => "markdown", (&["go"], "file_type_go.svg"),
"cxx" | "cc" | "c++" => "cpp", (&["json"], "file_type_json.svg"),
s => s, (&["markdown", "md"], "file_type_markdown.svg"),
}, (&["rs"], "file_type_rust.svg"),
(&["toml"], "file_type_toml.svg"),
(&["yaml"], "file_type_yaml.svg"),
];
for (exts, file_type) in TYPES {
for ext in exts.iter() {
if extension.eq_ignore_ascii_case(ext) {
return Some(*file_type);
}
}
}
None
})
.unwrap_or("default_file.svg")
}; };
get_svg(&format!("file_type_{}.svg", file_type))
.unwrap_or_else(|| get_svg("default_file.svg").unwrap()) get_svg(file_type).unwrap()
} }
pub fn symbol_svg_new(kind: &SymbolKind) -> Option<Svg> { pub fn symbol_svg_new(kind: &SymbolKind) -> Option<Svg> {
let kind_str = match kind { let kind_str = match kind {
SymbolKind::Array => "array", SymbolKind::Array => "symbol-array.svg",
SymbolKind::Boolean => "boolean", SymbolKind::Boolean => "symbol-boolean.svg",
SymbolKind::Class => "class", SymbolKind::Class => "symbol-class.svg",
SymbolKind::Constant => "constant", SymbolKind::Constant => "symbol-constant.svg",
SymbolKind::EnumMember => "enum-member", SymbolKind::EnumMember => "symbol-enum-member.svg",
SymbolKind::Enum => "enum", SymbolKind::Enum => "symbol-enum.svg",
SymbolKind::Event => "event", SymbolKind::Event => "symbol-event.svg",
SymbolKind::Field => "field", SymbolKind::Field => "symbol-field.svg",
SymbolKind::File => "file", SymbolKind::File => "symbol-file.svg",
SymbolKind::Interface => "interface", SymbolKind::Interface => "symbol-interface.svg",
SymbolKind::Key => "key", SymbolKind::Key => "symbol-key.svg",
SymbolKind::Function => "method", SymbolKind::Function => "symbol-method.svg",
SymbolKind::Method => "method", SymbolKind::Method => "symbol-method.svg",
SymbolKind::Object => "namespace", SymbolKind::Object => "symbol-namespace.svg",
SymbolKind::Namespace => "namespace", SymbolKind::Namespace => "symbol-namespace.svg",
SymbolKind::Number => "numeric", SymbolKind::Number => "symbol-numeric.svg",
SymbolKind::Operator => "operator", SymbolKind::Operator => "symbol-operator.svg",
SymbolKind::TypeParameter => "parameter", SymbolKind::TypeParameter => "symbol-parameter.svg",
SymbolKind::Property => "property", SymbolKind::Property => "symbol-property.svg",
SymbolKind::String => "string", SymbolKind::String => "symbol-string.svg",
SymbolKind::Struct => "structure", SymbolKind::Struct => "symbol-structure.svg",
SymbolKind::Variable => "variable", SymbolKind::Variable => "symbol-variable.svg",
_ => return None, _ => return None,
}; };
get_svg(&format!("symbol-{}.svg", kind_str)) get_svg(kind_str)
} }
pub fn completion_svg( pub fn completion_svg(
@ -104,12 +119,29 @@ pub fn completion_svg(
) -> Option<(Svg, Option<Color>)> { ) -> Option<(Svg, Option<Color>)> {
let kind = kind?; let kind = kind?;
let kind_str = match kind { let kind_str = match kind {
CompletionItemKind::Method => "symbol-method.svg",
CompletionItemKind::Function => "symbol-method.svg",
CompletionItemKind::Enum => "symbol-enum.svg",
CompletionItemKind::EnumMember => "symbol-enum-member.svg",
CompletionItemKind::Class => "symbol-class.svg",
CompletionItemKind::Variable => "symbol-variable.svg",
CompletionItemKind::Struct => "symbol-structure.svg",
CompletionItemKind::Keyword => "symbol-keyword.svg",
CompletionItemKind::Constant => "symbol-constant.svg",
CompletionItemKind::Property => "symbol-property.svg",
CompletionItemKind::Field => "symbol-field.svg",
CompletionItemKind::Interface => "symbol-interface.svg",
CompletionItemKind::Snippet => "symbol-snippet.svg",
CompletionItemKind::Module => "symbol-namespace.svg",
_ => "symbol-string.svg",
};
let theme_str = match kind {
CompletionItemKind::Method => "method", CompletionItemKind::Method => "method",
CompletionItemKind::Function => "method", CompletionItemKind::Function => "method",
CompletionItemKind::Enum => "enum", CompletionItemKind::Enum => "enum",
CompletionItemKind::EnumMember => "enum-member", CompletionItemKind::EnumMember => "enum-member",
CompletionItemKind::Class => "class", CompletionItemKind::Class => "class",
CompletionItemKind::Variable => "variable", CompletionItemKind::Variable => "field",
CompletionItemKind::Struct => "structure", CompletionItemKind::Struct => "structure",
CompletionItemKind::Keyword => "keyword", CompletionItemKind::Keyword => "keyword",
CompletionItemKind::Constant => "constant", CompletionItemKind::Constant => "constant",
@ -117,17 +149,12 @@ pub fn completion_svg(
CompletionItemKind::Field => "field", CompletionItemKind::Field => "field",
CompletionItemKind::Interface => "interface", CompletionItemKind::Interface => "interface",
CompletionItemKind::Snippet => "snippet", CompletionItemKind::Snippet => "snippet",
CompletionItemKind::Module => "namespace", CompletionItemKind::Module => "builtinType",
_ => "string", _ => "string",
}; };
let theme_str = match kind_str {
"namespace" => "builtinType",
"variable" => "field",
_ => kind_str,
};
Some(( Some((
get_svg(&format!("symbol-{}.svg", kind_str))?, get_svg(kind_str)?,
config.get_style_color(theme_str).cloned(), config.get_style_color(theme_str).cloned(),
)) ))
} }