Skip to content

Commit

Permalink
Add formattingToggle.affects configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-Bonnike committed Oct 15, 2019
1 parent 326105a commit a2d5669
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 108 deletions.
51 changes: 27 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Formatting Toggle

A VS Code extension that allows you to toggle the formatter (Prettier, Beautify, …) ON and OFF with a simple click.
You can [customise its behaviour](#customisation) so you can have full control over the formatting to avoid large diffs in projects or even specific files that aren’t using such formatters.

## Installation

Expand All @@ -13,34 +12,38 @@ The extension should show up on the right side of the status bar. Simply click i

## Customisation

### Customise which events should be toggled ON
By default, Formatting Toggle toggles the formatter for all formatting events: `formatOnPaste`, `formatOnSave` and `formatOnType`. To ignore one of these and leave the value that is defined in your settings unchanged, you can use the `formattingToggle.affects` setting in your editor settings (Code > Preferences > Settings).

By default, Formatting Toggle only enables the formatter for the `PASTE` and `SAVE` events. To completely disable it for one of those two or to re-enable it for the `TYPE` event, you will have to use the `formattingToggle.activateFor` setting.
### Examples

#### Examples
#### Keeping `formatOnPaste` and `formatOnType` enabled at all times:

To allow the formatter to **only** be enabled for the `SAVE` event:
```json
{
"editor.formatOnPaste": true,
"editor.formatOnSave": false,
"editor.formatOnType": true,
"formattingToggle.affects": ["formatOnSave"]
}
```

- Go to your Settings (Code > Preferences > Settings).
- Add `"formattingToggle.activateFor": ["formatOnSave"]`.
#### Keeping `formatOnType` disabled at all times:

To allow the formatter to be enabled for **all** events:
```json
{
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnType": false,
"formattingToggle.affects": ["formatOnPaste", "formatOnSave"]
}
```

- Go to your Settings (Code > Preferences > Settings).
- Add `"formattingToggle.activateFor": ["formatOnPaste", "formatOnSave", "formatOnType"]`.
Note: this was the default behaviour before version 1.6.0 of the extension.

### Customise which events should be toggled OFF
#### Allow the formatter to be toggled for all events (default):

By default, Formatting Toggle disables the formatter for **all** events. If for some reason you want it to be enabled at all times for one of them, you will have to configure it using the `formattingToggle.deactivateFor` setting.

#### Examples

To allow the formatter to **only** be disabled for the `SAVE` event:

- Go to your Settings (Code > Preferences > Settings).
- Add `"formattingToggle.deactivateFor": ["formatOnSave"]`.

To allow the formatter to be disabled for both the `SAVE` and `PASTE` events, leaving the formatter enabled **at all times** for the `TYPE` event:

- Go to your Settings (Code > Preferences > Settings).
- Add `"formattingToggle.deactivateFor": ["formatOnPaste", "formatOnSave"]`.
```json
{
"formattingToggle.affects": ["formatOnPaste", "formatOnSave", "formatOnType"]
}
```
13 changes: 2 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,15 @@
"type": "object",
"title": "Formatting Toggle’s configuration",
"properties": {
"formattingToggle.activateFor": {
"scope": "resource",
"type": "array",
"default": [
"formatOnPaste",
"formatOnSave"
],
"description": "The events for which the formatter should be toggled back ON. Ignores `formatOnType` by default."
},
"formattingToggle.deactivateFor": {
"formattingToggle.affects": {
"scope": "resource",
"type": "array",
"default": [
"formatOnPaste",
"formatOnSave",
"formatOnType"
],
"description": "The events (all of them by default) for which the formatter should be toggled back OFF."
"description": "The events for which the extension should toggle the formatting for. Affects all events (formatOnPaste, formatOnSave, formatOnType) by default."
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
// `package.json` file: `contributes.commands[0].command`.
export const COMMAND_NAME = 'formattingToggle.toggleFormat'

export const FORMATTING_SETTINGS = [
export type FormattingEvents = 'formatOnPaste' | 'formatOnSave' | 'formatOnType'

export const FORMATTING_SETTINGS: FormattingEvents[] = [
'formatOnPaste',
'formatOnSave',
'formatOnType'
]

// This should be kept in sync with the default specified in the `package.json`
// file: `contributes.configuration.properties.formattingToggle.activateFor.default`.
export const DEFAULT_ACTIVATION_CONFIGURATION = [
'formatOnPaste',
'formatOnSave'
]

// This should be kept in sync with the default specified in the `package.json`
// file: `contributes.configuration.properties.formattingToggle.deactivateFor.default`.
// By default, we want to always deactivate all formatting configurations.
export const DEFAULT_DEACTIVATION_CONFIGURATION = [...FORMATTING_SETTINGS]
// file: `contributes.configuration.properties.formattingToggle.affects.default`.
// By default, we want to always toggle all formatting configurations.
export const DEFAULT_AFFECTS_CONFIGURATION = [...FORMATTING_SETTINGS]
5 changes: 4 additions & 1 deletion src/getOnDidChangeConfigurationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import getStatusBarText from './helpers/getStatusBarText'

const getOnDidChangeConfigurationHandler = (statusBar: StatusBarItem) =>
workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration('editor')) {
if (
event.affectsConfiguration('editor') ||
event.affectsConfiguration('formattingToggle')
) {
statusBar.text = getStatusBarText()
}
})
Expand Down
24 changes: 17 additions & 7 deletions src/helpers/isFormattingActivated/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { FORMATTING_SETTINGS } from '../../constants'
import {
FORMATTING_SETTINGS,
DEFAULT_AFFECTS_CONFIGURATION,
FormattingEvents
} from '../../constants'
import getConfiguration from '../getConfiguration'

export type FormattingConfiguration = {
formatOnPaste: boolean
formatOnSave: boolean
formatOnType: boolean
[key in FormattingEvents]: boolean
}

const isFormattingActivated = () => {
const editorConfiguration = getConfiguration('editor')
const isAnySettingActivated = FORMATTING_SETTINGS.some(setting =>
editorConfiguration.get(setting, false)
const formattingToggleConfiguration = getConfiguration('formattingToggle')
const affectsConfiguration = formattingToggleConfiguration.get(
'affects',
DEFAULT_AFFECTS_CONFIGURATION
)
const isAnyRelevantSettingActivated = FORMATTING_SETTINGS.some(setting => {
const isRelevantSetting = affectsConfiguration.includes(setting)
const isSettingActivated = editorConfiguration.get(setting, false)

return isAnySettingActivated
return isRelevantSetting && isSettingActivated
})

return isAnyRelevantSettingActivated
}

export default isFormattingActivated
Loading

0 comments on commit a2d5669

Please sign in to comment.