DevTools: [CodeMirror] collapse single selection on esc key

Review URL: https://codereview.chromium.org/235933007

git-svn-id: svn://svn.chromium.org/blink/trunk@171473 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent ee02e4c6
...@@ -182,10 +182,10 @@ WebInspector.CodeMirrorTextEditor = function(url, delegate) ...@@ -182,10 +182,10 @@ WebInspector.CodeMirrorTextEditor = function(url, delegate)
this._setupWhitespaceHighlight(); this._setupWhitespaceHighlight();
} }
/** @typedef {{canceled: boolean, from: CodeMirror.Pos, to: CodeMirror.Pos, text: string, origin: string, cancel: function()}} */ /** @typedef {{canceled: boolean, from: !CodeMirror.Pos, to: !CodeMirror.Pos, text: string, origin: string, cancel: function()}} */
WebInspector.CodeMirrorTextEditor.BeforeChangeObject; WebInspector.CodeMirrorTextEditor.BeforeChangeObject;
/** @typedef {{from: CodeMirror.Pos, to: CodeMirror.Pos, origin: string, text: !Array.<string>, removed: !Array.<string>}} */ /** @typedef {{from: !CodeMirror.Pos, to: !CodeMirror.Pos, origin: string, text: !Array.<string>, removed: !Array.<string>}} */
WebInspector.CodeMirrorTextEditor.ChangeObject; WebInspector.CodeMirrorTextEditor.ChangeObject;
WebInspector.CodeMirrorTextEditor.maxHighlightLength = 1000; WebInspector.CodeMirrorTextEditor.maxHighlightLength = 1000;
...@@ -266,11 +266,18 @@ CodeMirror.commands.redoAndReveal = function(codemirror) ...@@ -266,11 +266,18 @@ CodeMirror.commands.redoAndReveal = function(codemirror)
CodeMirror.commands.dismissMultipleSelections = function(codemirror) CodeMirror.commands.dismissMultipleSelections = function(codemirror)
{ {
if (codemirror.getSelections().length <= 1) var selections = codemirror.listSelections();
var selection = selections[0];
if (selections.length === 1) {
if (WebInspector.CodeMirrorUtils.toRange(selection.anchor, selection.head).isEmpty())
return CodeMirror.Pass; return CodeMirror.Pass;
var range = codemirror.listSelections()[0]; codemirror.setSelection(selection.anchor, selection.anchor, {scroll: false});
codemirror.setSelection(range.anchor, range.head, {scroll: false}); codemirror._codeMirrorTextEditor._revealLine(selection.anchor.line);
codemirror._codeMirrorTextEditor._revealLine(range.anchor.line); return;
}
codemirror.setSelection(selection.anchor, selection.head, {scroll: false});
codemirror._codeMirrorTextEditor._revealLine(selection.anchor.line);
} }
WebInspector.CodeMirrorTextEditor.LongLineModeLineLengthThreshold = 2000; WebInspector.CodeMirrorTextEditor.LongLineModeLineLengthThreshold = 2000;
...@@ -486,7 +493,7 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -486,7 +493,7 @@ WebInspector.CodeMirrorTextEditor.prototype = {
y >= gutterBox.y && y <= gutterBox.y + gutterBox.height) y >= gutterBox.y && y <= gutterBox.y + gutterBox.height)
return null; return null;
var coords = this._codeMirror.coordsChar({left: x, top: y}); var coords = this._codeMirror.coordsChar({left: x, top: y});
return this._toRange(coords, coords); return WebInspector.CodeMirrorUtils.toRange(coords, coords);
}, },
/** /**
...@@ -514,7 +521,7 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -514,7 +521,7 @@ WebInspector.CodeMirrorTextEditor.prototype = {
*/ */
copyRange: function(textRange) copyRange: function(textRange)
{ {
var pos = this._toPos(textRange.normalize()); var pos = WebInspector.CodeMirrorUtils.toPos(textRange.normalize());
return this._codeMirror.getRange(pos.start, pos.end); return this._codeMirror.getRange(pos.start, pos.end);
}, },
...@@ -646,7 +653,7 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -646,7 +653,7 @@ WebInspector.CodeMirrorTextEditor.prototype = {
highlightRange: function(range, cssClass) highlightRange: function(range, cssClass)
{ {
cssClass = "CodeMirror-persist-highlight " + cssClass; cssClass = "CodeMirror-persist-highlight " + cssClass;
var pos = this._toPos(range); var pos = WebInspector.CodeMirrorUtils.toPos(range);
++pos.end.ch; ++pos.end.ch;
return this._codeMirror.markText(pos.start, pos.end, { return this._codeMirror.markText(pos.start, pos.end, {
className: cssClass, className: cssClass,
...@@ -893,9 +900,9 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -893,9 +900,9 @@ WebInspector.CodeMirrorTextEditor.prototype = {
*/ */
editRange: function(range, text) editRange: function(range, text)
{ {
var pos = this._toPos(range); var pos = WebInspector.CodeMirrorUtils.toPos(range);
this._codeMirror.replaceRange(text, pos.start, pos.end); this._codeMirror.replaceRange(text, pos.start, pos.end);
var newRange = this._toRange(pos.start, this._codeMirror.posFromIndex(this._codeMirror.indexFromPos(pos.start) + text.length)); var newRange = WebInspector.CodeMirrorUtils.toRange(pos.start, this._codeMirror.posFromIndex(this._codeMirror.indexFromPos(pos.start) + text.length));
this._delegate.onTextChanged(range, newRange); this._delegate.onTextChanged(range, newRange);
if (WebInspector.settings.textEditorAutoDetectIndent.get()) if (WebInspector.settings.textEditorAutoDetectIndent.get())
this._updateEditorIndentation(); this._updateEditorIndentation();
...@@ -929,7 +936,7 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -929,7 +936,7 @@ WebInspector.CodeMirrorTextEditor.prototype = {
*/ */
_changeObjectToEditOperation: function(changeObject) _changeObjectToEditOperation: function(changeObject)
{ {
var oldRange = this._toRange(changeObject.from, changeObject.to); var oldRange = WebInspector.CodeMirrorUtils.toRange(changeObject.from, changeObject.to);
var newRange = oldRange.clone(); var newRange = oldRange.clone();
var linesAdded = changeObject.text.length; var linesAdded = changeObject.text.length;
if (linesAdded === 0) { if (linesAdded === 0) {
...@@ -979,7 +986,7 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -979,7 +986,7 @@ WebInspector.CodeMirrorTextEditor.prototype = {
{ {
var start = this._codeMirror.getCursor("anchor"); var start = this._codeMirror.getCursor("anchor");
var end = this._codeMirror.getCursor("head"); var end = this._codeMirror.getCursor("head");
this._delegate.selectionChanged(this._toRange(start, end)); this._delegate.selectionChanged(WebInspector.CodeMirrorUtils.toRange(start, end));
if (!this._tokenHighlighter.highlightedRegex()) if (!this._tokenHighlighter.highlightedRegex())
this._codeMirror.operation(this._tokenHighlighter.highlightSelectedTokens.bind(this._tokenHighlighter)); this._codeMirror.operation(this._tokenHighlighter.highlightSelectedTokens.bind(this._tokenHighlighter));
}, },
...@@ -996,7 +1003,7 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -996,7 +1003,7 @@ WebInspector.CodeMirrorTextEditor.prototype = {
if (!selection.ranges.length) if (!selection.ranges.length)
return; return;
var primarySelection = selection.ranges[0]; var primarySelection = selection.ranges[0];
this._reportJump(this.selection(), this._toRange(primarySelection.anchor, primarySelection.head)); this._reportJump(this.selection(), WebInspector.CodeMirrorUtils.toRange(primarySelection.anchor, primarySelection.head));
}, },
/** /**
...@@ -1058,7 +1065,7 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -1058,7 +1065,7 @@ WebInspector.CodeMirrorTextEditor.prototype = {
var start = this._codeMirror.getCursor("anchor"); var start = this._codeMirror.getCursor("anchor");
var end = this._codeMirror.getCursor("head"); var end = this._codeMirror.getCursor("head");
return this._toRange(start, end); return WebInspector.CodeMirrorUtils.toRange(start, end);
}, },
/** /**
...@@ -1070,7 +1077,7 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -1070,7 +1077,7 @@ WebInspector.CodeMirrorTextEditor.prototype = {
var result = []; var result = [];
for (var i = 0; i < selectionList.length; ++i) { for (var i = 0; i < selectionList.length; ++i) {
var selection = selectionList[i]; var selection = selectionList[i];
result.push(this._toRange(selection.anchor, selection.head)); result.push(WebInspector.CodeMirrorUtils.toRange(selection.anchor, selection.head));
} }
return result; return result;
}, },
...@@ -1089,7 +1096,7 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -1089,7 +1096,7 @@ WebInspector.CodeMirrorTextEditor.prototype = {
setSelection: function(textRange) setSelection: function(textRange)
{ {
this._lastSelection = textRange; this._lastSelection = textRange;
var pos = this._toPos(textRange); var pos = WebInspector.CodeMirrorUtils.toPos(textRange);
this._codeMirror.setSelection(pos.start, pos.end); this._codeMirror.setSelection(pos.start, pos.end);
}, },
...@@ -1101,7 +1108,7 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -1101,7 +1108,7 @@ WebInspector.CodeMirrorTextEditor.prototype = {
{ {
var selections = []; var selections = [];
for (var i = 0; i < ranges.length; ++i) { for (var i = 0; i < ranges.length; ++i) {
var selection = this._toPos(ranges[i]); var selection = WebInspector.CodeMirrorUtils.toPos(ranges[i]);
selections.push({ selections.push({
anchor: selection.start, anchor: selection.start,
head: selection.end head: selection.end
...@@ -1154,7 +1161,7 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -1154,7 +1161,7 @@ WebInspector.CodeMirrorTextEditor.prototype = {
{ {
var lineCount = this.linesCount; var lineCount = this.linesCount;
var lastLine = this._codeMirror.getLine(lineCount - 1); var lastLine = this._codeMirror.getLine(lineCount - 1);
return this._toRange(new CodeMirror.Pos(0, 0), new CodeMirror.Pos(lineCount - 1, lastLine.length)); return WebInspector.CodeMirrorUtils.toRange(new CodeMirror.Pos(0, 0), new CodeMirror.Pos(lineCount - 1, lastLine.length));
}, },
/** /**
...@@ -1214,23 +1221,6 @@ WebInspector.CodeMirrorTextEditor.prototype = { ...@@ -1214,23 +1221,6 @@ WebInspector.CodeMirrorTextEditor.prototype = {
delete handle.attributes[name]; delete handle.attributes[name];
}, },
/**
* @param {!WebInspector.TextRange} range
* @return {!{start: !CodeMirror.Pos, end: !CodeMirror.Pos}}
*/
_toPos: function(range)
{
return {
start: new CodeMirror.Pos(range.startLine, range.startColumn),
end: new CodeMirror.Pos(range.endLine, range.endColumn)
}
},
_toRange: function(start, end)
{
return new WebInspector.TextRange(start.line, start.ch, end.line, end.ch);
},
/** /**
* @param {number} lineNumber * @param {number} lineNumber
* @param {number} columnNumber * @param {number} columnNumber
...@@ -1321,7 +1311,7 @@ WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype = { ...@@ -1321,7 +1311,7 @@ WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype = {
this._setHighlighter(this._searchHighlighter.bind(this, this._highlightRegex), selectionStart); this._setHighlighter(this._searchHighlighter.bind(this, this._highlightRegex), selectionStart);
} }
if (this._highlightRange) { if (this._highlightRange) {
var pos = WebInspector.CodeMirrorTextEditor.prototype._toPos(this._highlightRange); var pos = WebInspector.CodeMirrorUtils.toPos(this._highlightRange);
this._searchResultMarker = this._codeMirror.markText(pos.start, pos.end, {className: "cm-column-with-selection"}); this._searchResultMarker = this._codeMirror.markText(pos.start, pos.end, {className: "cm-column-with-selection"});
} }
}, },
......
...@@ -37,6 +37,29 @@ WebInspector.CodeMirrorUtils = function() ...@@ -37,6 +37,29 @@ WebInspector.CodeMirrorUtils = function()
WebInspector.InplaceEditor.call(this); WebInspector.InplaceEditor.call(this);
} }
/**
* @param {!WebInspector.TextRange} range
* @return {!{start: !CodeMirror.Pos, end: !CodeMirror.Pos}}
*/
WebInspector.CodeMirrorUtils.toPos = function(range)
{
return {
start: new CodeMirror.Pos(range.startLine, range.startColumn),
end: new CodeMirror.Pos(range.endLine, range.endColumn)
}
},
/**
* @param {!CodeMirror.Pos} start
* @param {!CodeMirror.Pos} end
* @return {!WebInspector.TextRange}
*/
WebInspector.CodeMirrorUtils.toRange = function(start, end)
{
return new WebInspector.TextRange(start.line, start.ch, end.line, end.ch);
},
WebInspector.CodeMirrorUtils.prototype = { WebInspector.CodeMirrorUtils.prototype = {
/** /**
* @return {string} * @return {string}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment