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
|
import { Range } from 'vscode-languageserver'
import lineColumn from 'line-column'
import { ensureArray } from './array'
export function removeRangesFromString(
str: string,
rangeOrRanges: Range | Range[]
): string {
let ranges = ensureArray(rangeOrRanges)
let finder = lineColumn(str + '\n', { origin: 0 })
let indexRanges: { start: number; end: number }[] = []
ranges.forEach((range) => {
let start = finder.toIndex(range.start.line, range.start.character)
let end = finder.toIndex(range.end.line, range.end.character)
for (let i = start - 1; i >= 0; i--) {
if (/\s/.test(str.charAt(i))) {
start = i
} else {
break
}
}
indexRanges.push({ start, end })
})
indexRanges.sort((a, b) => a.start - b.start)
let result = ''
let i = 0
indexRanges.forEach((indexRange) => {
result += str.substring(i, indexRange.start)
i = indexRange.end
})
result += str.substring(i)
return result.trim()
}
|