mirror of
https://github.com/RustyRaptor/brackets.git
synced 2026-12-04 00:15:22 -07:00
Fix Issue #3863 - jump-to-def goes to exports line.
When the result of jump-to-def looks like: exports.foo = foo; we now will try and jump-to-def on the rhs, as that's what the user most likely really wants. Jumping to the exports line is rarely useful. When we get the response for a jump-to-def, if it looks like we're jumping to a member expression like the example above, then we will grab the RHS, and trigger another jump-to-def on that, which should take us to the correct place.
This commit is contained in:
1 parent
360dd8a56a
commit
269c3b77c9
2 files changed
+150
-25
No files matched your search
@@ -104,6 +104,16 @@ define(function (require, exports, module) {
|
||||
Session.prototype.getOffset = function () {
|
||||
var cursor = this.getCursor();
|
||||
|
||||
return this.getOffsetFromCursor(cursor);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the offset of a cursor position
|
||||
*
|
||||
* @param {{line: number, ch: number}} the line/col info
|
||||
* @return {number} - the offset into the current document of the cursor
|
||||
*/
|
||||
Session.prototype.getOffsetFromCursor = function (cursor) {
|
||||
return this.editor.indexFromPos(cursor);
|
||||
};
|
||||
|
||||
@@ -192,6 +202,36 @@ define(function (require, exports, module) {
|
||||
|
||||
return prev;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the token after the one at the given cursor position
|
||||
*
|
||||
* @param {{line: number, ch: number}} cursor - cursor position after
|
||||
* which a token should be retrieved
|
||||
* @return {Object} - the CodeMirror token after the one at the given
|
||||
* cursor position
|
||||
*/
|
||||
Session.prototype.getNextToken = function (cursor, skipWhitespace) {
|
||||
var token = this.getToken(cursor),
|
||||
next = token,
|
||||
doc = this.editor.document;
|
||||
|
||||
do {
|
||||
if (next.start > cursor.ch) {
|
||||
cursor.ch = next.end;
|
||||
} else if (next.end > 0) {
|
||||
cursor.ch = next.end + 1;
|
||||
} else if (cursor.line > 0) {
|
||||
cursor.ch = doc.getLine(cursor.line + 1).length;
|
||||
cursor.line++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
next = this.getToken(cursor);
|
||||
} while (skipWhitespace && next.string.trim() === "");
|
||||
|
||||
return next;
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate a query string relative to the current cursor position
|
||||
|
||||
@@ -591,8 +591,9 @@ define(function (require, exports, module) {
|
||||
*/
|
||||
function handleJumpToDefinition() {
|
||||
var offset,
|
||||
response;
|
||||
handleJumpResponse;
|
||||
|
||||
|
||||
// Only provide jump-to-definition results when cursor is in JavaScript content
|
||||
if (session.editor.getModeForSelection() !== "javascript") {
|
||||
return null;
|
||||
@@ -600,34 +601,118 @@ define(function (require, exports, module) {
|
||||
|
||||
var result = new $.Deferred();
|
||||
|
||||
offset = session.getOffset();
|
||||
response = ScopeManager.requestJumptoDef(session, session.editor.document, offset);
|
||||
|
||||
if (response.hasOwnProperty("promise")) {
|
||||
response.promise.done(function (jumpResp) {
|
||||
|
||||
if (jumpResp.resultFile) {
|
||||
if (jumpResp.resultFile !== jumpResp.file) {
|
||||
var resolvedPath = ScopeManager.getResolvedPath(jumpResp.resultFile);
|
||||
if (resolvedPath) {
|
||||
CommandManager.execute(Commands.FILE_OPEN, {fullPath: resolvedPath})
|
||||
.done(function () {
|
||||
session.editor.setSelection(jumpResp.start, jumpResp.end, true);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
session.editor.setSelection(jumpResp.start, jumpResp.end, true);
|
||||
}
|
||||
result.resolve(true);
|
||||
} else {
|
||||
/**
|
||||
* Make a jump-to-def request based on the session and offset passed in.
|
||||
* @param {Session} session - the session
|
||||
* @param {number} offset - the offset of where to jump from
|
||||
*/
|
||||
function requestJumpToDef(session, offset) {
|
||||
var response = ScopeManager.requestJumptoDef(session, session.editor.document, offset);
|
||||
|
||||
if (response.hasOwnProperty("promise")) {
|
||||
response.promise.done(handleJumpResponse).fail(function () {
|
||||
result.reject();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}).fail(function () {
|
||||
result.reject();
|
||||
});
|
||||
/**
|
||||
* Sets the selection to move the cursor to the result position.
|
||||
* Assumes that the editor has already changed files, if necessary.
|
||||
*
|
||||
* Additionally, this will check to see if the selection looks like an
|
||||
* assignment to a member expression - if it is, and the type is a function,
|
||||
* then we will attempt to jump to the RHS of the expression.
|
||||
*
|
||||
* 'exports.foo = foo'
|
||||
*
|
||||
* if the selection is 'foo' in 'exports.foo', then we will attempt to jump to def
|
||||
* on the rhs of the assignment.
|
||||
*
|
||||
* @param {number} start - the start of the selection
|
||||
* @param {number} end - the end of the selection
|
||||
*/
|
||||
function setJumpSelection(start, end) {
|
||||
|
||||
/**
|
||||
* helper function to decide if the tokens on the RHS of an assignment
|
||||
* look like an identifier, or member expr.
|
||||
*/
|
||||
function validIdOrProp(token) {
|
||||
if (token.string === ".") {
|
||||
return true;
|
||||
}
|
||||
var type = token.type;
|
||||
if (type === "variable-2" || type === "variable" || type === "property") {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// set the selection
|
||||
session.editor.setSelection(start, end, true);
|
||||
var cursor = session.getCursor(),
|
||||
prev = session._getPreviousToken(cursor),
|
||||
next,
|
||||
token,
|
||||
offset,
|
||||
madeNewRequest;
|
||||
|
||||
// see if the selection is preceded by a '.', indicating we're in a member expr
|
||||
if (prev.string === ".") {
|
||||
cursor = session.getCursor();
|
||||
next = session.getNextToken(cursor, true);
|
||||
// check if the next token indicates an assignment
|
||||
if (next.string === "=") {
|
||||
next = session.getNextToken(cursor, true);
|
||||
// find the last token of the identifier, or member expr
|
||||
while (validIdOrProp(next)) {
|
||||
offset = session.getOffsetFromCursor({line: cursor.line, ch: next.end});
|
||||
next = session.getNextToken(cursor, false);
|
||||
}
|
||||
if (offset) {
|
||||
// trigger another jump to def based on the offset of the RHS
|
||||
requestJumpToDef(session, offset);
|
||||
madeNewRequest = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// We didn't make a new jump-to-def request, so we can resolve the promise
|
||||
if (!madeNewRequest) {
|
||||
result.resolve(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* handle processing of the completed jump-to-def request.
|
||||
* will open the appropriate file, and set the selection based
|
||||
* on the response.
|
||||
*/
|
||||
handleJumpResponse = function (jumpResp) {
|
||||
|
||||
if (jumpResp.resultFile) {
|
||||
if (jumpResp.resultFile !== jumpResp.file) {
|
||||
var resolvedPath = ScopeManager.getResolvedPath(jumpResp.resultFile);
|
||||
if (resolvedPath) {
|
||||
CommandManager.execute(Commands.FILE_OPEN, {fullPath: resolvedPath})
|
||||
.done(function () {
|
||||
setJumpSelection(jumpResp.start, jumpResp.end);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
setJumpSelection(jumpResp.start, jumpResp.end);
|
||||
}
|
||||
} else {
|
||||
result.reject();
|
||||
}
|
||||
};
|
||||
|
||||
offset = session.getOffset();
|
||||
// request a jump-to-def
|
||||
requestJumpToDef(session, offset);
|
||||
|
||||
return result.promise();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user