Skip to content

Commit de7c355

Browse files
committed
Switch between controller/component/template/route
1 parent 8e79955 commit de7c355

File tree

3 files changed

+117
-7
lines changed

3 files changed

+117
-7
lines changed

keymaps/ember-cli-helper.cson

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
'atom-workspace':
1111
'ctrl-alt-o': 'ember-cli-helper:toggle'
1212
'ctrl-alt-g': 'ember-cli-helper:generate-file'
13+
'ctrl-alt-e': 'ember-cli-helper:switch-file'
14+
'ctrl-alt-r': 'ember-cli-helper:switch-route'

lib/ember-cli-helper-view.coffee

+112-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{Task, BufferedProcess} = require 'atom'
22
{View} = require 'atom-space-pen-views'
33
GeneratorListView = require './generator-list-view'
4+
path = require 'path'
5+
fs = require 'fs'
46

57
module.exports =
68
class EmberCliHelperView extends View
@@ -9,19 +11,23 @@ class EmberCliHelperView extends View
911
@div class: 'ember-cli-helper tool-panel panel-bottom native-key-bindings', =>
1012
@div class: 'ember-cli-btn-group', =>
1113
@div class: 'block', =>
12-
@button outlet: 'server', click: 'startServer', class: 'btn btn-sm inline-block-tight', 'Server'
13-
@button outlet: 'test', click: 'startTesting', class: 'btn btn-sm inline-block-tight', 'Test'
14-
@button outlet: 'generate', click: 'showGeneratorList', class: 'btn btn-sm inline-block-tight', 'Generate'
15-
@button outlet: 'exit', click: 'stopProcess', class: 'btn btn-sm inline-block-tight', 'Exit'
16-
@button outlet: 'hide', click: 'toggle', class: 'btn btn-sm inline-block-tight btn-right', 'Close'
17-
@button outlet: 'mini', click: 'minimize', class: 'btn btn-sm inline-block-tight btn-right', 'Minimize'
14+
@button outlet: 'switch', click: 'switchFile', class: 'btn btn-sm inline-block-tight', 'c/t'
15+
@button outlet: 'route', click: 'switchRoute', class: 'btn btn-sm inline-block-tight', 'r'
16+
@button outlet: 'server', click: 'startServer', class: 'btn btn-sm inline-block-tight', 'Server'
17+
@button outlet: 'test', click: 'startTesting', class: 'btn btn-sm inline-block-tight', 'Test'
18+
@button outlet: 'generate', click: 'showGeneratorList', class: 'btn btn-sm inline-block-tight', 'Generate'
19+
@button outlet: 'exit', click: 'stopProcess', class: 'btn btn-sm inline-block-tight', 'Exit'
20+
@button outlet: 'hide', click: 'toggle', class: 'btn btn-sm inline-block-tight btn-right', 'Close'
21+
@button outlet: 'mini', click: 'minimize', class: 'btn btn-sm inline-block-tight btn-right', 'Minimize'
1822
@div outlet: 'panel', class: 'panel-body padded hidden', =>
1923
@ul outlet: 'messages', class: 'list-group'
2024

2125
initialize: ->
2226
# Register Commands
2327
atom.commands.add 'atom-text-editor',
2428
"ember-cli-helper:toggle": => @toggle()
29+
"ember-cli-helper:switch-file": => @switchFile()
30+
"ember-cli-helper:switch-route": => @switchRoute()
2531
"ember-cli-helper:generate-file": => @showGeneratorList()
2632

2733
# Add the path to the Node executable to the $PATH
@@ -66,6 +72,106 @@ class EmberCliHelperView extends View
6672
minimize: ->
6773
@panel.toggleClass 'hidden'
6874

75+
getPathComponents: ->
76+
separator = path.sep
77+
editor = atom.workspace.getActivePaneItem()
78+
file = editor?.buffer.file
79+
fullPath = file?.getPath()
80+
fileName = file?.getBaseName()
81+
82+
# must have a file with an extension under /app/*
83+
return [] if fileName.indexOf('.') == -1 || fullPath.split(separator).indexOf('app') == -1
84+
85+
extension = path.extname(fileName)
86+
87+
# TODO: choose only the last "app" folder
88+
pathUntilApp = fullPath.split(separator+'app'+separator)[0] + separator + 'app' + separator
89+
pathInApp = fullPath.split(separator+'app'+separator)[1].split(separator)
90+
pathInApp.pop()
91+
92+
[pathUntilApp, pathInApp, fileName, extension]
93+
94+
switchFile: ->
95+
[pathUntilApp, paths, fileName, extension] = @getPathComponents()
96+
return unless pathUntilApp
97+
98+
separator = path.sep
99+
goodPaths = []
100+
101+
# script to template
102+
if extension == '.coffee' || extension == '.js'
103+
newFileName = fileName.replace(/\.(js|coffee)$/, '.hbs')
104+
105+
# components/*.js -> templates/components/*.hbs
106+
if paths[0] == 'components'
107+
goodPaths.push ["templates"].concat(paths).concat([newFileName]).join(separator)
108+
109+
# controllers/*.js -> templates/*.hbs
110+
# routes/*.js -> templates/*.hbs
111+
else if paths[0] == 'controllers' || paths[0] == 'routes'
112+
paths.shift()
113+
goodPaths.push ["templates"].concat(paths).concat([newFileName]).join(separator)
114+
115+
# template to script
116+
else if extension == '.hbs'
117+
newFileNameJs = fileName.replace(/\.hbs$/, '.js')
118+
newFileNameCoffee = fileName.replace(/\.hbs$/, '.coffee')
119+
120+
# templates/components/*.hbs -> components/*.js
121+
if paths[0] == 'templates' && paths[1] == 'components'
122+
paths.shift()
123+
goodPaths.push paths.concat([newFileNameJs]).join(separator)
124+
goodPaths.push paths.concat([newFileNameCoffee]).join(separator)
125+
126+
# templates/xyz/*.hbz -> controllers/
127+
else
128+
paths.shift()
129+
goodPaths.push ["controllers"].concat(paths).concat([newFileNameJs]).join(separator)
130+
goodPaths.push ["controllers"].concat(paths).concat([newFileNameCoffee]).join(separator)
131+
132+
@openBestMatch(pathUntilApp, goodPaths)
133+
134+
switchRoute: ->
135+
[pathUntilApp, paths, fileName, extension] = @getPathComponents()
136+
return unless pathUntilApp
137+
138+
separator = path.sep
139+
goodPaths = []
140+
141+
newFileNameJs = fileName.replace(/\.(js|coffee|hbs)$/, '.js')
142+
newFileNameCoffee = fileName.replace(/\.(js|coffee|hbs)$/, '.coffee')
143+
144+
# script to template
145+
if extension == '.coffee' || extension == '.js'
146+
# routes/*.js -> controllers/*.js
147+
if paths[0] == 'routes'
148+
paths.shift()
149+
goodPaths.push ["controllers"].concat(paths).concat([newFileNameJs]).join(separator)
150+
goodPaths.push ["controllers"].concat(paths).concat([newFileNameCoffee]).join(separator)
151+
152+
# controllers/*.js -> routes/*.js
153+
else if paths[0] == 'controllers'
154+
paths.shift()
155+
goodPaths.push ["routes"].concat(paths).concat([newFileNameJs]).join(separator)
156+
goodPaths.push ["routes"].concat(paths).concat([newFileNameCoffee]).join(separator)
157+
158+
# template to script
159+
else if extension == '.hbs'
160+
# templates/(!components/)*.hbs -> routes/*.js
161+
if paths[0] == 'templates' && paths[1] != 'components'
162+
paths.shift()
163+
goodPaths.push ["routes"].concat(paths).concat([newFileNameJs]).join(separator)
164+
goodPaths.push ["routes"].concat(paths).concat([newFileNameCoffee]).join(separator)
165+
166+
@openBestMatch(pathUntilApp, goodPaths)
167+
168+
openBestMatch: (pathUntilApp, goodPaths) ->
169+
legitPaths = goodPaths.filter (pathToTry) =>
170+
fs.existsSync(pathUntilApp + pathToTry)
171+
172+
bestPath = legitPaths[0] || goodPaths[0]
173+
if bestPath
174+
atom.workspace.open(pathUntilApp + bestPath)
69175

70176
startServer: ->
71177
@runCommand 'Ember CLI Server Started'.fontcolor("green"), 'server'

menus/ember-cli-helper.cson

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
'label': 'Ember CLI'
77
'submenu': [
88
{ 'label': 'Toggle Panel', 'command': 'ember-cli-helper:toggle' },
9-
{ 'label': 'Generate Files', 'command': 'ember-cl-helper:generate-file'}
9+
{ 'label': 'Generate Files', 'command': 'ember-cl-helper:generate-file'},
10+
{ 'label': 'Switch between .js/.hbs', 'command': 'ember-cli-helper:switch-file' },
11+
{ 'label': 'Switch between route/controller', 'command': 'ember-cli-helper:switch-route' },
1012
]
1113
]
1214
}

0 commit comments

Comments
 (0)