-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathatom-react.coffee
More file actions
351 lines (265 loc) · 13.2 KB
/
atom-react.coffee
File metadata and controls
351 lines (265 loc) · 13.2 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
{CompositeDisposable, Disposable} = require 'atom'
contentCheckRegex = null
defaultDetectReactFilePattern = '/((require\\([\'"]react(?:(-native|\\/addons))?[\'"]\\)))|(import\\s+[\\w{},\\s]+\\s+from\\s+[\'"]react(?:(-native|\\/addons))?[\'"])/'
autoCompleteTagStartRegex = /(<)([a-zA-Z0-9\.:$_]+)/g
autoCompleteTagCloseRegex = /(<\/)([^>]+)(>)/g
jsxTagStartPattern = '(?x)((^|=|return)\\s*<([^!/?](?!.+?(</.+?>))))'
jsxComplexAttributePattern = '(?x)\\{ [^}"\']* $|\\( [^)"\']* $'
decreaseIndentForNextLinePattern = '(?x)
\\S+\\s*/>\\s*(,|;)?\\s*$
| ^(?!\\s*\\?)\\s*\\S+.*</[-_\\.A-Za-z0-9]+>$'
class AtomReact
config:
enabledForAllJavascriptFiles:
type: 'boolean'
default: false
description: 'Enable grammar, snippets and other features automatically for all .js files.'
disableAutoClose:
type: 'boolean'
default: false
description: 'Disabled tag autocompletion'
skipUndoStackForAutoCloseInsertion:
type: 'boolean'
default: true
description: 'When enabled, auto insert/remove closing tag mutation is skipped from normal undo/redo operation'
detectReactFilePattern:
type: 'string'
default: defaultDetectReactFilePattern
jsxTagStartPattern:
type: 'string'
default: jsxTagStartPattern
jsxComplexAttributePattern:
type: 'string'
default: jsxComplexAttributePattern
decreaseIndentForNextLinePattern:
type: 'string'
default: decreaseIndentForNextLinePattern
constructor: ->
patchEditorLangModeAutoDecreaseIndentForBufferRow: (editor) ->
self = this
fn = editor.autoDecreaseIndentForBufferRow
return if fn.jsxPatch
editor.autoDecreaseIndentForBufferRow = (bufferRow, options) ->
return fn.call(editor, bufferRow, options) unless editor.getGrammar().scopeName == "source.js.jsx"
scopeDescriptor = @scopeDescriptorForBufferPosition([bufferRow, 0])
decreaseNextLineIndentRegex = @tokenizedBuffer.regexForPattern(atom.config.get('react.decreaseIndentForNextLinePattern') || decreaseIndentForNextLinePattern)
decreaseIndentRegex = @tokenizedBuffer.decreaseIndentRegexForScopeDescriptor(scopeDescriptor)
increaseIndentRegex = @tokenizedBuffer.increaseIndentRegexForScopeDescriptor(scopeDescriptor)
precedingRow = @tokenizedBuffer.buffer.previousNonBlankRow(bufferRow)
return if precedingRow < 0
precedingLine = @tokenizedBuffer.buffer.lineForRow(precedingRow)
line = @tokenizedBuffer.buffer.lineForRow(bufferRow)
if precedingLine and decreaseNextLineIndentRegex.testSync(precedingLine) and
not (increaseIndentRegex and increaseIndentRegex.testSync(precedingLine)) and
not @isBufferRowCommented(precedingRow)
currentIndentLevel = @indentationForBufferRow(precedingRow)
currentIndentLevel -= 1 if decreaseIndentRegex and decreaseIndentRegex.testSync(line)
desiredIndentLevel = currentIndentLevel - 1
if desiredIndentLevel >= 0 and desiredIndentLevel < currentIndentLevel
@setIndentationForBufferRow(bufferRow, desiredIndentLevel)
else if not @isBufferRowCommented(bufferRow)
fn.call(editor, bufferRow, options)
patchEditorLangModeSuggestedIndentForBufferRow: (editor) ->
self = this
fn = editor.suggestedIndentForBufferRow
return if fn.jsxPatch
editor.suggestedIndentForBufferRow = (bufferRow, options) ->
indent = fn.call(editor, bufferRow, options)
return indent unless editor.getGrammar().scopeName == "source.js.jsx" and bufferRow > 1
scopeDescriptor = @scopeDescriptorForBufferPosition([bufferRow, 0])
decreaseNextLineIndentRegex = @tokenizedBuffer.regexForPattern(atom.config.get('react.decreaseIndentForNextLinePattern') || decreaseIndentForNextLinePattern)
increaseIndentRegex = @tokenizedBuffer.increaseIndentRegexForScopeDescriptor(scopeDescriptor)
decreaseIndentRegex = @tokenizedBuffer.decreaseIndentRegexForScopeDescriptor(scopeDescriptor)
tagStartRegex = @tokenizedBuffer.regexForPattern(atom.config.get('react.jsxTagStartPattern') || jsxTagStartPattern)
complexAttributeRegex = @tokenizedBuffer.regexForPattern(atom.config.get('react.jsxComplexAttributePattern') || jsxComplexAttributePattern)
precedingRow = @tokenizedBuffer.buffer.previousNonBlankRow(bufferRow)
return indent if precedingRow < 0
precedingLine = @tokenizedBuffer.buffer.lineForRow(precedingRow)
return indent if not precedingLine?
if @isBufferRowCommented(bufferRow) and @isBufferRowCommented(precedingRow)
return @indentationForBufferRow(precedingRow)
tagStartTest = tagStartRegex.testSync(precedingLine)
decreaseIndentTest = decreaseIndentRegex.testSync(precedingLine)
indent += 1 if tagStartTest and complexAttributeRegex.testSync(precedingLine) and not @isBufferRowCommented(precedingRow)
indent -= 1 if precedingLine and not decreaseIndentTest and decreaseNextLineIndentRegex.testSync(precedingLine) and not @isBufferRowCommented(precedingRow)
return Math.max(indent, 0)
patchEditorLangMode: (editor) ->
@patchEditorLangModeSuggestedIndentForBufferRow(editor)?.jsxPatch = true
@patchEditorLangModeAutoDecreaseIndentForBufferRow(editor)?.jsxPatch = true
isReact: (text) ->
return true if atom.config.get('react.enabledForAllJavascriptFiles')
if not contentCheckRegex?
match = (atom.config.get('react.detectReactFilePattern') || defaultDetectReactFilePattern).match(new RegExp('^/(.*?)/([gimy]*)$'));
contentCheckRegex = new RegExp(match[1], match[2])
return text.match(contentCheckRegex)?
isReactEnabledForEditor: (editor) ->
return editor? && editor.getGrammar().scopeName in ["source.js.jsx", "source.coffee.jsx"]
autoSetGrammar: (editor) ->
return if @isReactEnabledForEditor editor
path = require 'path'
# Check if file extension is .jsx or the file requires React
extName = path.extname(editor.getPath() or '')
if extName is ".jsx" or ((extName is ".js" or extName is ".es6") and @isReact(editor.getText()))
jsxGrammar = atom.grammars.grammarForScopeName("source.js.jsx")
editor.setGrammar jsxGrammar if jsxGrammar
onHTMLToJSX: ->
jsxformat = require 'jsxformat'
HTMLtoJSX = require './htmltojsx'
converter = new HTMLtoJSX(createClass: false)
editor = atom.workspace.getActiveTextEditor()
return if not @isReactEnabledForEditor editor
selections = editor.getSelections()
editor.transact =>
for selection in selections
try
selectionText = selection.getText()
jsxOutput = converter.convert(selectionText)
try
jsxformat.setOptions({});
jsxOutput = jsxformat.format(jsxOutput)
selection.insertText(jsxOutput);
range = selection.getBufferRange();
editor.autoIndentBufferRows(range.start.row, range.end.row)
onReformat: ->
jsxformat = require 'jsxformat'
_ = require 'lodash'
editor = atom.workspace.getActiveTextEditor()
return if not @isReactEnabledForEditor editor
selections = editor.getSelections()
editor.transact =>
for selection in selections
try
range = selection.getBufferRange();
serializedRange = range.serialize()
bufStart = serializedRange[0]
bufEnd = serializedRange[1]
jsxformat.setOptions({});
result = jsxformat.format(selection.getText())
originalLineCount = editor.getLineCount()
selection.insertText(result)
newLineCount = editor.getLineCount()
editor.autoIndentBufferRows(bufStart[0], bufEnd[0] + (newLineCount - originalLineCount))
editor.setCursorBufferPosition(bufStart)
catch err
# Parsing/formatting the selection failed lets try to parse the whole file but format the selection only
range = selection.getBufferRange().serialize()
# esprima ast line count starts for 1
range[0][0]++
range[1][0]++
jsxformat.setOptions({range: range});
# TODO: use fold
original = editor.getText();
try
result = jsxformat.format(original)
selection.clear()
originalLineCount = editor.getLineCount()
editor.setText(result)
newLineCount = editor.getLineCount()
firstChangedLine = range[0][0] - 1
lastChangedLine = range[1][0] - 1 + (newLineCount - originalLineCount)
editor.autoIndentBufferRows(firstChangedLine, lastChangedLine)
# return back
editor.setCursorBufferPosition([firstChangedLine, range[0][1]])
autoCloseTag: (eventObj, editor) ->
return if atom.config.get('react.disableAutoClose')
return if not @isReactEnabledForEditor(editor) or editor != atom.workspace.getActiveTextEditor()
if eventObj?.newText is '>' and !eventObj.oldText
# auto closing multiple cursors is a little bit tricky so lets disable it for now
return if editor.getCursorBufferPositions().length > 1;
tokenizedLine = editor.tokenizedBuffer?.tokenizedLineForRow(eventObj.newRange.end.row)
return if not tokenizedLine?
token = tokenizedLine.tokenAtBufferColumn(eventObj.newRange.end.column - 1)
if not token? or token.scopes.indexOf('tag.open.js') == -1 or token.scopes.indexOf('punctuation.definition.tag.end.js') == -1
return
lines = editor.buffer.getLines()
row = eventObj.newRange.end.row
line = lines[row]
line = line.substr 0, eventObj.newRange.end.column
# Tag is self closing
return if line.substr(line.length - 2, 1) is '/'
tagName = null
while line? and not tagName?
match = line.match autoCompleteTagStartRegex
if match? && match.length > 0
tagName = match.pop().substr(1)
row--
line = lines[row]
if tagName?
if atom.config.get('react.skipUndoStackForAutoCloseInsertion')
options = {undo: 'skip'}
else
options = {}
editor.insertText('</' + tagName + '>', options)
editor.setCursorBufferPosition(eventObj.newRange.end)
else if eventObj?.oldText is '>' and eventObj?.newText is ''
lines = editor.buffer.getLines()
row = eventObj.newRange.end.row
fullLine = lines[row]
tokenizedLine = editor.tokenizedBuffer?.tokenizedLineForRow(eventObj.newRange.end.row)
return if not tokenizedLine?
token = tokenizedLine.tokenAtBufferColumn(eventObj.newRange.end.column - 1)
if not token? or token.scopes.indexOf('tag.open.js') == -1
return
line = fullLine.substr 0, eventObj.newRange.end.column
# Tag is self closing
return if line.substr(line.length - 1, 1) is '/'
tagName = null
while line? and not tagName?
match = line.match autoCompleteTagStartRegex
if match? && match.length > 0
tagName = match.pop().substr(1)
row--
line = lines[row]
if tagName?
rest = fullLine.substr(eventObj.newRange.end.column)
if rest.indexOf('</' + tagName + '>') == 0
# rest is closing tag
if atom.config.get('react.skipUndoStackForAutoCloseInsertion')
options = {undo: 'skip'}
else
options = {}
serializedEndPoint = [eventObj.newRange.end.row, eventObj.newRange.end.column];
editor.setTextInBufferRange(
[
serializedEndPoint,
[serializedEndPoint[0], serializedEndPoint[1] + tagName.length + 3]
]
, '', options)
else if eventObj? and eventObj.newText.match /\r?\n/
lines = editor.buffer.getLines()
row = eventObj.newRange.end.row
lastLine = lines[row - 1]
fullLine = lines[row]
if />$/.test(lastLine) and fullLine.search(autoCompleteTagCloseRegex) == 0
while lastLine?
match = lastLine.match autoCompleteTagStartRegex
if match? && match.length > 0
break
row--
lastLine = lines[row]
lastLineSpaces = lastLine.match(/^\s*/)
lastLineSpaces = if lastLineSpaces? then lastLineSpaces[0] else ''
editor.insertText('\n' + lastLineSpaces)
editor.setCursorBufferPosition(eventObj.newRange.end)
processEditor: (editor) ->
@patchEditorLangMode(editor)
@autoSetGrammar(editor)
disposableBufferEvent = editor.buffer.onDidChange (e) =>
@autoCloseTag e, editor
@disposables.add editor.onDidDestroy => disposableBufferEvent.dispose()
@disposables.add(disposableBufferEvent);
deactivate: ->
@disposables.dispose()
activate: ->
@disposables = new CompositeDisposable();
# Bind events
disposableConfigListener = atom.config.observe 'react.detectReactFilePattern', (newValue) ->
contentCheckRegex = null
disposableReformat = atom.commands.add 'atom-workspace', 'react:reformat-JSX', => @onReformat()
disposableHTMLTOJSX = atom.commands.add 'atom-workspace', 'react:HTML-to-JSX', => @onHTMLToJSX()
disposableProcessEditor = atom.workspace.observeTextEditors @processEditor.bind(this)
@disposables.add disposableConfigListener
@disposables.add disposableReformat
@disposables.add disposableHTMLTOJSX
@disposables.add disposableProcessEditor
module.exports = AtomReact