<commit_before>// HyperMD, copyright (c) by laobubu
// Distributed under an MIT license: http://laobubu.net/hypermd/LICENSE
//
// align table columns by adjusting table separators ("|") styles
// insert a <style> for every table and dynamically update it
//

(function (mod) {
  var CODEMIRROR_ROOT = window.CODEMIRROR_ROOT || "codemirror/";
  if (typeof exports == "object" && typeof module == "object") // CommonJS
    mod(
      require(CODEMIRROR_ROOT + "lib/codemirror"),
      require("./../hypermd")
    );
  else if (typeof define == "function" && define.amd) // AMD
    define([
      CODEMIRROR_ROOT + "lib/codemirror",
      "./../hypermd",
    ], mod);
  else // Plain browser env
    mod(CodeMirror, HyperMD);
})(function (CodeMirror, HyperMD) {
  "use strict";

  var DEBUG = false

  function isTableRow(cm, line) {
    return (line < cm.lineCount()) && cm.getStateAfter(line).overlay.table
  }

  /**
   * Create a table instance since `line`
   *
   * @param {Aligner} aligner
   * @param {number} line
   */
  function Table(aligner, line) {
    var styleEl = document.createElement("style")

    this.cm = aligner.cm
    this.aligner = aligner
    this.line = line
    this.mark = this.cm.setBookmark({ line: line, ch: 0 }, { widget: styleEl })
    this.styleEl = styleEl
    this.lastStyle = ""

    this.lineCount = 0
  }

  /**
   * re-measure every column's width, then update the spans' style
   *
   * @returns {boolean} style is updated or not
   */
  Table.prototype.measureAndAlign = function () {
    var cm = this.cm

    // we only care about visible lines
    // FIXME: assuming no row line is folded!
    var tmp = cm.getViewport()
    var vrow_from = tmp.from, vrow_to = tmp.to
    var lvs = cm.display.view

    var table_from = this.line, table_to = table_from
    while (isTableRow(cm, table_to + 1)) table_to++

    if (vrow_from > table_from) table_from = vrow_from
    if (vrow_to < table_to) table_to = vrow_to

    // now start checking every row
    // compute every column's width and fetch every separator <span> (both element and column width)

    /** @type {[HTMLSpanElement, number][][]} */
    var seps = []

    var col_widths = []
    var sep_widths = [] // separators' width, useful when calc position . note that sep_widths.length <= col_widths.width

    for (var y = table_from; y <= table_to; y++) {
      var lv = lvs[y - vrow_from]
      var span_and_colwidth_s = []

      var seps_raw = lv && lv.text && lv.text.querySelectorAll('span.cm-hmd-table-sep') || []
      var lastLeft = 0

      //FIXME: assuming every separator has the same width and no padding....
      var sep_width = 4
      if (seps_raw.length > 0) {<commit_msg>Fix: when inserting lines before table, style will disappear<commit_after>// HyperMD, copyright (c) by laobubu
// Distributed under an MIT license: http://laobubu.net/hypermd/LICENSE
//
// align table columns by adjusting table separators ("|") styles
// insert a <style> for every table and dynamically update it
//

(function (mod) {
  var CODEMIRROR_ROOT = window.CODEMIRROR_ROOT || "codemirror/";
  if (typeof exports == "object" && typeof module == "object") // CommonJS
    mod(
      require(CODEMIRROR_ROOT + "lib/codemirror"),
      require("./../hypermd")
    );
  else if (typeof define == "function" && define.amd) // AMD
    define([
      CODEMIRROR_ROOT + "lib/codemirror",
      "./../hypermd",
    ], mod);
  else // Plain browser env
    mod(CodeMirror, HyperMD);
})(function (CodeMirror, HyperMD) {
  "use strict";

  var DEBUG = false

  function isTableRow(cm, line) {
    return (line < cm.lineCount()) && cm.getStateAfter(line).overlay.table
  }

  /**
   * Create a table instance since `line`
   *
   * @param {Aligner} aligner
   * @param {number} line
   */
  function Table(aligner, line) {
    var styleEl = document.createElement("style")

    this.cm = aligner.cm
    this.aligner = aligner
    this.line = line
    this.mark = this.cm.setBookmark({ line: line, ch: 0 }, { widget: styleEl })
    this.styleEl = styleEl
    this.lastStyle = ""

    this.lineCount = 0
  }

  /**
   * re-measure every column's width, then update the spans' style
   *
   * @returns {boolean} style is updated or not
   */
  Table.prototype.measureAndAlign = function () {
    var cm = this.cm

    // use mark's position. this is much more reliable
    var start_pos = this.mark.find()
    if (!start_pos) {
      // seems table is removed
      this.lineCount = 0
      this.lastStyle = ""
      return false
    }
    this.line = start_pos.line

    // we only care about visible lines
    // FIXME: assuming no row line is folded!
    var tmp = cm.getViewport()
    var vrow_from = tmp.from, vrow_to = tmp.to
    var lvs = cm.display.view

    var table_from = this.line, table_to = table_from
    while (isTableRow(cm, table_to + 1)) table_to++

    if (vrow_from > table_from) table_from = vrow_from
    if (vrow_to < table_to) table_to = vrow_to

    // now start checking every row
    // compute every column's width and fetch every separator <span> (both element and column width)

    /** @type {[HTMLSpanElement, number][][]} */
    var seps = []

    var col_widths = []
    var sep_widths = [] // separators' width, useful when calc position . note that sep_widths.length <= col_widths.width

    for (var y = table_from; y <= table_to; y++) {
      var lv = lvs[y - vrow_from]
      var span_and_colwidth_s = []

      var seps_raw = lv && lv.text && lv.text.querySelectorAll('span.cm-hmd-table-sep') || []
      var lastLeft = 0

      //FIXME: assuming every separator has the same width and no padding....
      var sep_width = 4
      if (seps_raw.length > 0) {
{'fraction_non_alphanumeric': 0.1293330987154672, 'fraction_numerical': 0.0022875241949674467, 'mean_word_length': 2.7345597897503287, 'pattern_counts': {'":': 0, '<': 15, '<?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': '5910231', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1840, 'n_words': 550}