forked from zucska/extract-component
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
270 lines (209 loc) · 7.87 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
let vscode = require('vscode');
const fs = require('fs');
var mkdirp = require('mkdirp');
var getDirName = require('path').dirname;
var _ = require('lodash');
var lineColumn = require("line-column");
function activate(context) {
let disposable = vscode.commands.registerCommand('extension.extractComponent', function () {
var editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
var selection = editor.selection;
var original = editor.document.getText()
var text = editor.document.getText(selection);
let actEdit = vscode.window.activeTextEditor;
const start = lineColumn(original).fromIndex(original.indexOf('extends'))
let line = null
if (start && start.line) {
line = start.line - 1
}
vscode.window.showInputBox({
prompt: 'Insert component name',
value: ''
}).then(function (e) {
if (!e || e == '') return
const nameFile = e.toLowerCase()
createFile(nameFile, text, original, function (err, resp) {
if (err) {
vscode.window.showInformationMessage(err);
return
}
actEdit.edit(function (edit) {
const name = capitalizeFirstLetter(_.camelCase(e))
const Position = vscode.Position
const stringImport = `import ${name} from '../${nameFile}'\n\n`
if (line)
edit.insert(new Position(line, 0), stringImport)
edit.replace(selection, '<' + name + ' ' + resp + ' />')
})
})
})
});
let disposableMethod = vscode.commands.registerCommand('extension.extractMethodComponent', function () {
var editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
var selection = editor.selection;
var original = editor.document.getText()
var text = editor.document.getText(selection)
let actEdit = vscode.window.activeTextEditor
let rowInsert = 0
let column = 0
if (original.indexOf('render()') > -1) {
const start = lineColumn(original).fromIndex(original.indexOf('render()'))
rowInsert = start.line - 1
column = start.column
} else {
return
}
vscode.window.showInputBox({
prompt: 'Insert name method (render__NAME__)',
value: ''
}).then(function (e) {
if (!e || e == '') return
const nameMethod = capitalizeFirstLetter(_.camelCase(e))
actEdit.edit(function (edit) {
const Position = vscode.Position
const newtext = `\n\trender${nameMethod}(){\nreturn (\n ${text}\n)\n}\n\n`
edit.replace(selection, '\t\t{ this.render' + nameMethod + '() }')
edit.insert(new Position(rowInsert, column), newtext)
})
})
});
let disposableEmbed = vscode.commands.registerCommand('extension.embedComponent', function () {
var editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
var selection = editor.selection;
var text = editor.document.getText(selection);
let actEdit = vscode.window.activeTextEditor;
vscode.window.showInputBox({
prompt: 'Insert component name',
value: 'View'
}).then(function (e) {
if (!e || e == '') return
actEdit.edit(function (edit) {
edit.replace(selection, '<' + e + '>\n' + text + '\n</' + e + '>')
})
})
});
let disposableStyle = vscode.commands.registerCommand('extension.extractStyleComponent', function () {
var editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
var selection = editor.selection;
var original = editor.document.getText()
var text = editor.document.getText(selection)
let actEdit = vscode.window.activeTextEditor
let newStyle = true
let rowInsert = actEdit.document.lineCount + 1
if (original.indexOf('StyleSheet.create') > -1) {
const start = lineColumn(original).fromIndex(original.indexOf('StyleSheet.create'))
//const end = start.toIndex(0, original.indexOf('})'))
rowInsert = start.line
newStyle = false
}
vscode.window.showInputBox({
prompt: 'Insert name',
value: ''
}).then(function (e) {
if (!e || e == '') return
actEdit.edit(function (edit) {
const Position = vscode.Position
let stylesText = ''
if (newStyle)
stylesText = `\n\nconst styles = StyleSheet.create({\n ${e}:${text} \n})`
else
stylesText = `${e}:${text},\n`
edit.replace(selection, `styles.${e}`)
edit.insert(new Position(rowInsert, 0), stylesText);
})
})
});
context.subscriptions.push(disposable)
context.subscriptions.push(disposableMethod)
context.subscriptions.push(disposableEmbed)
context.subscriptions.push(disposableStyle)
}
exports.activate = activate;
function deactivate() {
}
function createFile(name, contents, original, cb) {
// todo add configuration root path
const path = vscode.workspace.rootPath + '/src/components/' + name + '/index.js'
if (fs.existsSync(path)) {
cb('File exist')
return
}
readTemplate(function (template) {
const props = ['', ''] //createProps(contents)
let newContent = template.replace(new RegExp('componentName', 'g'), capitalizeFirstLetter(_.camelCase(name)))
newContent = newContent.replace("__CONTENTS__", contents)
newContent = newContent.replace("__PROPS__", props[0])
// se il file contiene react-native clono anche la riga
newContent = newContent.replace("__IMPORT__", generateImport(original))
mkdirp(getDirName(path), function (err) {
if (err) return cb(err);
fs.writeFile(path, newContent, () => {
cb(null, props[1])
});
});
})
}
function generateImport(str) {
const regex = /import (.*) from 'react-native'/g;
let m;
let result = '';
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
m.forEach((match, groupIndex) => {
if (groupIndex == 0)
result = result + match + "\n";
});
}
return result
}
// DISABLED
function createProps(contents) {
const regex = /([a-zA-Z0-9-_]*)={([^0-9]*?)}/g
let m;
let props = ''
let tag = ''
while ((m = regex.exec(contents)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
m.forEach((match, groupIndex) => {
//console.log(`Found match, group ${groupIndex}: ${match}`);
});
let val = m[2].replace('this.', '')
if (m[1] != 'style') {
props = props + `${val}, `
tag = tag + `${val}={${val}} `
}
}
props = props.slice(0, -2)
return [props, tag]
}
function readTemplate(cb) {
const ext = vscode.extensions.getExtension('zucska.extractcomponent');
// todo add version template for reactjs and react native
fs.readFile(ext.extensionPath + '/template.js', "utf-8", function read(err, data) {
if (err) {
throw err;
}
cb(data.toString())
});
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
exports.deactivate = deactivate;