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

Commit 0ca4305

Browse files
committed
Merge pull request #3097 from adobe/rlim/word-wrap
Provide editor options "line numbers", "active line" and "word wrap" as ...
2 parents c3952f9 + 9996e84 commit 0ca4305

17 files changed

+479
-39
lines changed

Gruntfile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ module.exports = function (grunt) {
7676
'src/thirdparty/CodeMirror2/lib/codemirror.js',
7777
'src/thirdparty/CodeMirror2/lib/util/dialog.js',
7878
'src/thirdparty/CodeMirror2/lib/util/searchcursor.js',
79+
'src/thirdparty/CodeMirror2/addon/edit/closetag.js',
80+
'src/thirdparty/CodeMirror2/addon/selection/active-line.js',
7981
'src/thirdparty/mustache/mustache.js',
8082
'src/thirdparty/path-utils/path-utils.min',
8183
'src/thirdparty/less-1.3.0.min.js'

src/brackets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ define(function (require, exports, module) {
100100
require("document/ChangedDocumentTracker");
101101
require("editor/EditorStatusBar");
102102
require("editor/EditorCommandHandlers");
103+
require("editor/EditorOptionHandlers");
103104
require("view/ViewCommandHandlers");
104105
require("help/HelpCommandHandlers");
105106
require("search/FindInFiles");

src/command/Commands.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ define(function (require, exports, module) {
8181
exports.VIEW_RESTORE_FONT_SIZE = "view.restoreFontSize";
8282
exports.VIEW_SCROLL_LINE_UP = "view.scrollLineUp";
8383
exports.VIEW_SCROLL_LINE_DOWN = "view.scrollLineDown";
84+
exports.TOGGLE_LINE_NUMBERS = "view.toggleLineNumbers";
85+
exports.TOGGLE_ACTIVE_LINE = "view.toggleActiveLine";
86+
exports.TOGGLE_WORD_WRAP = "view.toggleWordWrap";
8487
exports.TOGGLE_JSLINT = "debug.jslint";
8588
exports.SORT_WORKINGSET_BY_ADDED = "view.sortWorkingSetByAdded";
8689
exports.SORT_WORKINGSET_BY_NAME = "view.sortWorkingSetByName";

src/command/DefaultMenus.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ define(function (require, exports, module) {
110110
menu.addMenuItem(Commands.VIEW_DECREASE_FONT_SIZE);
111111
menu.addMenuItem(Commands.VIEW_RESTORE_FONT_SIZE);
112112
menu.addMenuDivider();
113+
menu.addMenuItem(Commands.TOGGLE_LINE_NUMBERS);
114+
menu.addMenuItem(Commands.TOGGLE_ACTIVE_LINE);
115+
menu.addMenuItem(Commands.TOGGLE_WORD_WRAP);
116+
menu.addMenuDivider();
113117
menu.addMenuItem(Commands.TOGGLE_JSLINT);
114118

115119
/*

src/editor/Editor.js

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ define(function (require, exports, module) {
7272
TokenUtils = require("utils/TokenUtils"),
7373
ViewUtils = require("utils/ViewUtils");
7474

75-
var defaultPrefs = { useTabChar: false, tabSize: 4, indentUnit: 4, closeBrackets: false };
76-
75+
var defaultPrefs = { useTabChar: false, tabSize: 4, indentUnit: 4, closeBrackets: false,
76+
showLineNumbers: true, styleActiveLine: true, wordWrap: true };
77+
7778
/** Editor preferences */
7879
var _prefs = PreferencesManager.getPreferenceStorage(module, defaultPrefs);
7980
//TODO: Remove preferences migration code
@@ -91,6 +92,15 @@ define(function (require, exports, module) {
9192
/** @type {boolean} Global setting: Auto closes (, {, [, " and ' */
9293
var _closeBrackets = _prefs.getValue("closeBrackets");
9394

95+
/** @type {boolean} Global setting: Show line numbers in the gutter */
96+
var _showLineNumbers = _prefs.getValue("showLineNumbers");
97+
98+
/** @type {boolean} Global setting: Highlight the background of the line that has the cursor */
99+
var _styleActiveLine = _prefs.getValue("styleActiveLine");
100+
101+
/** @type {boolean} Global setting: Auto wrap lines */
102+
var _wordWrap = _prefs.getValue("wordWrap");
103+
94104
/** @type {boolean} Guard flag to prevent focus() reentrancy (via blur handlers), even across Editors */
95105
var _duringFocus = false;
96106

@@ -345,9 +355,11 @@ define(function (require, exports, module) {
345355
indentWithTabs: _useTabChar,
346356
tabSize: _tabSize,
347357
indentUnit: _indentUnit,
348-
lineNumbers: true,
358+
lineNumbers: _showLineNumbers,
359+
lineWrapping: _wordWrap,
360+
styleActiveLine: _styleActiveLine,
349361
matchBrackets: true,
350-
dragDrop: false, // work around issue #1123
362+
dragDrop: true,
351363
extraKeys: codeMirrorKeyMap,
352364
autoCloseBrackets: _closeBrackets,
353365
autoCloseTags: {
@@ -1300,17 +1312,29 @@ define(function (require, exports, module) {
13001312
// Global settings that affect all Editor instances (both currently open Editors as well as those created
13011313
// in the future)
13021314

1315+
1316+
/**
1317+
* @private
1318+
* Updates Editor option and the corresponding preference with the given value. Affects all Editors.
1319+
* @param {boolean | number} value
1320+
* @param {string} cmOption - CodeMirror option string
1321+
* @param {string} prefName - preference name string
1322+
*/
1323+
function _setEditorOptionAndPref(value, cmOption, prefName) {
1324+
_instances.forEach(function (editor) {
1325+
editor._codeMirror.setOption(cmOption, value);
1326+
});
1327+
1328+
_prefs.setValue(prefName, (typeof value === "boolean") ? Boolean(value) : value);
1329+
}
1330+
13031331
/**
13041332
* Sets whether to use tab characters (vs. spaces) when inserting new text. Affects all Editors.
13051333
* @param {boolean} value
13061334
*/
13071335
Editor.setUseTabChar = function (value) {
13081336
_useTabChar = value;
1309-
_instances.forEach(function (editor) {
1310-
editor._codeMirror.setOption("indentWithTabs", _useTabChar);
1311-
});
1312-
1313-
_prefs.setValue("useTabChar", Boolean(_useTabChar));
1337+
_setEditorOptionAndPref(value, "indentWithTabs", "useTabChar");
13141338
};
13151339

13161340
/** @type {boolean} Gets whether all Editors use tab characters (vs. spaces) when inserting new text */
@@ -1324,11 +1348,7 @@ define(function (require, exports, module) {
13241348
*/
13251349
Editor.setTabSize = function (value) {
13261350
_tabSize = value;
1327-
_instances.forEach(function (editor) {
1328-
editor._codeMirror.setOption("tabSize", _tabSize);
1329-
});
1330-
1331-
_prefs.setValue("tabSize", _tabSize);
1351+
_setEditorOptionAndPref(value, "tabSize", "tabSize");
13321352
};
13331353

13341354
/** @type {number} Get indent unit */
@@ -1342,11 +1362,7 @@ define(function (require, exports, module) {
13421362
*/
13431363
Editor.setIndentUnit = function (value) {
13441364
_indentUnit = value;
1345-
_instances.forEach(function (editor) {
1346-
editor._codeMirror.setOption("indentUnit", _indentUnit);
1347-
});
1348-
1349-
_prefs.setValue("indentUnit", _indentUnit);
1365+
_setEditorOptionAndPref(value, "indentUnit", "indentUnit");
13501366
};
13511367

13521368
/** @type {number} Get indentation width */
@@ -1360,18 +1376,56 @@ define(function (require, exports, module) {
13601376
*/
13611377
Editor.setCloseBrackets = function (value) {
13621378
_closeBrackets = value;
1363-
_instances.forEach(function (editor) {
1364-
editor._codeMirror.setOption("autoCloseBrackets", _closeBrackets);
1365-
});
1366-
1367-
_prefs.setValue("closeBrackets", Boolean(_closeBrackets));
1379+
_setEditorOptionAndPref(value, "autoCloseBrackets", "closeBrackets");
13681380
};
13691381

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

1387+
/**
1388+
* Sets show line numbers option and reapply it to all open editors.
1389+
* @param {boolean} value
1390+
*/
1391+
Editor.setShowLineNumbers = function (value) {
1392+
_showLineNumbers = value;
1393+
_setEditorOptionAndPref(value, "lineNumbers", "showLineNumbers");
1394+
};
1395+
1396+
/** @type {boolean} Returns true if show line numbers is enabled for all editors */
1397+
Editor.getShowLineNumbers = function () {
1398+
return _showLineNumbers;
1399+
};
1400+
1401+
/**
1402+
* Sets show active line option and reapply it to all open editors.
1403+
* @param {boolean} value
1404+
*/
1405+
Editor.setShowActiveLine = function (value) {
1406+
_styleActiveLine = value;
1407+
_setEditorOptionAndPref(value, "styleActiveLine", "styleActiveLine");
1408+
};
1409+
1410+
/** @type {boolean} Returns true if show active line is enabled for all editors */
1411+
Editor.getShowActiveLine = function () {
1412+
return _styleActiveLine;
1413+
};
1414+
1415+
/**
1416+
* Sets word wrap option and reapply it to all open editors.
1417+
* @param {boolean} value
1418+
*/
1419+
Editor.setWordWrap = function (value) {
1420+
_wordWrap = value;
1421+
_setEditorOptionAndPref(value, "lineWrapping", "wordWrap");
1422+
};
1423+
1424+
/** @type {boolean} Returns true if word wrap is enabled for all editors */
1425+
Editor.getWordWrap = function () {
1426+
return _wordWrap;
1427+
};
1428+
13751429
// Define public API
13761430
exports.Editor = Editor;
13771431
exports.BOUNDARY_CHECK_NORMAL = BOUNDARY_CHECK_NORMAL;

src/editor/EditorManager.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -684,21 +684,8 @@ define(function (require, exports, module) {
684684
return result.promise();
685685
}
686686

687-
/**
688-
* @private
689-
* Activates/Deactivates the automatic close brackets option
690-
*/
691-
function _toggleCloseBrackets() {
692-
Editor.setCloseBrackets(!Editor.getCloseBrackets());
693-
CommandManager.get(Commands.TOGGLE_CLOSE_BRACKETS).setChecked(Editor.getCloseBrackets());
694-
}
695-
696-
697687
// Initialize: command handlers
698688
CommandManager.register(Strings.CMD_TOGGLE_QUICK_EDIT, Commands.TOGGLE_QUICK_EDIT, _toggleQuickEdit);
699-
CommandManager.register(Strings.CMD_TOGGLE_CLOSE_BRACKETS, Commands.TOGGLE_CLOSE_BRACKETS, _toggleCloseBrackets)
700-
.setChecked(Editor.getCloseBrackets());
701-
702689

703690
// Initialize: register listeners
704691
$(DocumentManager).on("currentDocumentChange", _onCurrentDocumentChange);

src/editor/EditorOptionHandlers.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
* DEALINGS IN THE SOFTWARE.
21+
*
22+
*/
23+
24+
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
25+
/*global define */
26+
27+
define(function (require, exports, module) {
28+
"use strict";
29+
30+
var AppInit = require("utils/AppInit"),
31+
Editor = require("editor/Editor").Editor,
32+
Commands = require("command/Commands"),
33+
CommandManager = require("command/CommandManager"),
34+
Strings = require("strings");
35+
36+
/**
37+
* @private
38+
* Activates/Deactivates showing line numbers option
39+
*/
40+
function _toggleLineNumbers() {
41+
Editor.setShowLineNumbers(!Editor.getShowLineNumbers());
42+
CommandManager.get(Commands.TOGGLE_LINE_NUMBERS).setChecked(Editor.getShowLineNumbers());
43+
}
44+
45+
46+
/**
47+
* @private
48+
* Activates/Deactivates showing active line option
49+
*/
50+
function _toggleActiveLine() {
51+
Editor.setShowActiveLine(!Editor.getShowActiveLine());
52+
CommandManager.get(Commands.TOGGLE_ACTIVE_LINE).setChecked(Editor.getShowActiveLine());
53+
}
54+
55+
56+
/**
57+
* @private
58+
* Activates/Deactivates word wrap option
59+
*/
60+
function _toggleWordWrap() {
61+
Editor.setWordWrap(!Editor.getWordWrap());
62+
CommandManager.get(Commands.TOGGLE_WORD_WRAP).setChecked(Editor.getWordWrap());
63+
}
64+
65+
/**
66+
* @private
67+
* Activates/Deactivates the automatic close brackets option
68+
*/
69+
function _toggleCloseBrackets() {
70+
Editor.setCloseBrackets(!Editor.getCloseBrackets());
71+
CommandManager.get(Commands.TOGGLE_CLOSE_BRACKETS).setChecked(Editor.getCloseBrackets());
72+
}
73+
74+
function _init() {
75+
CommandManager.get(Commands.TOGGLE_LINE_NUMBERS).setChecked(Editor.getShowLineNumbers());
76+
CommandManager.get(Commands.TOGGLE_ACTIVE_LINE).setChecked(Editor.getShowActiveLine());
77+
CommandManager.get(Commands.TOGGLE_WORD_WRAP).setChecked(Editor.getWordWrap());
78+
CommandManager.get(Commands.TOGGLE_CLOSE_BRACKETS).setChecked(Editor.getCloseBrackets());
79+
}
80+
81+
CommandManager.register(Strings.CMD_TOGGLE_LINE_NUMBERS, Commands.TOGGLE_LINE_NUMBERS, _toggleLineNumbers);
82+
CommandManager.register(Strings.CMD_TOGGLE_ACTIVE_LINE, Commands.TOGGLE_ACTIVE_LINE, _toggleActiveLine);
83+
CommandManager.register(Strings.CMD_TOGGLE_WORD_WRAP, Commands.TOGGLE_WORD_WRAP, _toggleWordWrap);
84+
CommandManager.register(Strings.CMD_TOGGLE_CLOSE_BRACKETS, Commands.TOGGLE_CLOSE_BRACKETS, _toggleCloseBrackets);
85+
86+
AppInit.htmlReady(_init);
87+
});

src/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@
101101
<script src="thirdparty/CodeMirror2/lib/codemirror.js"></script>
102102
<script src="thirdparty/CodeMirror2/addon/edit/matchbrackets.js"></script>
103103
<script src="thirdparty/CodeMirror2/addon/edit/closebrackets.js"></script>
104-
104+
<script src="thirdparty/CodeMirror2/addon/edit/closetag.js"></script>
105+
<script src="thirdparty/CodeMirror2/addon/selection/active-line.js"></script>
106+
105107
<!-- JS for CodeMirror search support -->
106108
<script src="thirdparty/CodeMirror2/addon/search/searchcursor.js"></script>
107-
<script src="thirdparty/CodeMirror2/addon/edit/closetag.js"></script>
108109

109110
</head>
110111
<body>

src/nls/root/strings.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ define({
215215
"CMD_RESTORE_FONT_SIZE" : "Restore Font Size",
216216
"CMD_SCROLL_LINE_UP" : "Scroll Line Up",
217217
"CMD_SCROLL_LINE_DOWN" : "Scroll Line Down",
218+
"CMD_TOGGLE_LINE_NUMBERS" : "Show Line Numbers",
219+
"CMD_TOGGLE_ACTIVE_LINE" : "Show Active Line",
220+
"CMD_TOGGLE_WORD_WRAP" : "Enable Word Wrap",
218221
"CMD_SORT_WORKINGSET_BY_ADDED" : "Sort by Added",
219222
"CMD_SORT_WORKINGSET_BY_NAME" : "Sort by Name",
220223
"CMD_SORT_WORKINGSET_BY_TYPE" : "Sort by Type",

src/styles/brackets.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,10 @@ a, img {
555555
height: auto;
556556
}
557557

558+
.CodeMirror-activeline-background {
559+
background: darken(@activeline-bgcolor, @bc-color-step-size / 2) !important;
560+
}
561+
558562
.inline-editor-header {
559563
padding: 10px 10px 0px 10px;
560564

0 commit comments

Comments
 (0)