Skip to content

Commit 7430701

Browse files
committed
feat: Use ThemeKey everywhere when getting Styles
This also means we can avoid allocating `String`s in a lot of cases but means we allocate a `Vec` more often. Comparing the perf difference is hard but I have not seen Helix slow down because of this.
1 parent abe5eef commit 7430701

File tree

18 files changed

+243
-207
lines changed

18 files changed

+243
-207
lines changed

helix-core/src/syntax.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,10 @@ pub fn read_query(language: &str, filename: &str) -> String {
459459
}
460460

461461
impl LanguageConfiguration {
462-
fn initialize_highlight(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> {
462+
fn initialize_highlight<T: AsRef<str>>(
463+
&self,
464+
scopes: &[T],
465+
) -> Option<Arc<HighlightConfiguration>> {
463466
let highlights_query = read_query(&self.language_id, "highlights.scm");
464467
// always highlight syntax errors
465468
// highlights_query += "\n(ERROR) @error";
@@ -493,13 +496,16 @@ impl LanguageConfiguration {
493496
}
494497
}
495498

496-
pub fn reconfigure(&self, scopes: &[String]) {
499+
pub fn reconfigure(&self, scopes: &[&str]) {
497500
if let Some(Some(config)) = self.highlight_config.get() {
498501
config.configure(scopes);
499502
}
500503
}
501504

502-
pub fn highlight_config(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> {
505+
pub fn highlight_config<T: AsRef<str>>(
506+
&self,
507+
scopes: &[T],
508+
) -> Option<Arc<HighlightConfiguration>> {
503509
self.highlight_config
504510
.get_or_init(|| self.initialize_highlight(scopes))
505511
.clone()
@@ -681,16 +687,17 @@ impl Loader {
681687
self.language_configs.iter()
682688
}
683689

684-
pub fn set_scopes(&self, scopes: Vec<String>) {
685-
self.scopes.store(Arc::new(scopes));
690+
pub fn set_scopes(&self, scopes: &[&str]) {
691+
self.scopes
692+
.store(Arc::new(scopes.iter().map(ToString::to_string).collect()));
686693

687694
// Reconfigure existing grammars
688695
for config in self
689696
.language_configs
690697
.iter()
691698
.filter(|cfg| cfg.is_highlight_initialized())
692699
{
693-
config.reconfigure(&self.scopes());
700+
config.reconfigure(scopes);
694701
}
695702
}
696703

@@ -1568,7 +1575,7 @@ impl HighlightConfiguration {
15681575
///
15691576
/// When highlighting, results are returned as `Highlight` values, which contain the index
15701577
/// of the matched highlight this list of highlight names.
1571-
pub fn configure(&self, recognized_names: &[String]) {
1578+
pub fn configure<T: AsRef<str>>(&self, recognized_names: &[T]) {
15721579
let mut capture_parts = Vec::new();
15731580
let indices: Vec<_> = self
15741581
.query
@@ -1584,7 +1591,7 @@ impl HighlightConfiguration {
15841591
let recognized_name = recognized_name;
15851592
let mut len = 0;
15861593
let mut matches = true;
1587-
for (i, part) in recognized_name.split('.').enumerate() {
1594+
for (i, part) in recognized_name.as_ref().split('.').enumerate() {
15881595
match capture_parts.get(i) {
15891596
Some(capture_part) if *capture_part == part => len += 1,
15901597
_ => {
@@ -2312,7 +2319,7 @@ mod test {
23122319

23132320
#[test]
23142321
fn test_parser() {
2315-
let highlight_names: Vec<String> = [
2322+
let highlight_names: &[&str] = &[
23162323
"attribute",
23172324
"constant",
23182325
"function.builtin",
@@ -2331,11 +2338,7 @@ mod test {
23312338
"variable",
23322339
"variable.builtin",
23332340
"variable.parameter",
2334-
]
2335-
.iter()
2336-
.cloned()
2337-
.map(String::from)
2338-
.collect();
2341+
];
23392342

23402343
let loader = Loader::new(Configuration { language: vec![] });
23412344

@@ -2349,7 +2352,7 @@ mod test {
23492352
"", // locals.scm
23502353
)
23512354
.unwrap();
2352-
config.configure(&highlight_names);
2355+
config.configure(highlight_names);
23532356

23542357
let source = Rope::from_str(
23552358
"

helix-core/tests/indent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn test_treesitter_indent(file_name: &str, lang_scope: &str) {
3838
std::env::set_var("HELIX_RUNTIME", runtime.to_str().unwrap());
3939

4040
let language_config = loader.language_config_for_scope(lang_scope).unwrap();
41-
let highlight_config = language_config.highlight_config(&[]).unwrap();
41+
let highlight_config = language_config.highlight_config::<String>(&[]).unwrap();
4242
let syntax = Syntax::new(&doc, highlight_config, std::sync::Arc::new(loader));
4343
let indent_query = language_config.indent_query().unwrap();
4444
let text = doc.slice(..);

helix-term/src/commands/dap.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use dap::{StackFrame, Thread, ThreadStates};
88
use helix_core::syntax::{DebugArgumentValue, DebugConfigCompletion, DebugTemplate};
99
use helix_dap::{self as dap, Client};
1010
use helix_lsp::block_on;
11-
use helix_view::editor::Breakpoint;
11+
use helix_view::{editor::Breakpoint, theme::ThemeKey};
1212

1313
use serde_json::{to_value, Value};
1414
use tokio_stream::wrappers::UnboundedReceiverStream;
@@ -500,9 +500,9 @@ pub fn dap_variables(cx: &mut Context) {
500500
let mut variables = Vec::new();
501501

502502
let theme = &cx.editor.theme;
503-
let scope_style = theme.get("ui.linenr.selected");
504-
let type_style = theme.get("ui.text");
505-
let text_style = theme.get("ui.text.focus");
503+
let scope_style = theme.get(ThemeKey::Ui_Linenr_Selected);
504+
let type_style = theme.get(ThemeKey::Ui_Text);
505+
let text_style = theme.get(ThemeKey::Ui_Text_Focus);
506506

507507
for scope in scopes.iter() {
508508
// use helix_view::graphics::Style;

helix-term/src/commands/lsp.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ use tui::text::{Span, Spans};
1010
use super::{align_view, push_jump, Align, Context, Editor, Open};
1111

1212
use helix_core::{path, Selection};
13-
use helix_view::{apply_transaction, document::Mode, editor::Action, theme::Style};
13+
use helix_view::{
14+
apply_transaction,
15+
document::Mode,
16+
editor::Action,
17+
theme::{Style, ThemeKey},
18+
};
1419

1520
use crate::{
1621
compositor::{self, Compositor},
@@ -275,10 +280,10 @@ fn diag_picker(
275280
}
276281

277282
let styles = DiagnosticStyles {
278-
hint: cx.editor.theme.get("hint"),
279-
info: cx.editor.theme.get("info"),
280-
warning: cx.editor.theme.get("warning"),
281-
error: cx.editor.theme.get("error"),
283+
hint: cx.editor.theme.get(ThemeKey::Hint),
284+
info: cx.editor.theme.get(ThemeKey::Info),
285+
warning: cx.editor.theme.get(ThemeKey::Warning),
286+
error: cx.editor.theme.get(ThemeKey::Error),
282287
};
283288

284289
FilePicker::new(

helix-term/src/ui/completion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::compositor::{Component, Context, Event, EventResult};
2-
use helix_view::{apply_transaction, editor::CompleteAction, ViewId};
2+
use helix_view::{apply_transaction, editor::CompleteAction, theme::ThemeKey, ViewId};
33
use tui::buffer::Buffer as Surface;
44
use tui::text::Spans;
55

@@ -479,7 +479,7 @@ impl Component for Completion {
479479
};
480480

481481
// clear area
482-
let background = cx.editor.theme.get("ui.popup");
482+
let background = cx.editor.theme.get(ThemeKey::Ui_Popup);
483483
surface.clear_with(area, background);
484484
markdown_doc.render(area, surface, cx);
485485
}

0 commit comments

Comments
 (0)