<commit_before># Copyright (C) 2014  <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Not to be confused with GhostScript utility...

{Point} = require 'atom'
{CompositeDisposable} = require 'event-kit'
{$, View} = require 'space-pen'

module.exports =
class HackyGhostView
  subscriptions: null
  $ghostText: null

  constructor: (predictions, @$editor) ->
    @subscriptions = new CompositeDisposable

    @subscriptions.add predictions.onDidChangeIndex (index) =>
      @removeAll()
      return if not predictions.current()?

      {tokens, position} = predictions.current()
      console.assert tokens.length >= 1
      console.assert position.length is 2
      @setAt tokens, position

    @subscriptions.add predictions.onDidInvalidate (index) =>
      @removeAll()

    @subscriptions.add predictions.onDidChangePredictions (index) =>
      # TODO: Should anything even go in here?

  setAt: (tokens, position) ->
    [row, column] = position

    # XXX: This is a *disgusting* and fragile way to add the ghost-text; I
    # easily expect this to be broken in in the near future. I really
    # shouldn't be messing around with the editor DOM, but it's effective as
    # long as we're responsible with it...
    $row = $(".line[data-screen-row=#{row}]")
    console.assert not $row.empty()

    $sourceSpan = $row.children('.source, .text').first()
    @$ghostText = new GhostTextView(tokens)

    # TODO: place the element at the given column!
    $sourceSpan.append @$ghostText

    # This class will be useful in selectors.
    @$editor.addClass 'gamboge'

  removeAll: ->
    return unless @$ghostText?
    @$editor.find('.gamboge-ghost, .gamboge-invisible').remove()
    @$editor.removeClass 'gamboge'
    @$ghostText = null

  destroy: ->
    @removeAll()
    @subscriptions.dispose()


# A SpacePen view for a single continuous stretch of ghost-text.
class GhostTextView extends View
  @content: (tokens) ->
    @div class: 'gamboge-ghost', =>
      for token in tokens
        # Add a space, just to make sure we're still sane.
        @text ' '
        if token of specialChars<commit_msg>Automatically coerce editor DOM -> jQuery.<commit_after># Copyright (C) 2014  <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Not to be confused with GhostScript utility...

{Point} = require 'atom'
{CompositeDisposable} = require 'event-kit'
{$, View} = require 'space-pen'

module.exports =
class HackyGhostView
  subscriptions: null
  $ghostText: null

  constructor: (predictions, editorElement) ->
    @subscriptions = new CompositeDisposable
    # Coerce the input into a jQuery.
    @$editor = $(editorElement)
    console.assert not @$editor.empty()

    @subscriptions.add predictions.onDidChangeIndex (index) =>
      @removeAll()
      return if not predictions.current()?

      {tokens, position} = predictions.current()
      console.assert tokens.length >= 1
      console.assert position.length is 2
      @setAt tokens, position

    @subscriptions.add predictions.onDidInvalidate (index) =>
      @removeAll()

    @subscriptions.add predictions.onDidChangePredictions (index) =>
      # TODO: Should anything even go in here?

  setAt: (tokens, position) ->
    [row, column] = position

    debugger

    # XXX: This is a *disgusting* and fragile way to add the ghost-text; I
    # easily expect this to be broken in in the near future. I really
    # shouldn't be messing around with the editor DOM, but it's effective as
    # long as we're responsible with it...
    $row = @$editor.find(".line[data-screen-row=#{row}]")
    console.assert not $row.empty()

    $sourceSpan = $row.children('.source, .text').first()
    @$ghostText = new GhostTextView(tokens)

    # TODO: place the element at the given column!
    $sourceSpan.append @$ghostText

    # This class will be useful in selectors.
    @$editor.addClass('gamboge')

  removeAll: ->
    return unless @$ghostText?
    @$editor.find('.gamboge-ghost, .gamboge-invisible').remove()
    @$editor.removeClass('gamboge')
    @$ghostText = null

  destroy: ->
    @removeAll()
    @subscriptions.dispose()


# A SpacePen view for a single continuous stretch of ghost-text.
class GhostTextView extends View
  @content: (tokens) ->
    @div class: 'gamboge-ghost', =>
      for token in tokens
        # Add a space, just to make sure we're still sane.
        @text ' '
        if token of specialChars
{'fraction_non_alphanumeric': 0.10156527926004981, 'fraction_numerical': 0.0024902170046246885, 'mean_word_length': 3.389539422326308, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5427811', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1583, 'n_words': 617}