Remove all config observation from text editor

This commit is contained in:
Max Brunsfeld
2016-07-11 12:43:18 -07:00
parent 54a9f3004d
commit 5acab853cd
3 changed files with 172 additions and 48 deletions

View File

@@ -10,6 +10,9 @@ const EDITOR_SETTER_NAMES_BY_SETTING_KEY = [
['editor.invisibles', 'setInvisibles'],
['editor.showIndentGuide', 'setShowIndentGuide'],
['editor.softWrap', 'setSoftWrapped'],
['editor.softWrapHangingIndent', 'setSoftWrapIndentLength'],
['editor.softWrapAtPreferredLineLength', 'setSoftWrapAtPreferredLineLength'],
['editor.preferredLineLength', 'setPreferredLineLength'],
]
// Experimental: This global registry tracks registered `TextEditors`.
@@ -87,6 +90,17 @@ export default class TextEditorRegistry {
for (const [settingKey, setterName] of EDITOR_SETTER_NAMES_BY_SETTING_KEY) {
editor[setterName](atom.config.get(settingKey, configOptions))
}
const updateTabTypes = () => {
editor.setSoftTabs(shouldEditorUseSoftTabs(
editor,
atom.config.get('editor.tabType', configOptions),
atom.config.get('editor.softTabs', configOptions)
))
}
updateTabTypes()
this.subscriptions.add(editor.onDidTokenize(updateTabTypes))
}
subscribeToSettingsForEditorScope (editor) {
@@ -97,6 +111,7 @@ export default class TextEditorRegistry {
this.scopesWithConfigSubscriptions.add(scopeChain)
const configOptions = {scope: scopeDescriptor}
for (const [settingKey, setterName] of EDITOR_SETTER_NAMES_BY_SETTING_KEY) {
this.subscriptions.add(
this.config.onDidChange(settingKey, configOptions, ({newValue}) => {
@@ -108,6 +123,40 @@ export default class TextEditorRegistry {
})
)
}
const updateTabTypes = () => {
const tabType = this.config.get('editor.tabType', configOptions)
const softTabs = this.config.get('editor.softTabs', configOptions)
this.editorsWithMaintainedConfig.forEach(editor => {
if (editor.getRootScopeDescriptor().getScopeChain() === scopeChain) {
editor.setSoftTabs(shouldEditorUseSoftTabs(editor, tabType, softTabs))
}
})
}
this.subscriptions.add(
this.config.onDidChange('editor.tabType', configOptions, updateTabTypes),
this.config.onDidChange('editor.softTabs', configOptions, updateTabTypes)
)
}
}
}
function shouldEditorUseSoftTabs (editor, tabType, softTabs) {
switch (tabType) {
case 'hard':
return false
case 'soft':
return true
case 'auto':
switch (editor.usesSoftTabs()) {
case true:
return true
case false:
return false
default:
return softTabs
}
}
}

View File

@@ -162,12 +162,9 @@ class TextEditor extends Model
@decorateMarkerLayer(@displayLayer.foldsMarkerLayer, {type: 'line-number', class: 'folded'})
@disposables.add @tokenizedBuffer.observeGrammar @subscribeToScopedConfigSettings
for marker in @selectionsMarkerLayer.getMarkers()
@addSelection(marker)
@subscribeToTabTypeConfig()
@subscribeToBuffer()
@subscribeToDisplayLayer()
@@ -226,36 +223,19 @@ class TextEditor extends Model
onDidTerminatePendingState: (callback) ->
@emitter.on 'did-terminate-pending-state', callback
subscribeToScopedConfigSettings: =>
@scopedConfigSubscriptions?.dispose()
@scopedConfigSubscriptions = subscriptions = new CompositeDisposable
scopeDescriptor = @getRootScopeDescriptor()
subscriptions.add @config.onDidChange 'editor.softWrapHangingIndent', scope: scopeDescriptor, @resetDisplayLayer.bind(this)
subscriptions.add @config.onDidChange 'editor.softWrapAtPreferredLineLength', scope: scopeDescriptor, @resetDisplayLayer.bind(this)
subscriptions.add @config.onDidChange 'editor.preferredLineLength', scope: scopeDescriptor, @resetDisplayLayer.bind(this)
@resetDisplayLayer()
subscribeToDisplayLayer: ->
@disposables.add @selectionsMarkerLayer.onDidCreateMarker @addSelection.bind(this)
@disposables.add @tokenizedBuffer.onDidChangeGrammar @handleGrammarChange.bind(this)
@disposables.add @tokenizedBuffer.onDidTokenize @handleTokenization.bind(this)
@disposables.add @displayLayer.onDidChangeSync (e) =>
@mergeIntersectingSelections()
@emitter.emit 'did-change', e
subscribeToTabTypeConfig: ->
@tabTypeSubscription?.dispose()
@tabTypeSubscription = @config.observe 'editor.tabType', scope: @getRootScopeDescriptor(), =>
@softTabs = @shouldUseSoftTabs(defaultValue: @softTabs)
resetDisplayLayer: ->
@displayLayer.reset({
invisibles: @getInvisibles(),
softWrapColumn: @getSoftWrapColumn(),
showIndentGuides: not @isMini() and @config.get('editor.showIndentGuide', scope: @getRootScopeDescriptor()),
atomicSoftTabs: @config.get('editor.atomicSoftTabs', scope: @getRootScopeDescriptor()),
showIndentGuides: not @isMini() and @doesShowIndentGuide(),
atomicSoftTabs: @hasAtomicSoftTabs(),
tabLength: @getTabLength(),
ratioForCharacter: @ratioForCharacter.bind(this),
isWrapBoundary: isWrapBoundary,
@@ -2765,6 +2745,13 @@ class TextEditor extends Model
@showIndentGuide = showIndentGuide
@resetDisplayLayer()
setSoftWrapIndentLength: (softWrapIndentLength) ->
return if softWrapIndentLength is @softWrapIndentLength
@softWrapIndentLength = softWrapIndentLength
@resetDisplayLayer()
getSoftWrapIndentLength: -> @softWrapIndentLength
# Extended: Determine if the buffer uses hard or soft tabs.
#
# Returns `true` if the first non-comment line with leading whitespace starts
@@ -2796,20 +2783,6 @@ class TextEditor extends Model
return unless @getSoftTabs()
@scanInBufferRange /\t/g, bufferRange, ({replace}) => replace(@getTabText())
# Private: Computes whether or not this editor should use softTabs based on
# the `editor.tabType` setting.
#
# Returns a {Boolean}
shouldUseSoftTabs: ({defaultValue}) ->
tabType = @config.get('editor.tabType', scope: @getRootScopeDescriptor())
switch tabType
when 'auto'
@usesSoftTabs() ? defaultValue ? @config.get('editor.softTabs') ? true
when 'hard'
false
when 'soft'
true
###
Section: Soft Wrap Behavior
###
@@ -2838,6 +2811,20 @@ class TextEditor extends Model
else
@isSoftWrapped()
setSoftWrapAtPreferredLineLength: (softWrapAtPreferredLineLength) ->
return if softWrapAtPreferredLineLength is @softWrapAtPreferredLineLength
@softWrapAtPreferredLineLength = softWrapAtPreferredLineLength
@resetDisplayLayer()
doesSoftWrapAtPreferredLineLength: -> @softWrapAtPreferredLineLength
setPreferredLineLength: (preferredLineLength) ->
return if preferredLineLength is @preferredLineLength
@preferredLineLength = preferredLineLength
@resetDisplayLayer()
getPreferredLineLength: -> @preferredLineLength
# Essential: Toggle soft wrapping for this editor
#
# Returns a {Boolean}.
@@ -3342,12 +3329,8 @@ class TextEditor extends Model
Section: Event Handlers
###
handleTokenization: ->
@softTabs = @shouldUseSoftTabs(defaultValue: @softTabs)
handleGrammarChange: ->
@unfoldAll()
@subscribeToTabTypeConfig()
@emitter.emit 'did-change-grammar', @getGrammar()
###