Skip to main content

CodeView

Struct CodeView 

Source
pub struct CodeView<Msg> { /* private fields */ }
Expand description

Code with syntax colours, line numbers and wrapping of long lines.

While focused, c copies the code to the clipboard and flashes. The code is a text selection region: a mouse drag selects inside it (turn it off with NodeMut::selectable), and clean copies leave out the line numbers and the signs of marked lines.

For reviews, line_marks shows a diff and highlight_lines tints lines to look at; either adds a sign column at the left edge. reveal scrolls the enclosing ScrollView to a line.

Style keys: code (bg, padding) with focus and pressed; code-line-number; code-token.<kind> where kind is keyword, type, function, macro, string, number, comment, attribute, lifetime, punctuation, table, key, variable or plain; code-line.<look> (bg for the line, fg for its sign) where look is added, removed, accent or warning. Icons: line-added, line-removed, warning and the pillar.

Implementations§

Source§

impl<Msg: 'static> CodeView<Msg>

Source

pub fn new(code: impl Into<String>, language: Language) -> Self

Shows code in language.

Source

pub fn line_marks(self, marks: impl IntoIterator<Item = LineMark>) -> Self

Marks lines as a diff: the first mark belongs to line 1, the next to line 2, and lines past the last mark are unchanged. Added lines are tinted with the success colour and signed +, removed ones with the danger colour and , in the sign column at the left edge.

The line numbers then follow the files rather than the text: a diff puts the lines of two versions one after another, so counting from the top would number neither file. A removed line carries the old file’s number, an added line the new file’s, and a line in both carries the new file’s, which is the one a finding such as PKGBUILD:22 means. Give line_numbers_from instead when the diff starts part way into the file, and reach a line by its number with reveal_number.

Source

pub fn highlight_lines( self, lines: impl RangeBounds<usize>, tone: LineTone, ) -> Self

Tints lines (counted from 1, like the line numbers) in tone, over any diff mark, and puts the tone’s sign in the sign column. Call it again for more lines; where ranges meet, the later call wins. The tint is separate from a text selection, which draws over it.

Source

pub fn reveal(self, line: usize) -> Self

Scrolls the enclosing ScrollView just enough to show line (counted from 1; past the end, the last line) with two rows of context, gliding there unless motion is reduced. It happens when the revealed line changes, so the user can scroll away afterwards; outside a scroll view it does nothing.

Source

pub fn line_numbers(self, show: bool) -> Self

Shows or hides line numbers; shown by default.

Source

pub fn line_numbers_from( self, numbers: impl IntoIterator<Item = Option<usize>>, ) -> Self

Gives each line its own number outright: the first number belongs to the first line of the code, and None leaves that line’s column blank, as a hunk header has no number of its own. Lines past the last number are blank too.

This is for a diff that starts part way into a file, where nothing in the text says the hunk began at line 120. A whole-file diff needs only line_marks, which numbers the lines from the marks. Numbers given here win over that.

Source

pub fn reveal_number(self, number: usize) -> Self

Scrolls to the line whose number is number, the way reveal scrolls to a line of the text. In a diff the two are not the same line, so this is what a finding that names a file and a line asks for.

Two lines can carry one number — the line a version lost and the line that took its place. The line the new file numbers that way is the one reached, because that is the file a finding is about; a number only a removed line carries reaches that line. A number no line carries scrolls nowhere. Given as well as reveal, this wins.

Source

pub fn on_copy(self, message: Msg) -> Self

Message sent after the code was copied with c.

Trait Implementations§

Source§

impl<Msg: Clone + 'static> Widget<Msg> for CodeView<Msg>

Source§

fn measure(&self, cx: &mut MeasureCx<'_>, available: Size) -> Size

The size the widget wants when it may use up to available.
Source§

fn paint(&self, cx: &mut PaintCx<'_>, area: Rect)

Draws the widget into area.
Source§

fn event(&self, cx: &mut EventCx<'_, Msg>, event: &Event) -> bool

Handles input. Returns true when the event was used; unused key and scroll events bubble to the parent widget.
Source§

fn focusable(&self) -> bool

Whether the widget can take keyboard focus.
Source§

fn paint_overlay(&self, _cx: &mut PaintCx<'_>, _anchor: Rect)

Draws the widget’s overlay after the whole view was painted, when it asked for one with PaintCx::request_overlay. anchor is the area the widget was painted in.
Source§

fn children(&self) -> &[Node<Msg>]

Child nodes, for widgets that contain other widgets.
Source§

fn children_mut(&mut self) -> &mut [Node<Msg>]

Mutable child nodes, used to assign ids.

Auto Trait Implementations§

§

impl<Msg> Freeze for CodeView<Msg>
where Option<Msg>: Freeze,

§

impl<Msg> RefUnwindSafe for CodeView<Msg>
where Option<Msg>: RefUnwindSafe,

§

impl<Msg> Send for CodeView<Msg>
where Option<Msg>: Send,

§

impl<Msg> Sync for CodeView<Msg>
where Option<Msg>: Sync,

§

impl<Msg> Unpin for CodeView<Msg>
where Option<Msg>: Unpin,

§

impl<Msg> UnsafeUnpin for CodeView<Msg>
where Option<Msg>: UnsafeUnpin,

§

impl<Msg> UnwindSafe for CodeView<Msg>
where Option<Msg>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.