Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Fix calculation of scoped setting defaults #967

Merged
merged 4 commits into from
Jul 16, 2017
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
26 changes: 15 additions & 11 deletions lib/settings-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,23 @@ export default class SettingsPanel extends CollapsibleSectionPanel {
}

getDefault (name) {
let params = {excludeSources: [atom.config.getUserConfigPath()]}
if (this.options.scopeName != null) {
return atom.config.get(name)
} else {
let params = {excludeSources: [atom.config.getUserConfigPath()]}
if (this.options.scopeName != null) {
params.scope = [this.options.scopeName]
params.scope = [this.options.scopeName]
}

let defaultValue = atom.config.get(name, params)
if (this.options.scopeName != null) {
// If the unscoped default is the same as the scoped default, check the actual config.cson
// to make sure that there isn't a non-default value that is overriding the scoped value
// For example: the default editor.tabLength is 2, but if someone sets it to 4
// the above check still returns 2 and not 4 for a scoped editor.tabLength,
// because it bypasses config.cson.
if (atom.config.get(name, {excludeSources: [atom.config.getUserConfigPath()]}) === defaultValue) {
defaultValue = atom.config.get(name)
}
return atom.config.get(name, params)
}
return defaultValue
}

set (name, value) {
Expand Down Expand Up @@ -210,11 +218,7 @@ export default class SettingsPanel extends CollapsibleSectionPanel {
let type = editorElement.getAttribute('type')

if (defaultValue = this.valueToString(this.getDefault(name))) {
if (this.options.scopeName != null) {
editor.setPlaceholderText(`Unscoped value: ${defaultValue}`)
} else {
editor.setPlaceholderText(`Default: ${defaultValue}`)
}
editor.setPlaceholderText(`Default: ${defaultValue}`)
}

const subscriptions = new CompositeDisposable()
Expand Down
33 changes: 29 additions & 4 deletions spec/settings-panel-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,46 @@ describe "SettingsPanel", ->
settingsPanel.set('foo.testZero', 0)
expect(settingsPanel.isDefault('foo.testZero')).toBe true

describe 'when displaying scoped settings', ->
it 'displays the settings unscoped value of a setting as its default', ->
describe 'scoped settings', ->
beforeEach ->
schema =
scopes:
'.source.python':
default: 4

atom.config.setScopedDefaultsFromSchema('editor.tabLength', schema)
expect(atom.config.get('editor.tabLength')).toBe(2)

it 'displays the scoped default', ->
settingsPanel = new SettingsPanel({namespace: "editor", includeTitle: false, scopeName: '.source.python'})
tabLengthEditor = settingsPanel.element.querySelector('[id="editor.tabLength"]')
expect(tabLengthEditor.getModel().getText()).toBe('')
expect(tabLengthEditor.getModel().getPlaceholderText()).toBe('Default: 4')

it 'allows the scoped setting to be changed to its normal default if the unscoped value is different', ->
atom.config.set('editor.tabLength', 8)

settingsPanel = new SettingsPanel({namespace: "editor", includeTitle: false, scopeName: '.source.js'})
tabLengthEditor = settingsPanel.element.querySelector('[id="editor.tabLength"]')
expect(tabLengthEditor.getModel().getText()).toBe('')
expect(tabLengthEditor.getModel().getPlaceholderText()).toBe('Unscoped value: 8')
expect(tabLengthEditor.getModel().getPlaceholderText()).toBe('Default: 8')

# This is the default value, but it differs from the unscoped value
# This is the unscoped default, but it differs from the current unscoped value
settingsPanel.set('editor.tabLength', 2)
expect(tabLengthEditor.getModel().getText()).toBe('2')
expect(atom.config.get('editor.tabLength', {scope: ['source.js']})).toBe(2)

it 'allows the scoped setting to be changed to the unscoped default if it is different', ->
settingsPanel = new SettingsPanel({namespace: "editor", includeTitle: false, scopeName: '.source.python'})
tabLengthEditor = settingsPanel.element.querySelector('[id="editor.tabLength"]')
expect(tabLengthEditor.getModel().getText()).toBe('')
expect(tabLengthEditor.getModel().getPlaceholderText()).toBe('Default: 4')

# This is the unscoped default, but it differs from the scoped default
settingsPanel.set('editor.tabLength', 2)
expect(tabLengthEditor.getModel().getText()).toBe('2')
expect(atom.config.get('editor.tabLength', {scope: ['source.python']})).toBe(2)

describe 'grouped settings', ->
beforeEach ->
config =
Expand Down