-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
54 lines (45 loc) · 1.36 KB
/
extension.js
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
const vscode = require('vscode')
const numRE = /\s*[$¢€¥£]?\s*([+-]?[0-9]*[.]?[0-9]*)/
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
const disposable = vscode.commands.registerCommand('sumnumbers.sumNumbers', function () {
const editor = vscode.window.activeTextEditor
if (!editor) {
return
}
const document = editor.document
const selection = editor.selection
// set lastLineNum to the line number after selection end,
// regardless of whether the selection ends within a line
var lastLineNum = editor.selection.end.line
if (editor.selection.end.character > 0) {
lastLineNum++
}
const numArray = []
var sum = 0
for (var lineNum = selection.start.line; lineNum < lastLineNum; ++lineNum) {
const line = document.lineAt(lineNum)
const numMatchArr = line.text.match(numRE)
if ((numMatchArr == null) || (numMatchArr.length < 2)) {
continue
}
const value = parseFloat(numMatchArr[1])
if (isNaN(value)) {
continue
}
numArray.push(value)
sum += value
}
const result = "\nsum = " + sum + " of " + numArray.length + " numbers: " + numArray.join(", ") + "\n"
const nextLinePosition = new vscode.Position(lastLineNum, 0)
editor.edit(editBuilder => {
editBuilder.insert(nextLinePosition, result)
})
})
context.subscriptions.push(disposable)
}
module.exports = {
activate
}