pub struct CodeEditor { /* private fields */ }Expand description
A code editor. Create it once and keep the entity in your view:
let editor = cx.new(|cx| CodeEditor::new(cx).path("src/main.rs").with_text(&source));
cx.subscribe(&editor, |this, editor, event, cx| {
if *event == CodeEditorEvent::Save {
std::fs::write(path, editor.read(cx).text()).ok();
editor.update(cx, |e, cx| e.mark_saved(cx));
}
});It fills its parent (give it a height) and draws no background, like super::TextArea.
Not included yet: code folding, multiple cursors, soft wrap, find and replace, and tab stops
for hard tabs (a '\t' is drawn one column wide).
Implementations§
Source§impl CodeEditor
impl CodeEditor
pub fn new(cx: &mut Context<'_, Self>) -> Self
Sourcepub fn path(self, path: impl Into<SharedString>) -> Self
pub fn path(self, path: impl Into<SharedString>) -> Self
File path, reported by CodeEditor::selection_context.
Sourcepub fn font_size(self, size: Pixels) -> Self
pub fn font_size(self, size: Pixels) -> Self
Text size (13 px by default); lines are 1.5 times as tall.
Sourcepub fn line_numbers(self, show: bool) -> Self
pub fn line_numbers(self, show: bool) -> Self
Shows the line number gutter (on by default).
Sourcepub fn auto_pair(self, on: bool) -> Self
pub fn auto_pair(self, on: bool) -> Self
Inserts and skips over closing brackets and quotes (on by default).
pub fn highlighter(self, highlighter: impl LineHighlighter) -> Self
pub fn buffer(&self) -> &CodeBuffer
Sourcepub fn set_text(&mut self, text: &str, cx: &mut Context<'_, Self>)
pub fn set_text(&mut self, text: &str, cx: &mut Context<'_, Self>)
Replaces the whole text (loading a file): clears the history and marks it saved.
pub fn set_path( &mut self, path: Option<SharedString>, cx: &mut Context<'_, Self>, )
pub fn file_path(&self) -> Option<&SharedString>
Sourcepub fn set_highlighter(
&mut self,
highlighter: Option<Box<dyn LineHighlighter>>,
cx: &mut Context<'_, Self>,
)
pub fn set_highlighter( &mut self, highlighter: Option<Box<dyn LineHighlighter>>, cx: &mut Context<'_, Self>, )
Swaps the highlighter (or removes it with None).
Sourcepub fn refresh_highlights(&mut self, cx: &mut Context<'_, Self>)
pub fn refresh_highlights(&mut self, cx: &mut Context<'_, Self>)
Forgets the cached colors, e.g. after the theme changed (the highlighter is asked again).
pub fn set_read_only(&mut self, read_only: bool, cx: &mut Context<'_, Self>)
pub fn mark_saved(&mut self, cx: &mut Context<'_, Self>)
Sourcepub fn selection_context(&self) -> CodeSelection
pub fn selection_context(&self) -> CodeSelection
The selection (or the caret’s line) with the file path, to send as context.
pub fn selection(&self) -> BufferSelection
Sourcepub fn go_to(&mut self, line: usize, col: usize, cx: &mut Context<'_, Self>)
pub fn go_to(&mut self, line: usize, col: usize, cx: &mut Context<'_, Self>)
Moves the caret to a zero-based line and byte column and scrolls to it.
Sourcepub fn select(&mut self, selection: BufferSelection, cx: &mut Context<'_, Self>)
pub fn select(&mut self, selection: BufferSelection, cx: &mut Context<'_, Self>)
Selects a range and scrolls to it.
pub fn focus(&self, window: &mut Window)
pub fn is_focused(&self, window: &Window) -> bool
pub fn undo(&mut self, cx: &mut Context<'_, Self>)
pub fn redo(&mut self, cx: &mut Context<'_, Self>)
Trait Implementations§
Source§impl EntityInputHandler for CodeEditor
impl EntityInputHandler for CodeEditor
Source§fn text_for_range(
&mut self,
range_utf16: Range<usize>,
actual_range: &mut Option<Range<usize>>,
_: &mut Window,
_: &mut Context<'_, Self>,
) -> Option<String>
fn text_for_range( &mut self, range_utf16: Range<usize>, actual_range: &mut Option<Range<usize>>, _: &mut Window, _: &mut Context<'_, Self>, ) -> Option<String>
InputHandler::text_for_range for detailsSource§fn selected_text_range(
&mut self,
_: bool,
_: &mut Window,
_: &mut Context<'_, Self>,
) -> Option<UTF16Selection>
fn selected_text_range( &mut self, _: bool, _: &mut Window, _: &mut Context<'_, Self>, ) -> Option<UTF16Selection>
InputHandler::selected_text_range for detailsSource§fn marked_text_range(
&self,
_: &mut Window,
_: &mut Context<'_, Self>,
) -> Option<Range<usize>>
fn marked_text_range( &self, _: &mut Window, _: &mut Context<'_, Self>, ) -> Option<Range<usize>>
InputHandler::marked_text_range for detailsSource§fn unmark_text(&mut self, _: &mut Window, _: &mut Context<'_, Self>)
fn unmark_text(&mut self, _: &mut Window, _: &mut Context<'_, Self>)
InputHandler::unmark_text for detailsSource§fn replace_text_in_range(
&mut self,
range_utf16: Option<Range<usize>>,
text: &str,
_: &mut Window,
cx: &mut Context<'_, Self>,
)
fn replace_text_in_range( &mut self, range_utf16: Option<Range<usize>>, text: &str, _: &mut Window, cx: &mut Context<'_, Self>, )
InputHandler::replace_text_in_range for detailsSource§fn replace_and_mark_text_in_range(
&mut self,
range_utf16: Option<Range<usize>>,
text: &str,
new_selected_range_utf16: Option<Range<usize>>,
_: &mut Window,
cx: &mut Context<'_, Self>,
)
fn replace_and_mark_text_in_range( &mut self, range_utf16: Option<Range<usize>>, text: &str, new_selected_range_utf16: Option<Range<usize>>, _: &mut Window, cx: &mut Context<'_, Self>, )
InputHandler::replace_and_mark_text_in_range for detailsSource§fn bounds_for_range(
&mut self,
range_utf16: Range<usize>,
bounds: Bounds<Pixels>,
_: &mut Window,
_: &mut Context<'_, Self>,
) -> Option<Bounds<Pixels>>
fn bounds_for_range( &mut self, range_utf16: Range<usize>, bounds: Bounds<Pixels>, _: &mut Window, _: &mut Context<'_, Self>, ) -> Option<Bounds<Pixels>>
InputHandler::bounds_for_range for detailsSource§fn character_index_for_point(
&mut self,
point: Point<Pixels>,
_: &mut Window,
_: &mut Context<'_, Self>,
) -> Option<usize>
fn character_index_for_point( &mut self, point: Point<Pixels>, _: &mut Window, _: &mut Context<'_, Self>, ) -> Option<usize>
InputHandler::character_index_for_point for detailsimpl EventEmitter<CodeEditorEvent> for CodeEditor
Source§impl Focusable for CodeEditor
impl Focusable for CodeEditor
Source§fn focus_handle(&self, _: &App) -> FocusHandle
fn focus_handle(&self, _: &App) -> FocusHandle
Source§impl Render for CodeEditor
impl Render for CodeEditor
Auto Trait Implementations§
impl !RefUnwindSafe for CodeEditor
impl !Send for CodeEditor
impl !Sync for CodeEditor
impl !UnwindSafe for CodeEditor
impl Forget for CodeEditor
impl Freeze for CodeEditor
impl Move for CodeEditor
impl Unpin for CodeEditor
impl UnsafeUnpin for CodeEditor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more