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

Provide editor options "line numbers", "active line" and "word wrap" as ... #3097

Merged
merged 15 commits into from
Mar 20, 2013
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ module.exports = function (grunt) {
'src/thirdparty/CodeMirror2/lib/codemirror.js',
'src/thirdparty/CodeMirror2/lib/util/dialog.js',
'src/thirdparty/CodeMirror2/lib/util/searchcursor.js',
'src/thirdparty/CodeMirror2/addon/edit/closetag.js',
'src/thirdparty/CodeMirror2/addon/selection/active-line.js',
'src/thirdparty/mustache/mustache.js',
'src/thirdparty/path-utils/path-utils.min'
],
Expand Down
1 change: 1 addition & 0 deletions src/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ define(function (require, exports, module) {
require("document/ChangedDocumentTracker");
require("editor/EditorStatusBar");
require("editor/EditorCommandHandlers");
require("editor/EditorOptionHandlers");
require("view/ViewCommandHandlers");
require("help/HelpCommandHandlers");
require("search/FindInFiles");
Expand Down
3 changes: 3 additions & 0 deletions src/command/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ define(function (require, exports, module) {
exports.VIEW_RESTORE_FONT_SIZE = "view.restoreFontSize";
exports.VIEW_SCROLL_LINE_UP = "view.scrollLineUp";
exports.VIEW_SCROLL_LINE_DOWN = "view.scrollLineDown";
exports.TOGGLE_LINE_NUMBERS = "view.toggleLineNumbers";
exports.TOGGLE_ACTIVE_LINE = "view.toggleActiveLine";
exports.TOGGLE_WORD_WRAP = "view.toggleWordWrap";
exports.TOGGLE_JSLINT = "debug.jslint";
exports.SORT_WORKINGSET_BY_ADDED = "view.sortWorkingSetByAdded";
exports.SORT_WORKINGSET_BY_NAME = "view.sortWorkingSetByName";
Expand Down
4 changes: 4 additions & 0 deletions src/command/DefaultMenus.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ define(function (require, exports, module) {
menu.addMenuItem(Commands.VIEW_DECREASE_FONT_SIZE);
menu.addMenuItem(Commands.VIEW_RESTORE_FONT_SIZE);
menu.addMenuDivider();
menu.addMenuItem(Commands.TOGGLE_LINE_NUMBERS);
menu.addMenuItem(Commands.TOGGLE_ACTIVE_LINE);
menu.addMenuItem(Commands.TOGGLE_WORD_WRAP);
menu.addMenuDivider();
menu.addMenuItem(Commands.TOGGLE_JSLINT);

/*
Expand Down
102 changes: 78 additions & 24 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ define(function (require, exports, module) {
TokenUtils = require("utils/TokenUtils"),
ViewUtils = require("utils/ViewUtils");

var defaultPrefs = { useTabChar: false, tabSize: 4, indentUnit: 4, closeBrackets: false };

defaultPrefs = { useTabChar: false, tabSize: 4, indentUnit: 4, closeBrackets: false,
showLineNumbers: true, styleActiveLine: true, wordWrap: true };
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing the var now. Seems like this is what made the build fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I was unable to launch and still looking for the culprit.


/** Editor preferences */
var _prefs = PreferencesManager.getPreferenceStorage(module, defaultPrefs);
//TODO: Remove preferences migration code
Expand All @@ -91,6 +92,15 @@ define(function (require, exports, module) {
/** @type {boolean} Global setting: Auto closes (, {, [, " and ' */
var _closeBrackets = _prefs.getValue("closeBrackets");

/** @type {boolean} Global setting: Show line numbers in the gutter */
var _showLineNumbers = _prefs.getValue("showLineNumbers");

/** @type {boolean} Global setting: Highlight the background of the line that has the cursor */
var _styleActiveLine = _prefs.getValue("styleActiveLine");

/** @type {boolean} Global setting: Auto wrap lines */
var _wordWrap = _prefs.getValue("wordWrap");

/** @type {boolean} Guard flag to prevent focus() reentrancy (via blur handlers), even across Editors */
var _duringFocus = false;

Expand Down Expand Up @@ -345,9 +355,11 @@ define(function (require, exports, module) {
indentWithTabs: _useTabChar,
tabSize: _tabSize,
indentUnit: _indentUnit,
lineNumbers: true,
lineNumbers: _showLineNumbers,
lineWrapping: _wordWrap,
styleActiveLine: _styleActiveLine,
matchBrackets: true,
dragDrop: false, // work around issue #1123
dragDrop: true,
extraKeys: codeMirrorKeyMap,
autoCloseBrackets: _closeBrackets,
autoCloseTags: {
Expand Down Expand Up @@ -1300,17 +1312,29 @@ define(function (require, exports, module) {
// Global settings that affect all Editor instances (both currently open Editors as well as those created
// in the future)


/**
* @private
* Updates Editor option and the corresponding preference with the given value. Affects all Editors.
* @param {boolean | number} value
* @param {string} cmOption - CodeMirror option string
* @param {string} prefName - preference name string
*/
function _setEditorOptionAndPref(value, cmOption, prefName) {
_instances.forEach(function (editor) {
editor._codeMirror.setOption(cmOption, value);
});

_prefs.setValue(prefName, (typeof value === "boolean") ? Boolean(value) : value);
}

/**
* Sets whether to use tab characters (vs. spaces) when inserting new text. Affects all Editors.
* @param {boolean} value
*/
Editor.setUseTabChar = function (value) {
_useTabChar = value;
_instances.forEach(function (editor) {
editor._codeMirror.setOption("indentWithTabs", _useTabChar);
});

_prefs.setValue("useTabChar", Boolean(_useTabChar));
_setEditorOptionAndPref(value, "indentWithTabs", "useTabChar");
};

/** @type {boolean} Gets whether all Editors use tab characters (vs. spaces) when inserting new text */
Expand All @@ -1324,11 +1348,7 @@ define(function (require, exports, module) {
*/
Editor.setTabSize = function (value) {
_tabSize = value;
_instances.forEach(function (editor) {
editor._codeMirror.setOption("tabSize", _tabSize);
});

_prefs.setValue("tabSize", _tabSize);
_setEditorOptionAndPref(value, "tabSize", "tabSize");
};

/** @type {number} Get indent unit */
Expand All @@ -1342,11 +1362,7 @@ define(function (require, exports, module) {
*/
Editor.setIndentUnit = function (value) {
_indentUnit = value;
_instances.forEach(function (editor) {
editor._codeMirror.setOption("indentUnit", _indentUnit);
});

_prefs.setValue("indentUnit", _indentUnit);
_setEditorOptionAndPref(value, "indentUnit", "indentUnit");
};

/** @type {number} Get indentation width */
Expand All @@ -1360,18 +1376,56 @@ define(function (require, exports, module) {
*/
Editor.setCloseBrackets = function (value) {
_closeBrackets = value;
_instances.forEach(function (editor) {
editor._codeMirror.setOption("autoCloseBrackets", _closeBrackets);
});

_prefs.setValue("closeBrackets", Boolean(_closeBrackets));
_setEditorOptionAndPref(value, "autoCloseBrackets", "closeBrackets");
};

/** @type {boolean} Gets whether all Editors use auto close brackets */
Editor.getCloseBrackets = function () {
return _closeBrackets;
};

/**
* Sets show line numbers option and reapply it to all open editors.
* @param {boolean} value
*/
Editor.setShowLineNumbers = function (value) {
_showLineNumbers = value;
_setEditorOptionAndPref(value, "lineNumbers", "showLineNumbers");
};

/** @type {boolean} Returns true if show line numbers is enabled for all editors */
Editor.getShowLineNumbers = function () {
return _showLineNumbers;
};

/**
* Sets show active line option and reapply it to all open editors.
* @param {boolean} value
*/
Editor.setShowActiveLine = function (value) {
_styleActiveLine = value;
_setEditorOptionAndPref(value, "styleActiveLine", "styleActiveLine");
};

/** @type {boolean} Returns true if show active line is enabled for all editors */
Editor.getShowActiveLine = function () {
Copy link
Member

Choose a reason for hiding this comment

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

wording..."Returns true if show active line is enabled for all editors"

return _styleActiveLine;
};

/**
* Sets word wrap option and reapply it to all open editors.
* @param {boolean} value
*/
Editor.setWordWrap = function (value) {
_wordWrap = value;
_setEditorOptionAndPref(value, "lineWrapping", "wordWrap");
};

/** @type {boolean} Returns true if word wrap is enabled for all editors */
Editor.getWordWrap = function () {
return _wordWrap;
};

// Define public API
exports.Editor = Editor;
exports.BOUNDARY_CHECK_NORMAL = BOUNDARY_CHECK_NORMAL;
Expand Down
13 changes: 0 additions & 13 deletions src/editor/EditorManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,21 +684,8 @@ define(function (require, exports, module) {
return result.promise();
}

/**
* @private
* Activates/Deactivates the automatic close brackets option
*/
function _toggleCloseBrackets() {
Editor.setCloseBrackets(!Editor.getCloseBrackets());
CommandManager.get(Commands.TOGGLE_CLOSE_BRACKETS).setChecked(Editor.getCloseBrackets());
}


// Initialize: command handlers
CommandManager.register(Strings.CMD_TOGGLE_QUICK_EDIT, Commands.TOGGLE_QUICK_EDIT, _toggleQuickEdit);
CommandManager.register(Strings.CMD_TOGGLE_CLOSE_BRACKETS, Commands.TOGGLE_CLOSE_BRACKETS, _toggleCloseBrackets)
.setChecked(Editor.getCloseBrackets());


// Initialize: register listeners
$(DocumentManager).on("currentDocumentChange", _onCurrentDocumentChange);
Expand Down
87 changes: 87 additions & 0 deletions src/editor/EditorOptionHandlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define */

define(function (require, exports, module) {
"use strict";

var AppInit = require("utils/AppInit"),
Editor = require("editor/Editor").Editor,
Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
Strings = require("strings");

/**
* @private
* Activates/Deactivates showing line numbers option
*/
function _toggleLineNumbers() {
Editor.setShowLineNumbers(!Editor.getShowLineNumbers());
CommandManager.get(Commands.TOGGLE_LINE_NUMBERS).setChecked(Editor.getShowLineNumbers());
}


/**
* @private
* Activates/Deactivates showing active line option
*/
function _toggleActiveLine() {
Editor.setShowActiveLine(!Editor.getShowActiveLine());
CommandManager.get(Commands.TOGGLE_ACTIVE_LINE).setChecked(Editor.getShowActiveLine());
}


/**
* @private
* Activates/Deactivates word wrap option
*/
function _toggleWordWrap() {
Editor.setWordWrap(!Editor.getWordWrap());
CommandManager.get(Commands.TOGGLE_WORD_WRAP).setChecked(Editor.getWordWrap());
}

/**
* @private
* Activates/Deactivates the automatic close brackets option
*/
function _toggleCloseBrackets() {
Editor.setCloseBrackets(!Editor.getCloseBrackets());
CommandManager.get(Commands.TOGGLE_CLOSE_BRACKETS).setChecked(Editor.getCloseBrackets());
}

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.CMD_TOGGLE_CLOSE_BRACKETS).setChecked(Editor.getCloseBrackets());
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be Commands.TOGGLE_CLOSE_BRACKETS

}

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);

AppInit.htmlReady(_init);
Copy link
Member

Choose a reason for hiding this comment

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

Curious...do we need to wait for htmlReady for either html or native menus?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm just trying to make sure that we successfully update the menu items. So let me know if I need to remove it.

});
5 changes: 3 additions & 2 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@
<script src="thirdparty/CodeMirror2/lib/codemirror.js"></script>
<script src="thirdparty/CodeMirror2/addon/edit/matchbrackets.js"></script>
<script src="thirdparty/CodeMirror2/addon/edit/closebrackets.js"></script>

<script src="thirdparty/CodeMirror2/addon/edit/closetag.js"></script>
<script src="thirdparty/CodeMirror2/addon/selection/active-line.js"></script>
Copy link
Member

Choose a reason for hiding this comment

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

Keep these dependencies in sync with SpecRunner.html and Gruntfile.js

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice to know! I'll update them.


<!-- JS for CodeMirror search support -->
<script src="thirdparty/CodeMirror2/addon/search/searchcursor.js"></script>
<script src="thirdparty/CodeMirror2/addon/edit/closetag.js"></script>

</head>
<body>
Expand Down
3 changes: 3 additions & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ define({
"CMD_RESTORE_FONT_SIZE" : "Restore Font Size",
"CMD_SCROLL_LINE_UP" : "Scroll Line Up",
"CMD_SCROLL_LINE_DOWN" : "Scroll Line Down",
"CMD_TOGGLE_LINE_NUMBERS" : "Show Line Numbers",
"CMD_TOGGLE_ACTIVE_LINE" : "Show Active Line",
"CMD_TOGGLE_WORD_WRAP" : "Enable Word Wrap",
Copy link
Member

Choose a reason for hiding this comment

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

CMD_TOGGLE_LINE_NUMBERS and CMD_TOGGLE_ACTIVE_LINE are missing, causing the console errors I noted earlier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Somehow I didn't get them in when resolving the conflicts in merging master to my branch.

"CMD_SORT_WORKINGSET_BY_ADDED" : "Sort by Added",
"CMD_SORT_WORKINGSET_BY_NAME" : "Sort by Name",
"CMD_SORT_WORKINGSET_BY_TYPE" : "Sort by Type",
Expand Down
4 changes: 4 additions & 0 deletions src/styles/brackets.less
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@ a, img {
height: auto;
}

.CodeMirror-activeline-background {
background: darken(@activeline-bgcolor, @bc-color-step-size / 2) !important;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@GarthDB -- any suggestion on the adjustment of @activeline-bgcolor?

Copy link
Member

Choose a reason for hiding this comment

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

Looks ok to me. If we don't hear from @GarthDB, we can merge and address it later. I tested Find to confirm that search results still appear highlighted even with !important here.

}

.inline-editor-header {
padding: 10px 10px 0px 10px;

Expand Down
4 changes: 4 additions & 0 deletions src/styles/brackets_codemirror_override.less
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

@code-padding: 15px;

.CodeMirror-activeline-background {
background: @activeline-bgcolor !important;
}

.cm-s-default {
span.cm-keyword {color: @accent-keyword;}
span.cm-atom {color: @accent-atom;}
Expand Down
3 changes: 3 additions & 0 deletions src/styles/brackets_theme_default.less
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
@selection-color-focused: #d2dcf8;
@selection-color-unfocused: #d9d9d9;

/* background color of the line that has the cursor */
@activeline-bgcolor: #e8f2ff;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@GarthDB -- see if you have any comment on this color.


/* Code font formatting
*
* NOTE (JRB): In order to get the web font to load early enough, we have a div called "dummy-text" that
Expand Down
1 change: 1 addition & 0 deletions test/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<script src="../src/thirdparty/CodeMirror2/lib/codemirror.js"></script>
<script src="../src/thirdparty/CodeMirror2/addon/search/searchcursor.js"></script>
<script src="../src/thirdparty/CodeMirror2/addon/edit/closetag.js"></script>
<script src="thirdparty/CodeMirror2/addon/selection/active-line.js"></script>
<script src="../src/thirdparty/mustache/mustache.js"></script>
<script src="../src/thirdparty/path-utils/path-utils.js"></script>
<script src="thirdparty/bootstrap2/js/bootstrap.min.js"></script>
Expand Down
1 change: 1 addition & 0 deletions test/UnitTestSuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ define(function (require, exports, module) {
require("spec/DocumentCommandHandlers-test");
require("spec/Editor-test");
require("spec/EditorCommandHandlers-test");
require("spec/EditorOptionHandlers-test");
require("spec/EditorManager-test");
require("spec/ExtensionInstallation-test");
require("spec/ExtensionUtils-test");
Expand Down
1 change: 1 addition & 0 deletions test/spec/EditorOptionHandlers-test-files/test.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading