This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Syntax for line:col jumps in QuickOpen search queries #5612
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d6501e4
Generalizes cursor position in QuickOpen search queries
jbalsas 67e3c82
Updates cursorPos regular expression
jbalsas f44f125
Add line,col quickopen tests
jbalsas a0708a1
Return null instead of just undefined
jbalsas 82d3e40
Merge remote-tracking branch 'origin' into jbalsas/quickopen_cols
TomMalbran 9290a6e
Nits fixed
TomMalbran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,8 @@ define(function (require, exports, module) { | |
ViewUtils = require("utils/ViewUtils"); | ||
|
||
|
||
var cursorPosRegExp = new RegExp(":([^,]+)?(,(.+)?)?"); | ||
|
||
/** @type Array.<QuickOpenPlugin> */ | ||
var plugins = []; | ||
|
||
|
@@ -249,22 +251,29 @@ define(function (require, exports, module) { | |
* is followed by a colon. Callers should explicitly test result with isNaN() | ||
* | ||
* @param {string} query string to extract line number from | ||
* @returns {number} line number. Returns NaN to indicate no line number was found | ||
* @returns {{query: string, local: boolean, line: number, ch: number}} A cursorPos object with | ||
* the extracted line and column numbers, and two additional fields; query with the original position | ||
* string and local indicating if the cursor position should be applied to the current file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove the "cursorPos", since is not really that, but and object with the line and column position. It would also be nice to add that it returns null if there was a problem. |
||
*/ | ||
function extractLineNumber(query) { | ||
// only match : at beginning of query for now | ||
// TODO: match any location of : when QuickOpen._handleItemFocus() is modified to | ||
// dynamic open files | ||
if (query.indexOf(":") !== 0) { | ||
return NaN; | ||
} | ||
|
||
var result = NaN; | ||
var regInfo = query.match(/(!?:)(\d+)/); // colon followed by a digit | ||
if (regInfo) { | ||
result = regInfo[2] - 1; | ||
function extractCursorPos(query) { | ||
var regInfo = query.match(cursorPosRegExp), | ||
result; | ||
|
||
if (query.length <= 1 || | ||
!regInfo || | ||
(regInfo[1] && isNaN(regInfo[1])) || | ||
(regInfo[3] && isNaN(regInfo[3]))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this is easier to read as:
Or in one line, to group the second or in one line. |
||
|
||
return null; | ||
} | ||
|
||
|
||
result = { | ||
query: regInfo[0], | ||
local: query.indexOf(":") === 0, | ||
line: regInfo[1] - 1 || 0, | ||
ch: regInfo[3] - 1 || 0 | ||
}; | ||
|
||
return result; | ||
} | ||
|
||
|
@@ -311,17 +320,14 @@ define(function (require, exports, module) { | |
|
||
var selectedItem = domItemToSearchResult(selectedDOMItem), | ||
doClose = true, | ||
self = this; | ||
self = this, | ||
query = this.$searchField.val(), | ||
cursorPos = extractCursorPos(query); | ||
|
||
// Delegate to current plugin | ||
if (currentPlugin) { | ||
currentPlugin.itemSelect(selectedItem); | ||
} else { | ||
|
||
// extract line number, if any | ||
var query = this.$searchField.val(), | ||
gotoLine = extractLineNumber(query); | ||
|
||
// Navigate to file and line number | ||
var fullPath = selectedItem && selectedItem.fullPath; | ||
if (fullPath) { | ||
|
@@ -334,16 +340,16 @@ define(function (require, exports, module) { | |
this.modalBar.prepareClose(); | ||
CommandManager.execute(Commands.FILE_ADD_TO_WORKING_SET, {fullPath: fullPath}) | ||
.done(function () { | ||
if (!isNaN(gotoLine)) { | ||
if (cursorPos) { | ||
var editor = EditorManager.getCurrentFullEditor(); | ||
editor.setCursorPos(gotoLine, 0, true); | ||
editor.setCursorPos(cursorPos.line, cursorPos.ch, true); | ||
} | ||
}) | ||
.always(function () { | ||
self.close(); | ||
}); | ||
} else if (!isNaN(gotoLine)) { | ||
EditorManager.getCurrentFullEditor().setCursorPos(gotoLine, 0, true); | ||
} else if (cursorPos) { | ||
EditorManager.getCurrentFullEditor().setCursorPos(cursorPos.line, cursorPos.ch, true); | ||
} | ||
} | ||
|
||
|
@@ -420,11 +426,11 @@ define(function (require, exports, module) { | |
return true; | ||
} | ||
|
||
var lineNum = extractLineNumber(query), | ||
var cursorPos = extractCursorPos(query), | ||
editor = EditorManager.getCurrentFullEditor(); | ||
|
||
// We could just use 0 and lineCount() here, but in future we might want this logic to work for inline editors as well. | ||
return (!isNaN(lineNum) && editor && lineNum >= editor.getFirstVisibleLine() && lineNum <= editor.getLastVisibleLine()); | ||
return (cursorPos && editor && cursorPos.line >= editor.getFirstVisibleLine() && cursorPos.line <= editor.getLastVisibleLine()); | ||
}; | ||
|
||
/** | ||
|
@@ -541,6 +547,11 @@ define(function (require, exports, module) { | |
return asyncResult.promise(); | ||
} | ||
|
||
var cursorPos = extractCursorPos(query); | ||
if (cursorPos && !cursorPos.local && cursorPos.query !== "") { | ||
query = query.replace(cursorPos.query, ""); | ||
} | ||
|
||
// First pass: filter based on search string; convert to SearchResults containing extra info | ||
// for sorting & display | ||
var filteredList = $.map(fileList, function (fileInfo) { | ||
|
@@ -583,10 +594,10 @@ define(function (require, exports, module) { | |
this._updateDialogLabel(query); | ||
|
||
// "Go to line" mode is special-cased | ||
var gotoLine = extractLineNumber(query); | ||
if (!isNaN(gotoLine)) { | ||
var from = {line: gotoLine, ch: 0}; | ||
var to = {line: gotoLine, ch: 99999}; | ||
var cursorPos = extractCursorPos(query); | ||
if (cursorPos && cursorPos.local) { | ||
var from = {line: cursorPos.line, ch: cursorPos.ch}; | ||
var to = {line: cursorPos.line}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Join the vars, and align the equals. |
||
|
||
EditorManager.getCurrentFullEditor().setSelection(from, to, true); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still requires Docs, to know what is this RegExp for, and to mark it as a constant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or since is used only in
extractCursorPos
, maybe it could just be a variable there?