bolt/src/text.ts

60 lines
900 B
TypeScript
Raw Normal View History

2020-05-10 15:56:34 +02:00
import * as path from "path"
2020-05-23 22:48:24 +02:00
import * as fs from "fs"
2020-05-10 15:56:34 +02:00
export class TextFile {
2020-05-23 22:48:24 +02:00
private cachedText: string | null = null;
2020-05-10 15:56:34 +02:00
constructor(public origPath: string) {
}
2020-05-23 22:48:24 +02:00
public get fullPath() {
2020-05-10 15:56:34 +02:00
return path.resolve(this.origPath)
}
2020-05-23 22:48:24 +02:00
public getText(): string {
if (this.cachedText !== null) {
return this.cachedText;
}
const text = fs.readFileSync(this.fullPath, 'utf8');
this.cachedText = text;
return text
}
2020-05-10 15:56:34 +02:00
}
export class TextPos {
constructor(
public offset: number,
public line: number,
public column: number
) {
}
2020-05-23 22:48:24 +02:00
public clone() {
2020-05-10 15:56:34 +02:00
return new TextPos(this.offset, this.line, this.column)
}
}
export class TextSpan {
constructor(
public file: TextFile,
public start: TextPos,
public end: TextPos
) {
}
2020-05-23 22:48:24 +02:00
public clone() {
2020-05-10 15:56:34 +02:00
return new TextSpan(this.file, this.start.clone(), this.end.clone());
}
}