<commit_before>'use babel'

/* @flow */

import { CompositeDisposable, Emitter } from 'atom'
import Editor from './editor'
import { getEditorsMap } from './helpers'
import type { Disposable, TextEditor } from 'atom'
import type { Linter$Difference, Linter$Message, Config$ShowIssues } from './types'

export default class Editors {
  emitter: Emitter;
  editors: Set<Editor>;
  messages: Array<Linter$Message>;
  subscriptions: CompositeDisposable;
  showIssuesFrom: Config$ShowIssues;
  highlightIssues: boolean;

  constructor() {
    this.emitter = new Emitter()
    this.editors = new Set()
    this.messages = []
    this.subscriptions = new CompositeDisposable()
    this.subscriptions.add(atom.config.observe('linter-ui-default.showIssuesFrom', showIssuesFrom => {
      this.showIssuesFrom = showIssuesFrom
    }))
    this.highlightIssues = atom.config.get('linter-ui-default.highlightIssues')
    this.subscriptions.add(atom.config.onDidChange('linter-ui-default.highlightIssues', function({ newValue }) {
      this.highlightIssues = newValue
    }))

    this.subscriptions.add(this.emitter)
  }
  activate() {
    this.subscriptions.add(atom.workspace.observeTextEditors(textEditor => {
      const editor = this.getEditor(textEditor)
      console.log(editor)
    }))
  }
  apply(difference: Linter$Difference) {
    if (!this.highlightIssues) {
      // Do not paint anything if highlighting issues is disabled
      return
    }
    this.messages = difference.messages

    const { editorsMap, filePaths } = getEditorsMap(this)
    for (const message of difference.added) {
      if (message.filePath && message.range && editorsMap[message.filePath]) {
        editorsMap[message.filePath].added.push(message)
      }
    }
    for (const message of difference.removed) {
      if (message.filePath && message.range && editorsMap[message.filePath]) {
        editorsMap[message.filePath].removed.push(message)
      }
    }
    for (const filePath of filePaths) {
      const value = editorsMap[filePath]
      if (value.added.length || value.removed.length) {
        for (const entry of value.editors) {
          entry.apply(value.added, value.removed)
        }
      }
    }
  }
  filterAndApply(editor: Editor) {
    const messages = []
    const editorPath = editor.textEditor.getPath()
    for (const message of this.messages) {
      if (message.filePath && message.range && message.filePath === editorPath) {
        messages.push(message)
      }
    }
    editor.apply(messages, editor.messages)
  }
  getEditor(textEditor: TextEditor): Editor {
    for (const entry of this.editors) {
      if (entry.textEditor === textEditor) {
        return textEditor
      }
    }
    const editor = new Editor(textEditor, this.highlightIssues)
    this.editors.add(editor)
    editor.onDidDestroy(() => {
      this.editors.delete(editor)
    })
    editor.onShouldRender(() => {
      if (this.showIssuesFrom === 'Current Line') {
        this.emitter.emit('should-render')
      }
    })<commit_msg>:racehorse: Minor optimization in apply difference<commit_after>'use babel'

/* @flow */

import { CompositeDisposable, Emitter } from 'atom'
import Editor from './editor'
import { getEditorsMap } from './helpers'
import type { Disposable, TextEditor } from 'atom'
import type { Linter$Difference, Linter$Message, Config$ShowIssues } from './types'

export default class Editors {
  emitter: Emitter;
  editors: Set<Editor>;
  messages: Array<Linter$Message>;
  subscriptions: CompositeDisposable;
  showIssuesFrom: Config$ShowIssues;
  highlightIssues: boolean;

  constructor() {
    this.emitter = new Emitter()
    this.editors = new Set()
    this.messages = []
    this.subscriptions = new CompositeDisposable()
    this.subscriptions.add(atom.config.observe('linter-ui-default.showIssuesFrom', showIssuesFrom => {
      this.showIssuesFrom = showIssuesFrom
    }))
    this.highlightIssues = atom.config.get('linter-ui-default.highlightIssues')
    this.subscriptions.add(atom.config.onDidChange('linter-ui-default.highlightIssues', function({ newValue }) {
      this.highlightIssues = newValue
    }))

    this.subscriptions.add(this.emitter)
  }
  activate() {
    this.subscriptions.add(atom.workspace.observeTextEditors(textEditor => {
      const editor = this.getEditor(textEditor)
      console.log(editor)
    }))
  }
  apply(difference: Linter$Difference) {
    if (!this.highlightIssues) {
      // Do not paint anything if highlighting issues is disabled
      return
    }
    this.messages = difference.messages

    const { editorsMap, filePaths } = getEditorsMap(this)
    for (const message of difference.added) {
      if (message.filePath && message.range && editorsMap[message.filePath]) {
        editorsMap[message.filePath].added.push(message)
      }
    }
    for (const message of difference.removed) {
      if (message.filePath && message.range && editorsMap[message.filePath]) {
        editorsMap[message.filePath].removed.push(message)
      }
    }
    for (const filePath of filePaths) {
      const value = editorsMap[filePath]
      if (value.added.length || value.removed.length) {
        if (value.editors.length === 1) {
          value.editors[0].apply(value.added, value.removed)
        } else if (value.editors.length === 2) {
          value.editors[0].apply(value.added, value.removed)
          value.editors[1].apply(value.added, value.removed)
        } else {
          for (const entry of value.editors) {
            entry.apply(value.added, value.removed)
          }
        }
      }
    }
  }
  filterAndApply(editor: Editor) {
    const messages = []
    const editorPath = editor.textEditor.getPath()
    for (const message of this.messages) {
      if (message.filePath && message.range && message.filePath === editorPath) {
        messages.push(message)
      }
    }
    editor.apply(messages, editor.messages)
  }
  getEditor(textEditor: TextEditor): Editor {
    for (const entry of this.editors) {
      if (entry.textEditor === textEditor) {
        return textEditor
      }
    }
    const editor = new Editor(textEditor, this.highlightIssues)
    this.editors.add(editor)
    editor.onDidDestroy(() => {
      this.editors.delete(editor)
    })
    editor.onShouldRender(() => {
      if (this.showIssuesFrom === 'Current Line') {
        this.emitter.emit('should-render')
      }
    })
{'fraction_non_alphanumeric': 0.12296365914786968, 'fraction_numerical': 0.0007832080200501253, 'mean_word_length': 3.103470437017995, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1623605', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1913, 'n_words': 377}