Added Goto Definition.

This commit is contained in:
jeffkenton committed 2013-04-08 15:50:11 -04:00
1 parent bef501fda0
commit af9a7f01ab
6 files changed
+153 -1

No files matched your search

@@ -32,6 +32,7 @@ define(function (require, exports, module) {
SINGLE_QUOTE = "\'",
DOUBLE_QUOTE = "\"",
TERN_INIT_MSG = "Init",
TERN_JUMPTODEF_MSG = "JumptoDef",
TERN_COMPLETIONS_MSG = "Completions",
TERN_GET_FILE_MSG = "GetFile",
TERN_GET_PROPERTIES_MSG = "Properties",
@@ -200,6 +201,7 @@ define(function (require, exports, module) {
exports.LANGUAGE_ID = LANGUAGE_ID;
exports.SINGLE_QUOTE = SINGLE_QUOTE;
exports.DOUBLE_QUOTE = DOUBLE_QUOTE;
exports.TERN_JUMPTODEF_MSG = TERN_JUMPTODEF_MSG;
exports.TERN_COMPLETIONS_MSG = TERN_COMPLETIONS_MSG;
exports.TERN_INIT_MSG = TERN_INIT_MSG;
exports.TERN_GET_FILE_MSG = TERN_GET_FILE_MSG;
@@ -139,6 +139,63 @@ define(function (require, exports, module) {
}
}
/**
* Get a Promise for the definition from TernJS, for the file & offset passed in.
* @return {jQuery.Promise} - a promise that will resolve to definition when
* it is done
*/
function getJumptoDef(dir, file, offset, text) {
ternWorker.postMessage({
type: HintUtils.TERN_JUMPTODEF_MSG,
dir:dir,
file:file,
offset:offset,
text:text
});
var $deferredJump = $.Deferred();
pendingTernRequests[file] = $deferredJump;
return $deferredJump.promise();
}
/**
* Request Jump-To-Definition from Tern.
*
* @param {session} session - the session
* @param {Document} document - the document
* @param {number} offset - the offset into the document
* @return {jQuery.Promise} - The promise will not complete until tern
* has completed.
*/
function requestJumptoDef(session, document, offset) {
var path = document.file.fullPath,
split = HintUtils.splitPath(path),
dir = split.dir,
file = split.file;
var ternPromise = getJumptoDef(dir, file, offset, document.getText());
return {promise:ternPromise};
}
/**
* Handle the response from the tern web worker when
* it responds with the definition
*
* @param response - the response from the worker
*/
function handleJumptoDef(response) {
var file = response.file;
var $deferredJump = pendingTernRequests[file];
pendingTernRequests[file] = null;
if( $deferredJump ) {
$deferredJump.resolveWith(null, [response]);
}
}
/**
* Request hints from Tern.
*
@@ -425,6 +482,8 @@ define(function (require, exports, module) {
} else if ( type === HintUtils.TERN_GET_FILE_MSG ) {
// handle a request for the contents of a file
handleTernGetFile(response);
} else if ( type === HintUtils.TERN_JUMPTODEF_MSG ) {
handleJumptoDef(response);
} else{
console.log("Worker: " + (response.log || response));
}
@@ -461,6 +520,7 @@ define(function (require, exports, module) {
exports.handleEditorChange = handleEditorChange;
exports.handleFileChange = handleFileChange;
exports.requestJumptoDef = requestJumptoDef;
exports.requestHints = requestHints;
exports.isScopeDirty = isScopeDirty;
exports.getTernHints = getTernHints;
@@ -0,0 +1,5 @@
{
"jumptoDefinition": [
"Ctrl-J"
]
}
@@ -29,6 +29,10 @@ define(function (require, exports, module) {
var CodeHintManager = brackets.getModule("editor/CodeHintManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
Commands = brackets.getModule("command/Commands"),
CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
Strings = brackets.getModule("strings"),
AppInit = brackets.getModule("utils/AppInit"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
StringUtils = brackets.getModule("utils/StringUtils"),
@@ -36,6 +40,10 @@ define(function (require, exports, module) {
ScopeManager = require("ScopeManager"),
Session = require("Session");
var KeyboardPrefs = JSON.parse(require("text!keyboard.json"));
var JUMPTO_DEFINITION = "navigate.jumptoDefinition";
var session = null, // object that encapsulates the current session state
cachedHints = null, // sorted hints for the current hinting session
cachedType = null, // describes the lookup type and the object context
@@ -376,6 +384,33 @@ define(function (require, exports, module) {
installEditorListeners(current);
}
/*
* Handle JumptoDefiniton menu/keyboard command.
*/
function handleJumpToDefinition() {
var offset = session.getOffset(),
response = ScopeManager.requestJumptoDef(session, session.editor.document, offset);
if (response.hasOwnProperty("promise")) {
response.promise.done(function (jumpResp) {
session.editor.setSelection(jumpResp.start, jumpResp.end);
session.editor.setCursorPos(jumpResp.start);
session.editor.centerOnCursor();
}).fail(function () {
response.reject();
});
}
}
// Register command handler
CommandManager.register(Strings.CMD_JUMPTO_DEFINITION, JUMPTO_DEFINITION, handleJumpToDefinition);
// Add the menu item
var menu = Menus.getMenu(Menus.AppMenuBar.NAVIGATE_MENU);
menu.addMenuItem(JUMPTO_DEFINITION, KeyboardPrefs.jumptoDefinition, Menus.BEFORE, Commands.NAVIGATE_GOTO_DEFINITION);
ExtensionUtils.loadStyleSheet(module, "styles/brackets-js-hints.css");
// uninstall/install change listener as the active editor changes
@@ -102,6 +102,49 @@ importScripts("thirdparty/requirejs/require.js");
return request;
}
/**
* Get definition location
* @param {string} dir - the directory
* @param {string} file - the file name
* @param {number} offset - the offset into the file for cursor
* @param {string} text - the text of the file
*/
function getJumptoDef(dir, file, offset, text) {
function buildRequest(dir, file, query, offset){
query = {type:query};
query.start = offset;
query.end = offset;
query.file = file;
query.filter = false;
query.sort = false;
query.depths = true;
query.types = true;
query.lineCharPositions = true;
var request = {query:query, files:[], offset:offset};
request.files.push({type:"full", name:file, text:text});
return request;
}
var request = buildRequest(dir, file, "definition", offset);
ternServer.request(request, function(error, data) {
if (error) {
_log("definition error: " + JSON.stringify(data));
self.postMessage({type: HintUtils.TERN_JUMPTODEF_MSG,});
return;
}
// Post a message back to the main thread with the definition
self.postMessage({type: HintUtils.TERN_JUMPTODEF_MSG,
file: data.file,
start: data.start,
end: data.end
});
});
}
/**
* Get the completions for the given offset
@@ -232,9 +275,15 @@ importScripts("thirdparty/requirejs/require.js");
text = request.text;
var pos = request.pos;
handleFunctionType(dir, file, pos, text);
} else if ( type === HintUtils.TERN_JUMPTODEF_MSG ) {
var file = request.file,
dir = request.dir,
offset = request.offset,
text = request.text;
getJumptoDef(dir, file, offset, text);
} else {
_log("Unknown message: " + JSON.stringify(request));
}
});
}());
}());