aboutsummaryrefslogtreecommitdiff
path: root/src/text.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/text.rs')
-rw-r--r--src/text.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/text.rs b/src/text.rs
index ac1a54a75..4084bf44e 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -1,14 +1,17 @@
1use std::fmt; 1use std::fmt;
2use std::ops; 2use std::ops;
3 3
4/// An text position in a source file
4#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 5#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub struct TextUnit(u32); 6pub struct TextUnit(u32);
6 7
7impl TextUnit { 8impl TextUnit {
9 /// The positional offset required for one character
8 pub fn len_of_char(c: char) -> TextUnit { 10 pub fn len_of_char(c: char) -> TextUnit {
9 TextUnit(c.len_utf8() as u32) 11 TextUnit(c.len_utf8() as u32)
10 } 12 }
11 13
14 #[allow(missing_docs)]
12 pub fn new(val: u32) -> TextUnit { 15 pub fn new(val: u32) -> TextUnit {
13 TextUnit(val) 16 TextUnit(val)
14 } 17 }
@@ -64,6 +67,7 @@ impl ops::SubAssign<TextUnit> for TextUnit {
64 } 67 }
65} 68}
66 69
70/// A range of text in a source file
67#[derive(Clone, Copy, PartialEq, Eq)] 71#[derive(Clone, Copy, PartialEq, Eq)]
68pub struct TextRange { 72pub struct TextRange {
69 start: TextUnit, 73 start: TextUnit,
@@ -83,10 +87,12 @@ impl fmt::Display for TextRange {
83} 87}
84 88
85impl TextRange { 89impl TextRange {
90 /// An length-0 range of text
86 pub fn empty() -> TextRange { 91 pub fn empty() -> TextRange {
87 TextRange::from_to(TextUnit::new(0), TextUnit::new(0)) 92 TextRange::from_to(TextUnit::new(0), TextUnit::new(0))
88 } 93 }
89 94
95 /// The left-inclusive range (`[from..to)`) between to points in the text
90 pub fn from_to(from: TextUnit, to: TextUnit) -> TextRange { 96 pub fn from_to(from: TextUnit, to: TextUnit) -> TextRange {
91 assert!(from <= to, "Invalid text range [{}; {})", from, to); 97 assert!(from <= to, "Invalid text range [{}; {})", from, to);
92 TextRange { 98 TextRange {
@@ -95,22 +101,27 @@ impl TextRange {
95 } 101 }
96 } 102 }
97 103
104 /// The range from some point over some length
98 pub fn from_len(from: TextUnit, len: TextUnit) -> TextRange { 105 pub fn from_len(from: TextUnit, len: TextUnit) -> TextRange {
99 TextRange::from_to(from, from + len) 106 TextRange::from_to(from, from + len)
100 } 107 }
101 108
109 /// The starting position of this range
102 pub fn start(&self) -> TextUnit { 110 pub fn start(&self) -> TextUnit {
103 self.start 111 self.start
104 } 112 }
105 113
114 /// The end position of this range
106 pub fn end(&self) -> TextUnit { 115 pub fn end(&self) -> TextUnit {
107 self.end 116 self.end
108 } 117 }
109 118
119 /// The length of this range
110 pub fn len(&self) -> TextUnit { 120 pub fn len(&self) -> TextUnit {
111 self.end - self.start 121 self.end - self.start
112 } 122 }
113 123
124 /// Is this range empty of any content?
114 pub fn is_empty(&self) -> bool { 125 pub fn is_empty(&self) -> bool {
115 self.start() == self.end() 126 self.start() == self.end()
116 } 127 }