-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathTextDocumentWrapper.ts
More file actions
45 lines (35 loc) · 1.21 KB
/
TextDocumentWrapper.ts
File metadata and controls
45 lines (35 loc) · 1.21 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
/*!
* Copyright 2018-2019 VMware, Inc.
* SPDX-License-Identifier: MIT
*/
import { Position, TextDocument } from "vscode-languageserver-textdocument"
export class TextDocumentWrapper {
constructor(public readonly textDocument: TextDocument) {}
getWordAt(position: Position): string {
const text = this.textDocument.getText()
const offset = this.textDocument.offsetAt(position)
let start = offset
while (0 < start && !this.isWhitespace(text[start])) {
start--
}
let end = offset
while (end < text.length && !this.isWhitespace(text[end])) {
end++
}
return text.substring(start, end)
}
getLineContentUntil(position: Position): string {
const startPosition = {
character: 0,
line: position.line
}
const endPosition = position
const startOffset = this.textDocument.offsetAt(startPosition)
const endOffset = this.textDocument.offsetAt(endPosition)
const lineContent = this.textDocument.getText().substring(startOffset, endOffset)
return lineContent
}
private isWhitespace(str: string): boolean {
return str.trim() === ""
}
}