Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Expand options for preferences lookups #6715

Merged
merged 17 commits into from
Feb 14, 2014
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
270 changes: 135 additions & 135 deletions src/editor/Editor.js

Large diffs are not rendered by default.

79 changes: 29 additions & 50 deletions src/editor/EditorOptionHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,25 @@ define(function (require, exports, module) {
Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
PreferencesManager = require("preferences/PreferencesManager"),
Strings = require("strings");
Strings = require("strings"),
_ = require("thirdparty/lodash");

// Constants for the preferences referred to in this file
var SHOW_LINE_NUMBERS = "showLineNumbers",
STYLE_ACTIVE_LINE = "styleActiveLine",
WORD_WRAP = "wordWrap",
CLOSE_BRACKETS = "closeBrackets";

/**
* @private
*
* Maps from preference names to the parameters needed to update the checked status.
* Maps from preference names to the command names needed to update the checked status.
*/
var _optionMapping = {
showLineNumbers: [Commands.TOGGLE_LINE_NUMBERS, "getShowLineNumbers"],
styleActiveLine: [Commands.TOGGLE_ACTIVE_LINE, "getShowActiveLine"],
wordWrap: [Commands.TOGGLE_WORD_WRAP, "getWordWrap"],
closeBrackets: [Commands.TOGGLE_CLOSE_BRACKETS, "getCloseBrackets"]
};
var _optionMapping = {};
_optionMapping[SHOW_LINE_NUMBERS] = Commands.TOGGLE_LINE_NUMBERS;
_optionMapping[STYLE_ACTIVE_LINE] = Commands.TOGGLE_ACTIVE_LINE;
_optionMapping[WORD_WRAP] = Commands.TOGGLE_WORD_WRAP;
_optionMapping[CLOSE_BRACKETS] = Commands.TOGGLE_CLOSE_BRACKETS;

/**
* @private
Expand All @@ -59,7 +65,7 @@ define(function (require, exports, module) {
if (!mapping) {
return;
}
CommandManager.get(mapping[0]).setChecked(Editor[mapping[1]]());
CommandManager.get(mapping).setChecked(PreferencesManager.get(name));
}

// Listen to preference changes for the preferences we care about
Expand All @@ -71,53 +77,26 @@ define(function (require, exports, module) {

/**
* @private
* Activates/Deactivates showing line numbers option
*/
function _toggleLineNumbers() {
Editor.setShowLineNumbers(!Editor.getShowLineNumbers());
_updateCheckedState("showLineNumbers");
}


/**
* @private
* Activates/Deactivates showing active line option
*/
function _toggleActiveLine() {
Editor.setShowActiveLine(!Editor.getShowActiveLine());
_updateCheckedState("styleActiveLine");
}


/**
* @private
* Activates/Deactivates word wrap option
*/
function _toggleWordWrap() {
Editor.setWordWrap(!Editor.getWordWrap());
_updateCheckedState("wordWrap");
}

/**
* @private
* Activates/Deactivates the automatic close brackets option
* Creates a function that will toggle the named preference.
*
* @param {string} prefName Name of preference that should be toggled by the function
*/
function _toggleCloseBrackets() {
Editor.setCloseBrackets(!Editor.getCloseBrackets());
_updateCheckedState("closeBrackets");
function _getToggler(prefName) {
return function _toggleLineNumbers() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_toggleLineNumbers() seems to be an unnecessary name. If it's necessary, then it's misnamed. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was a copy/pasto :)

PreferencesManager.set(prefName, !PreferencesManager.get(prefName));
};
}

function _init() {
CommandManager.get(Commands.TOGGLE_LINE_NUMBERS).setChecked(Editor.getShowLineNumbers());
CommandManager.get(Commands.TOGGLE_ACTIVE_LINE).setChecked(Editor.getShowActiveLine());
CommandManager.get(Commands.TOGGLE_WORD_WRAP).setChecked(Editor.getWordWrap());
CommandManager.get(Commands.TOGGLE_CLOSE_BRACKETS).setChecked(Editor.getCloseBrackets());
_.each(_optionMapping, function (commandName, prefName) {
CommandManager.get(commandName).setChecked(PreferencesManager.get(prefName));
});
}

CommandManager.register(Strings.CMD_TOGGLE_LINE_NUMBERS, Commands.TOGGLE_LINE_NUMBERS, _toggleLineNumbers);
CommandManager.register(Strings.CMD_TOGGLE_ACTIVE_LINE, Commands.TOGGLE_ACTIVE_LINE, _toggleActiveLine);
CommandManager.register(Strings.CMD_TOGGLE_WORD_WRAP, Commands.TOGGLE_WORD_WRAP, _toggleWordWrap);
CommandManager.register(Strings.CMD_TOGGLE_CLOSE_BRACKETS, Commands.TOGGLE_CLOSE_BRACKETS, _toggleCloseBrackets);
CommandManager.register(Strings.CMD_TOGGLE_LINE_NUMBERS, Commands.TOGGLE_LINE_NUMBERS, _getToggler(SHOW_LINE_NUMBERS));
CommandManager.register(Strings.CMD_TOGGLE_ACTIVE_LINE, Commands.TOGGLE_ACTIVE_LINE, _getToggler(STYLE_ACTIVE_LINE));
CommandManager.register(Strings.CMD_TOGGLE_WORD_WRAP, Commands.TOGGLE_WORD_WRAP, _getToggler(WORD_WRAP));
CommandManager.register(Strings.CMD_TOGGLE_CLOSE_BRACKETS, Commands.TOGGLE_CLOSE_BRACKETS, _getToggler(CLOSE_BRACKETS));

AppInit.htmlReady(_init);
});
21 changes: 21 additions & 0 deletions src/file/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,26 @@ define(function (require, exports, module) {

return baseName.substr(idx + 1);
}

/**
* Computes filename as relative to the basePath. For example:
* basePath: /foo/bar/, filename: /foo/bar/baz.txt
* returns: baz.txt
*
* The net effect is that the common prefix is stripped away. If basePath is not
* a prefix of filename, then undefined is returned.
*
* @param {string} basePath Path against which we're computing the relative path
* @param {string} filename Full path to the file for which we are computing a relative path
* @return {string} relative path
*/
function getRelativeFilename(basePath, filename) {
if (!filename || filename.substr(0, basePath.length) !== basePath) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference with ProjectManager.makeProjectRelativeIfPossible is that this one returns undefined when it's not possible to make it relative and some of the uses seem to rely on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The big difference is that this one is just a general utility function that is not tied to projects. It's a bit surprising that we didn't have this kind of function in FileUtils already,.

}

return filename.substr(basePath.length);
}

/** @const - hard-coded for now, but may want to make these preferences */
var _staticHtmlFileExts = ["htm", "html"],
Expand Down Expand Up @@ -404,6 +424,7 @@ define(function (require, exports, module) {
exports.isServerHtmlFileExt = isServerHtmlFileExt;
exports.getDirectoryPath = getDirectoryPath;
exports.getBaseName = getBaseName;
exports.getRelativeFilename = getRelativeFilename;
exports.getFileExtension = getFileExtension;
exports.compareFilenames = compareFilenames;
});
Loading