-
Notifications
You must be signed in to change notification settings - Fork 50
/
webviewcontent.ts
39 lines (30 loc) · 1.15 KB
/
webviewcontent.ts
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
'use strict';
import * as fs from 'fs';
export class WebviewContent {
private content: string[];
constructor(originalFile: string) {
const rawContent = fs.readFileSync(originalFile, 'utf-8');
this.content = rawContent.split(/\r?\n/);
}
public getLines() {
return this.content;
}
public getLine(lineNumber: number) {
return this.content[lineNumber - 1];
}
public insertAfterLine(content: string, lineNumber: number) {
const indentationAmount = this.content[lineNumber - 1].split(/[^ ]/)[0].length;
const indentation = ' '.repeat(indentationAmount);
let splitContent = content.split('\n');
splitContent = splitContent.map((line: string) => indentation + line);
this.content.splice(lineNumber, 0, ...splitContent);
}
public replaceWithinLine(sourceContent: string, destinationContent: string, lineNumber: number) {
let line = this.content[lineNumber - 1];
line = line.replace(sourceContent, destinationContent);
this.content[lineNumber - 1] = line;
}
public getContent() {
return this.content.join('\n');
}
}