/
TextDocument.ts
164 lines (145 loc) · 4.6 KB
/
TextDocument.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { getLanguagesForBasename } from '../LanguageIds';
import * as Uri from './Uri';
import { TextDocument as VsTextDocument } from 'vscode-languageserver-textdocument';
import assert from 'assert';
export type DocumentUri = Uri.Uri;
export interface Position {
line: number;
character: number;
}
/**
* Range offset tuple.
*/
export type SimpleRange = [start: number, end: number];
export interface TextDocumentLine {
readonly text: string;
readonly offset: number;
readonly position: Position;
}
/**
* A simple text document. Not to be implemented. The document keeps the content
* as string.
*/
export interface TextDocument {
/**
* The associated URI for this document. Most documents have the __file__-scheme, indicating that they
* represent files on disk. However, some documents may have other schemes indicating that they are not
* available on disk.
*/
readonly uri: DocumentUri;
/**
* The identifier of the language associated with this document.
*/
readonly languageId: string | string[];
/**
* The version number of this document (it will increase after each
* change, including undo/redo).
*/
readonly version: number;
/**
* the raw Document Text
*/
readonly text: string;
/**
* The natural language locale.
*/
readonly locale?: string | undefined;
positionAt(offset: number): Position;
offsetAt(position: Position): number;
lineAt(offset: number): TextDocumentLine;
}
class TextDocumentImpl implements TextDocument {
private vsTextDoc: VsTextDocument;
constructor(
readonly uri: DocumentUri,
text: string,
readonly languageId: string | string[],
readonly locale: string | undefined,
version: number
) {
const primaryLanguageId = typeof languageId === 'string' ? languageId : languageId[0] || 'plaintext';
this.vsTextDoc = VsTextDocument.create(uri.toString(), primaryLanguageId, version, text);
}
get version(): number {
return this.vsTextDoc.version;
}
get text(): string {
return this.vsTextDoc.getText();
}
positionAt(offset: number): Position {
return this.vsTextDoc.positionAt(offset);
}
offsetAt(position: Position): number {
return this.vsTextDoc.offsetAt(position);
}
lineAt(offset: number): TextDocumentLine {
const position = this.vsTextDoc.positionAt(offset);
position.character = 0;
const lineOffset = this.vsTextDoc.offsetAt(position);
const range = {
start: position,
end: { line: position.line + 1, character: 0 },
};
let _text: string | undefined;
const getText = () => this.vsTextDoc.getText(range);
return {
get text() {
return _text ?? (_text = getText());
},
offset: lineOffset,
position,
};
}
/**
* Apply edits to the text.
* Note: the edits are applied one after the other.
* @param edits - changes to the text
* @param version - optional version to use.
* @returns this
*/
update(edits: TextDocumentContentChangeEvent[], version?: number): this {
version = version ?? this.version + 1;
for (const edit of edits) {
const vsEdit = edit.range
? {
range: { start: this.positionAt(edit.range[0]), end: this.positionAt(edit.range[1]) },
text: edit.text,
}
: edit;
VsTextDocument.update(this.vsTextDoc, [vsEdit], version);
}
return this;
}
}
export interface CreateTextDocumentParams {
uri: DocumentUri | string;
content: string;
languageId?: string | string[] | undefined;
locale?: string | undefined;
version?: number | undefined;
}
export interface TextDocumentContentChangeEvent {
range?: SimpleRange;
text: string;
}
export function createTextDocument({
uri,
content,
languageId,
locale,
version,
}: CreateTextDocumentParams): TextDocument {
version = version ?? 1;
uri = Uri.toUri(uri);
languageId = languageId ?? getLanguagesForBasename(Uri.basename(uri));
languageId = languageId.length === 0 ? 'text' : languageId;
return new TextDocumentImpl(uri, content, languageId, locale, version);
}
export function updateTextDocument(
doc: TextDocument,
edits: TextDocumentContentChangeEvent[],
version?: number
): TextDocument {
assert(doc instanceof TextDocumentImpl, 'Unknown TextDocument type');
return doc.update(edits, version);
}