-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtext-editor.ts
More file actions
58 lines (47 loc) · 1.85 KB
/
text-editor.ts
File metadata and controls
58 lines (47 loc) · 1.85 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
54
55
56
57
58
import {Selection, TextEditor as VsTextEditor, ViewColumn} from 'vscode';
import {basename} from 'path';
import {LineRange} from '../types/selection-info';
export default class TextEditor {
constructor(private readonly vsEditor: VsTextEditor) {}
get fileName(): string {
return basename(this.vsEditor.document.fileName);
}
get fileUri(): string {
return this.vsEditor.document.fileName;
}
get viewColumn(): ViewColumn {
return this.vsEditor.viewColumn!;
}
get getText(): string {
return this.vsEditor.document.getText();
}
get selectedText(): string {
const validSelections = this.collectNonEmptySelections(this.vsEditor.selections);
return this.extractText(validSelections);
}
get selectedLineRanges(): LineRange[] {
const validSelections = this.collectNonEmptySelections(this.vsEditor.selections);
return this.extractLineRanges(validSelections);
}
private collectNonEmptySelections(selections: Selection[]): Selection[] {
return selections.filter(s => !s.isEmpty).sort((s1, s2) => {
const lineComparison = s1.start.line - s2.start.line;
return lineComparison !== 0
? lineComparison
: s1.start.character - s2.start.character;
});
}
private extractText(selections: Selection[]): string {
return selections.length === 0
? this.extractTextFromSelection()
: selections.map(this.extractTextFromSelection).join('\n');
}
private extractTextFromSelection = (selection?: Selection) =>
this.vsEditor.document.getText(selection)
private extractLineRanges(selections: Selection[]): LineRange[] {
return selections.map(selection => ({
start: selection.start.line,
end: selection.end.line
}));
}
}