<commit_before>/*
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {DEFAULT_THEMES, THEME_PROPERTIES, setTheme} from './themes.js';

const WARN_LINES = 15;
const WARN_LINE_LENGTH = 80;

const $editor = $('#editor');
const $output = $('#output');

let config = {
  code: localStorage.highlighterCode || '',
  theme: localStorage.highlighterTheme || 'light',
  lang: localStorage.highlighterLang || '(auto)',
  font: localStorage.highlighterFont || 'Roboto Mono',
  tabSize: Number(localStorage.highlighterTabSize || '4'),
  typeSize: Number(localStorage.highlighterTypeSize || '40'),
  selectionTreatment: localStorage.highlighterSelectionTreatment || '--',
  customTheme: JSON.parse(localStorage.customTheme || JSON.stringify(DEFAULT_THEMES['light'])),
};

if (config.lang == '--') {
  config.lang = '(auto)';
}

let editor;

setupToolbar();
setupEditor();
setupOutputArea();
updateOutputArea();
setupCustomThemeEditor();
loadFont();
installServiceWorker();


function setupEditor() {
  let updateCode_ = code => {
    localStorage.highlighterCode = config.code = code;
    updateOutputArea();
  };

  if (navigator.userAgent.match(/iP(hone|od|ad)|Android/)) {
    // Ace editor is pretty busted on mobile, just use a <textarea>
    let $textArea = $('<textarea>')
        .attr('autocapitalize', 'off')
        .attr('spellcheck', 'false')
        .val(config.code)
        .on('input', () => updateCode_($textArea.val()))
        .appendTo($editor);
    return;
  }

  editor = ace.edit($editor.get(0));
  editor.$blockScrolling = Infinity;
  editor.setValue(config.code, -1);
  editor.setTheme('ace/theme/chrome');
  editor.getSession().setMode('ace/mode/text');
  editor.setOptions({
    fontFamily: 'Roboto Mono',
    fontSize: '11pt',
  });
  editor.on('change', () => updateCode_(editor.getValue()));
  editor.getSelection().on('changeCursor', () => updateOutputArea());
  editor.getSelection().on('changeSelection', () => updateOutputArea());
  updateEditorParams();
}


function updateEditorParams() {
  if (!editor) {
    return;
  }

  editor.setOptions({
    fontFamily: config.font,
    fontSize: '11pt',
  });
  editor.getSession().setTabSize(config.tabSize);
}


function setupOutputArea() {
  // select all on click
  $output.click(() => {
    var selection = window.getSelection();
    var range = document.createRange();
    range.selectNodeContents($output.find('pre').get(0));
    selection.removeAllRanges();
    selection.addRange(range);
  });

  // re-layout on window resize
  $(window).on('resize', () => updateOutputArea());
  document.fonts.ready.then(() => updateOutputArea());
}


function setupToolbar() {
  $('#theme')
      .val(config.theme)
      .on('input', ev => {
        localStorage.highlighterTheme = config.theme = $(ev.target).val();
        updateOutputArea();
      });

  $('#lang')
      .val(config.lang)
      .on('input', ev => {
        localStorage.highlighterLang = config.lang = $(ev.target).val();
        updateOutputArea();
      });

  let $dl = $('#lang-datalist');
  let langs = Object.keys(Prism.languages)
      .filter(s => typeof Prism.languages[s] == 'object');
  for (let lang of langs) {
    $dl.append($('<option>').attr('value', lang));
  }

  $('#tab-size')
      .val(config.tabSize)
      .on('input', ev => {
        localStorage.highlighterTabSize = $(ev.target).val();
        config.tabSize = Number(localStorage.highlighterTabSize);
        updateEditorParams();
        updateOutputArea();
      });

  $('#font')
      .val(config.font)
      .on('input', ev => {
        localStorage.highlighterFont = config.font = $(ev.target).val();
        loadFont();
      });

  $('#selection-treatment')
      .val(config.selectionTreatment)
      .on('input', ev => {
        localStorage.highlighterSelectionTreatment = config.selectionTreatment = $(ev.target).val();
        updateOutputArea();
      });

  let $typeSize = $('#type-size');

  let setTypeSize_ = size => {
    if ($typeSize.val() != String(size)) {
      $typeSize.val(size);
    }
    config.typeSize = size;
    localStorage.highlighterTypeSize = String(config.typeSize);
    updateOutputArea();
  };

  $typeSize
      .val(config.typeSize)
      .on('input', () => {
        let val = parseInt($typeSize.val(), 10);
        if (!isNaN(val) && val > 8) {
          setTypeSize_(val);
        }
      })
      .on('keydown', ev => {
        if (!ev.shiftKey) {
          if (ev.keyCode == 38 || ev.keyCode == 40) {
            setTypeSize_(parseInt($typeSize.val(), 10) + (ev.keyCode == 38 ? 1 : -1));
            ev.preventDefault();
          }
        }
      })<commit_msg>Don't freak out about really small type sizes<commit_after>/*
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {DEFAULT_THEMES, THEME_PROPERTIES, setTheme} from './themes.js';

const WARN_LINES = 15;
const WARN_LINE_LENGTH = 80;

const $editor = $('#editor');
const $output = $('#output');

let config = {
  code: localStorage.highlighterCode || '',
  theme: localStorage.highlighterTheme || 'light',
  lang: localStorage.highlighterLang || '(auto)',
  font: localStorage.highlighterFont || 'Roboto Mono',
  tabSize: Number(localStorage.highlighterTabSize || '4'),
  typeSize: Number(localStorage.highlighterTypeSize || '40'),
  selectionTreatment: localStorage.highlighterSelectionTreatment || '--',
  customTheme: JSON.parse(localStorage.customTheme || JSON.stringify(DEFAULT_THEMES['light'])),
};

if (config.lang == '--') {
  config.lang = '(auto)';
}

let editor;

setupToolbar();
setupEditor();
setupOutputArea();
updateOutputArea();
setupCustomThemeEditor();
loadFont();
installServiceWorker();


function setupEditor() {
  let updateCode_ = code => {
    localStorage.highlighterCode = config.code = code;
    updateOutputArea();
  };

  if (navigator.userAgent.match(/iP(hone|od|ad)|Android/)) {
    // Ace editor is pretty busted on mobile, just use a <textarea>
    let $textArea = $('<textarea>')
        .attr('autocapitalize', 'off')
        .attr('spellcheck', 'false')
        .val(config.code)
        .on('input', () => updateCode_($textArea.val()))
        .appendTo($editor);
    return;
  }

  editor = ace.edit($editor.get(0));
  editor.$blockScrolling = Infinity;
  editor.setValue(config.code, -1);
  editor.setTheme('ace/theme/chrome');
  editor.getSession().setMode('ace/mode/text');
  editor.setOptions({
    fontFamily: 'Roboto Mono',
    fontSize: '11pt',
  });
  editor.on('change', () => updateCode_(editor.getValue()));
  editor.getSelection().on('changeCursor', () => updateOutputArea());
  editor.getSelection().on('changeSelection', () => updateOutputArea());
  updateEditorParams();
}


function updateEditorParams() {
  if (!editor) {
    return;
  }

  editor.setOptions({
    fontFamily: config.font,
    fontSize: '11pt',
  });
  editor.getSession().setTabSize(config.tabSize);
}


function setupOutputArea() {
  // select all on click
  $output.click(() => {
    var selection = window.getSelection();
    var range = document.createRange();
    range.selectNodeContents($output.find('pre').get(0));
    selection.removeAllRanges();
    selection.addRange(range);
  });

  // re-layout on window resize
  $(window).on('resize', () => updateOutputArea());
  document.fonts.ready.then(() => updateOutputArea());
}


function setupToolbar() {
  $('#theme')
      .val(config.theme)
      .on('input', ev => {
        localStorage.highlighterTheme = config.theme = $(ev.target).val();
        updateOutputArea();
      });

  $('#lang')
      .val(config.lang)
      .on('input', ev => {
        localStorage.highlighterLang = config.lang = $(ev.target).val();
        updateOutputArea();
      });

  let $dl = $('#lang-datalist');
  let langs = Object.keys(Prism.languages)
      .filter(s => typeof Prism.languages[s] == 'object');
  for (let lang of langs) {
    $dl.append($('<option>').attr('value', lang));
  }

  $('#tab-size')
      .val(config.tabSize)
      .on('input', ev => {
        localStorage.highlighterTabSize = $(ev.target).val();
        config.tabSize = Number(localStorage.highlighterTabSize);
        updateEditorParams();
        updateOutputArea();
      });

  $('#font')
      .val(config.font)
      .on('input', ev => {
        localStorage.highlighterFont = config.font = $(ev.target).val();
        loadFont();
      });

  $('#selection-treatment')
      .val(config.selectionTreatment)
      .on('input', ev => {
        localStorage.highlighterSelectionTreatment = config.selectionTreatment = $(ev.target).val();
        updateOutputArea();
      });

  let $typeSize = $('#type-size');

  let setTypeSize_ = size => {
    if ($typeSize.val() != String(size)) {
      $typeSize.val(size);
    }
    config.typeSize = size;
    localStorage.highlighterTypeSize = String(config.typeSize);
    updateOutputArea();
  };

  $typeSize
      .val(config.typeSize)
      .on('input', () => {
        let val = parseInt($typeSize.val(), 10);
        if (!isNaN(val) && val > 4) {
          setTypeSize_(val);
        }
      })
      .on('keydown', ev => {
        if (!ev.shiftKey) {
          if (ev.keyCode == 38 || ev.keyCode == 40) {
            setTypeSize_(parseInt($typeSize.val(), 10) + (ev.keyCode == 38 ? 1 : -1));
            ev.preventDefault();
          }
        }
      })
{'fraction_non_alphanumeric': 0.16836931380107942, 'fraction_numerical': 0.006746337702390131, 'mean_word_length': 3.2598522167487687, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 45, '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': '2778746', 'n_tokens_mistral': 3262, 'n_tokens_neox': 3162, 'n_words': 673}