1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! Code highlighting plugin interface.
//!
//! The editor does not highlight code itself. A highlighter is a plugin:
//! any type implementing [`CodeHighlighter`]. With the `syntect` feature the
//! crate provides a ready-made implementation,
//! [`SyntectHighlighter`](crate::SyntectHighlighter).
//!
//! ```
//! use iced_markdown_editor::{CodeHighlighter, HighlightRequest, HighlightSpan};
//! use iced_markdown_editor::highlight::Color;
//!
//! /// Colours every line starting with `#` as a comment.
//! struct Comments;
//!
//! impl CodeHighlighter for Comments {
//! fn highlight(&self, request: &HighlightRequest<'_>) -> Vec<HighlightSpan> {
//! let mut spans = Vec::new();
//! let mut offset = 0;
//! for line in request.code.split('\n') {
//! let len = line.chars().count();
//! if line.trim_start().starts_with('#') {
//! spans.push(HighlightSpan {
//! range: offset..offset + len,
//! color: Color::from_rgb(0.4, 0.55, 0.4),
//! });
//! }
//! offset += len + 1;
//! }
//! spans
//! }
//! }
//! ```
use Range;
/// Colour type used by highlighters (re-export of `iced::Color`).
pub use Color;
/// One code block to highlight.
/// A coloured range of the code.
/// A code highlighting plugin. Results are cached per code block content,
/// language and background.
/// Language list used when no highlighter is set or it lists none.
pub const COMMON_LANGUAGES: & = &;
/// Relative luminance used to decide whether a background is dark.
pub