Skip to content

Commit 236b386

Browse files
committed
basic setup of rename-refactor command
1 parent 2e923b1 commit 236b386

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

lib/misc/blocks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export function getLocalContext (editor, row) {
132132
const context = range ? editor.getTextInBufferRange(range) : ''
133133
const startRow = range ? range[0][0] : null
134134
return {
135+
range,
135136
context,
136137
startRow
137138
}

lib/package/commands.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ module.exports =
5252
@withInk ->
5353
boot()
5454
juno.runtime.evaluation.toggleDocs()
55+
'julia-client:rename-refactor': =>
56+
@withInk ->
57+
boot()
58+
juno.runtime.refactor.refactor()
5559
# @NOTE: `'clear-workspace'` is now not handled by Atom.jl
5660
# 'julia-client:reset-workspace': =>
5761
# requireClient 'reset the workspace', ->

lib/runtime.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports =
1515
debuginfo: require './runtime/debuginfo'
1616
formatter: require './runtime/formatter'
1717
goto: require './runtime/goto'
18+
refactor: require './runtime/refactor'
1819

1920
activate: ->
2021
@subs = new CompositeDisposable()
@@ -29,7 +30,7 @@ module.exports =
2930
consumeInk: (ink) ->
3031
@evaluation.ink = ink
3132
@frontend.ink = ink
32-
for mod in [@console, @debugger, @profiler, @linter, @goto, @outline]
33+
for mod in [@console, @debugger, @profiler, @linter, @goto, @outline, @refactor]
3334
mod.activate(ink)
3435
for mod in [@workspace, @plots]
3536
mod.ink = ink

lib/runtime/refactor.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)