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

Commit

Permalink
feat(command): Add a command to toggle "Format on Save"
Browse files Browse the repository at this point in the history
Fixes #117
  • Loading branch information
darahak committed Apr 23, 2017
1 parent c3d3358 commit bc112c6
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions decls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ declare var atom: {
},
config: {
get: (key: string) => any,
set: (key: string) => any,
},
notifications: {
addError: (message: string, options?: { detail?: string, dismissable?: boolean }) => void,
Expand Down
5 changes: 5 additions & 0 deletions dist/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ var getConfigOption = function getConfigOption(key) {
return atom.config.get('prettier-atom.' + key);
};

var setConfigOption = function setConfigOption(key, value) {
return atom.config.set('prettier-atom.' + key, value);
};

var shouldDisplayErrors = function shouldDisplayErrors() {
return !getConfigOption('silenceErrors');
};
Expand Down Expand Up @@ -202,6 +206,7 @@ var getDebugInfo = function getDebugInfo() {

module.exports = {
getConfigOption: getConfigOption,
setConfigOption: setConfigOption,
shouldDisplayErrors: shouldDisplayErrors,
getPrettierOption: getPrettierOption,
getPrettierEslintOption: getPrettierEslintOption,
Expand Down
11 changes: 11 additions & 0 deletions dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var format = null;
var formatOnSave = null;
var warnAboutLinterEslintFixOnSave = null;
var displayDebugInfo = null;
var toggleFormatOnSave = null;
var subscriptions = null;

// HACK: lazy load most of the code we need for performance
Expand Down Expand Up @@ -49,12 +50,22 @@ var lazyDisplayDebugInfo = function lazyDisplayDebugInfo() {
displayDebugInfo();
};

var lazyToggleFormatOnSave = function lazyToggleFormatOnSave() {
if (!toggleFormatOnSave) {
// eslint-disable-next-line global-require
toggleFormatOnSave = require('./toggleFormatOnSave');
}
toggleFormatOnSave();
};

// public API
var activate = function activate() {
subscriptions = new CompositeDisposable();

subscriptions.add(atom.commands.add('atom-workspace', 'prettier:format', lazyFormat));
subscriptions.add(atom.commands.add('atom-workspace', 'prettier:debug', lazyDisplayDebugInfo));
subscriptions.add(atom.commands.add('atom-workspace', 'prettier:toggle-format-on-save', lazyToggleFormatOnSave));

subscriptions.add(atom.workspace.observeTextEditors(function (editor) {
return subscriptions.add(editor.getBuffer().onWillSave(function () {
return lazyFormatOnSave(editor);
Expand Down
12 changes: 12 additions & 0 deletions dist/toggleFormatOnSave.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

var _require = require('./helpers'),
getConfigOption = _require.getConfigOption,
setConfigOption = _require.setConfigOption;

var toggleFormatOnSave = function toggleFormatOnSave() {
var key = 'formatOnSaveOptions.enabled';
setConfigOption(key, !getConfigOption(key));
};

module.exports = toggleFormatOnSave;
4 changes: 4 additions & 0 deletions menus/prettier-js.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
{
"label": "Debug",
"command": "prettier:debug"
},
{
"label": "Toggle Format on Save",
"command": "prettier:toggle-format-on-save"
}
]
}
Expand Down
3 changes: 3 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ const getDepPath = (dep: string) => path.join(__dirname, '../node_modules', dep)
// public helpers
const getConfigOption = (key: string) => atom.config.get(`prettier-atom.${key}`);

const setConfigOption = (key: string, value: any) => atom.config.set(`prettier-atom.${key}`, value);

const shouldDisplayErrors = () => !getConfigOption('silenceErrors');

const getPrettierOption = (key: string) => getConfigOption(`prettierOptions.${key}`);
Expand Down Expand Up @@ -153,6 +155,7 @@ const getDebugInfo = () => ({

module.exports = {
getConfigOption,
setConfigOption,
shouldDisplayErrors,
getPrettierOption,
getPrettierEslintOption,
Expand Down
13 changes: 13 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let format = null;
let formatOnSave = null;
let warnAboutLinterEslintFixOnSave = null;
let displayDebugInfo = null;
let toggleFormatOnSave = null;
let subscriptions = null;

// HACK: lazy load most of the code we need for performance
Expand Down Expand Up @@ -43,12 +44,24 @@ const lazyDisplayDebugInfo = () => {
displayDebugInfo();
};

const lazyToggleFormatOnSave = () => {
if (!toggleFormatOnSave) {
// eslint-disable-next-line global-require
toggleFormatOnSave = require('./toggleFormatOnSave');
}
toggleFormatOnSave();
};

// public API
const activate = () => {
subscriptions = new CompositeDisposable();

subscriptions.add(atom.commands.add('atom-workspace', 'prettier:format', lazyFormat));
subscriptions.add(atom.commands.add('atom-workspace', 'prettier:debug', lazyDisplayDebugInfo));
subscriptions.add(
atom.commands.add('atom-workspace', 'prettier:toggle-format-on-save', lazyToggleFormatOnSave),
);

subscriptions.add(
atom.workspace.observeTextEditors(editor =>
subscriptions.add(editor.getBuffer().onWillSave(() => lazyFormatOnSave(editor))),
Expand Down
8 changes: 8 additions & 0 deletions src/toggleFormatOnSave.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { getConfigOption, setConfigOption } = require('./helpers');

const toggleFormatOnSave = () => {
const key = 'formatOnSaveOptions.enabled';
setConfigOption(key, !getConfigOption(key));
};

module.exports = toggleFormatOnSave;

0 comments on commit bc112c6

Please sign in to comment.