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

Commit 6d23bbc

Browse files
committed
Merge pull request #4579 from adobe/pflynn/selection-info
Enhance status bar to show length of selection
2 parents bc6b359 + 04536cc commit 6d23bbc

4 files changed

Lines changed: 102 additions & 16 deletions

File tree

src/editor/Editor.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -713,23 +713,31 @@ define(function (require, exports, module) {
713713
var cursor = this._codeMirror.getCursor();
714714

715715
if (expandTabs) {
716-
var line = this._codeMirror.getRange({line: cursor.line, ch: 0}, cursor),
717-
tabSize = Editor.getTabSize(),
718-
column = 0,
719-
i;
716+
cursor.ch = this.getColOffset(cursor);
717+
}
718+
return cursor;
719+
};
720+
721+
/**
722+
* Returns the display column (zero-based) for a given string-based pos. Differs from pos.ch only
723+
* when the line contains preceding \t chars. Result depends on the current tab size setting.
724+
* @param {!{line:number, ch:number}}
725+
* @return {number}
726+
*/
727+
Editor.prototype.getColOffset = function (pos) {
728+
var line = this._codeMirror.getRange({line: pos.line, ch: 0}, pos),
729+
tabSize = Editor.getTabSize(),
730+
column = 0,
731+
i;
720732

721-
for (i = 0; i < line.length; i++) {
722-
if (line[i] === '\t') {
723-
column += (tabSize - (column % tabSize));
724-
} else {
725-
column++;
726-
}
733+
for (i = 0; i < line.length; i++) {
734+
if (line[i] === '\t') {
735+
column += (tabSize - (column % tabSize));
736+
} else {
737+
column++;
727738
}
728-
729-
cursor.ch = column;
730739
}
731-
732-
return cursor;
740+
return column;
733741
};
734742

735743
/**

src/editor/EditorStatusBar.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ define(function (require, exports, module) {
4949
$indentWidthInput;
5050

5151

52+
function _formatCountable(number, singularStr, pluralStr) {
53+
return StringUtils.format(number > 1 ? pluralStr : singularStr, number);
54+
}
55+
5256
function _updateLanguageInfo(editor) {
5357
$languageInfo.text(editor.document.getLanguage().getName());
5458
}
5559

5660
function _updateFileInfo(editor) {
5761
var lines = editor.lineCount();
58-
$fileInfo.text(StringUtils.format(lines > 1 ? Strings.STATUSBAR_LINE_COUNT_PLURAL : Strings.STATUSBAR_LINE_COUNT_SINGULAR, lines));
62+
$fileInfo.text(_formatCountable(lines, Strings.STATUSBAR_LINE_COUNT_SINGULAR, Strings.STATUSBAR_LINE_COUNT_PLURAL));
5963
}
6064

6165
function _updateIndentType() {
@@ -87,7 +91,26 @@ define(function (require, exports, module) {
8791
// compute columns, account for tab size
8892
var cursor = editor.getCursorPos(true);
8993

90-
$cursorInfo.text(StringUtils.format(Strings.STATUSBAR_CURSOR_POSITION, cursor.line + 1, cursor.ch + 1));
94+
var cursorStr = StringUtils.format(Strings.STATUSBAR_CURSOR_POSITION, cursor.line + 1, cursor.ch + 1);
95+
if (editor.hasSelection()) {
96+
// Show info about selection size when one exists
97+
var sel = editor.getSelection(),
98+
selStr;
99+
100+
if (sel.start.line !== sel.end.line) {
101+
var lines = sel.end.line - sel.start.line + 1;
102+
if (sel.end.ch === 0) {
103+
lines--; // end line is exclusive if ch is 0, inclusive otherwise
104+
}
105+
selStr = _formatCountable(lines, Strings.STATUSBAR_SELECTION_LINE_SINGULAR, Strings.STATUSBAR_SELECTION_LINE_PLURAL);
106+
} else {
107+
var cols = editor.getColOffset(sel.end) - editor.getColOffset(sel.start); // end ch is exclusive always
108+
selStr = _formatCountable(cols, Strings.STATUSBAR_SELECTION_CH_SINGULAR, Strings.STATUSBAR_SELECTION_CH_PLURAL);
109+
}
110+
$cursorInfo.text(cursorStr + selStr);
111+
} else {
112+
$cursorInfo.text(cursorStr);
113+
}
91114
}
92115

93116
function _changeIndentWidth(value) {

src/nls/root/strings.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ define({
158158
* StatusBar strings
159159
*/
160160
"STATUSBAR_CURSOR_POSITION" : "Line {0}, Column {1}",
161+
"STATUSBAR_SELECTION_CH_SINGULAR" : " \u2014 Selected {0} column",
162+
"STATUSBAR_SELECTION_CH_PLURAL" : " \u2014 Selected {0} columns",
163+
"STATUSBAR_SELECTION_LINE_SINGULAR" : " \u2014 Selected {0} line",
164+
"STATUSBAR_SELECTION_LINE_PLURAL" : " \u2014 Selected {0} lines",
161165
"STATUSBAR_INDENT_TOOLTIP_SPACES" : "Click to switch indentation to spaces",
162166
"STATUSBAR_INDENT_TOOLTIP_TABS" : "Click to switch indentation to tabs",
163167
"STATUSBAR_INDENT_SIZE_TOOLTIP_SPACES" : "Click to change number of spaces used when indenting",

test/spec/Editor-test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,56 @@ define(function (require, exports, module) {
200200
});
201201

202202
});
203+
204+
describe("Column/ch conversion", function () {
205+
it("should get mode in HTML file", function () {
206+
var content =
207+
"foo () {\n" +
208+
" one;\n" +
209+
"\ttwo;\n" +
210+
"}\n" +
211+
"\n" +
212+
"\tA\tB";
213+
createTestEditor(content, "javascript");
214+
215+
// Tab size 4
216+
217+
expect(myEditor.getColOffset({line: 1, ch: 0})).toBe(0);
218+
expect(myEditor.getColOffset({line: 1, ch: 1})).toBe(1);
219+
expect(myEditor.getColOffset({line: 1, ch: 2})).toBe(2);
220+
expect(myEditor.getColOffset({line: 1, ch: 3})).toBe(3);
221+
expect(myEditor.getColOffset({line: 1, ch: 4})).toBe(4);
222+
expect(myEditor.getColOffset({line: 1, ch: 5})).toBe(5);
223+
expect(myEditor.getColOffset({line: 2, ch: 0})).toBe(0);
224+
expect(myEditor.getColOffset({line: 2, ch: 1})).toBe(4);
225+
expect(myEditor.getColOffset({line: 2, ch: 2})).toBe(5);
226+
expect(myEditor.getColOffset({line: 4, ch: 0})).toBe(0);
227+
expect(myEditor.getColOffset({line: 5, ch: 1})).toBe(4);
228+
expect(myEditor.getColOffset({line: 5, ch: 2})).toBe(5);
229+
expect(myEditor.getColOffset({line: 5, ch: 3})).toBe(8);
230+
expect(myEditor.getColOffset({line: 5, ch: 4})).toBe(9);
231+
232+
// Tab size 2
233+
Editor.setTabSize(2);
234+
235+
expect(myEditor.getColOffset({line: 1, ch: 0})).toBe(0); // first line is all spaces: should be unchanged
236+
expect(myEditor.getColOffset({line: 1, ch: 1})).toBe(1);
237+
expect(myEditor.getColOffset({line: 1, ch: 2})).toBe(2);
238+
expect(myEditor.getColOffset({line: 1, ch: 3})).toBe(3);
239+
expect(myEditor.getColOffset({line: 1, ch: 4})).toBe(4);
240+
expect(myEditor.getColOffset({line: 1, ch: 5})).toBe(5);
241+
expect(myEditor.getColOffset({line: 2, ch: 0})).toBe(0); // but line with a tab shows different behavior
242+
expect(myEditor.getColOffset({line: 2, ch: 1})).toBe(2);
243+
expect(myEditor.getColOffset({line: 2, ch: 2})).toBe(3);
244+
expect(myEditor.getColOffset({line: 4, ch: 0})).toBe(0);
245+
expect(myEditor.getColOffset({line: 5, ch: 1})).toBe(2); // same here
246+
expect(myEditor.getColOffset({line: 5, ch: 2})).toBe(3);
247+
expect(myEditor.getColOffset({line: 5, ch: 3})).toBe(4);
248+
expect(myEditor.getColOffset({line: 5, ch: 4})).toBe(5);
249+
250+
// Restore default
251+
Editor.setTabSize(4);
252+
});
253+
});
203254
});
204255
});

0 commit comments

Comments
 (0)