Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formattingToggle.affects option #51

Merged
merged 3 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,35 @@ The extension should show up on the right side of the status bar. Simply click i

## Customisation

By default, Formatting Toggle only puts the formatter back ON for the `PASTE` and `SAVE` events. To disable one of those or to re-enable the formatter for the `TYPE` event, you will have to configure it using the `formattingToggle.activateFor` setting.
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).

#### Examples
### Examples

To allow the formatter to be put back ON **only** for the `SAVE` event:
#### Keeping `formatOnPaste` and `formatOnType` enabled at all times:

- Go to your Settings (Code > Preferences > Settings).
- Add `"formattingToggle.activateFor": ["formatOnSave"]`.
```json
{
"editor.formatOnPaste": true,
"editor.formatOnType": true,
"formattingToggle.affects": ["formatOnSave"]
}
```

To allow the formatter to be put back ON for **all** events:
#### Keeping `formatOnType` disabled at all times:

- Go to your Settings (Code > Preferences > Settings).
- Add `"formattingToggle.activateFor": ["formatOnPaste", "formatOnSave", "formatOnType"]`.
```json
{
"editor.formatOnType": false,
"formattingToggle.affects": ["formatOnPaste", "formatOnSave"]
}
```

Note: this was the default behaviour before version 2.0.0 of the extension.

#### Allow the formatter to be toggled for all events (default):

```json
{
"formattingToggle.affects": ["formatOnPaste", "formatOnSave", "formatOnType"]
}
```
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
"type": "object",
"title": "Formatting Toggle’s configuration",
"properties": {
"formattingToggle.activateFor": {
"formattingToggle.affects": {
"scope": "resource",
"type": "array",
"default": [
"formatOnPaste",
"formatOnSave"
"formatOnSave",
"formatOnType"
],
"description": "The events for which the formatter should be toggled back ON. Ignores `formatOnType` by default."
"description": "The events for which the extension should toggle the formatting for. Affects all events (formatOnPaste, formatOnSave, formatOnType) by default."
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// This should be kept in sync with the command name specified in the
// `package.json` file: `contributes.commands[0].command`.
export const COMMAND_NAME = 'formattingToggle.toggleFormat'
export const FORMATTING_SETTINGS = [

export type FormattingSettings =
| 'formatOnPaste'
| 'formatOnSave'
| 'formatOnType'

export const FORMATTING_SETTINGS: FormattingSettings[] = [
'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'
]
// file: `contributes.configuration.properties.formattingToggle.affects.default`.
// By default, we want to always toggle all formatting settings.
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
29 changes: 29 additions & 0 deletions src/helpers/getIsFormattingActivated/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
FORMATTING_SETTINGS,
DEFAULT_AFFECTS_CONFIGURATION,
FormattingSettings
} from '../../constants'
import getConfiguration from '../getConfiguration'

export type FormattingConfiguration = {
[key in FormattingSettings]: boolean
}

const getIsFormattingActivated = () => {
const editorConfiguration = getConfiguration('editor')
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 isRelevantSetting && isSettingActivated
})

return isAnyRelevantSettingActivated
}

export default getIsFormattingActivated
252 changes: 252 additions & 0 deletions src/helpers/getIsFormattingActivated/spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
import { FormattingSettings } from '../../constants'
import getConfiguration from '../getConfiguration'
import getIsFormattingActivated, { FormattingConfiguration } from './'

jest.mock('../getConfiguration')

const mockedGetConfiguration = getConfiguration as jest.Mock

type ConfigurationMock = {
editor: FormattingConfiguration
formattingToggle?: { affects: FormattingSettings[] }
}

type Setting = keyof (FormattingConfiguration &
ConfigurationMock['formattingToggle'])

const mockGetConfiguration = (configurationMock: ConfigurationMock) =>
mockedGetConfiguration.mockImplementation(
(settingsNamespace: keyof ConfigurationMock) => {
const relevantConfiguration = configurationMock[settingsNamespace] || {}

return {
get: (setting: Setting, defaultValue: any) =>
// @ts-ignore 😤
relevantConfiguration[setting] || defaultValue
}
}
)

describe('The `getIsFormattingActivated` helper', () => {
it('should return `true` if any of the formatting settings is enabled (without any custom `affects` configuration)', () => {
const testCases: ConfigurationMock[] = [
{
editor: {
formatOnPaste: true,
formatOnSave: false,
formatOnType: false
}
},
{
editor: {
formatOnPaste: false,
formatOnSave: true,
formatOnType: false
}
},
{
editor: {
formatOnPaste: false,
formatOnSave: false,
formatOnType: true
}
},
{
editor: {
formatOnPaste: false,
formatOnSave: true,
formatOnType: true
}
},
{
editor: {
formatOnPaste: true,
formatOnSave: false,
formatOnType: true
}
},
{
editor: {
formatOnPaste: true,
formatOnSave: true,
formatOnType: false
}
},
{
editor: {
formatOnPaste: true,
formatOnSave: true,
formatOnType: true
}
}
]

testCases.forEach((configurationMock: ConfigurationMock) => {
mockGetConfiguration(configurationMock)

const expected = true
const actual = getIsFormattingActivated()

expect(actual).toEqual(expected)
})
})

it('should return `true` if any of the formatting settings in the `affects` configuration is enabled', () => {
const testCases: ConfigurationMock[] = [
{
editor: {
formatOnPaste: true,
formatOnSave: false,
formatOnType: false
},
formattingToggle: { affects: ['formatOnPaste'] }
},
{
editor: {
formatOnPaste: false,
formatOnSave: true,
formatOnType: false
},
formattingToggle: { affects: ['formatOnSave'] }
},
{
editor: {
formatOnPaste: false,
formatOnSave: false,
formatOnType: true
},
formattingToggle: { affects: ['formatOnType'] }
},
{
editor: {
formatOnPaste: false,
formatOnSave: true,
formatOnType: true
},
formattingToggle: { affects: ['formatOnSave', 'formatOnType'] }
},
{
editor: {
formatOnPaste: true,
formatOnSave: false,
formatOnType: true
},
formattingToggle: { affects: ['formatOnPaste', 'formatOnType'] }
},
{
editor: {
formatOnPaste: true,
formatOnSave: true,
formatOnType: false
},
formattingToggle: { affects: ['formatOnPaste', 'formatOnSave'] }
},
{
editor: {
formatOnPaste: true,
formatOnSave: true,
formatOnType: true
},
formattingToggle: {
affects: ['formatOnPaste', 'formatOnSave', 'formatOnType']
}
}
]

testCases.forEach((configurationMock: ConfigurationMock) => {
mockGetConfiguration(configurationMock)

const expected = true
const actual = getIsFormattingActivated()

expect(actual).toEqual(expected)
})
})

it('should return `false` if all of the formatting settings are disabled (without any custom `affects` configuration)', () => {
mockGetConfiguration({
editor: {
formatOnPaste: false,
formatOnSave: false,
formatOnType: false
}
})

const expected = false
const actual = getIsFormattingActivated()

expect(actual).toEqual(expected)
})

it('should return `false` if all of the formatting settings in the `affects` configuration are disabled', () => {
const testCases: ConfigurationMock[] = [
{
editor: {
formatOnPaste: false,
formatOnSave: true,
formatOnType: true
},
formattingToggle: { affects: ['formatOnPaste'] }
},
{
editor: {
formatOnPaste: true,
formatOnSave: false,
formatOnType: true
},
formattingToggle: { affects: ['formatOnSave'] }
},
{
editor: {
formatOnPaste: true,
formatOnSave: true,
formatOnType: false
},
formattingToggle: { affects: ['formatOnType'] }
},
{
editor: {
formatOnPaste: true,
formatOnSave: false,
formatOnType: false
},
formattingToggle: { affects: ['formatOnSave', 'formatOnType'] }
},
{
editor: {
formatOnPaste: false,
formatOnSave: true,
formatOnType: false
},
formattingToggle: { affects: ['formatOnPaste', 'formatOnType'] }
},
{
editor: {
formatOnPaste: false,
formatOnSave: false,
formatOnType: true
},
formattingToggle: { affects: ['formatOnPaste', 'formatOnSave'] }
},
{
editor: {
formatOnPaste: false,
formatOnSave: false,
formatOnType: false
},
formattingToggle: {
affects: ['formatOnPaste', 'formatOnSave', 'formatOnType']
}
}
]

testCases.forEach((configurationMock: ConfigurationMock) => {
mockGetConfiguration(configurationMock)

const expected = false
const actual = getIsFormattingActivated()

expect(actual).toEqual(expected)
})
})
})
6 changes: 3 additions & 3 deletions src/helpers/getStatusBarText/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import isFormattingActivated from '../isFormattingActivated'
import getIsFormattingActivated from '../getIsFormattingActivated'

export const ENABLED_TEXT = 'Formatting: $(check)'
export const DISABLED_TEXT = 'Formatting: $(x)'

const getStatusBarText = () => {
const shouldDisableFormatting = isFormattingActivated()
const isFormattingActivated = getIsFormattingActivated()

return shouldDisableFormatting ? ENABLED_TEXT : DISABLED_TEXT
return isFormattingActivated ? ENABLED_TEXT : DISABLED_TEXT
}

export default getStatusBarText
Loading