This repository was archived by the owner on Oct 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension.js
88 lines (69 loc) · 3.2 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
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
'use strict';
var vscode = require('vscode');
function activate(context) {
console.log('Congratulations, your extension "break-from-comma" is now active!');
const disposable = vscode.commands.registerCommand('extension.breakFromComma', () => {
const editor = vscode.window.activeTextEditor
if (!editor) {
return
}
const { document, selection } = editor
if (selection.isEmpty) {
return
}
const text = document.getText(selection)
// Range of selection to be replaced
let range = new vscode.Range(selection.start, selection.end)
const entireLine = document.lineAt(selection.start.line)
const remainingLine = document.getText(new vscode.Range(selection.end, entireLine.range.end))
// Count the leading spaces and include it in the selection range if any
const leadingSpaces = remainingLine.search(/\S/)
if (leadingSpaces > 0) {
range = new vscode.Range(selection.start.line,
selection.start.character,
selection.start.line,
selection.end.character + leadingSpaces)
}
/**
* RegEx Explanation
, ','
(?= look ahead to see if there is:
(?: group, but do not capture (0 or more times):
(?: group, but do not capture (2 times):
[^'"]* any character except: '"' (0 or more times)
(?:'|")" '"'
){2} end of grouping
)* end of grouping
[^'"]* any character except: '"' (0 or more times)
$ before an optional \n, and the end of the string
) end of look-ahead
*/
let linesToIndent = text.split(/,(?=(?:(?:[^'"]*(?:'|")){2})*[^'"]*$)/)
.map(t => t.trim() + ',')
const { insertSpaces, tabSize } = editor.options
const indentChar = insertSpaces ? ' ' : '\t';
const start = editor.selection.start;
const offset = Math.ceil(start.character / tabSize) * (insertSpaces ? tabSize : 1)
const chars = indentChar.repeat(offset)
const lastOffset = offset - (insertSpaces ? tabSize : 1)
const lastIndent = (lastOffset > 0) ? indentChar.repeat(lastOffset) : ''
linesToIndent = linesToIndent.map((line, index) => {
let tempChars = chars
if (index === 0 && start.character !== 0) {
tempChars = '\n' + chars
}
return tempChars + line
})
// Remove the last comma and add indentation
linesToIndent[linesToIndent.length-1] = linesToIndent[linesToIndent.length-1].slice(0,-1) + '\n' + lastIndent
editor.edit(editBuilder => {
editBuilder.replace(range, linesToIndent.join('\n'))
})
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
function deactivate() {
vscode.window.showInformationMessage('Feel free to break-from-comma again!')
}
exports.deactivate = deactivate;