Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
jlongster committed Jan 10, 2017
0 parents commit d529318
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
29 changes: 29 additions & 0 deletions 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
20 changes: 20 additions & 0 deletions 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.
23 changes: 23 additions & 0 deletions README.md
@@ -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.
5 changes: 5 additions & 0 deletions keymaps/prettier-js.json
@@ -0,0 +1,5 @@
{
"atom-text-editor": {
"ctrl-alt-f": "prettier:format"
}
}
109 changes: 109 additions & 0 deletions lib/prettier.js
@@ -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 } }
};
26 changes: 26 additions & 0 deletions menus/prettier-js.json
@@ -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"
}
]
}
]
}
]
}
21 changes: 21 additions & 0 deletions package.json
@@ -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": {}
}

0 comments on commit d529318

Please sign in to comment.