diff --git a/README.md b/README.md index bae2197ed..6b9a727a3 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,10 @@ $ jscodefmt file.js ``` It's most useful when integrated with your editor, so see `editors` to -for editor support. Currently only Emacs integration exists. +for editor support. Atom and Emacs is currently supported. -To integrate in Emacs, add the following code. This will format the -file when saved. +More docs on editor integration will come soon. To integrate in Emacs, +add the following code. This will format the file when saved. ```elisp (require 'jscodefmt) diff --git a/bin/jscodefmt b/bin/jscodefmt index de325951c..69dd78711 100755 --- a/bin/jscodefmt +++ b/bin/jscodefmt @@ -8,7 +8,7 @@ const filename = argv["_"][0]; const printWidth = argv['print-width'] || 80; const tabWidth = argv['tab-width'] || 2; -jscodefmt.format(fs.readFileSync(filename, "utf8"), { +console.log(jscodefmt.format(fs.readFileSync(filename, "utf8"), { printWidth, tabWidth -}); +})); diff --git a/editors/jscodefmt-atom/CHANGELOG.md b/editors/jscodefmt-atom/CHANGELOG.md new file mode 100644 index 000000000..3bac86628 --- /dev/null +++ b/editors/jscodefmt-atom/CHANGELOG.md @@ -0,0 +1,29 @@ +## 2.4.0 - Support `standard --fix` + +- Use `standard --fix` as the default formatter for Standard Style +- Keep existing `standard-format` module as a formatter option + +## 1.0.0 - Honor Package Settings +- Format On Save honors any `ignore` configuration in the file's nearest package.json. +- JSX files are supported + +## 0.7.0 - Multiple Styles +- Support standard and semi-standard styles using settings +- Fix: Editor selection is ignored when formatting on save + +## 0.6.0 - Format selection +- If a text selection is made, `ctrl-alt-f` will format the selection only. Otherwise +`ctrl-alt-f` will format file contents as normal. + +## 0.5.1 - Cursor position and syntax error handling +- Maintain cursor position after format/save +- Catch errors thrown by transform due to syntax errors + +## 0.5.0 - Format on save and Javascript instead of Coffeescript +- **Format on save**: Added setting to enable format on save. Defaults to off. +- **Less irony**: This package is now written in Javascript using Javascript Standard Style. +- **Faster startup time** + +## 0.1.0 - First Release +* Use [standard-format](https://github.com/maxogden/standard-format) to format current Javascript file +* `ctrl-alt-f` keybinding diff --git a/editors/jscodefmt-atom/LICENSE.md b/editors/jscodefmt-atom/LICENSE.md new file mode 100644 index 000000000..b352b9c41 --- /dev/null +++ b/editors/jscodefmt-atom/LICENSE.md @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) Stephen Kubovic + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/editors/jscodefmt-atom/README.md b/editors/jscodefmt-atom/README.md new file mode 100644 index 000000000..0020b7e57 --- /dev/null +++ b/editors/jscodefmt-atom/README.md @@ -0,0 +1,65 @@ +# atom-standard-formatter + +Atom package to format your Javascript using [Standard Style](https://github.com/feross/standard) +, [Semi-Standard Style](https://github.com/Flet/semistandard) +or [Happiness Style](https://github.com/jedwatson/happiness). + +![](https://cloud.githubusercontent.com/assets/5852428/8020717/adbf10c0-0c51-11e5-8537-2714c9f698e5.gif) + +### Usage + +#### Keybindings + +Use `ctrl-alt-f` to format the current Javascript file. If a text selection is made, only the selected text will be formatted. + +#### Format On Save + +Automatically format your Javascript file on save by enabling the *Format On Save* package setting. This is off by default. + +#### Menu + +*Packages > standard-formatter > Format* + +### Settings + +#### formatOnSave (default: false) + +Format Javascript files when saving. + +#### checkStyleDevDependencies (default: false) + +Check code style in package.json `devDependencies`. If a valid style is not found it won't format. + +| Note: This will use the nearest package.json + +#### style (default: standard) + +Choose the style formatter module you want to use. If `checkStyleDevDependencies` is `true` this setting will be ignored. + +* [standard](https://github.com/feross/standard) - equivalent to running `standard --fix` +* [standard-format](https://github.com/maxogden/standard-format) +* [semistandard-format](https://github.com/ricardofbarros/semistandard-format) +* [happiness-format](https://github.com/martinheidegger/hapiness-format) + +#### honorPackageConfig (default: true) + +Don't auto-format files included in the package.json's `"ignore"` configuration for the detected style. + +| Note: This will use the nearest package.json + +### A note on formatting + +This package relies on the excellent work from the following modules to perform formatting: + +- [standard](https://github.com/feross/standard) +- [standard-format](https://github.com/maxogden/standard-format) +- [semistandard-format](https://github.com/ricardofbarros/semistandard-format) +- [happiness-format](https://github.com/martinheidegger/hapiness-format) + +If parts of your Javascript are not being formatted as you'd expect, is is likely an issue with one of these modules and not this Atom package. To verify this, you can try to format your file directly using the above modules and examine the output. + +For example, to format `my-file.js` and output to stdout: +``` +$ npm install -g standard-format +$ standard-format my-file.js +``` diff --git a/editors/jscodefmt-atom/keymaps/standard-formatter.json b/editors/jscodefmt-atom/keymaps/standard-formatter.json new file mode 100644 index 000000000..b7a8b4dea --- /dev/null +++ b/editors/jscodefmt-atom/keymaps/standard-formatter.json @@ -0,0 +1,5 @@ +{ + "atom-text-editor": { + "ctrl-alt-f": "jscodefmt:format" + } +} diff --git a/editors/jscodefmt-atom/lib/jscodefmt.js b/editors/jscodefmt-atom/lib/jscodefmt.js new file mode 100644 index 000000000..7d1588b5a --- /dev/null +++ b/editors/jscodefmt-atom/lib/jscodefmt.js @@ -0,0 +1,101 @@ +/* global atom */ + +var path = require('path') +var findRoot = require('find-root') +var jscodefmt = require('jscodefmt'); + +module.exports = { + style: null, + fileTypes: ['.js', '.jsx'], + + fileSupported: function (file) { + // Ensure file is a supported file type. + var ext = path.extname(file) + return !!~this.fileTypes.indexOf(ext) + }, + + activate: function () { + this.commands = atom.commands.add('atom-workspace', 'jscodefmt:format', function () { + this.format() + }.bind(this)) + + this.editorObserver = atom.workspace.observeTextEditors(this.handleEvents.bind(this)) + }, + + deactivate: function () { + this.commands.dispose() + this.editorObserver.dispose() + }, + + format: function (options) { + if (options === undefined) { + options = {} + } + var selection = typeof options.selection === 'undefined' ? true : !!options.selection + var editor = atom.workspace.getActiveTextEditor() + if (!editor) { + // Return if the current active item is not a `TextEditor` + return + } + var selectedText = selection ? editor.getSelectedText() : null + var text = selectedText || editor.getText() + var cursorPosition = editor.getCursorScreenPosition() + + try { + var transformed = jscodefmt.format(text, { printWidth: options.printWidth }); + } + catch(e) { + console.log('Error transforming using jscodefmt:', e) + transformed = text; + } + + if (selectedText) { + editor.setTextInBufferRange(editor.getSelectedBufferRange(), transformed) + } else { + editor.setText(transformed) + } + editor.setCursorScreenPosition(cursorPosition) + }, + + handleEvents: function (editor) { + editor.getBuffer().onWillSave(function () { + var path = editor.getPath() + if (!path) return + + if (!editor.getBuffer().isModified()) return + + // var formatOnSave = atom.config.get('jscodefmt.formatOnSave', {scope: editor.getRootScopeDescriptor()}) + // if (!formatOnSave) return + + // Set the relative path based on the file's nearest package.json. + // If no package.json is found, use path verbatim. + var relativePath + try { + var projectPath = findRoot(path) + relativePath = path.replace(projectPath, '').substring(1) + } catch (e) { + relativePath = path + } + + if (this.fileSupported(relativePath)) { + this.format({selection: false}) + } + }.bind(this)) + + if(editor.editorElement) { + window.addEventListener("resize", e => { + const { width } = window.document.body.getBoundingClientRect(); + const columns = (width / editor.editorElement.getDefaultCharacterWidth() | 0) - 10; + console.log(width, columns); + this.format({selection: false, printWidth: columns}); + }); + } + }, + + config: { + formatOnSave: { + type: 'boolean', + default: false + } + } +} diff --git a/editors/jscodefmt-atom/menus/standard-formatter.json b/editors/jscodefmt-atom/menus/standard-formatter.json new file mode 100644 index 000000000..4e20eaf38 --- /dev/null +++ b/editors/jscodefmt-atom/menus/standard-formatter.json @@ -0,0 +1,26 @@ +{ + "context-menu": { + "atom-text-editor[data-grammar='source js']": [ + { + "label": "Format With jscodefmt", + "command": "jscodefmt:format" + } + ] + }, + "menu": [ + { + "label": "Packages", + "submenu": [ + { + "label": "jscodefmt", + "submenu": [ + { + "label": "Format", + "command": "jscodefmt:format" + } + ] + } + ] + } + ] +} diff --git a/editors/jscodefmt-atom/package.json b/editors/jscodefmt-atom/package.json new file mode 100644 index 000000000..7fefde28b --- /dev/null +++ b/editors/jscodefmt-atom/package.json @@ -0,0 +1,21 @@ +{ + "name": "jscodefmt-atom", + "main": "./lib/jscodefmt.js", + "version": "0.0.1", + "description": "Format file contents using jscodefmt", + "keywords": [ + "javascript", + "formatter" + ], + "activationCommands": [], + "repository": "https://github.com/stephenkubovic/atom-standard-formatter", + "license": "MIT", + "engines": { + "atom": ">=0.174.0 <2.0.0" + }, + "dependencies": { + "find-root": "^0.1.1", + "jscodefmt": "^0.0.1" + }, + "devDependencies": {} +} diff --git a/index.js b/index.js index 23c2c1541..6893c20d9 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ var babylonOptions = { module.exports = { format: function(text, opts={}) { - const { tabWidth, printWidth } = opts; + let { tabWidth = 2, printWidth = 80 } = opts; const ast = recast.parse(text, { parser: { @@ -36,6 +36,6 @@ module.exports = { }); const result = recast.prettyPrint(ast, { tabWidth, wrapColumn: printWidth }); - console.log(result.code); + return result.code; } };