├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── keymaps └── format.cson ├── lib ├── config.coffee ├── format.coffee ├── not-supported-view.coffee └── observer.coffee ├── menus └── format.cson ├── package.json └── spec ├── format-spec.coffee └── specfiles └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | npm-debug.log 3 | node_modules 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | #[0.9.3](4d757524c86bdb79412ad88bfe0bdd9cfa0ec4e3) 2 | 3 | * Fix undefined editor error (#69) 4 | 5 | #[0.9.2](9e8566eef41084727a93f58f3a613503bff5a06e) 6 | 7 | * Fix indentation error 8 | 9 | #[0.9.1](10e6187d593c9e47e62c83d9f5c4eea0789e72cc) 10 | 11 | * Fix Unsupported Language Notification using deprecated API 12 | 13 | #[0.9.0](34060f0b30a406c7d2baac954da8a124fa51fd72) 14 | 15 | * Fix 1.0 API deprecations 16 | * Add JS Beautify comma first option 17 | * Add support for other JavaScript syntax highlighting grammars 18 | 19 | #[0.8.1](9da3be4f1e09cf2abcb34d2fa5410c1ce94071fc) 20 | 21 | * Fix CSON format deprecation 22 | 23 | #[0.8.0](29c3e5c7c92198e66b0980f9287b6c0607346d23) 24 | 25 | * Fix deprecation issues with Atom 1.0 API 26 | 27 | #[0.7.18](2b6854304a0b32514608c71763105a8e63478ed9) 28 | 29 | * Indentation style is now based on editor setting and not JSFormat's setting 30 | 31 | #[0.7.17](7381fa3ba031aeaf0e74f1542b10130176ae5eb2) 32 | 33 | * Code cleanup 34 | 35 | #[0.7.16](995fa1da818b2c195d89a6325606168f77501ed0) 36 | 37 | * Ignore files option to ignore files like `.jshintrc` 38 | 39 | #[0.7.15](f480d7ba6453dd7d3a10c3bcbe51421e75caed0f) 40 | 41 | * Fixed error on preferences page 42 | * Small performance optimizations when formatting text 43 | 44 | #[0.7.14](21007152bf3e60990fe38fb22f01f85f41f6782c) 45 | 46 | * Possible fix for `cannot dispose of undefined` error (UPDATE: Not fixed) 47 | 48 | #[0.7.13](1039677dd3e59200b8dacea75f15b85ea0cd0800) 49 | 50 | * Fixed keybindings not working 51 | * Fixed package not being labeled properly in command palette 52 | 53 | #[0.7.12](949eb2305ea52c5836c08b2c034c0d4ff7d151cb) 54 | 55 | * Fixed some deprecation messages 56 | * Updated command palette info 57 | 58 | #[0.7.11](28f0093925f28302390edff166aeb63e584834ee) 59 | 60 | * Performance improvements 61 | * Checking file language before doing anything else 62 | * checking beautified text against current editor text 63 | * Better unsupported language message 64 | 65 | #[0.7.10](63b0131bb3dc0ce1ff63da4e8c7c04a167ab6c2c) 66 | 67 | * Updated dependencies 68 | * [atom/event-kit](https://github.com/atom/event-kit) 0.7.2 -> 0.8.0 69 | 70 | #[0.7.9](3eaff5c7a0fd8ca0cf707a9128d6268f9928a6de) 71 | 72 | * Smarter Format on save 73 | 74 | # [0.7.8](140ace97c44f2b3343dd80a8fa34ed5437c121a2) 75 | 76 | * Added `end_with_newline` setting. 77 | 78 | # [0.7.7](357ee8761a801f505305fadd475a13255eb18b73) 79 | 80 | * Fixed regression where closing a tab would throw an error 81 | 82 | # [0.7.6](212aace57a36176386dd9891a2a23e7de712bb5f) 83 | 84 | * Updated JS-Beautify to 1.5.4 85 | * Better descriptions in the config page 86 | * More tests 87 | 88 | # [0.7.5](330b5cbc654618e142713a95c0147b0ca117fa68) 89 | 90 | * Better config page 91 | * More tests 92 | 93 | # [0.7.4](b42275f887278b6df0566811056e989007cdc645) 94 | 95 | * Fixed error in tests, all current tests should pass 96 | 97 | # [0.7.3](edf6256095dcc340511375cb60abd369767aa279) 98 | 99 | * Made retrieving package config simpler and faster 100 | 101 | # [0.7.2](4df0961a82b4b3f9c6c5da41f51a0d7716520869) 102 | 103 | * Updated JS-Beautify to 1.5.3 104 | 105 | # [0.7.1](feec74e518a1cee789d3b3131c493e3786fecd19) 106 | 107 | * Added support for JS-Beautify's `space_after_anon_function` option. 108 | 109 | # [0.7.0](30d27619df09947825944d17ed3fa8bd5ea373ec) 110 | 111 | * Switched to Atom's new v0.133.0 config schema 112 | 113 | # [0.6.7](52339804cd2690f9d2279b1bd93ca459490d38f3) 114 | 115 | * Fixed Observer class not being exported 116 | * Started working on using the Observer class 117 | 118 | # [0.6.6](9a61e537c06e2ffe19a83a86299f2b043c89a7da) 119 | 120 | * Moved to workspace model API to prevent deprecation warnings about workspaceView API 121 | 122 | # [0.6.5](6c9e291b8a1c923c86973168f44412d9dcf38064) 123 | 124 | * Cursor repositioning now works a lot better 125 | 126 | # [0.6.4](98df7a37ad25dd059f61014efa53e9e7a7386fc6) 127 | 128 | * Fixed error in cursor position fix 129 | 130 | # [0.6.3](68cfca3693b1d88c584aba7a862c45ea43de3974) 131 | 132 | * Started working spec files 133 | 134 | # [0.6.2](ecfb6b13b58afdabb7e676d3842c2a098f6eab64) 135 | 136 | * Updated CHANGELOG.md 137 | 138 | # [0.6.0](958e3503d57ad9edc95cdccec57c84b025912038) 139 | 140 | * Cursor position is now positioned more accurately by using its location before formatting the file/selection 141 | * Added a CHANGELOG.md file with all the versions 142 | * Updated README.md with `format_on_save` setting information 143 | 144 | # [0.3.2](fcf5a60ad6979fee7a11c5f49d3590171e7b6aa4) 145 | 146 | * Fixed issue where closing a file would cause Atom to freeze because of extra subscribers from format on save 147 | 148 | # [0.3.0](9583bd2e5e249e3b02e41c5237b53c6ed0f56601) 149 | 150 | * Format on save functionality added 151 | 152 | # [0.2.1](1d4e6c2b3242da7330149c2fbb78a8ee6c514635) 153 | 154 | * Uses editor's tablength to format text 155 | 156 | # [0.2.0](f7b17bb86146d8df9d354fbe70aa8304fd550f0d) 157 | 158 | * Added the ability to format currently selected text 159 | * Now using file's grammar (which can be manually set by user) to decide whether or not to format the selection/file 160 | 161 | # [0.1.0](00a665c9f0f24d3841851b5fb01ecc66b7f71eeb) 162 | 163 | * Added a lot more settings and configuration options 164 | 165 | # [0.0.6](453f4c64fbc18df17a9c0425001878b8be3c8835) 166 | 167 | * Fixed issue when no editors are open 168 | * Fixed not supported view message 169 | 170 | # [0.0.5](2ae80654323d1e5a8eeee3f1d93e75c07043f922) 171 | 172 | * Improved the readme 173 | 174 | # [0.0.4](39d733ffdb379b4aeb9b114015172ea5a698b7b9) 175 | 176 | * No changes, test publish 177 | 178 | # [0.0.3](607832d0df3a5377820e6d76910f00afa66cbbdd) 179 | 180 | * Fixed package.json formatting 181 | 182 | # [0.0.2](ef80454a596dca78db3eb36c5cd2766e4e3206c3) 183 | 184 | * Added keywords to package.json to make package easier to find 185 | 186 | # [0.0.1](d71190e08ec13bc74413db8f3cb31904d49a472c) 187 | 188 | * Initial JSFormat plugin 189 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JsFormat 2 | 3 | JavaScript + JSON formatting/beautification for the Atom text editor. 4 | 5 | #### Keybinding 6 | `ctrl+alt+f` 7 | 8 | #### Command Palette 9 | `Jsformat: Format` 10 | 11 | #### Settings 12 | JsFormat uses whatever tab/indent settings are configured with the standard ```Tab Length``` Atom settings. 13 | 14 | The ```Preferred Line Length``` Atom setting is only respected when the ```Soft Wrap At Preferred Line Length``` Atom setting is enabled. 15 | 16 | By default, JSFormat's Format on save is set to true. 17 | 18 | The following **JsBeautifier** settings are available through the JsFormat settings panel (defaults shown below). Check out the official [jsbeautifier documentation](https://github.com/einars/js-beautify#options) for more details on the options: 19 | 20 | * Indent with tabs: false 21 | * End with newline: true 22 | * Max preserve newlines: 4 23 | * Preserve newlines: true 24 | * Space in parentheses: false 25 | * JSLint happy: false 26 | * Brace style: "collapse" 27 | * Keep array indentation: false 28 | * Keep function indentation: false 29 | * Space after anonymous functions: false 30 | * Space before conditional: true 31 | * Evaluate code: false 32 | * Unescape strings: false 33 | * Break Chained Methods: false 34 | * e4x style: false 35 | -------------------------------------------------------------------------------- /keymaps/format.cson: -------------------------------------------------------------------------------- 1 | # Keybindings require three things to be fully defined: A selector that is 2 | # matched against the focused element, the keystroke and the command to 3 | # execute. 4 | # 5 | # Below is a basic keybinding which registers on all platforms by applying to 6 | # the root workspace element. 7 | 8 | # For more detailed documentation see 9 | # https://atom.io/docs/latest/advanced/keymaps 10 | 'atom-workspace': 11 | 'ctrl-alt-f': 'jsformat:format' 12 | -------------------------------------------------------------------------------- /lib/config.coffee: -------------------------------------------------------------------------------- 1 | # config schema 2 | module.exports = 3 | format_on_save: 4 | description: 'JSFormat will format the file before it is saved' 5 | type: 'boolean' 6 | default: true 7 | indent_with_tabs: 8 | type: 'boolean' 9 | default: false 10 | end_with_newline: 11 | type: 'boolean' 12 | default: true 13 | max_preserve_newlines: 14 | type: 'integer' 15 | default: 4 16 | preserve_newlines: 17 | type: 'boolean' 18 | default: true 19 | space_in_paren: 20 | title: 'Space in parentheses' 21 | type: 'boolean' 22 | default: false 23 | jslint_happy: 24 | title: 'JSLint happy' 25 | type: 'boolean' 26 | default: false 27 | brace_style: 28 | type: 'string' 29 | default: 'collapse' 30 | enum: ['collapse', 'expand', 'end-expand'] 31 | keep_array_indentation: 32 | type: 'boolean' 33 | default: false 34 | keep_function_indentation: 35 | type: 'boolean' 36 | default: false 37 | space_after_anon_function: 38 | title: 'Space after anonymous functions' 39 | type: 'boolean' 40 | default: false 41 | space_before_conditional: 42 | type: 'boolean' 43 | default: true 44 | eval_code: 45 | title: 'Evaluate code' 46 | type: 'boolean' 47 | default: false 48 | unescape_strings: 49 | type: 'boolean' 50 | default: false 51 | break_chained_methods: 52 | type: 'boolean' 53 | default: false 54 | e4x: 55 | title: 'e4x style' 56 | type: 'boolean' 57 | default: false 58 | comma_first: 59 | title: 'comma first' 60 | type: 'boolean' 61 | default: false 62 | ignore_files: 63 | type: 'array' 64 | default: ['.jshintrc'] 65 | items: 66 | type: 'string' 67 | -------------------------------------------------------------------------------- /lib/format.coffee: -------------------------------------------------------------------------------- 1 | ### 2 | Package dependencies 3 | ### 4 | jsbeautify = (require 'js-beautify').js_beautify 5 | path = require 'path' 6 | 7 | packgeConfig = require './config' 8 | Observer = require './observer' 9 | FileTypeNotSupportedView = require './not-supported-view' 10 | 11 | module.exports = 12 | config: packgeConfig 13 | 14 | activate: (state) -> 15 | atom.commands.add 'atom-workspace', 'jsformat:format', => @format(state) 16 | 17 | @editorSaveSubscriptions = {} 18 | @editorCloseSubscriptions = {} 19 | 20 | # @editorSaveSubscriptions = new Observer() 21 | # @editorCloseSubscriptions = new Observer() 22 | 23 | atom.config.observe 'jsformat.format_on_save', => 24 | @subscribeToEvents() 25 | 26 | format: (state) -> 27 | editor = atom.workspace.getActiveTextEditor() 28 | if !editor 29 | return 30 | 31 | grammar = editor.getGrammar().name 32 | 33 | if (!(grammar is 'JSON' or /JavaScript/.test(grammar))) 34 | @displayUnsupportedLanguageNotification(grammar) 35 | else if (atom.config.get('jsformat.ignore_files').indexOf(editor.getTitle()) != -1) 36 | return 37 | else 38 | mainCursor = editor.getCursors()[0] 39 | textBuffer = editor.getBuffer() 40 | nonWhitespaceRegex = /\S/g 41 | whitespaceRegex = /\s/g 42 | currentCursorPosition = mainCursor.getBufferPosition() 43 | mainCursor.setBufferPosition([currentCursorPosition.row, currentCursorPosition.column + 1]) 44 | isBeforeWord = mainCursor.isInsideWord() 45 | mainCursor.setBufferPosition(currentCursorPosition) 46 | 47 | if mainCursor.isInsideWord() 48 | # The cursor is inside a word, so let's use the beginning as the reference 49 | currentPosition = mainCursor.getBeginningOfCurrentWordBufferPosition() 50 | 51 | # ideally we could do mainCursor.setBufferPosition([currentCursorPosition.row, currentCursorPosition.column + 1]).isInsideWord() 52 | # but .setBufferPosition returns undefined :( 53 | # So we have to define some stuff above instead... 54 | 55 | else if isBeforeWord 56 | # The cursor is right before a word in this case, so let's use the current cursor position as a reference 57 | mainCursor.setBufferPosition(currentCursorPosition) 58 | currentPosition = currentCursorPosition 59 | 60 | textuntilCursor = textBuffer.getTextInRange([[0, 0], currentPosition]) 61 | 62 | nonWhitespaceCharacters = textuntilCursor.match(nonWhitespaceRegex) 63 | whitespaceCharacters = textuntilCursor.match(whitespaceRegex) 64 | 65 | nonWhitespaceCharacters = if nonWhitespaceCharacters then nonWhitespaceCharacters.length else 0 66 | whitespaceCharacters = if whitespaceCharacters then whitespaceCharacters.length else 0 67 | 68 | @formatJavascript(editor) 69 | 70 | text = editor.getText() 71 | 72 | newCursorPosition = textBuffer.positionForCharacterIndex(nonWhitespaceCharacters + whitespaceCharacters); 73 | 74 | mainCursor.setBufferPosition(newCursorPosition) 75 | 76 | formatJavascript: (editor) -> 77 | editorSettings = atom.config.get('editor') 78 | 79 | opts = atom.config.get('jsformat') 80 | opts.indent_with_tabs = !editor.getSoftTabs() 81 | opts.indent_size = editorSettings.tabLength 82 | opts.wrap_line_length = editorSettings.preferredLineLength 83 | 84 | editorText = editor.getText() 85 | beautifiedText = jsbeautify(editorText, opts) 86 | 87 | if (editorText != beautifiedText) 88 | if @selectionsAreEmpty(editor) 89 | editor.setText(beautifiedText) 90 | 91 | else 92 | for selection in editor.getSelections() 93 | selection.insertText(jsbeautify(selection.getText(), opts), { select: true }) 94 | 95 | selectionsAreEmpty: (editor) -> 96 | for selection in editor.getSelections() 97 | return false unless selection.isEmpty() 98 | return true 99 | 100 | subscribeToEvents: (state) -> 101 | if (atom.config.get('jsformat.format_on_save')) 102 | @editorCreationSubscription = atom.workspace.observeTextEditors (editor) => 103 | grammar = editor.getGrammar().scopeName 104 | 105 | if grammar is 'source.js' or grammar is 'source.json' 106 | buffer = editor.getBuffer() 107 | 108 | @editorSaveSubscriptions[editor.id] = buffer.onWillSave => 109 | if buffer.isModified() 110 | buffer.transact => 111 | @format(state) 112 | 113 | @editorCloseSubscriptions[editor.id] = buffer.onDidDestroy => 114 | @editorSaveSubscriptions[editor.id]?.dispose() 115 | @editorCloseSubscriptions[editor.id]?.dispose() 116 | 117 | delete @editorSaveSubscriptions[editor.id] 118 | delete @editorCloseSubscriptions[editor.id] 119 | 120 | # saveSubscription = buffer.onWillSave => 121 | # buffer.transact => 122 | # @format(state) 123 | # 124 | # closeSubscription = buffer.onDidDestroy => 125 | # debugger 126 | # 127 | # @editorSaveSubscriptions.addSubscription(saveSubscription) 128 | # @editorCloseSubscriptions.addSubscription(closeSubscription) 129 | else 130 | if @editorCreationSubscription 131 | @editorCreationSubscription.dispose() 132 | @editorCreationSubscription = null 133 | 134 | for subscriptionId, subscription of @editorSaveSubscriptions 135 | # unsubscribe to the save event of the editor 136 | subscription.dispose() 137 | # delete the keys to reduce memory usage 138 | delete @editorSaveSubscriptions[subscriptionId] 139 | 140 | for subscriptionId, subscription of @editorCloseSubscriptions 141 | # unsubscribe to the save event of the editor 142 | subscription.dispose() 143 | # delete the keys to reduce memory usage 144 | delete @editorCloseSubscriptions[subscriptionId] 145 | 146 | # @editorSaveSubscriptions.dispose() 147 | # @editorSaveSubscriptions = new Observer() 148 | # 149 | # @editorCloseSubscriptions.dispose() 150 | # @editorCloseSubscriptions = new Observer() 151 | 152 | displayUnsupportedLanguageNotification: (language) -> 153 | editor = atom.workspace.getActiveTextEditor() 154 | title = editor.getTitle() 155 | ext = path.extname title 156 | message = ext.length > 0 ? ext : title 157 | atom.notifications.addWarning("Formatting '#{message}' files is not yet supported.", { dismissable: true }); 158 | -------------------------------------------------------------------------------- /lib/not-supported-view.coffee: -------------------------------------------------------------------------------- 1 | {View} = require 'atom-space-pen-views' 2 | path = require 'path' 3 | 4 | module.exports = 5 | class NotSupportedNotificationView extends View 6 | constructor: (@fileType) -> 7 | this.fileType = @fileType 8 | super() 9 | 10 | @content: -> 11 | editor = atom.workspace.getActivePaneItem() 12 | title = editor.getTitle() 13 | ext = path.extname title 14 | message = ext.length > 0 ? ext : title 15 | 16 | @div class: 'test overlay from-top', => 17 | @div "Formatting '#{message}' files is not yet supported.", class: "message" 18 | -------------------------------------------------------------------------------- /lib/observer.coffee: -------------------------------------------------------------------------------- 1 | # include event-kit to make use of CompositeDisposable 2 | {CompositeDisposable} = require 'event-kit' 3 | 4 | module.exports = 5 | class Observer 6 | constructor: () -> 7 | @subscriptions = new CompositeDisposable() 8 | 9 | addSubscription: (disposableSubscription) -> 10 | @subscriptions.add disposableSubscription 11 | 12 | dispose: -> 13 | @subscriptions.dispose() # Dispose of all subscriptions at once 14 | -------------------------------------------------------------------------------- /menus/format.cson: -------------------------------------------------------------------------------- 1 | # See https://atom.io/docs/latest/creating-a-package#menus for more details 2 | 'context-menu': 3 | '.overlayer': [ 4 | { 5 | 'label': 'Enable JSFormat' 6 | 'command': 'jsformat:format' 7 | } 8 | ] 9 | 10 | 'menu': [ 11 | { 12 | 'label': 'Packages' 13 | 'submenu': [ 14 | { 15 | 'label': 'JSFormat' 16 | 'submenu': [ 17 | { 18 | 'label': 'Format JavaScript', 19 | 'command': 'jsformat:format' 20 | } 21 | ] 22 | } 23 | ] 24 | } 25 | ] 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jsformat", 3 | "main": "./lib/format", 4 | "version": "0.9.3", 5 | "private": true, 6 | "description": "javascript formatting for the atom text editor", 7 | "keywords": [ 8 | "format", 9 | "javascript", 10 | "beautify", 11 | "prettify" 12 | ], 13 | "activationCommands": [], 14 | "repository": "https://github.com/jdc0589/jsformat-atom", 15 | "license": "MIT", 16 | "engines": { 17 | "atom": ">=0.100.0" 18 | }, 19 | "dependencies": { 20 | "event-kit": "~0.8.1", 21 | "js-beautify": "~1.5.4", 22 | "atom-space-pen-views": "~2.0.3" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spec/format-spec.coffee: -------------------------------------------------------------------------------- 1 | {WorkspaceView} = require 'atom' 2 | require 'fs' 3 | 4 | format = require '../lib/format' 5 | 6 | describe "JSFormat package tests", -> 7 | beforeEach -> 8 | atom.workspaceView = new WorkspaceView() 9 | atom.workspace = atom.workspaceView.getModel() 10 | 11 | # JSFormat tests here 12 | 13 | describe "when the textbuffer is being formatted", -> 14 | beforeEach -> 15 | atom.workspaceView.attachToDom() 16 | 17 | it "can format the whole buffer with the use of the command", -> 18 | # general format test 19 | 20 | waitsForPromise -> 21 | atom.workspace.open('specfiles/index.js') 22 | 23 | runs -> 24 | @fileText = atom.workspace.getActiveTextEditor().getText() 25 | atom.workspaceView.getActiveView().trigger 'jsformat:format' 26 | 27 | runs -> 28 | # just check that some whitespace and other goodies got added 29 | expect(atom.workspace.getActiveTextEditor().getText()).not.toMatch(@fileText) 30 | 31 | it "can format a selection of the whole buffer with the use of the command", -> 32 | # general selected text format test 33 | 34 | waitsForPromise -> 35 | atom.workspace.open('specfiles/index.js') 36 | 37 | runs -> 38 | # select text until end of first require 39 | atom.workspace.getActiveTextEditor().setSelectedBufferRange([[0, 0], [14, 7]]) 40 | @fileText = atom.workspace.getActiveTextEditor().getSelectedText() 41 | @restOfFileText = atom.workspace.getActiveTextEditor().getTextInBufferRange([[15, 0], [31, 0]]) 42 | atom.workspaceView.getActiveView().trigger 'jsformat:format' 43 | 44 | runs -> 45 | # check that some whitespace and other goodies got added 46 | expect(atom.workspace.getActiveTextEditor().getSelectedText()).not.toMatch(@fileText) 47 | # check that the rest of the file wasn't touched 48 | expect(atom.workspace.getActiveTextEditor().getTextInBufferRange([[15, 0], [31, 0]])).not.toMatch(@restOfFileText) 49 | 50 | 51 | it "can format the whole buffer if Format on save is turned on", -> 52 | # format_on_save test 53 | 54 | waitsForPromise -> 55 | atom.workspace.open('specfiles/index.js') 56 | 57 | runs -> 58 | fileText = atom.workspace.getActiveTextEditor().getText() 59 | atom.config.set('jsformat.format_on_save', true) 60 | atom.workspace.getActiveTextEditor().save() 61 | 62 | # just check that some whitespace and other goodies got added 63 | expect(atom.workspace.getActiveTextEditor().getText()).not.toMatch(fileText) 64 | atom.config.set('jsformat.format_on_save', false) 65 | 66 | it "can subscribe and unsubscribe to editors when format_on_save is enabled and editors are opened and closed", -> 67 | # format_on_save test 68 | 69 | waitsForPromise -> 70 | spyOn(format, 'subscribeToEvents').andCallThrough() 71 | atom.workspace.open('specfiles/index.js') 72 | 73 | waitsForPromise -> 74 | atom.packages.activatePackage('jsformat') 75 | 76 | runs -> 77 | atom.config.set('jsformat.format_on_save', true) 78 | console.log(format.editorSaveSubscriptions) 79 | console.log(format.editorCloseSubscriptions) 80 | atom.workspace.getActiveTextEditor().destroy() 81 | 82 | atom.workspace.open('file.js') 83 | console.log(format.editorSaveSubscriptions) 84 | console.log(format.editorCloseSubscriptions) 85 | # atom.workspace.getActiveTextEditor().destroy() 86 | # expect(format.editorSaveSubscriptions) 87 | 88 | # expect() 89 | # TODO add a check here to see that the subscriptions were disposed and deleted from the objects 90 | 91 | it "can subscribe and unsubscribe to events when format_on_save is changed", -> 92 | # format_on_save test 93 | 94 | waitsForPromise -> 95 | spyOn(format, 'subscribeToEvents').andCallThrough() 96 | atom.workspace.open('specfiles/index.js') 97 | 98 | waitsForPromise -> 99 | atom.packages.activatePackage('jsformat') 100 | 101 | runs -> 102 | # check that subscribeToEvents has run after toggling format_on_save 103 | expect(format.subscribeToEvents.callCount).toEqual(1) 104 | atom.config.set('jsformat.format_on_save', false) 105 | 106 | # waitsFor -> 107 | expect(format.subscribeToEvents.callCount).toEqual(2) 108 | atom.config.set('jsformat.format_on_save', true) 109 | # , "Waiting for subscribeToEvents to run", 250 110 | 111 | # waitsFor -> 112 | expect(format.subscribeToEvents.callCount).toEqual(3) 113 | # , "Wait for subscribeToEvents to run", 250 114 | 115 | it "displays a notification for unsupported languages", -> 116 | # NotSupportedNotificationView test 117 | 118 | waitsForPromise -> 119 | atom.packages.activatePackage('jsformat') 120 | 121 | waitsForPromise -> 122 | spyOn(format, 'displayUnsupportedLanguageNotification').andCallThrough() 123 | atom.workspace.open('xyz.coffee') 124 | 125 | runs -> 126 | atom.workspaceView.getActiveView().trigger('jsformat:format') 127 | 128 | expect(format.displayUnsupportedLanguageNotification).toHaveBeenCalled() 129 | expect(format.displayUnsupportedLanguageNotification.callCount).toEqual(1) 130 | -------------------------------------------------------------------------------- /spec/specfiles/index.js: -------------------------------------------------------------------------------- 1 | window.onload = function() { 2 | var path = require('path'); 3 | var ipc = require('ipc'); 4 | try { 5 | // Skip "?loadSettings=". 6 | var loadSettings = JSON.parse(decodeURIComponent(location.search.substr(14))); 7 | 8 | // Start the crash reporter before anything else. 9 | require('crash-reporter').start({ 10 | productName: 'Atom', 11 | companyName: 'GitHub', 12 | // By explicitly passing the app version here, we could save the call 13 | // of "require('remote').require('app').getVersion()". 14 | extra: {_version: loadSettings.appVersion} 15 | }); 16 | 17 | require('vm-compatibility-layer'); 18 | require('coffee-script').register(); 19 | require(path.resolve(__dirname, '..', 'src', 'coffee-cache')).register(); 20 | require(loadSettings.bootstrapScript); 21 | ipc.sendChannel('window-command', 'window:loaded'); 22 | } 23 | catch (error) { 24 | var currentWindow = require('remote').getCurrentWindow(); 25 | currentWindow.setSize(800, 600); 26 | currentWindow.center(); 27 | currentWindow.show(); 28 | currentWindow.openDevTools(); 29 | console.error(error.stack || error); 30 | } 31 | }; 32 | --------------------------------------------------------------------------------