-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathprint-sum-of-selected-numbers.ts
More file actions
91 lines (73 loc) · 3.26 KB
/
Copy pathprint-sum-of-selected-numbers.ts
File metadata and controls
91 lines (73 loc) · 3.26 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
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
// Copyright (C) 2021 Patrick Maué
//
// This file is part of vscode-journal.
//
// vscode-journal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// vscode-journal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with vscode-journal. If not, see <http://www.gnu.org/licenses/>.
//
'use strict';
import * as vscode from 'vscode';
import { Ctrl, isNullOrUndefined } from '../util';
export class PrintSumCommand implements vscode.Command, vscode.Disposable {
title: string = "Print sum of two selected numbers";
command: string = "journal.printSum";
protected constructor(public ctrl: Ctrl) {}
public async dispose(): Promise<void> {
// do nothing
}
public static create(ctrl: Ctrl): vscode.Disposable {
const cmd = new this(ctrl);
vscode.commands.registerCommand(cmd.command, () => cmd.execute());
return cmd;
}
/**
* Prints the sum of the selected numbers in the current editor at the selection location
*/
public async execute(): Promise<void> {
this.ctrl.logger.trace("Executing command: ", this.command);
try {
let editor: vscode.TextEditor = <vscode.TextEditor>vscode.window.activeTextEditor;
let regExp: RegExp = /(\d+[,\.]?\d*\s?)|(\s)/;
let target: vscode.Position;
let numbers: number[] = [];
editor.selections.forEach((selection: vscode.Selection) => {
let range: vscode.Range | undefined = editor.document.getWordRangeAtPosition(selection.active, regExp);
if (isNullOrUndefined(range)) {
target = selection.active;
return;
}
let text = editor.document.getText(range);
// check if empty string
if (text.trim().length === 0) {
target = selection.active;
return;
}
// check if number
let number: number = Number(text);
if (number > 0) {
numbers.push(number);
return;
}
});
if (numbers.length < 2) { throw Error("You have to select at least two numbers"); } // tslint:disable-line
else if (isNullOrUndefined(target!)) { throw Error("No valid target selected for printing the sum."); } // tslint:disable-line
else {
let result: string = numbers.reduce((previous, current) => previous + current).toString();
this.ctrl.inject.injectString(editor.document, result + "", target!);
}
} catch (error) {
this.ctrl.logger.error("Failed to execute command: ", this.command, "Reason: ", error);
this.ctrl.ui.showError("Failed to print sum of selected numbers.");
}
}
}