Update atom package

master
James Long 2017-01-09 23:27:17 -05:00
parent a0edc2a520
commit dd8dda101d
5 changed files with 113 additions and 110 deletions

View File

@ -0,0 +1,5 @@
{
"atom-text-editor": {
"ctrl-alt-f": "prettier:format"
}
}

View File

@ -1,5 +0,0 @@
{
"atom-text-editor": {
"ctrl-alt-f": "jscodefmt:format"
}
}

View File

@ -1,101 +0,0 @@
/* 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);
console.log(width, columns);
this.format({selection: false, printWidth: columns});
});
}
},
config: {
formatOnSave: {
type: 'boolean',
default: false
}
}
}

View File

@ -0,0 +1,104 @@
/* 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.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;
console.log(width, columns);
this.format({ selection: false, printWidth: columns });
});
}
},
config: { formatOnSave: { type: "boolean", default: false } }
};

View File

@ -2,8 +2,8 @@
"context-menu": {
"atom-text-editor[data-grammar='source js']": [
{
"label": "Format With jscodefmt",
"command": "jscodefmt:format"
"label": "Format With prettier",
"command": "prettier:format"
}
]
},
@ -12,11 +12,11 @@
"label": "Packages",
"submenu": [
{
"label": "jscodefmt",
"label": "prettier",
"submenu": [
{
"label": "Format",
"command": "jscodefmt:format"
"command": "prettier:format"
}
]
}