<commit_before>Grim = require 'grim'

# TODO: remove references to logical display buffer when it is released.

describe "WrapGuide", ->
  [editor, editorElement, wrapGuide, workspaceElement] = []

  getLeftPosition = (element) ->
    parseInt(element.style.left)

  beforeEach ->
    workspaceElement = atom.views.getView(atom.workspace)
    workspaceElement.style.height = "200px"
    workspaceElement.style.width = "1500px"

    jasmine.attachToDOM(workspaceElement)

    waitsForPromise ->
      atom.packages.activatePackage('wrap-guide')

    waitsForPromise ->
      atom.packages.activatePackage('language-javascript')

    waitsForPromise ->
      atom.packages.activatePackage('language-coffee-script')

    waitsForPromise ->
      atom.workspace.open('sample.js')

    runs ->
      editor = atom.workspace.getActiveTextEditor()
      editorElement = editor.getElement()
      wrapGuide = editorElement.rootElement.querySelector(".wrap-guide")

  describe ".activate", ->
    getWrapGuides  = ->
      wrapGuides = []
      atom.workspace.getTextEditors().forEach (editor) ->
        guide = editor.getElement().rootElement.querySelector(".wrap-guide")
        wrapGuides.push(guide) if guide
      wrapGuides

    it "appends a wrap guide to all existing and new editors", ->
      expect(atom.workspace.getPanes().length).toBe 1
      expect(getWrapGuides().length).toBe 1
      expect(getLeftPosition(getWrapGuides()[0])).toBeGreaterThan(0)

      atom.workspace.getActivePane().splitRight(copyActiveItem: true)
      expect(atom.workspace.getPanes().length).toBe 2
      expect(getWrapGuides().length).toBe 2
      expect(getLeftPosition(getWrapGuides()[0])).toBeGreaterThan(0)
      expect(getLeftPosition(getWrapGuides()[1])).toBeGreaterThan(0)

    it "positions the guide at the configured column", ->
      width = editor.getDefaultCharWidth() * wrapGuide.getDefaultColumn()
      expect(width).toBeGreaterThan(0)
      expect(Math.abs(getLeftPosition(wrapGuide) - width)).toBeLessThan 1
      expect(wrapGuide).toBeVisible()

  describe "when the font size changes", ->
    it "updates the wrap guide position", ->
      initial = getLeftPosition(wrapGuide)
      expect(initial).toBeGreaterThan(0)
      fontSize = atom.config.get("editor.fontSize")
      atom.config.set("editor.fontSize", fontSize + 10)

      advanceClock(1)
      expect(getLeftPosition(wrapGuide)).toBeGreaterThan(initial)
      expect(wrapGuide).toBeVisible()

  describe "when the column config changes", ->
    it "updates the wrap guide position", ->
      initial = getLeftPosition(wrapGuide)
      expect(initial).toBeGreaterThan(0)
      column = atom.config.get("editor.preferredLineLength")
      atom.config.set("editor.preferredLineLength", column + 10)
      expect(getLeftPosition(wrapGuide)).toBeGreaterThan(initial)
      expect(wrapGuide).toBeVisible()

      atom.config.set("wrap-guide.columns", [{pattern: ".*", column: column - 10}])
      expect(getLeftPosition(wrapGuide)).toBeLessThan(initial)
      expect(wrapGuide).toBeVisible()

  describe "when the editor's scroll left changes", ->
    it "updates the wrap guide position to a relative position on screen", ->
      editor.setText("a long line which causes the editor to scroll")
      if editorElement.logicalDisplayBuffer
        editorElement.setWidth(100)
      else
        editor.setWidth(100)

      initial = getLeftPosition(wrapGuide)
      expect(initial).toBeGreaterThan(0)

      if editorElement.logicalDisplayBuffer
        editorElement.setScrollLeft(10)
      else
        editor.setScrollLeft(10)

      expect(getLeftPosition(wrapGuide)).toBe(initial - 10)
      expect(wrapGuide).toBeVisible()

  describe "when the editor's grammar changes", ->
    it "updates the wrap guide position", ->
      atom.config.set('editor.preferredLineLength', 20, scopeSelector: '.source.js')
      initial = getLeftPosition(wrapGuide)
      expect(initial).toBeGreaterThan(0)
      expect(wrapGuide).toBeVisible()

      editor.setGrammar(atom.grammars.grammarForScopeName('text.plain.null-grammar'))
      expect(getLeftPosition(wrapGuide)).toBeGreaterThan(initial)
      expect(wrapGuide).toBeVisible()

    it 'listens for preferredLineLength updates for the new grammar', ->
      editor.setGrammar(atom.grammars.grammarForScopeName('source.coffee'))
      spyOn(wrapGuide, 'updateGuide')

      atom.config.set('editor.preferredLineLength', 20, scopeSelector: '.source.coffee')

      expect(wrapGuide.updateGuide).toHaveBeenCalled()

    it 'listens for wrap-guide.enabled updates for the new grammar', ->
      editor.setGrammar(atom.grammars.grammarForScopeName('source.coffee'))
      spyOn(wrapGuide, 'updateGuide')

      atom.config.set('wrap-guide.enabled', false, scopeSelecotr: '.source.coffee')<commit_msg>Perform a document poll after setting the editorElement width<commit_after>Grim = require 'grim'

# TODO: remove references to logical display buffer when it is released.

describe "WrapGuide", ->
  [editor, editorElement, wrapGuide, workspaceElement] = []

  getLeftPosition = (element) ->
    parseInt(element.style.left)

  beforeEach ->
    workspaceElement = atom.views.getView(atom.workspace)
    workspaceElement.style.height = "200px"
    workspaceElement.style.width = "1500px"

    jasmine.attachToDOM(workspaceElement)

    waitsForPromise ->
      atom.packages.activatePackage('wrap-guide')

    waitsForPromise ->
      atom.packages.activatePackage('language-javascript')

    waitsForPromise ->
      atom.packages.activatePackage('language-coffee-script')

    waitsForPromise ->
      atom.workspace.open('sample.js')

    runs ->
      editor = atom.workspace.getActiveTextEditor()
      editorElement = editor.getElement()
      wrapGuide = editorElement.rootElement.querySelector(".wrap-guide")

  describe ".activate", ->
    getWrapGuides  = ->
      wrapGuides = []
      atom.workspace.getTextEditors().forEach (editor) ->
        guide = editor.getElement().rootElement.querySelector(".wrap-guide")
        wrapGuides.push(guide) if guide
      wrapGuides

    it "appends a wrap guide to all existing and new editors", ->
      expect(atom.workspace.getPanes().length).toBe 1
      expect(getWrapGuides().length).toBe 1
      expect(getLeftPosition(getWrapGuides()[0])).toBeGreaterThan(0)

      atom.workspace.getActivePane().splitRight(copyActiveItem: true)
      expect(atom.workspace.getPanes().length).toBe 2
      expect(getWrapGuides().length).toBe 2
      expect(getLeftPosition(getWrapGuides()[0])).toBeGreaterThan(0)
      expect(getLeftPosition(getWrapGuides()[1])).toBeGreaterThan(0)

    it "positions the guide at the configured column", ->
      width = editor.getDefaultCharWidth() * wrapGuide.getDefaultColumn()
      expect(width).toBeGreaterThan(0)
      expect(Math.abs(getLeftPosition(wrapGuide) - width)).toBeLessThan 1
      expect(wrapGuide).toBeVisible()

  describe "when the font size changes", ->
    it "updates the wrap guide position", ->
      initial = getLeftPosition(wrapGuide)
      expect(initial).toBeGreaterThan(0)
      fontSize = atom.config.get("editor.fontSize")
      atom.config.set("editor.fontSize", fontSize + 10)

      advanceClock(1)
      expect(getLeftPosition(wrapGuide)).toBeGreaterThan(initial)
      expect(wrapGuide).toBeVisible()

  describe "when the column config changes", ->
    it "updates the wrap guide position", ->
      initial = getLeftPosition(wrapGuide)
      expect(initial).toBeGreaterThan(0)
      column = atom.config.get("editor.preferredLineLength")
      atom.config.set("editor.preferredLineLength", column + 10)
      expect(getLeftPosition(wrapGuide)).toBeGreaterThan(initial)
      expect(wrapGuide).toBeVisible()

      atom.config.set("wrap-guide.columns", [{pattern: ".*", column: column - 10}])
      expect(getLeftPosition(wrapGuide)).toBeLessThan(initial)
      expect(wrapGuide).toBeVisible()

  describe "when the editor's scroll left changes", ->
    it "updates the wrap guide position to a relative position on screen", ->
      editor.setText("a long line which causes the editor to scroll")
      editorElement.style.width = "100px"
      atom.views.performDocumentPoll()
      initial = getLeftPosition(wrapGuide)
      expect(initial).toBeGreaterThan(0)
      editorElement.setScrollLeft(10)
      expect(getLeftPosition(wrapGuide)).toBe(initial - 10)
      expect(wrapGuide).toBeVisible()

  describe "when the editor's grammar changes", ->
    it "updates the wrap guide position", ->
      atom.config.set('editor.preferredLineLength', 20, scopeSelector: '.source.js')
      initial = getLeftPosition(wrapGuide)
      expect(initial).toBeGreaterThan(0)
      expect(wrapGuide).toBeVisible()

      editor.setGrammar(atom.grammars.grammarForScopeName('text.plain.null-grammar'))
      expect(getLeftPosition(wrapGuide)).toBeGreaterThan(initial)
      expect(wrapGuide).toBeVisible()

    it 'listens for preferredLineLength updates for the new grammar', ->
      editor.setGrammar(atom.grammars.grammarForScopeName('source.coffee'))
      spyOn(wrapGuide, 'updateGuide')

      atom.config.set('editor.preferredLineLength', 20, scopeSelector: '.source.coffee')

      expect(wrapGuide.updateGuide).toHaveBeenCalled()

    it 'listens for wrap-guide.enabled updates for the new grammar', ->
      editor.setGrammar(atom.grammars.grammarForScopeName('source.coffee'))
      spyOn(wrapGuide, 'updateGuide')

      atom.config.set('wrap-guide.enabled', false, scopeSelecotr: '.source.coffee')
{'fraction_non_alphanumeric': 0.11961320159764557, 'fraction_numerical': 0.009144418751313853, 'mean_word_length': 3.844704684317719, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, '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': '787040', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2695, 'n_words': 501}