Repository: atom/atom
File: src/text-editor-registry.js (JavaScript)

Code before:
  // for the given editor.
  getGrammarOverride (editor) {
    return editor.getBuffer().getLanguageMode().grammar.scopeName
  }

  // Deprecated: Remove any grammar override that has been set for the given {TextEditor}.
  //
  // * `editor` The editor.
  clearGrammarOverride (editor) {
    atom.grammars.autoAssignLanguageMode(editor.getBuffer())
  }

  updateEditorSettingsForLanguageMode(editor, oldLanguageMode) {
    const newLanguageMode = editor.buffer.getLanguageMode()

    if (oldLanguageMode) {
      const newSettings = this.textEditorParamsForScope(newLanguageMode.scopeDescriptor);
      const oldSettings = this.textEditorParamsForScope(oldLanguageMode.scopeDescriptor);

      let updatedSettings = {}
      for (const [settingsKey, paramName, preserveSetting] of EDITOR_PARAMS_BY_SETTING_KEY) {
        // Log the updated setting if it doesn't need to be preserved or if
        // it has changed between the two language modes.  This prevents user-
        // modified settings in an editor (like 'softWrapped') from being reset
        // when the language mode changes.
        if (!preserveSetting || !_.isEqual(newSettings[paramName], oldSettings[paramName])) {
          console.log(`'${paramName}': ${oldSettings[paramName]} --> ${newSettings[paramName]}`)
          updatedSettings[paramName] = newSettings[paramName]
        }
      }

      if (_.size(updatedSettings) > 0) {
        editor.update(updatedSettings)
      }
    }
    else {
      editor.update(this.textEditorParamsForScope(newLanguageMode.scopeDescriptor))
    }
  }

  async subscribeToSettingsForEditorScope (editor, newLanguageMode, oldLanguageMode) {
    await this.initialPackageActivationPromise

    const scopeDescriptor = editor.getRootScopeDescriptor()
    const scopeChain = scopeDescriptor.getScopeChain()

    if (!this.scopesWithConfigSubscriptions.has(scopeChain)) {
      this.scopesWithConfigSubscriptions.add(scopeChain)
      const configOptions = {scope: scopeDescriptor}

      for (const [settingKey, paramName] of EDITOR_PARAMS_BY_SETTING_KEY) {

Review comment (suggestion):
Can you explain why the `preserveSetting` flag is needed? I would think that the comparison between `oldSettings` and `newSettings` would be enough to fix the problem.

Code after:
  }

  // Deprecated: Remove any grammar override that has been set for the given {TextEditor}.
  //
  // * `editor` The editor.
  clearGrammarOverride (editor) {
    atom.grammars.autoAssignLanguageMode(editor.getBuffer())
  }

  async updateAndMonitorEditorSettings (editor, oldLanguageMode) {
    await this.initialPackageActivationPromise
    this.updateEditorSettingsForLanguageMode(editor, oldLanguageMode)
    await this.subscribeToSettingsForEditorScope(editor)
  }

  updateEditorSettingsForLanguageMode (editor, oldLanguageMode) {
    const newLanguageMode = editor.buffer.getLanguageMode()

    if (oldLanguageMode) {
      const newSettings = this.textEditorParamsForScope(newLanguageMode.rootScopeDescriptor)
      const oldSettings = this.textEditorParamsForScope(oldLanguageMode.rootScopeDescriptor)

      const updatedSettings = {}
      for (const [, paramName] of EDITOR_PARAMS_BY_SETTING_KEY) {
        // Update the setting only if it has changed between the two language
        // modes.  This prevents user-modified settings in an editor (like
        // 'softWrapped') from being reset when the language mode changes.
        if (!_.isEqual(newSettings[paramName], oldSettings[paramName])) {
          updatedSettings[paramName] = newSettings[paramName]
        }
      }

      if (_.size(updatedSettings) > 0) {
        editor.update(updatedSettings)
      }
    } else {
      editor.update(this.textEditorParamsForScope(newLanguageMode.rootScopeDescriptor))
    }
  }

  async subscribeToSettingsForEditorScope (editor) {
    const scopeDescriptor = editor.getRootScopeDescriptor()
    const scopeChain = scopeDescriptor.getScopeChain()

    if (!this.scopesWithConfigSubscriptions.has(scopeChain)) {
      this.scopesWithConfigSubscriptions.add(scopeChain)
      const configOptions = {scope: scopeDescriptor}

      for (const [settingKey, paramName] of EDITOR_PARAMS_BY_SETTING_KEY) {
        this.subscriptions.add(
          this.config.onDidChange(settingKey, configOptions, ({newValue}) => {