From bb121a3e4b97efef1380414f33361404900d6f72 Mon Sep 17 00:00:00 2001 From: Kirawi <67773714+kirawi@users.noreply.github.com> Date: Sat, 10 Jul 2021 21:40:18 -0400 Subject: [PATCH] Injection Query Support (#430) * wip * wip * fixed unsafe * fix clippy * move out reference variable * fmt * remove arc * change safety comment --- helix-term/src/ui/editor.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index ef13004c..9a2fbf57 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -64,6 +64,7 @@ impl EditorView { surface: &mut Surface, theme: &Theme, is_focused: bool, + loader: &syntax::Loader, ) { let area = Rect::new( view.area.x + OFFSET, @@ -72,7 +73,7 @@ impl EditorView { view.area.height.saturating_sub(1), ); // - 1 for statusline - self.render_buffer(doc, view, area, surface, theme, is_focused); + self.render_buffer(doc, view, area, surface, theme, is_focused, loader); // if we're not at the edge of the screen, draw a right border if viewport.right() != view.area.right() { @@ -98,6 +99,7 @@ impl EditorView { self.render_statusline(doc, view, area, surface, theme, is_focused); } + #[allow(clippy::too_many_arguments)] pub fn render_buffer( &self, doc: &Document, @@ -106,6 +108,7 @@ impl EditorView { surface: &mut Surface, theme: &Theme, is_focused: bool, + loader: &syntax::Loader, ) { let text = doc.text().slice(..); @@ -122,8 +125,26 @@ impl EditorView { // TODO: range doesn't actually restrict source, just highlight range let highlights: Vec<_> = match doc.syntax() { Some(syntax) => { + let scopes = theme.scopes(); syntax - .highlight_iter(text.slice(..), Some(range), None, |_| None) + .highlight_iter(text.slice(..), Some(range), None, |language| { + loader + .language_config_for_scope(&format!("source.{}", language)) + .and_then(|language_config| { + let config = language_config.highlight_config(scopes)?; + let config_ref = config.as_ref(); + // SAFETY: the referenced `HighlightConfiguration` behind + // the `Arc` is guaranteed to remain valid throughout the + // duration of the highlight. + let config_ref = unsafe { + std::mem::transmute::< + _, + &'static syntax::HighlightConfiguration, + >(config_ref) + }; + Some(config_ref) + }) + }) .collect() // TODO: we collect here to avoid holding the lock, fix later } None => vec![Ok(HighlightEvent::Source { @@ -719,7 +740,16 @@ impl Component for EditorView { for (view, is_focused) in cx.editor.tree.views() { let doc = cx.editor.document(view.doc).unwrap(); - self.render_view(doc, view, area, surface, &cx.editor.theme, is_focused); + let loader = &cx.editor.syn_loader; + self.render_view( + doc, + view, + area, + surface, + &cx.editor.theme, + is_focused, + loader, + ); } if let Some(info) = std::mem::take(&mut cx.editor.autoinfo) {