@codemirror/text
Advanced tools
Comparing version 0.19.0 to 0.19.1
@@ -0,1 +1,7 @@ | ||
## 0.19.1 (2021-08-23) | ||
### New features | ||
`Text` objects now expose an `iterLines` method to create an iterator over their lines. | ||
## 0.19.0 (2021-08-11) | ||
@@ -2,0 +8,0 @@ |
@@ -121,2 +121,10 @@ /** | ||
/** | ||
Return a cursor that iterates over the given range of lines, | ||
_without_ returning the line breaks between, and yielding empty | ||
strings for empty lines. | ||
When `from` and `to` are given, they should be 1-based line numbers. | ||
*/ | ||
iterLines(from?: number, to?: number): LineCursor; | ||
/** | ||
Convert the document to an array of lines (which can be | ||
@@ -135,2 +143,11 @@ deserialized again via [`Text.of`](https://codemirror.net/6/docs/ref/#text.Text^of)). | ||
} | ||
declare class LineCursor implements TextIterator { | ||
readonly inner: TextIterator; | ||
afterBreak: boolean; | ||
value: string; | ||
done: boolean; | ||
constructor(inner: TextIterator); | ||
next(skip?: number): this; | ||
get lineBreak(): boolean; | ||
} | ||
/** | ||
@@ -137,0 +154,0 @@ This type describes a line in the document. It is created |
@@ -218,2 +218,22 @@ // Compressed representation of the Grapheme_Cluster_Break=Extend | ||
/** | ||
Return a cursor that iterates over the given range of lines, | ||
_without_ returning the line breaks between, and yielding empty | ||
strings for empty lines. | ||
When `from` and `to` are given, they should be 1-based line numbers. | ||
*/ | ||
iterLines(from, to) { | ||
let inner; | ||
if (from == null) { | ||
inner = this.iter(); | ||
} | ||
else { | ||
if (to == null) | ||
to = this.lines + 1; | ||
let start = this.line(from).from; | ||
inner = this.iterRange(start, Math.max(start, to == this.lines + 1 ? this.length : to <= 1 ? 0 : this.line(to - 1).to)); | ||
} | ||
return new LineCursor(inner); | ||
} | ||
/** | ||
@internal | ||
@@ -578,2 +598,32 @@ */ | ||
} | ||
class LineCursor { | ||
constructor(inner) { | ||
this.inner = inner; | ||
this.afterBreak = true; | ||
this.value = ""; | ||
this.done = false; | ||
} | ||
next(skip = 0) { | ||
let { done, lineBreak, value } = this.inner.next(skip); | ||
if (done) { | ||
this.done = true; | ||
this.value = ""; | ||
} | ||
else if (lineBreak) { | ||
if (this.afterBreak) { | ||
this.value = ""; | ||
} | ||
else { | ||
this.afterBreak = true; | ||
this.next(); | ||
} | ||
} | ||
else { | ||
this.value = value; | ||
this.afterBreak = false; | ||
} | ||
return this; | ||
} | ||
get lineBreak() { return false; } | ||
} | ||
/** | ||
@@ -580,0 +630,0 @@ This type describes a line in the document. It is created |
{ | ||
"name": "@codemirror/text", | ||
"version": "0.19.0", | ||
"version": "0.19.1", | ||
"description": "Document data structure for the CodeMirror code editor", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
59516
1514