Revert "Merge pull request #3697 from jeffslofish/Issue-3676"

This reverts commit 9e99101057, reversing
changes made to a9e27717f4.
This commit is contained in:
Randy Edmunds committed 2013-05-05 10:46:07 -07:00
1 parent 9e99101057
commit 53bf87ed22
35 files changed
+996 -104

No files matched your search

@@ -72,7 +72,7 @@ define(function (require, exports, module) {
* @return {boolean} - could the token be hintable?
*/
function hintable(token) {
switch (token.type) {
switch (token.className) {
case "comment":
case "number":
case "regexp":
@@ -316,7 +316,7 @@ define(function (require, exports, module) {
}
}
}
if (token.type === "property") {
if (token.className === "property") {
propertyLookup = true;
}
if (this.findPreviousDot()) {
@@ -40,7 +40,8 @@ define(function (require, exports, module) {
StringMatch = brackets.getModule("utils/StringMatch"),
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"));
@@ -53,7 +54,6 @@ define(function (require, exports, module) {
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.
@@ -210,18 +210,18 @@ define(function (require, exports, module) {
return true;
}
if (token.type === null) {
if (token.className === null) {
token = session.getNextTokenOnLine(cursor);
}
if (lastToken && lastToken.type === null) {
if (lastToken && lastToken.className === 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) {
token.className !== lastToken.className) {
return true;
}
@@ -376,8 +376,46 @@ 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;
@@ -392,11 +430,17 @@ 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) {
function initializeSession(editor, primePump) {
ScopeManager.handleEditorChange(editor.document);
session = new Session(editor);
cachedHints = null;
// prime pump for hints so the first user request is fast
if (primePump) {
ScopeManager.requestHints(session, session.editor.document, 0);
}
}
/*
@@ -412,7 +456,7 @@ define(function (require, exports, module) {
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);
@@ -42,4 +42,8 @@ require(["MyModule"], function (myModule) {
});
funB();
var x = A1;
var x = A1;
var arr = [];
arr["my-key"] = 1;
arr["for"] = 10;
@@ -332,7 +332,7 @@ define(function (require, exports, module) {
// create Editor instance (containing a CodeMirror instance)
runs(function () {
testEditor = createMockEditor(testDoc);
JSCodeHints.initializeSession(testEditor);
JSCodeHints.initializeSession(testEditor, false);
});
});
@@ -731,6 +731,53 @@ define(function (require, exports, module) {
});
});
it("should insert hint as [\"my-key\"] since 'my-key' is not a valid property name", function () {
var start = { line: 49, ch: 0 },
middle = { line: 49, ch: 5 },
end = { line: 49, ch: 13 };
testDoc.replaceRange("arr.m", start, start);
testEditor.setCursorPos(middle);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
selectHint(JSCodeHints.jsHintProvider, hintObj, "my-key");
runs(function () {
expect(testEditor.getCursorPos()).toEqual(end);
expect(testDoc.getRange(start, end)).toEqual("arr[\"my-key\"]");
expect(testDoc.getLine(end.line).length).toEqual(13);
});
});
it("should insert hint as [\"my-key\"] make sure this works if nothing is typed after the '.'", function () {
var start = { line: 49, ch: 0 },
middle = { line: 49, ch: 4 },
end = { line: 49, ch: 13 };
testDoc.replaceRange("arr.", start, start);
testEditor.setCursorPos(middle);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
selectHint(JSCodeHints.jsHintProvider, hintObj, "my-key");
runs(function () {
expect(testEditor.getCursorPos()).toEqual(end);
expect(testDoc.getRange(start, end)).toEqual("arr[\"my-key\"]");
expect(testDoc.getLine(end.line).length).toEqual(13);
});
});
it("should insert hint as '.for' since keywords can be used as property names", function () {
var start = { line: 49, ch: 0 },
middle = { line: 49, ch: 5 },
end = { line: 49, ch: 7 };
testDoc.replaceRange("arr.f", start, start);
testEditor.setCursorPos(middle);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
selectHint(JSCodeHints.jsHintProvider, hintObj, "for");
runs(function () {
expect(testEditor.getCursorPos()).toEqual(end);
expect(testDoc.getRange(start, end)).toEqual("arr.for");
expect(testDoc.getLine(end.line).length).toEqual(7);
});
});
});
});