This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d529318
Showing
8 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Prettier formatter for Atom | ||
|
||
Atom package to format your Javascript using [Prettier](https://github.com/jlongster/prettier). | ||
|
||
### 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"atom-text-editor": { | ||
"ctrl-alt-f": "prettier:format" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* global atom */ | ||
|
||
var path = require("path"); | ||
var findRoot = require("find-root"); | ||
var prettier = require("prettier"); | ||
|
||
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", | ||
"prettier: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 = prettier.format(text, { | ||
printWidth: options.printWidth | ||
}); | ||
} catch (e) { | ||
console.log("Error transforming using prettier:", 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("prettier-atom.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) | ||
); | ||
// Uncomment this to format on resize. Not ready yet. :) | ||
// | ||
// if (editor.editorElement) { | ||
// window.addEventListener("resize", e => { | ||
// const { width } = window.document.body.getBoundingClientRect(); | ||
// const columns = width / | ||
// editor.editorElement.getDefaultCharacterWidth() | | ||
// 0; | ||
// console.log(width, columns); | ||
// this.format({ selection: false, printWidth: columns }); | ||
// }); | ||
// } | ||
}, | ||
config: { formatOnSave: { type: "boolean", default: false } } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"context-menu": { | ||
"atom-text-editor[data-grammar='source js']": [ | ||
{ | ||
"label": "Format With prettier", | ||
"command": "prettier:format" | ||
} | ||
] | ||
}, | ||
"menu": [ | ||
{ | ||
"label": "Packages", | ||
"submenu": [ | ||
{ | ||
"label": "prettier", | ||
"submenu": [ | ||
{ | ||
"label": "Format", | ||
"command": "prettier:format" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "prettier-atom", | ||
"main": "./lib/prettier.js", | ||
"version": "0.0.2", | ||
"description": "Format file contents using prettier", | ||
"keywords": [ | ||
"javascript", | ||
"formatter" | ||
], | ||
"activationCommands": [], | ||
"repository": "https://github.com/jlongster/prettier", | ||
"license": "MIT", | ||
"engines": { | ||
"atom": ">=0.174.0 <2.0.0" | ||
}, | ||
"dependencies": { | ||
"find-root": "^0.1.1", | ||
"prettier": "0.0.2" | ||
}, | ||
"devDependencies": {} | ||
} |