on master help sign in
Syntax-highlight regex prompts
0dc3753Michael Davis
helix-term/src/ui/mod.rs+2−1
@@ -151,7 +151,8 @@ pub fn regex_prompt(
151 }
151 }
152 }
152 }
153 },
153 },
154− );
154+ )
155+ .with_language("regex", std::sync::Arc::clone(&cx.editor.syn_loader));
155 // Calculate initial completion
156 // Calculate initial completion
156 prompt.recalculate_completion(cx.editor);
157 prompt.recalculate_completion(cx.editor);
157 // prompt
158 // prompt
helix-term/src/ui/prompt.rs+26−19
@@ -1,7 +1,9 @@
1use crate::compositor::{Component, Compositor, Context, Event, EventResult};
1use crate::compositor::{Component, Compositor, Context, Event, EventResult};
2use crate::{alt, ctrl, key, shift, ui};
2use crate::{alt, ctrl, key, shift, ui};
3+use helix_core::syntax;
3use helix_view::input::KeyEvent;
4use helix_view::input::KeyEvent;
4use helix_view::keyboard::KeyCode;
5use helix_view::keyboard::KeyCode;
6+use std::sync::Arc;
5use std::{borrow::Cow, ops::RangeFrom};
7use std::{borrow::Cow, ops::RangeFrom};
6use tui::buffer::Buffer as Surface;
8use tui::buffer::Buffer as Surface;
7use tui::widgets::{Block, Borders, Widget};
9use tui::widgets::{Block, Borders, Widget};
@@ -32,6 +34,7 @@ pub struct Prompt {
32 callback_fn: CallbackFn,
34 callback_fn: CallbackFn,
33 pub doc_fn: DocFn,
35 pub doc_fn: DocFn,
34 next_char_handler: Option<PromptCharHandler>,
36 next_char_handler: Option<PromptCharHandler>,
37+ language: Option<(&'static str, Arc<syntax::Loader>)>,
35}
38}
36
39
37#[derive(Clone, Copy, PartialEq, Eq)]
40#[derive(Clone, Copy, PartialEq, Eq)]
@@ -83,6 +86,7 @@ impl Prompt {
83 callback_fn: Box::new(callback_fn),
86 callback_fn: Box::new(callback_fn),
84 doc_fn: Box::new(|_| None),
87 doc_fn: Box::new(|_| None),
85 next_char_handler: None,
88 next_char_handler: None,
89+ language: None,
86 }
90 }
87 }
91 }
88
92
@@ -94,6 +98,11 @@ impl Prompt {
94 self
98 self
95 }
99 }
96
100
101+ pub fn with_language(mut self, language: &'static str, loader: Arc<syntax::Loader>) -> Self {
102+ self.language = Some((language, loader));
103+ self
104+ }
105+
97 pub fn line(&self) -> &String {
106 pub fn line(&self) -> &String {
98 &self.line
107 &self.line
99 }
108 }
@@ -456,30 +465,28 @@ impl Prompt {
456 // render buffer text
465 // render buffer text
457 surface.set_string(area.x, area.y + line, &self.prompt, prompt_color);
466 surface.set_string(area.x, area.y + line, &self.prompt, prompt_color);
458
467
468+ let line_area = area.clip_left(self.prompt.len() as u16).clip_top(line);
459− let (input, is_suggestion): (Cow<str>, bool) = if self.line.is_empty() {
469+ if self.line.is_empty() {
460− // latest value in the register list
470+ // Show the most recently entered value as a suggestion.
461− match self
471+ if let Some(suggestion) = self
462 .history_register
472 .history_register
463 .and_then(|reg| cx.editor.registers.last(reg))
473 .and_then(|reg| cx.editor.registers.last(reg))
464− .map(|entry| entry.into())
465 {
474 {
466− Some(value) => (value, true),
475+ surface.set_string(line_area.x, line_area.y, suggestion, suggestion_color);
467− None => (Cow::from(""), false),
468 }
476 }
477+ } else if let Some((language, loader)) = self.language.as_ref() {
478+ let mut text: ui::text::Text = crate::ui::markdown::highlighted_code_block(
479+ self.line.clone(),
480+ language,
481+ Some(&cx.editor.theme),
482+ loader.clone(),
483+ None,
484+ )
485+ .into();
486+ text.render(line_area, surface, cx);
469 } else {
487 } else {
470− (self.line.as_str().into(), false)
471− };
472−
473− surface.set_string(
488+ surface.set_string(line_area.x, line_area.y, self.line.clone(), prompt_color);
474− area.x + self.prompt.len() as u16,
489+ }
475− area.y + line,
476− &input,
477− if is_suggestion {
478− suggestion_color
479− } else {
480− prompt_color
481− },
482− );
483 }
490 }
484}
491}
485
492