summaryrefslogtreecommitdiff
path: root/src/parse/lines.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-06-09 00:37:13 +0200
committerLaurenz <laurmaedje@gmail.com>2021-06-09 00:37:13 +0200
commit5afb42ad89abb518a01a09051f0f9b6f75bd383e (patch)
treeb12368a287f22de711df8d759c20ee742ed5b4c2 /src/parse/lines.rs
parentd69dfa84ec957ac4037f60a3335416a9f73b97c8 (diff)
Lists with indent-based parsing
- Unordered lists with indent-based parsing and basic layout using stacks - Headings are now also indent based - Removes syntax functions since they will be superseded by select & transform
Diffstat (limited to 'src/parse/lines.rs')
-rw-r--r--src/parse/lines.rs28
1 files changed, 24 insertions, 4 deletions
diff --git a/src/parse/lines.rs b/src/parse/lines.rs
index 8693af44..bbdedaa5 100644
--- a/src/parse/lines.rs
+++ b/src/parse/lines.rs
@@ -32,6 +32,8 @@ impl<'s> LineMap<'s> {
let start = self.line_starts.get(line_index)?;
let head = self.src.get(start.to_usize() .. pos.to_usize())?;
+
+ // TODO: What about tabs?
let column_index = head.chars().count();
Some(Location {
@@ -52,12 +54,14 @@ impl<'s> LineMap<'s> {
let line = self.src.get(line_start.to_usize() .. line_end)?;
- // Find the index in the line. For the first column, the index is always zero. For
- // other columns, we have to look at which byte the char directly before the
- // column in question ends. We can't do `nth(column_idx)` directly since the
- // column may be behind the last char.
+ // Find the index in the line. For the first column, the index is always
+ // zero. For other columns, we have to look at which byte the char
+ // directly before the column in question ends. We can't do
+ // `nth(column_idx)` directly since the column may be behind the last
+ // char.
let column_idx = location.column.checked_sub(1)? as usize;
let line_offset = if let Some(prev_idx) = column_idx.checked_sub(1) {
+ // TODO: What about tabs?
let (idx, prev) = line.char_indices().nth(prev_idx)?;
idx + prev.len_utf8()
} else {
@@ -68,6 +72,22 @@ impl<'s> LineMap<'s> {
}
}
+/// Determine the column at the end of the string.
+pub fn search_column(src: &str) -> usize {
+ let mut column = 0;
+ for c in src.chars().rev() {
+ if is_newline(c) {
+ break;
+ } else if c == '\t' {
+ // TODO: How many columns per tab?
+ column += 2;
+ } else {
+ column += 1;
+ }
+ }
+ column
+}
+
/// Whether this character denotes a newline.
pub fn is_newline(character: char) -> bool {
matches!(