forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextIterator.ts
More file actions
53 lines (41 loc) · 1.41 KB
/
textIterator.ts
File metadata and controls
53 lines (41 loc) · 1.41 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { Position, Range, TextDocument } from 'vscode';
import { ITextIterator } from './types';
export class TextIterator implements ITextIterator {
private text: string;
constructor(text: string) {
this.text = text;
}
public charCodeAt(index: number): number {
if (index >= 0 && index < this.text.length) {
return this.text.charCodeAt(index);
}
return 0;
}
public get length(): number {
return this.text.length;
}
public getText(): string {
return this.text;
}
}
export class DocumentTextIterator implements ITextIterator {
public readonly length: number;
private document: TextDocument;
constructor(document: TextDocument) {
this.document = document;
const lastIndex = this.document.lineCount - 1;
const lastLine = this.document.lineAt(lastIndex);
const end = new Position(lastIndex, lastLine.range.end.character);
this.length = this.document.offsetAt(end);
}
public charCodeAt(index: number): number {
const position = this.document.positionAt(index);
return this.document.getText(new Range(position, position.translate(0, 1))).charCodeAt(position.character);
}
public getText(): string {
return this.document.getText();
}
}