mirror of
https://github.com/RustyRaptor/brackets.git
synced 2026-12-04 00:15:22 -07:00
Merge branch 'master' into dangoor/prefer-prefix
Conflicts: src/extensions/default/JavaScriptCodeHints/main.js
This commit is contained in:
commit
89ba0925e9
136 files changed
+4206
-1106
No files matched your search
@@ -38,19 +38,31 @@ define(function (require, exports, module) {
|
||||
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
|
||||
StringUtils = brackets.getModule("utils/StringUtils"),
|
||||
StringMatch = brackets.getModule("utils/StringMatch"),
|
||||
LanguageManager = brackets.getModule("language/LanguageManager"),
|
||||
HintUtils = require("HintUtils"),
|
||||
ScopeManager = require("ScopeManager"),
|
||||
Session = require("Session");
|
||||
Session = require("Session"),
|
||||
Acorn = require("thirdparty/acorn/acorn");
|
||||
|
||||
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
|
||||
cachedLine = null, // the line number for the cached scope
|
||||
matcher = null; // string matcher for hints
|
||||
var session = null, // object that encapsulates the current session state
|
||||
cachedCursor = null, // last cursor of the current hinting session
|
||||
cachedHints = null, // sorted hints for the current hinting session
|
||||
cachedType = null, // describes the lookup type and the object context
|
||||
cachedToken = null, // the token used in the current hinting session
|
||||
matcher = null; // string matcher for hints
|
||||
|
||||
/**
|
||||
* Get the value of current session.
|
||||
* Used for unit testing.
|
||||
* @returns {Session} - the current session.
|
||||
*/
|
||||
function getSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a hint response object. Filters the hint list using the query
|
||||
@@ -82,7 +94,7 @@ define(function (require, exports, module) {
|
||||
var $hintObj = $("<span>").addClass("brackets-js-hints");
|
||||
|
||||
// level indicates either variable scope or property confidence
|
||||
if (!type.property && token.depth !== undefined) {
|
||||
if (!type.property && !token.builtin && token.depth !== undefined) {
|
||||
switch (token.depth) {
|
||||
case 0:
|
||||
$hintObj.addClass("priority-high");
|
||||
@@ -173,14 +185,69 @@ define(function (require, exports, module) {
|
||||
JSHints.prototype.needNewHints = function (session) {
|
||||
var cursor = session.getCursor(),
|
||||
type = session.getType();
|
||||
|
||||
|
||||
return !cachedHints ||
|
||||
cachedLine !== cursor.line ||
|
||||
cachedCursor.line !== cursor.line ||
|
||||
type.property !== cachedType.property ||
|
||||
type.context !== cachedType.context ||
|
||||
type.showFunctionType !== cachedType.showFunctionType;
|
||||
};
|
||||
|
||||
/**
|
||||
* Have conditions have changed enough to justify closing the hints popup?
|
||||
*
|
||||
* @param {Session} session - the active hinting session
|
||||
* @return {boolean} - true if the hints popup should be closed.
|
||||
*/
|
||||
JSHints.prototype.shouldCloseHints = function (session) {
|
||||
|
||||
// close if the token className has changed then close the hints.
|
||||
var cursor = session.getCursor(),
|
||||
token = session.getToken(cursor),
|
||||
lastToken = cachedToken;
|
||||
|
||||
// if the line has changed, then close the hints
|
||||
if (!cachedCursor || cursor.line !== cachedCursor.line) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (token.type === null) {
|
||||
token = session.getNextTokenOnLine(cursor);
|
||||
}
|
||||
|
||||
if (lastToken && lastToken.type === null) {
|
||||
lastToken = session.getNextTokenOnLine(cachedCursor);
|
||||
}
|
||||
|
||||
// Both of the tokens should never be null (happens when token is off
|
||||
// the end of the line), so one is null then close the hints.
|
||||
if (!lastToken || !token ||
|
||||
token.type !== lastToken.type) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Test if one token string is a prefix of the other.
|
||||
// If one is a prefix of the other then consider it the
|
||||
// same token and don't close the hints.
|
||||
if (token.string.length >= lastToken.string.length) {
|
||||
return token.string.indexOf(lastToken.string) !== 0;
|
||||
} else {
|
||||
return lastToken.string.indexOf(token.string) !== 0;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @return {boolean} - true if the document is a html file
|
||||
*/
|
||||
function isHTMLFile(document) {
|
||||
var languageID = LanguageManager.getLanguageForPath(document.file.fullPath).getId();
|
||||
return languageID === "html";
|
||||
}
|
||||
|
||||
function isInlineScript(editor) {
|
||||
return editor.getModeForSelection() === "javascript";
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether hints are available for a given editor context
|
||||
*
|
||||
@@ -190,19 +257,28 @@ define(function (require, exports, module) {
|
||||
*/
|
||||
JSHints.prototype.hasHints = function (editor, key) {
|
||||
if (session && HintUtils.hintableKey(key)) {
|
||||
|
||||
if (isHTMLFile(session.editor.document)) {
|
||||
if (!isInlineScript(session.editor)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
var cursor = session.getCursor(),
|
||||
token = session.getToken(cursor);
|
||||
|
||||
// don't autocomplete within strings or comments, etc.
|
||||
if (token && HintUtils.hintable(token)) {
|
||||
if (session.isFunctionName()) {
|
||||
return false;
|
||||
}
|
||||
var offset = session.getOffset(),
|
||||
type = session.getType(),
|
||||
query = session.getQuery();
|
||||
|
||||
if (this.needNewHints(session)) {
|
||||
//console.log("clear hints");
|
||||
cachedLine = null;
|
||||
cachedCursor = null;
|
||||
cachedHints = null;
|
||||
cachedToken = null;
|
||||
matcher = null;
|
||||
}
|
||||
return true;
|
||||
@@ -226,16 +302,23 @@ define(function (require, exports, module) {
|
||||
var type = session.getType(),
|
||||
query = session.getQuery();
|
||||
|
||||
// If the hint context is changed and the hints are open, then
|
||||
// close the hints by returning null;
|
||||
if (CodeHintManager.isOpen() && this.shouldCloseHints(session)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
cachedCursor = cursor;
|
||||
cachedToken = token;
|
||||
|
||||
// Compute fresh hints if none exist, or if the session
|
||||
// type has changed since the last hint computation
|
||||
if (this.needNewHints(session)) {
|
||||
var offset = session.getOffset(),
|
||||
scopeResponse = ScopeManager.requestHints(session, session.editor.document, offset);
|
||||
var scopeResponse = ScopeManager.requestHints(session, session.editor.document);
|
||||
|
||||
if (scopeResponse.hasOwnProperty("promise")) {
|
||||
var $deferredHints = $.Deferred();
|
||||
scopeResponse.promise.done(function () {
|
||||
cachedLine = cursor.line;
|
||||
cachedType = session.getType();
|
||||
matcher = new StringMatch.StringMatcher({
|
||||
preferPrefixMatches: true
|
||||
@@ -255,8 +338,6 @@ define(function (require, exports, module) {
|
||||
});
|
||||
|
||||
return $deferredHints;
|
||||
} else {
|
||||
cachedLine = cursor.line;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,7 +353,7 @@ define(function (require, exports, module) {
|
||||
/**
|
||||
* Inserts the hint selected by the user into the current editor.
|
||||
*
|
||||
* @param {jQuery.Object} hint - hint object to insert into current editor
|
||||
* @param {jQuery.Object} $hintObj - hint object to insert into current editor
|
||||
* @return {boolean} - should a new hinting session be requested
|
||||
* immediately after insertion?
|
||||
*/
|
||||
@@ -287,8 +368,7 @@ define(function (require, exports, module) {
|
||||
delimiter;
|
||||
|
||||
if (token && token.string === ".") {
|
||||
var nextCursor = session.getNextCursorOnLine(),
|
||||
nextToken = nextCursor ? session.getToken(nextCursor) : null;
|
||||
var nextToken = session.getNextTokenOnLine(cursor);
|
||||
|
||||
if (nextToken && // don't replace delimiters, etc.
|
||||
HintUtils.maybeIdentifier(nextToken.string) &&
|
||||
@@ -318,15 +398,52 @@ define(function (require, exports, module) {
|
||||
// if we were displaying a function type
|
||||
return false;
|
||||
}
|
||||
|
||||
if (session.getType().property) {
|
||||
// if we're inserting a property name, we need to make sure the
|
||||
// hint is a valid property name.
|
||||
// to check this, run the hint through Acorns tokenizer
|
||||
// it should result in one token, and that token should either be
|
||||
// a 'name' or a 'keyword', as javascript allows keywords as property names
|
||||
var tokenizer = Acorn.tokenize(completion);
|
||||
var currentToken = tokenizer(),
|
||||
invalidPropertyName = false;
|
||||
|
||||
// the name is invalid if the hint is not a 'name' or 'keyword' token
|
||||
if (currentToken.type !== Acorn.tokTypes.name && !currentToken.type.keyword) {
|
||||
invalidPropertyName = true;
|
||||
} else {
|
||||
// check for a second token - if there is one (other than 'eof')
|
||||
// then the hint isn't a valid property name either
|
||||
currentToken = tokenizer();
|
||||
if (currentToken.type !== Acorn.tokTypes.eof) {
|
||||
invalidPropertyName = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (invalidPropertyName) {
|
||||
// need to walk back to the '.' and replace
|
||||
// with '["<hint>"]
|
||||
var dotCursor = session.findPreviousDot();
|
||||
if (dotCursor) {
|
||||
completion = "[\"" + completion + "\"]";
|
||||
start.line = dotCursor.line;
|
||||
start.ch = dotCursor.ch - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Replace the current token with the completion
|
||||
session.editor.document.replaceRange(completion, start, end);
|
||||
// HACK (tracking adobe/brackets#1688): We talk to the private CodeMirror instance
|
||||
// directly to replace the range instead of using the Document, as we should. The
|
||||
// reason is due to a flaw in our current document synchronization architecture when
|
||||
// inline editors are open.
|
||||
session.editor._codeMirror.replaceRange(completion, start, end);
|
||||
|
||||
// Return false to indicate that another hinting session is not needed
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
// load the extension
|
||||
// load the extension
|
||||
AppInit.appReady(function () {
|
||||
|
||||
/*
|
||||
@@ -334,10 +451,11 @@ define(function (require, exports, module) {
|
||||
* information, and reject any pending deferred requests.
|
||||
*
|
||||
* @param {Editor} editor - editor context to be initialized.
|
||||
* @param {boolean} primePump - true if the pump should be primed.
|
||||
*/
|
||||
function initializeSession(editor) {
|
||||
ScopeManager.handleEditorChange(editor.document);
|
||||
function initializeSession(editor, primePump) {
|
||||
session = new Session(editor);
|
||||
ScopeManager.handleEditorChange(session, editor.document, primePump);
|
||||
cachedHints = null;
|
||||
}
|
||||
|
||||
@@ -349,12 +467,12 @@ define(function (require, exports, module) {
|
||||
*/
|
||||
function installEditorListeners(editor) {
|
||||
// always clean up cached scope and hint info
|
||||
cachedLine = null;
|
||||
cachedCursor = null;
|
||||
cachedHints = null;
|
||||
cachedType = null;
|
||||
|
||||
if (editor && editor.getLanguageForSelection().getId() === HintUtils.LANGUAGE_ID) {
|
||||
initializeSession(editor);
|
||||
initializeSession(editor, true);
|
||||
$(editor)
|
||||
.on(HintUtils.eventName("change"), function () {
|
||||
ScopeManager.handleFileChange(editor.document);
|
||||
@@ -425,7 +543,7 @@ define(function (require, exports, module) {
|
||||
// Add the menu item
|
||||
var menu = Menus.getMenu(Menus.AppMenuBar.NAVIGATE_MENU);
|
||||
if (menu) {
|
||||
menu.addMenuItem(JUMPTO_DEFINITION, KeyboardPrefs.jumptoDefinition, Menus.BEFORE, Commands.NAVIGATE_GOTO_DEFINITION);
|
||||
menu.addMenuItem(JUMPTO_DEFINITION, KeyboardPrefs.jumptoDefinition, Menus.AFTER, Commands.NAVIGATE_GOTO_DEFINITION);
|
||||
}
|
||||
|
||||
ExtensionUtils.loadStyleSheet(module, "styles/brackets-js-hints.css");
|
||||
@@ -439,9 +557,10 @@ define(function (require, exports, module) {
|
||||
installEditorListeners(EditorManager.getActiveEditor());
|
||||
|
||||
var jsHints = new JSHints();
|
||||
CodeHintManager.registerHintProvider(jsHints, [HintUtils.LANGUAGE_ID], 0);
|
||||
CodeHintManager.registerHintProvider(jsHints, [HintUtils.LANGUAGE_ID, "html"], 0);
|
||||
|
||||
// for unit testing
|
||||
exports.getSession = getSession;
|
||||
exports.jsHintProvider = jsHints;
|
||||
exports.initializeSession = initializeSession;
|
||||
exports.handleJumpToDefinition = handleJumpToDefinition;
|
||||
|
||||
Reference in new issue
Block a user