<commit_before>'use babel';
'use strict';

import {
  Point,
  Range
}
from 'atom';

export function activate(state) {
  atom.commands.add(
    'atom-workspace', 'texter:narrow-whitespace', () => this.narrowWhitespace()
  );
}

/**
 * Narrow whitespace around each cursor to a single space. Or expand if there is
 * no whitespace.
 */
export function narrowWhitespace() {
  const editor = atom.workspace.getActivePaneItem();
  if (!editor) return;

  const cursors = editor.getCursors();
  for (const cursor of cursors) {
    let range = new Range(cursor.getBufferPosition(), cursor.getBufferPosition());

    while (range.start.row > 0 || range.start.column > 0) {
      let left = new Point(range.start.row, range.start.column);
      if (left.column > 0) {
        left.column--;
      } else {
        left.row--;
        left.column = editor.lineTextForBufferRow(left.row).length;
      }
      const char = editor.getTextInBufferRange(new Range(left, range.start));
      if (!char.match(/^\s$/)) {
        break;
      }
      range.start = left;
    }

    while (range.end.row <= editor.getLineCount()) {
      let right = new Point(range.end.row, range.end.column);
      if (right.column >= editor.lineTextForBufferRow(right.row).length) {
        right.column = 0;
        right.row++;
      } else {
        right.column++;
      }
      const char = editor.getTextInBufferRange(new Range(range.end, right));
      if (!char.match(/^\s$/)) {
        break;
      }
      range.end = right;
    }

    editor.setTextInBufferRange(range, " ");

    if (range.start.isEqual(cursor.getBufferPosition())) {
      cursor.moveRight();
    }
  }
} // narrow
<commit_msg>Wrapped point-related functionality in a class (`TexterPoint`)<commit_after>'use babel';
'use strict';

import {
  Point,
  Range
}
from 'atom';

export function activate(state) {
  atom.commands.add(
    'atom-workspace', 'texter:narrow-whitespace', () => this.narrowWhitespace()
  );
}

class TexterPoint {

  constructor(editor, point) {
    this.editor = editor;
    this.row = point.row;
    this.column = point.column;
  }

  clone() {
    return new TexterPoint(this.editor, new Point(this.row, this.column));
  }

  isBOB() {
    return this.isBOL() && this.row <= 0;
  }

  isBOL() {
    return this.column <= 0;
  }

  isEOB() {
    return this.isEOL() && this.row >= this.editor.getLineCount();
  }

  isEOL() {
    return this.column >= this.editor.lineTextForBufferRow(this.row).length;
  }

  moveLeft(chars = 1) {
    if (chars < 0) return this.moveRight(0 - chars);
    for (var i = 0; i < chars; i++) {
      if (this.isBOB()) return;
      if (this.isBOL()) {
        this.row--;
        this.column = this.editor.lineTextForBufferRow(this.row).length;
      } else {
        this.column--;
      }
    }
  }

  moveRight(chars = 1) {
    if (chars < 0) return this.moveLeft(0 - chars);
    for (var i = 0; i < chars; i++) {
      if (this.isEOB()) return;
      if (this.isEOL()) {
        this.row++;
        this.column = 0;
      } else {
        this.column++;
      }
    }
  }

  str(length = 1) {
    const other = this.clone();
    other.moveRight(length);
    return this.editor.getTextInBufferRange(new Range(
      this.toArray(),
      other.toArray()
    ));
  }

  toArray() {
    return [this.row, this.column];
  }

} // class TexterPoint

/**
 * Narrow whitespace around each cursor to a single space. Or expand if there is
 * no whitespace.
 */
export function narrowWhitespace() {
  const editor = atom.workspace.getActiveTextEditor();
  if (!editor) return;

  const cursors = editor.getCursors();
  for (const cursor of cursors) {
    let left = new TexterPoint(editor, cursor.getBufferPosition());
    let move = false;
    let right = left.clone();

    while (left.str(-1).match(/\s/)) {
      left.moveLeft();
    }

    while (right.str(1).match(/\s/)) {
      right.moveRight();
      move = true;
    }

    editor.setTextInBufferRange(new Range(
      left.toArray(),
      right.toArray()
    ), " ");

    if (move) cursor.moveRight();
  }
} // narrow

{'fraction_non_alphanumeric': 0.1545253863134658, 'fraction_numerical': 0.004415011037527594, 'mean_word_length': 2.5064488392089426, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 11, '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': '7505711', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1349, 'n_words': 297}