1
1
var vscode = require ( "vscode" )
2
+ var editorHelpers = require ( "./editorHelpers" ) ;
3
+ var tables = require ( "./tables" ) ;
4
+
2
5
module . exports = {
3
6
register : register
4
7
}
@@ -18,7 +21,9 @@ var _commands = [
18
21
new Command ( 'toggleTitleH4' , toggleTitleH4 , 'Toggle title H4' , '#### Title' , true ) ,
19
22
new Command ( 'toggleTitleH5' , toggleTitleH5 , 'Toggle title H5' , '##### Title' , true ) ,
20
23
new Command ( 'toggleTitleH6' , toggleTitleH6 , 'Toggle title H6' , '###### Title' , true ) ,
21
- new Command ( 'toggleCheckboxes' , toggleCheckboxes , 'Toggle checkboxes' , '- [x] Checkbox item' , true )
24
+ new Command ( 'toggleCheckboxes' , toggleCheckboxes , 'Toggle checkboxes' , '- [x] Checkbox item' , true ) ,
25
+ new Command ( 'addTable' , tables . addTable , 'Add table' , 'Tabular | values' , true ) ,
26
+ new Command ( 'addTableWithHeader' , tables . addTableWithHeader , 'Add table (with header)' , 'Tabular | values' , true )
22
27
]
23
28
24
29
function register ( context ) {
@@ -40,77 +45,77 @@ function showCommandPalette() {
40
45
}
41
46
42
47
function toggleBold ( ) {
43
- surroundSelection ( '**' )
48
+ editorHelpers . surroundSelection ( '**' )
44
49
}
45
50
46
51
function toggleItalic ( ) {
47
- surroundSelection ( '_' )
52
+ editorHelpers . surroundSelection ( '_' )
48
53
}
49
54
50
55
function toggleCodeBlock ( ) {
51
- surroundSelection ( '```\n' , '\n```' )
56
+ editorHelpers . surroundSelection ( '```\n' , '\n```' )
52
57
}
53
58
54
59
function toggleInlineCode ( ) {
55
- surroundSelection ( '`' )
60
+ editorHelpers . surroundSelection ( '`' )
56
61
}
57
62
58
63
function toggleTitleH1 ( ) {
59
- surroundSelection ( '# ' , '' )
64
+ editorHelpers . surroundSelection ( '# ' , '' )
60
65
}
61
66
62
67
function toggleTitleH2 ( ) {
63
- surroundSelection ( '## ' , '' )
68
+ editorHelpers . surroundSelection ( '## ' , '' )
64
69
}
65
70
66
71
function toggleTitleH3 ( ) {
67
- surroundSelection ( '### ' , '' )
72
+ editorHelpers . surroundSelection ( '### ' , '' )
68
73
}
69
74
70
75
function toggleTitleH4 ( ) {
71
- surroundSelection ( '#### ' , '' )
76
+ editorHelpers . surroundSelection ( '#### ' , '' )
72
77
}
73
78
74
79
function toggleTitleH5 ( ) {
75
- surroundSelection ( '##### ' , '' )
80
+ editorHelpers . surroundSelection ( '##### ' , '' )
76
81
}
77
82
78
83
function toggleTitleH6 ( ) {
79
- surroundSelection ( '###### ' , '' )
84
+ editorHelpers . surroundSelection ( '###### ' , '' )
80
85
}
81
86
82
87
var HasBullets = / ^ ( \s * ) \* ( .* ) $ / gm
83
88
var AddBullets = / ^ ( \s * ) ( .+ ) $ / gm
84
89
function toggleBullets ( ) {
85
90
86
- if ( ! isAnythingSelected ( ) ) {
87
- surroundSelection ( "* " , "" )
91
+ if ( ! editorHelpers . isAnythingSelected ( ) ) {
92
+ editorHelpers . surroundSelection ( "* " , "" )
88
93
return ;
89
94
}
90
95
91
- if ( isMatch ( HasBullets ) ) {
92
- replaceSelection ( ( text ) => text . replace ( HasBullets , "$1$2" ) )
96
+ if ( editorHelpers . isMatch ( HasBullets ) ) {
97
+ editorHelpers . replaceSelection ( ( text ) => text . replace ( HasBullets , "$1$2" ) )
93
98
}
94
99
else {
95
- replaceSelection ( ( text ) => text . replace ( AddBullets , "$1* $2" ) )
100
+ editorHelpers . replaceSelection ( ( text ) => text . replace ( AddBullets , "$1* $2" ) )
96
101
}
97
102
}
98
103
99
104
var HasNumbers = / ^ ( \s * ) [ 0 - 9 ] \. + ( .* ) $ / gm
100
105
var AddNumbers = / ^ ( \s * ) ( .+ ) $ / gm
101
106
function toggleNumberList ( ) {
102
107
103
- if ( ! isAnythingSelected ( ) ) {
104
- surroundSelection ( "1. " , "" )
108
+ if ( ! editorHelpers . isAnythingSelected ( ) ) {
109
+ editorHelpers . surroundSelection ( "1. " , "" )
105
110
return ;
106
111
}
107
112
108
- if ( isMatch ( HasNumbers ) ) {
109
- replaceSelection ( ( text ) => text . replace ( HasNumbers , "$1$2" ) )
113
+ if ( editorHelpers . isMatch ( HasNumbers ) ) {
114
+ editorHelpers . replaceSelection ( ( text ) => text . replace ( HasNumbers , "$1$2" ) )
110
115
}
111
116
else {
112
117
var lineNums = { } ;
113
- replaceSelection ( ( text ) => text . replace ( AddNumbers , ( match , whitespace , line ) => {
118
+ editorHelpers . replaceSelection ( ( text ) => text . replace ( AddNumbers , ( match , whitespace , line ) => {
114
119
if ( ! lineNums [ whitespace ] ) {
115
120
lineNums [ whitespace ] = 1
116
121
}
@@ -123,23 +128,23 @@ var HasCheckboxes = /^(\s*)- \[[ x]{1}\] (.*)$/gm
123
128
var AddCheckboxes = / ^ ( \s * ) ( .+ ) $ / gm
124
129
function toggleCheckboxes ( ) {
125
130
126
- if ( ! isAnythingSelected ( ) ) {
127
- surroundSelection ( "- [ ]" , "" )
131
+ if ( ! editorHelpers . isAnythingSelected ( ) ) {
132
+ editorHelpers . surroundSelection ( "- [ ]" , "" )
128
133
return ;
129
134
}
130
135
131
- if ( isMatch ( HasCheckboxes ) ) {
132
- replaceSelection ( ( text ) => text . replace ( HasCheckboxes , "$1$2" ) )
136
+ if ( editorHelpers . isMatch ( HasCheckboxes ) ) {
137
+ editorHelpers . replaceSelection ( ( text ) => text . replace ( HasCheckboxes , "$1$2" ) )
133
138
}
134
139
else {
135
- replaceSelection ( ( text ) => text . replace ( AddCheckboxes , "$1- [ ] $2" ) )
140
+ editorHelpers . replaceSelection ( ( text ) => text . replace ( AddCheckboxes , "$1- [ ] $2" ) )
136
141
}
137
142
}
138
143
139
144
function toggleLink ( ) {
140
145
141
- if ( isMatch ( / ^ \[ .+ \] \( .+ \) $ / i) ) {
142
- replaceSelection ( ( text ) => text . match ( / \[ ( .+ ) \] / ) [ 1 ] )
146
+ if ( editorHelpers . isMatch ( / ^ \[ .+ \] \( .+ \) $ / i) ) {
147
+ editorHelpers . replaceSelection ( ( text ) => text . match ( / \[ ( .+ ) \] / ) [ 1 ] )
143
148
return ;
144
149
}
145
150
else {
@@ -175,63 +180,10 @@ function toggleLink() {
175
180
function addTags ( options ) {
176
181
if ( ! options || ! options . url ) return ;
177
182
178
- surroundSelection ( "[" + options . text , "](" + options . url + ")" )
179
- }
180
- }
181
-
182
- function surroundSelection ( startPattern , endPattern )
183
- {
184
- if ( endPattern == undefined || endPattern == null ) endPattern = startPattern ;
185
-
186
- var editor = vscode . window . activeTextEditor ;
187
- var selection = editor . selection ;
188
-
189
- if ( ! isAnythingSelected ( ) ) {
190
- var position = selection . active ;
191
- var newPosition = position . with ( position . line , position . character + startPattern . length )
192
- editor . edit ( ( edit ) => {
193
- edit . insert ( selection . start , startPattern + endPattern ) ;
194
- } ) . then ( ( ) => {
195
- editor . selection = new vscode . Selection ( newPosition , newPosition )
196
- } )
197
- }
198
- else {
199
- if ( isMatch ( startPattern , endPattern ) ) {
200
- replaceSelection ( ( text ) => text . substr ( startPattern . length , text . length - startPattern . length - endPattern . length ) )
201
- }
202
- else {
203
- replaceSelection ( ( text ) => startPattern + text + endPattern )
204
- }
183
+ editorHelpers . surroundSelection ( "[" + options . text , "](" + options . url + ")" )
205
184
}
206
185
}
207
186
208
- function isAnythingSelected ( ) {
209
- return ! vscode . window . activeTextEditor . selection . isEmpty ;
210
- }
211
-
212
- function isMatch ( startPattern , endPattern ) {
213
-
214
- var editor = vscode . window . activeTextEditor ;
215
- var selection = editor . selection ;
216
-
217
- var text = editor . document . getText ( selection )
218
- if ( startPattern . constructor === RegExp ) {
219
- return startPattern . test ( text ) ;
220
- }
221
-
222
- return text . startsWith ( startPattern ) && text . endsWith ( endPattern )
223
- }
224
-
225
- function replaceSelection ( replaceFunc ) {
226
- var editor = vscode . window . activeTextEditor ;
227
- var selection = editor . selection ;
228
-
229
- var newText = replaceFunc ( editor . document . getText ( selection ) ) ;
230
- editor . edit ( ( edit ) => {
231
- edit . replace ( selection , newText )
232
- } )
233
- }
234
-
235
187
function Command ( command , callback , label , description , showInCommandPalette ) {
236
188
this . command = command ;
237
189
this . callback = callback ;
0 commit comments