<commit_before>/*******************************************************************************
 * @license
 * Copyright (c) 2012 VMware, Inc. All Rights Reserved.
 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
 * ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
 * CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
 * You can obtain a current copy of the Eclipse Public License from
 * http://www.opensource.org/licenses/eclipse-1.0.php
 *
 * Contributors:
 *     <NAME>
 ******************************************************************************/

//////////////////////////////////////////////////////////////////////////////////
// This module provides some API to manage keybindings in a scripted editor.
//
// Stuff like retrieve the 'current keybindings' from a live scripted editor.
// Change the keybindings etc.
//////////////////////////////////////////////////////////////////////////////////

define(['./keystroke', 'scripted/utils/os', 'servlets/config-client'], function (mKeystroke, OS, mConfig) {

var keybinding2keystroke = mKeystroke.fromKeyBinding;
var keystroke2keybinding = mKeystroke.toKeyBinding;
var getScriptedRcFile = mConfig.getScriptedRcFile;

/**
 * Information about the default keybindings. These are not statically known. They
 * are whatever keybindings are registered in the editor by the time 'keybinder' gets
 * a crack at changing them.
 *
 * These defaults may depend on things such as the current browser and operating system.
 * Keybindings from 'exec-keys' in .scripted also end up in the defaults. Maybe that
 * is undesirable, but for now this is how things work.
 */
var defaults = {
	keybindings: null,
	unboundNames: null
};

/**
 * Set (or unset) a keybinding on a specific editor.
 * A keybinding maps a keystroke to specific editor action.
 * @param editor Reference to a scripted editor.
 * @param {String} A Scripted keystroke string
 * @param {String|null} Name of an action to bind keystroke to an action or null to unbind.
 */
function setKeyBinding(editor, keySpec, actionName) {
	var tv = editor.getTextView();
	tv.setKeyBinding(
		keystroke2keybinding(keySpec),
		actionName
	);
}

function toJSON(kbs) {
	var json = {};
	for (var i = 0; i < kbs.length; i++) {
		var keyBinding = kbs[i];
		if (keyBinding.name) {
			json[keybinding2keystroke(keyBinding.keyBinding)] = keyBinding.name;
		}
	}
	return json;
}

/**
 * Retrieve an editor's current keybindings as JSON-able object in scripted format.
 * I.e. as a map where the property names are 'keystroke' strings and the values
 * are action names.
 */
function getKeyBindings(editor) {
	return toJSON(editor.getTextView()._keyBindings.slice(0));
}

/**
 * Dumps a current key bindings for a given editor onto the console
 * in json format. The printed format should be something we
 * can copy paste into a config file.
 */
function dumpCurrentKeyBindings(editor) {
	console.log(JSON.stringify(getKeyBindings(editor), null, '  '));
}

/**
 * Retrieve the user's custom keybindings and apply them to the given editor.
 */
function installOn(editor) {
	//Before modifying any key bindings capture current keybindings state as the defaults:
	if (!defaults.keybindings) {
		//note that defaults are only captured once even if we see multiple editor instances
		//over the lifetime of the browser window. This could be problematic if the 'defaults'
		//are not the same for all editor instances.
		console.log('default keybindings are: ');
		defaults.keybindings = getKeyBindings(editor);
		console.log(defaults.keybindings, null, '  ');
		defaults.unboundNames = getUnboundActionNames(editor);
		console.log('default unbound action names are: ');
		console.log(JSON.stringify(defaults.unboundNames));
	}<commit_msg>Fix logging code that dumps out default keybindings.<commit_after>/*******************************************************************************
 * @license
 * Copyright (c) 2012 VMware, Inc. All Rights Reserved.
 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
 * ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
 * CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
 * You can obtain a current copy of the Eclipse Public License from
 * http://www.opensource.org/licenses/eclipse-1.0.php
 *
 * Contributors:
 *     <NAME>
 ******************************************************************************/

//////////////////////////////////////////////////////////////////////////////////
// This module provides some API to manage keybindings in a scripted editor.
//
// Stuff like retrieve the 'current keybindings' from a live scripted editor.
// Change the keybindings etc.
//////////////////////////////////////////////////////////////////////////////////

define(['./keystroke', 'scripted/utils/os', 'servlets/config-client'], function (mKeystroke, OS, mConfig) {

var keybinding2keystroke = mKeystroke.fromKeyBinding;
var keystroke2keybinding = mKeystroke.toKeyBinding;
var getScriptedRcFile = mConfig.getScriptedRcFile;

/**
 * Information about the default keybindings. These are not statically known. They
 * are whatever keybindings are registered in the editor by the time 'keybinder' gets
 * a crack at changing them.
 *
 * These defaults may depend on things such as the current browser and operating system.
 * Keybindings from 'exec-keys' in .scripted also end up in the defaults. Maybe that
 * is undesirable, but for now this is how things work.
 */
var defaults = {
	keybindings: null,
	unboundNames: null
};

/**
 * Set (or unset) a keybinding on a specific editor.
 * A keybinding maps a keystroke to specific editor action.
 * @param editor Reference to a scripted editor.
 * @param {String} A Scripted keystroke string
 * @param {String|null} Name of an action to bind keystroke to an action or null to unbind.
 */
function setKeyBinding(editor, keySpec, actionName) {
	var tv = editor.getTextView();
	tv.setKeyBinding(
		keystroke2keybinding(keySpec),
		actionName
	);
}

function toJSON(kbs) {
	var json = {};
	for (var i = 0; i < kbs.length; i++) {
		var keyBinding = kbs[i];
		if (keyBinding.name) {
			json[keybinding2keystroke(keyBinding.keyBinding)] = keyBinding.name;
		}
	}
	return json;
}

/**
 * Retrieve an editor's current keybindings as JSON-able object in scripted format.
 * I.e. as a map where the property names are 'keystroke' strings and the values
 * are action names.
 */
function getKeyBindings(editor) {
	return toJSON(editor.getTextView()._keyBindings.slice(0));
}

/**
 * Dumps a current key bindings for a given editor onto the console
 * in json format. The printed format should be something we
 * can copy paste into a config file.
 */
function dumpCurrentKeyBindings(editor) {
	console.log(JSON.stringify(getKeyBindings(editor), null, '  '));
}

/**
 * Retrieve the user's custom keybindings and apply them to the given editor.
 */
function installOn(editor) {
	//Before modifying any key bindings capture current keybindings state as the defaults:
	if (!defaults.keybindings) {
		//note that defaults are only captured once even if we see multiple editor instances
		//over the lifetime of the browser window. This could be problematic if the 'defaults'
		//are not the same for all editor instances.
		console.log('default keybindings are: ');
		defaults.keybindings = getKeyBindings(editor);
		console.log(JSON.stringify(defaults.keybindings, null, '  '));
		defaults.unboundNames = getUnboundActionNames(editor);
		console.log('default unbound action names are: ');
		console.log(JSON.stringify(defaults.unboundNames));
	}
{'fraction_non_alphanumeric': 0.1707734879430755, 'fraction_numerical': 0.0031624719989458427, 'mean_word_length': 3.5667870036101084, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '5893198', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2043, 'n_words': 783}