Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3cd702c

Browse files
committed
feat(highligher): add method addTexts
1 parent 60ddea0 commit 3cd702c

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

src/model/highligher.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ export interface HighlightItem {
1111
hlGroup: string
1212
}
1313

14+
export interface TextItem {
15+
text: string
16+
hlGroup?: string
17+
}
18+
19+
const srcId = -1
1420
/**
1521
* Build highlights, with lines and highlights
1622
*/
1723
export default class Highlighter {
1824
private lines: string[] = []
1925
private highlights: HighlightItem[] = []
2026

21-
constructor(private srcId = -1) {
22-
}
23-
2427
public addLine(line: string, hlGroup?: string): void {
2528
if (line.includes('\n')) {
2629
for (let content of line.split(/\r?\n/)) {
@@ -59,6 +62,16 @@ export default class Highlighter {
5962
this.lines.push(...lines)
6063
}
6164

65+
/**
66+
* Add texts to new Lines
67+
*/
68+
public addTexts(items: TextItem[]): void {
69+
this.addLines('')
70+
for (let item of items) {
71+
this.addText(item.text, item.hlGroup)
72+
}
73+
}
74+
6275
public addText(text: string, hlGroup?: string): void {
6376
let { lines } = this
6477
let pre = lines[lines.length - 1] || ''
@@ -91,13 +104,14 @@ export default class Highlighter {
91104
// eslint-disable-next-line @typescript-eslint/no-floating-promises
92105
buffer.setLines(this.lines, { start, end, strictIndexing: false }, true)
93106
for (let item of this.highlights) {
107+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
94108
buffer.addHighlight({
95109
hlGroup: item.hlGroup,
96110
colStart: item.colStart,
97111
colEnd: item.colEnd == null ? -1 : item.colEnd,
98112
line: start + item.line,
99-
srcId: this.srcId
100-
}).logError()
113+
srcId
114+
})
101115
}
102116
}
103117
}

0 commit comments

Comments
 (0)