pub struct CodeView { /* private fields */ }Expand description
Code in a monospace editor: line numbers in a gutter, syntax highlighting (Rust, Python, JSON),
the current line highlighted, selection and copy (read-only too). Errors (CodeView::errors,
1-based line numbers as compilers report them) get a red mark in the gutter and a red
underline on their line, their message on hover, and are listed under the code: each entry
moves the cursor to its line.
Wraps gpui-kit’s EditorState (its tree-sitter grammars), so the look follows the theme
(theme::apply sets the code colours). An editable view emits CodeChanged.
let code = cx.new(|cx| CodeView::new(CodeLanguage::Rust, source, window, cx).errors(vec![(6, "mismatched types".into())]).editable(true));
cx.subscribe(&code, |this, _, CodeChanged(text), cx| { /* … */ }).detach();Implementations§
Source§impl CodeView
impl CodeView
Sourcepub fn new(
language: CodeLanguage,
text: impl Into<SharedString>,
window: &mut Window,
cx: &mut Context<'_, Self>,
) -> Self
pub fn new( language: CodeLanguage, text: impl Into<SharedString>, window: &mut Window, cx: &mut Context<'_, Self>, ) -> Self
A read-only view of text highlighted as language.
Sourcepub fn errors(self, errors: Vec<(usize, String)>) -> Self
pub fn errors(self, errors: Vec<(usize, String)>) -> Self
Marks these errors: (line, message), lines 1-based.
Sourcepub fn max_lines(self, lines: usize) -> Self
pub fn max_lines(self, lines: usize) -> Self
As tall as its text up to lines lines, then it scrolls (by default it fills the height
its parent gives it).
Sourcepub fn set_errors(
&mut self,
errors: Vec<(usize, String)>,
cx: &mut Context<'_, Self>,
)
pub fn set_errors( &mut self, errors: Vec<(usize, String)>, cx: &mut Context<'_, Self>, )
Marks these errors instead of the ones shown: (line, message), lines 1-based.
Sourcepub fn error_list(&self) -> &[(usize, String)]
pub fn error_list(&self) -> &[(usize, String)]
The errors shown.
Sourcepub fn set_changes(
&mut self,
changes: Vec<ChangeMark>,
cx: &mut Context<'_, Self>,
)
pub fn set_changes( &mut self, changes: Vec<ChangeMark>, cx: &mut Context<'_, Self>, )
Marks these changed lines in the gutter, git style: added green, modified in the accent,
removed as a red notch where the lines were (crate::diff::line_changes computes them).
An empty list clears the marks: the owner turns them off that way.
Sourcepub fn changes(&self) -> &[ChangeMark]
pub fn changes(&self) -> &[ChangeMark]
The changed lines marked.
Sourcepub fn set_editable(&mut self, editable: bool, cx: &mut Context<'_, Self>)
pub fn set_editable(&mut self, editable: bool, cx: &mut Context<'_, Self>)
Makes the view editable or read-only.
Sourcepub fn is_editable(&self) -> bool
pub fn is_editable(&self) -> bool
Whether the user can edit the text.
Sourcepub fn set_text(
&mut self,
text: impl Into<SharedString>,
window: &mut Window,
cx: &mut Context<'_, Self>,
)
pub fn set_text( &mut self, text: impl Into<SharedString>, window: &mut Window, cx: &mut Context<'_, Self>, )
Replaces the text (no CodeChanged); the errors shown are kept on their line numbers.
Sourcepub fn set_language(
&mut self,
language: CodeLanguage,
cx: &mut Context<'_, Self>,
)
pub fn set_language( &mut self, language: CodeLanguage, cx: &mut Context<'_, Self>, )
Highlights the text as language.
Sourcepub fn language(&self) -> CodeLanguage
pub fn language(&self) -> CodeLanguage
The language highlighted.
Sourcepub fn editor(&self) -> &Entity<EditorState>
pub fn editor(&self) -> &Entity<EditorState>
The editor underneath (for its focus handle, cursor, search).
Sourcepub fn go_to_line(
&mut self,
line: usize,
window: &mut Window,
cx: &mut Context<'_, Self>,
)
pub fn go_to_line( &mut self, line: usize, window: &mut Window, cx: &mut Context<'_, Self>, )
Moves the cursor to the start of line (1-based) and focuses the code.
Trait Implementations§
impl EventEmitter<CodeChanged> for CodeView
Auto Trait Implementations§
impl !RefUnwindSafe for CodeView
impl !Send for CodeView
impl !Sync for CodeView
impl !UnwindSafe for CodeView
impl Forget for CodeView
impl Freeze for CodeViewwhere
NonZero<u32>: Freeze,
*const (): Freeze,
*const ArcInner<RwLock<RawRwLock, EntityRefCounts>> is !null: Freeze,
PhantomData<fn(InputBaseState<EditorMode>) -> InputBaseState<EditorMode>>: Freeze,
*const u8 is !null: Freeze,
PhantomData<(usize, String)>: Freeze,
PhantomData<ChangeMark>: Freeze,
PhantomData<Subscription>: Freeze,
impl Move for CodeView
impl Unpin for CodeView
impl UnsafeUnpin for CodeViewwhere
NonZeroU32Inner: UnsafeUnpin,
*const (): UnsafeUnpin,
*const ArcInner<RwLock<RawRwLock, EntityRefCounts>> is !null: UnsafeUnpin,
PhantomData<fn(InputBaseState<EditorMode>) -> InputBaseState<EditorMode>>: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<(usize, String)>: UnsafeUnpin,
PhantomData<ChangeMark>: UnsafeUnpin,
PhantomData<Subscription>: UnsafeUnpin,
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.impl<T> ErasedDestructor for Twhere
T: 'static,
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