-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathrefactor.js
More file actions
101 lines (89 loc) · 2.98 KB
/
refactor.js
File metadata and controls
101 lines (89 loc) · 2.98 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
/** @babel */
import { client } from '../connection'
import modules from './modules'
import { getWordRangeAtBufferPosition, isValidWordToInspect } from '../misc/words'
import { getLocalContext } from '../misc/blocks'
const renamerefactor = client.import('renamerefactor')
const wordRegexWithoutDotAccessor = /@?[\u00A0-\uFFFF\w_!´]+/
class Refactor {
activate (ink) {
this.ink = ink
}
renameRefactor () {
const editor = atom.workspace.getActiveTextEditor()
const bufferPosition = editor.getCursorBufferPosition()
if (!client.isActive()) return
const range = getWordRangeAtBufferPosition(editor, bufferPosition, {
wordRegex: wordRegexWithoutDotAccessor
})
if (range.isEmpty()) return
const oldWord = editor.getTextInBufferRange(range)
if (!isValidWordToInspect(oldWord)) return
const rangeFull = getWordRangeAtBufferPosition(editor, bufferPosition)
const fullWord = editor.getTextInBufferRange(rangeFull)
this.ink.showBasicModal([{
name: 'Rename',
defaultText: oldWord,
message: `Enter an new name to which \`${oldWord}\` will be renamed.`
}]).then(items => {
// check the new name is a valid identifier
const newWord = items['Rename']
if (!isValidWordToInspect(newWord) || newWord.match(wordRegexWithoutDotAccessor) != newWord) {
atom.notifications.addWarning('Julia Client: Rename Refactor', {
description: `\`${newWord}\` isn't a valid identifier`
})
return
}
// local context
const { column, row } = bufferPosition
const { range, context, startRow } = getLocalContext(editor, row)
// module context
const currentModule = modules.current()
const mod = currentModule ? currentModule : 'Main'
renamerefactor({
oldWord,
fullWord,
newWord,
// local context
column: column + 1,
row: row + 1,
startRow,
context,
// module context
mod,
}).then(result => {
// local refactoring
if (result.text) {
editor.setTextInBufferRange(range, result.text)
}
if (result.success) {
atom.notifications.addSuccess('Julia Client: Rename Refactor', {
description: result.success,
dismissable: true
})
}
if (result.info) {
atom.notifications.addInfo('Julia Client: Rename Refactor', {
description: result.info,
dismissable: true
})
}
if (result.warning) {
atom.notifications.addWarning('Julia Client: Rename Refactor', {
description: result.warning,
dismissable: true
})
}
if (result.error) {
atom.notifications.addError('Julia Client: Rename Refactor', {
description: result.error,
dismissable: true
})
}
})
}).catch((err) => {
if (err) console.error(err)
})
}
}
export default new Refactor()