/ src / register-default-commands.coffee
register-default-commands.coffee
  1  {ipcRenderer} = require 'electron'
  2  Grim = require 'grim'
  3  
  4  module.exports = ({commandRegistry, commandInstaller, config, notificationManager, project, clipboard}) ->
  5    commandRegistry.add(
  6      'atom-workspace',
  7      {
  8        'pane:show-next-recently-used-item': -> @getModel().getActivePane().activateNextRecentlyUsedItem()
  9        'pane:show-previous-recently-used-item': -> @getModel().getActivePane().activatePreviousRecentlyUsedItem()
 10        'pane:move-active-item-to-top-of-stack': -> @getModel().getActivePane().moveActiveItemToTopOfStack()
 11        'pane:show-next-item': -> @getModel().getActivePane().activateNextItem()
 12        'pane:show-previous-item': -> @getModel().getActivePane().activatePreviousItem()
 13        'pane:show-item-1': -> @getModel().getActivePane().activateItemAtIndex(0)
 14        'pane:show-item-2': -> @getModel().getActivePane().activateItemAtIndex(1)
 15        'pane:show-item-3': -> @getModel().getActivePane().activateItemAtIndex(2)
 16        'pane:show-item-4': -> @getModel().getActivePane().activateItemAtIndex(3)
 17        'pane:show-item-5': -> @getModel().getActivePane().activateItemAtIndex(4)
 18        'pane:show-item-6': -> @getModel().getActivePane().activateItemAtIndex(5)
 19        'pane:show-item-7': -> @getModel().getActivePane().activateItemAtIndex(6)
 20        'pane:show-item-8': -> @getModel().getActivePane().activateItemAtIndex(7)
 21        'pane:show-item-9': -> @getModel().getActivePane().activateLastItem()
 22        'pane:move-item-right': -> @getModel().getActivePane().moveItemRight()
 23        'pane:move-item-left': -> @getModel().getActivePane().moveItemLeft()
 24        'window:increase-font-size': -> @getModel().increaseFontSize()
 25        'window:decrease-font-size': -> @getModel().decreaseFontSize()
 26        'window:reset-font-size': -> @getModel().resetFontSize()
 27        'application:about': -> ipcRenderer.send('command', 'application:about')
 28        'application:show-preferences': -> ipcRenderer.send('command', 'application:show-settings')
 29        'application:show-settings': -> ipcRenderer.send('command', 'application:show-settings')
 30        'application:quit': -> ipcRenderer.send('command', 'application:quit')
 31        'application:hide': -> ipcRenderer.send('command', 'application:hide')
 32        'application:hide-other-applications': -> ipcRenderer.send('command', 'application:hide-other-applications')
 33        'application:install-update': -> ipcRenderer.send('command', 'application:install-update')
 34        'application:unhide-all-applications': -> ipcRenderer.send('command', 'application:unhide-all-applications')
 35        'application:new-window': -> ipcRenderer.send('command', 'application:new-window')
 36        'application:new-file': -> ipcRenderer.send('command', 'application:new-file')
 37        'application:open': ->
 38          defaultPath = atom.workspace.getActiveTextEditor()?.getPath() ? atom.project.getPaths()?[0]
 39          ipcRenderer.send('open-chosen-any', defaultPath)
 40        'application:open-file': ->
 41          defaultPath = atom.workspace.getActiveTextEditor()?.getPath() ? atom.project.getPaths()?[0]
 42          ipcRenderer.send('open-chosen-file', defaultPath)
 43        'application:open-folder': ->
 44          defaultPath = atom.workspace.getActiveTextEditor()?.getPath() ? atom.project.getPaths()?[0]
 45          ipcRenderer.send('open-chosen-folder', defaultPath)
 46        'application:open-dev': -> ipcRenderer.send('command', 'application:open-dev')
 47        'application:open-safe': -> ipcRenderer.send('command', 'application:open-safe')
 48        'application:add-project-folder': -> atom.addProjectFolder()
 49        'application:minimize': -> ipcRenderer.send('command', 'application:minimize')
 50        'application:zoom': -> ipcRenderer.send('command', 'application:zoom')
 51        'application:bring-all-windows-to-front': -> ipcRenderer.send('command', 'application:bring-all-windows-to-front')
 52        'application:open-your-config': -> ipcRenderer.send('command', 'application:open-your-config')
 53        'application:open-your-init-script': -> ipcRenderer.send('command', 'application:open-your-init-script')
 54        'application:open-your-keymap': -> ipcRenderer.send('command', 'application:open-your-keymap')
 55        'application:open-your-snippets': -> ipcRenderer.send('command', 'application:open-your-snippets')
 56        'application:open-your-stylesheet': -> ipcRenderer.send('command', 'application:open-your-stylesheet')
 57        'application:open-license': -> @getModel().openLicense()
 58        'window:run-package-specs': -> @runPackageSpecs()
 59        'window:run-benchmarks': -> @runBenchmarks()
 60        'window:toggle-left-dock': -> @getModel().getLeftDock().toggle()
 61        'window:toggle-right-dock': -> @getModel().getRightDock().toggle()
 62        'window:toggle-bottom-dock': -> @getModel().getBottomDock().toggle()
 63        'window:focus-next-pane': -> @getModel().activateNextPane()
 64        'window:focus-previous-pane': -> @getModel().activatePreviousPane()
 65        'window:focus-pane-above': -> @focusPaneViewAbove()
 66        'window:focus-pane-below': -> @focusPaneViewBelow()
 67        'window:focus-pane-on-left': -> @focusPaneViewOnLeft()
 68        'window:focus-pane-on-right': -> @focusPaneViewOnRight()
 69        'window:move-active-item-to-pane-above': -> @moveActiveItemToPaneAbove()
 70        'window:move-active-item-to-pane-below': -> @moveActiveItemToPaneBelow()
 71        'window:move-active-item-to-pane-on-left': -> @moveActiveItemToPaneOnLeft()
 72        'window:move-active-item-to-pane-on-right': -> @moveActiveItemToPaneOnRight()
 73        'window:copy-active-item-to-pane-above': -> @moveActiveItemToPaneAbove(keepOriginal: true)
 74        'window:copy-active-item-to-pane-below': -> @moveActiveItemToPaneBelow(keepOriginal: true)
 75        'window:copy-active-item-to-pane-on-left': -> @moveActiveItemToPaneOnLeft(keepOriginal: true)
 76        'window:copy-active-item-to-pane-on-right': -> @moveActiveItemToPaneOnRight(keepOriginal: true)
 77        'window:save-all': -> @getModel().saveAll()
 78        'window:toggle-invisibles': -> config.set("editor.showInvisibles", not config.get("editor.showInvisibles"))
 79        'window:log-deprecation-warnings': -> Grim.logDeprecations()
 80        'window:toggle-auto-indent': -> config.set("editor.autoIndent", not config.get("editor.autoIndent"))
 81        'pane:reopen-closed-item': -> @getModel().reopenItem()
 82        'core:close': -> @getModel().closeActivePaneItemOrEmptyPaneOrWindow()
 83        'core:save': -> @getModel().saveActivePaneItem()
 84        'core:save-as': -> @getModel().saveActivePaneItemAs()
 85      },
 86      false
 87    )
 88  
 89  
 90    if process.platform is 'darwin'
 91      commandRegistry.add(
 92        'atom-workspace',
 93        'window:install-shell-commands',
 94        (-> commandInstaller.installShellCommandsInteractively()),
 95        false
 96      )
 97  
 98    commandRegistry.add(
 99      'atom-pane',
100      {
101        'pane:save-items': -> @getModel().saveItems()
102        'pane:split-left': -> @getModel().splitLeft()
103        'pane:split-right': -> @getModel().splitRight()
104        'pane:split-up': -> @getModel().splitUp()
105        'pane:split-down': -> @getModel().splitDown()
106        'pane:split-left-and-copy-active-item': -> @getModel().splitLeft(copyActiveItem: true)
107        'pane:split-right-and-copy-active-item': -> @getModel().splitRight(copyActiveItem: true)
108        'pane:split-up-and-copy-active-item': -> @getModel().splitUp(copyActiveItem: true)
109        'pane:split-down-and-copy-active-item': -> @getModel().splitDown(copyActiveItem: true)
110        'pane:split-left-and-move-active-item': -> @getModel().splitLeft(moveActiveItem: true)
111        'pane:split-right-and-move-active-item': -> @getModel().splitRight(moveActiveItem: true)
112        'pane:split-up-and-move-active-item': -> @getModel().splitUp(moveActiveItem: true)
113        'pane:split-down-and-move-active-item': -> @getModel().splitDown(moveActiveItem: true)
114        'pane:close': -> @getModel().close()
115        'pane:close-other-items': -> @getModel().destroyInactiveItems()
116        'pane:increase-size': -> @getModel().increaseSize()
117        'pane:decrease-size': -> @getModel().decreaseSize()
118      },
119      false
120    )
121  
122    commandRegistry.add(
123      'atom-text-editor',
124      stopEventPropagation({
125        'core:move-left': -> @moveLeft()
126        'core:move-right': -> @moveRight()
127        'core:select-left': -> @selectLeft()
128        'core:select-right': -> @selectRight()
129        'core:select-up': -> @selectUp()
130        'core:select-down': -> @selectDown()
131        'core:select-all': -> @selectAll()
132        'editor:select-word': -> @selectWordsContainingCursors()
133        'editor:consolidate-selections': (event) -> event.abortKeyBinding() unless @consolidateSelections()
134        'editor:move-to-beginning-of-next-paragraph': -> @moveToBeginningOfNextParagraph()
135        'editor:move-to-beginning-of-previous-paragraph': -> @moveToBeginningOfPreviousParagraph()
136        'editor:move-to-beginning-of-screen-line': -> @moveToBeginningOfScreenLine()
137        'editor:move-to-beginning-of-line': -> @moveToBeginningOfLine()
138        'editor:move-to-end-of-screen-line': -> @moveToEndOfScreenLine()
139        'editor:move-to-end-of-line': -> @moveToEndOfLine()
140        'editor:move-to-first-character-of-line': -> @moveToFirstCharacterOfLine()
141        'editor:move-to-beginning-of-word': -> @moveToBeginningOfWord()
142        'editor:move-to-end-of-word': -> @moveToEndOfWord()
143        'editor:move-to-beginning-of-next-word': -> @moveToBeginningOfNextWord()
144        'editor:move-to-previous-word-boundary': -> @moveToPreviousWordBoundary()
145        'editor:move-to-next-word-boundary': -> @moveToNextWordBoundary()
146        'editor:move-to-previous-subword-boundary': -> @moveToPreviousSubwordBoundary()
147        'editor:move-to-next-subword-boundary': -> @moveToNextSubwordBoundary()
148        'editor:select-to-beginning-of-next-paragraph': -> @selectToBeginningOfNextParagraph()
149        'editor:select-to-beginning-of-previous-paragraph': -> @selectToBeginningOfPreviousParagraph()
150        'editor:select-to-end-of-line': -> @selectToEndOfLine()
151        'editor:select-to-beginning-of-line': -> @selectToBeginningOfLine()
152        'editor:select-to-end-of-word': -> @selectToEndOfWord()
153        'editor:select-to-beginning-of-word': -> @selectToBeginningOfWord()
154        'editor:select-to-beginning-of-next-word': -> @selectToBeginningOfNextWord()
155        'editor:select-to-next-word-boundary': -> @selectToNextWordBoundary()
156        'editor:select-to-previous-word-boundary': -> @selectToPreviousWordBoundary()
157        'editor:select-to-next-subword-boundary': -> @selectToNextSubwordBoundary()
158        'editor:select-to-previous-subword-boundary': -> @selectToPreviousSubwordBoundary()
159        'editor:select-to-first-character-of-line': -> @selectToFirstCharacterOfLine()
160        'editor:select-line': -> @selectLinesContainingCursors()
161        'editor:select-larger-syntax-node': -> @selectLargerSyntaxNode()
162        'editor:select-smaller-syntax-node': -> @selectSmallerSyntaxNode()
163      }),
164      false
165    )
166  
167    commandRegistry.add(
168      'atom-text-editor:not([readonly])',
169      stopEventPropagation({
170        'core:undo': -> @undo()
171        'core:redo': -> @redo()
172      }),
173      false
174    )
175  
176    commandRegistry.add(
177      'atom-text-editor',
178      stopEventPropagationAndGroupUndo(
179        config,
180        {
181          'core:copy': -> @copySelectedText()
182          'editor:copy-selection': -> @copyOnlySelectedText()
183        }
184      ),
185      false
186    )
187  
188    commandRegistry.add(
189      'atom-text-editor:not([readonly])',
190      stopEventPropagationAndGroupUndo(
191        config,
192        {
193          'core:backspace': -> @backspace()
194          'core:delete': -> @delete()
195          'core:cut': -> @cutSelectedText()
196          'core:paste': -> @pasteText()
197          'editor:paste-without-reformatting': -> @pasteText({
198            normalizeLineEndings: false,
199            autoIndent: false,
200            preserveTrailingLineIndentation: true
201          })
202          'editor:delete-to-previous-word-boundary': -> @deleteToPreviousWordBoundary()
203          'editor:delete-to-next-word-boundary': -> @deleteToNextWordBoundary()
204          'editor:delete-to-beginning-of-word': -> @deleteToBeginningOfWord()
205          'editor:delete-to-beginning-of-line': -> @deleteToBeginningOfLine()
206          'editor:delete-to-end-of-line': -> @deleteToEndOfLine()
207          'editor:delete-to-end-of-word': -> @deleteToEndOfWord()
208          'editor:delete-to-beginning-of-subword': -> @deleteToBeginningOfSubword()
209          'editor:delete-to-end-of-subword': -> @deleteToEndOfSubword()
210          'editor:delete-line': -> @deleteLine()
211          'editor:cut-to-end-of-line': -> @cutToEndOfLine()
212          'editor:cut-to-end-of-buffer-line': -> @cutToEndOfBufferLine()
213          'editor:transpose': -> @transpose()
214          'editor:upper-case': -> @upperCase()
215          'editor:lower-case': -> @lowerCase()
216        }
217      ),
218      false
219    )
220  
221    commandRegistry.add(
222      'atom-text-editor:not([mini])',
223      stopEventPropagation({
224        'core:move-up': -> @moveUp()
225        'core:move-down': -> @moveDown()
226        'core:move-to-top': -> @moveToTop()
227        'core:move-to-bottom': -> @moveToBottom()
228        'core:page-up': -> @pageUp()
229        'core:page-down': -> @pageDown()
230        'core:select-to-top': -> @selectToTop()
231        'core:select-to-bottom': -> @selectToBottom()
232        'core:select-page-up': -> @selectPageUp()
233        'core:select-page-down': -> @selectPageDown()
234        'editor:add-selection-below': -> @addSelectionBelow()
235        'editor:add-selection-above': -> @addSelectionAbove()
236        'editor:split-selections-into-lines': -> @splitSelectionsIntoLines()
237        'editor:toggle-soft-tabs': -> @toggleSoftTabs()
238        'editor:toggle-soft-wrap': -> @toggleSoftWrapped()
239        'editor:fold-all': -> @foldAll()
240        'editor:unfold-all': -> @unfoldAll()
241        'editor:fold-current-row': ->
242          @foldCurrentRow()
243          @scrollToCursorPosition()
244        'editor:unfold-current-row': ->
245          @unfoldCurrentRow()
246          @scrollToCursorPosition()
247        'editor:fold-selection': -> @foldSelectedLines()
248        'editor:fold-at-indent-level-1': ->
249          @foldAllAtIndentLevel(0)
250          @scrollToCursorPosition()
251        'editor:fold-at-indent-level-2': ->
252          @foldAllAtIndentLevel(1)
253          @scrollToCursorPosition()
254        'editor:fold-at-indent-level-3': ->
255          @foldAllAtIndentLevel(2)
256          @scrollToCursorPosition()
257        'editor:fold-at-indent-level-4': ->
258          @foldAllAtIndentLevel(3)
259          @scrollToCursorPosition()
260        'editor:fold-at-indent-level-5': ->
261          @foldAllAtIndentLevel(4)
262          @scrollToCursorPosition()
263        'editor:fold-at-indent-level-6': ->
264          @foldAllAtIndentLevel(5)
265          @scrollToCursorPosition()
266        'editor:fold-at-indent-level-7': ->
267          @foldAllAtIndentLevel(6)
268          @scrollToCursorPosition()
269        'editor:fold-at-indent-level-8': ->
270          @foldAllAtIndentLevel(7)
271          @scrollToCursorPosition()
272        'editor:fold-at-indent-level-9': ->
273          @foldAllAtIndentLevel(8)
274          @scrollToCursorPosition()
275        'editor:log-cursor-scope': -> showCursorScope(@getCursorScope(), notificationManager)
276        'editor:log-cursor-syntax-tree-scope': -> showSyntaxTree(@getCursorSyntaxTreeScope(), notificationManager)
277        'editor:copy-path': -> copyPathToClipboard(this, project, clipboard, false)
278        'editor:copy-project-path': -> copyPathToClipboard(this, project, clipboard, true)
279        'editor:toggle-indent-guide': -> config.set('editor.showIndentGuide', not config.get('editor.showIndentGuide'))
280        'editor:toggle-line-numbers': -> config.set('editor.showLineNumbers', not config.get('editor.showLineNumbers'))
281        'editor:scroll-to-cursor': -> @scrollToCursorPosition()
282      }),
283      false
284    )
285  
286    commandRegistry.add(
287      'atom-text-editor:not([mini]):not([readonly])',
288      stopEventPropagationAndGroupUndo(
289        config,
290        {
291          'editor:indent': -> @indent()
292          'editor:auto-indent': -> @autoIndentSelectedRows()
293          'editor:indent-selected-rows': -> @indentSelectedRows()
294          'editor:outdent-selected-rows': -> @outdentSelectedRows()
295          'editor:newline': -> @insertNewline()
296          'editor:newline-below': -> @insertNewlineBelow()
297          'editor:newline-above': -> @insertNewlineAbove()
298          'editor:toggle-line-comments': -> @toggleLineCommentsInSelection()
299          'editor:checkout-head-revision': -> atom.workspace.checkoutHeadRevision(this)
300          'editor:move-line-up': -> @moveLineUp()
301          'editor:move-line-down': -> @moveLineDown()
302          'editor:move-selection-left': -> @moveSelectionLeft()
303          'editor:move-selection-right': -> @moveSelectionRight()
304          'editor:duplicate-lines': -> @duplicateLines()
305          'editor:join-lines': -> @joinLines()
306        }
307      ),
308      false
309    )
310  
311  stopEventPropagation = (commandListeners) ->
312    newCommandListeners = {}
313    for commandName, commandListener of commandListeners
314      do (commandListener) ->
315        newCommandListeners[commandName] = (event) ->
316          event.stopPropagation()
317          commandListener.call(@getModel(), event)
318    newCommandListeners
319  
320  stopEventPropagationAndGroupUndo = (config, commandListeners) ->
321    newCommandListeners = {}
322    for commandName, commandListener of commandListeners
323      do (commandListener) ->
324        newCommandListeners[commandName] = (event) ->
325          event.stopPropagation()
326          model = @getModel()
327          model.transact model.getUndoGroupingInterval(), ->
328            commandListener.call(model, event)
329    newCommandListeners
330  
331  showCursorScope = (descriptor, notificationManager) ->
332    list = descriptor.scopes.toString().split(',')
333    list = list.map (item) -> "* #{item}"
334    content = "Scopes at Cursor\n#{list.join('\n')}"
335  
336    notificationManager.addInfo(content, dismissable: true)
337  
338  showSyntaxTree = (descriptor, notificationManager) ->
339    list = descriptor.scopes.toString().split(',')
340    list = list.map (item) -> "* #{item}"
341    content = "Syntax tree at Cursor\n#{list.join('\n')}"
342  
343    notificationManager.addInfo(content, dismissable: true)
344  
345  copyPathToClipboard = (editor, project, clipboard, relative) ->
346    if filePath = editor.getPath()
347      filePath = project.relativize(filePath) if relative
348      clipboard.write(filePath)