|
| 1 | +'use babel' |
| 2 | + |
| 3 | +import { client } from '../connection' |
| 4 | +import modules from './modules' |
| 5 | +import { wordRegex, getWord, isValidWordToInspect } from '../misc/words' |
| 6 | +import { getLocalContext } from '../misc/blocks' |
| 7 | + |
| 8 | +const { refactor: _refactor } = client.import(['refactor']) |
| 9 | + |
| 10 | +class Refactor { |
| 11 | + activate (ink) { |
| 12 | + this.ink = ink |
| 13 | + } |
| 14 | + |
| 15 | + refactor () { |
| 16 | + const editor = atom.workspace.getActiveTextEditor() |
| 17 | + const bufferPosition = editor.getCursorBufferPosition() |
| 18 | + |
| 19 | + if (!client.isActive()) return |
| 20 | + |
| 21 | + const { word: old } = getWord(editor, bufferPosition) |
| 22 | + if (!isValidWordToInspect(old)) return |
| 23 | + |
| 24 | + this.ink.showBasicModal([{ |
| 25 | + name: 'Rename', |
| 26 | + defaultText: old, |
| 27 | + message: `Enter an new name to which \`${old}\` will be renamed.` |
| 28 | + }]).then(items => { |
| 29 | + // check the new name is a valid identifier |
| 30 | + const _new = items['Rename'] |
| 31 | + if (!isValidWordToInspect(_new) || _new.match(wordRegex) != _new) { |
| 32 | + atom.notifications.addWarning('Julia Client: Rename Refactor', { |
| 33 | + description: `\`${_new}\` is not a valid identifier` |
| 34 | + }) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + // local context |
| 39 | + const { column, row } = bufferPosition |
| 40 | + const { range, context, startRow } = getLocalContext(editor, row) |
| 41 | + |
| 42 | + // module context |
| 43 | + const currentModule = modules.current() |
| 44 | + const mod = currentModule ? currentModule : 'Main' |
| 45 | + |
| 46 | + _refactor({ |
| 47 | + old, |
| 48 | + new: _new, |
| 49 | + path: editor.getPath(), |
| 50 | + // local context |
| 51 | + column: column + 1, |
| 52 | + row: row + 1, |
| 53 | + startRow, |
| 54 | + context, |
| 55 | + // module context |
| 56 | + mod, |
| 57 | + }).then(result => { |
| 58 | + if (result.error) { |
| 59 | + atom.notifications.addWarning('Julia Client: Rename Refactor', { |
| 60 | + description: result.error |
| 61 | + }) |
| 62 | + return |
| 63 | + } |
| 64 | + // local refactoring |
| 65 | + if (result.text) { |
| 66 | + editor.setTextInBufferRange(range, result.text) |
| 67 | + } |
| 68 | + }) |
| 69 | + }).catch((err) => { |
| 70 | + if (err) console.error(err) |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export default new Refactor() |
0 commit comments