update the current file in tern when a new file is opened

exclude less*.min.js files
This commit is contained in:
dloverin committed 2013-05-16 16:13:45 -04:00
1 parent 64738dadb9
commit 18dd7a144b
5 files changed
+154 -45

No files matched your search

@@ -33,6 +33,7 @@ define(function (require, exports, module) {
SINGLE_QUOTE = "'",
DOUBLE_QUOTE = "\"",
TERN_ADD_FILES_MSG = "AddFiles",
TERN_UPDATE_FILE_MSG = "UpdateFile",
TERN_INIT_MSG = "Init",
TERN_JUMPTODEF_MSG = "JumptoDef",
TERN_COMPLETIONS_MSG = "Completions",
@@ -214,5 +215,6 @@ define(function (require, exports, module) {
exports.TERN_CALLED_FUNC_TYPE_MSG = TERN_CALLED_FUNC_TYPE_MSG;
exports.TERN_PRIME_PUMP_MSG = TERN_PRIME_PUMP_MSG;
exports.TERN_GET_GUESSES_MSG = TERN_GET_GUESSES_MSG;
exports.TERN_UPDATE_FILE_MSG = TERN_UPDATE_FILE_MSG;
exports.SUPPORTED_LANGUAGES = SUPPORTED_LANGUAGES;
});
@@ -58,7 +58,9 @@ define(function (require, exports, module) {
numAddedFiles = 0,
stopAddingFiles = false,
// exclude require and jquery since we have special knowledge of those
excludedFilesRegEx = /require\.js$|jquery-[\d]\.[\d]\.js$/,
// temporarily exclude less*min.js because it is causing instability in tern.
excludedFilesRegEx = /require\.js$|jquery[\w.\-]*\.js$|less[\w.\-]*\.min\.js$/,
isDocumentDirty = false,
_ternWorker = (function () {
var path = ExtensionUtils.getModulePath(module, "tern-worker.js");
return new Worker(path);
@@ -233,6 +235,8 @@ define(function (require, exports, module) {
* @param {string} dir - directory to list the files of.
* @param {function(Array.<string>)} successCallback - callback with
* array of file path names.
* @param {function(Array.<string>)} errorCallback - callback for
* when an error occurs.
*/
function getFilesInDirectory(dir, successCallback, errorCallback) {
var files = []; // file names without paths.
@@ -762,6 +766,41 @@ define(function (require, exports, module) {
}
}
/**
* Update tern with the new contents of a given file.
*
* @param {Document} document - the document to update
* @return {jQuery.Promise} - the promise for the request
*/
function updateTernFile(document) {
var path = document.file.fullPath;
_postMessageByPass({
type : HintUtils.TERN_UPDATE_FILE_MSG,
path : path,
text : document.getText()
});
return addPendingRequest(path, 0, HintUtils.TERN_UPDATE_FILE_MSG);
}
/**
* Handle the response from the tern web worker when
* it responds to the update file message.
*
* @param {{path:string, type: string}} response - the response from the worker
*/
function handleUpdateFile(response) {
var path = response.path,
type = response.type,
$deferredHints = getPendingRequest(path, 0, type);
if ($deferredHints) {
$deferredHints.resolve();
}
}
/**
* We can skip tern initialization if we are opening a file that has
* already been added to tern.
@@ -779,10 +818,11 @@ define(function (require, exports, module) {
* Do the work to initialize a code hinting session.
*
* @param {Session} session - the active hinting session
* @param {Document} document - the document of the editor that has changed
* @param {Document} document - the document the editor has changed to
* @param {Document} previousDocument - the document the editor has changed from
* @param {boolean} shouldPrimePump - true if the pump should be primed.
*/
function doEditorChange(session, document, shouldPrimePump) {
function doEditorChange(session, document, previousDocument, shouldPrimePump) {
var path = document.file.fullPath,
split = HintUtils.splitPath(path),
dir = split.dir,
@@ -790,19 +830,34 @@ define(function (require, exports, module) {
file = split.file,
pr;
pr = ProjectManager.getProjectRoot() ? ProjectManager.getProjectRoot().fullPath : null;
// avoid re-initializing tern if possible.
if (canSkipTernInitialization(path)) {
// skipping initializing tern
return;
}
var ternDeferred = $.Deferred(),
addFilesDeferred = $.Deferred();
ternPromise = ternDeferred.promise();
addFilesPromise = addFilesDeferred.promise();
pr = ProjectManager.getProjectRoot() ? ProjectManager.getProjectRoot().fullPath : null;
// avoid re-initializing tern if possible.
if (canSkipTernInitialization(path)) {
// skipping initializing tern
ternDeferred.resolveWith(null, [_ternWorker]);
// update the previous document in tern to prevent stale files.
if (isDocumentDirty && previousDocument) {
var updateFilePromise = updateTernFile(previousDocument);
updateFilePromise.done(function () {
primePump(path, document.getText());
addFilesDeferred.resolveWith(null, [_ternWorker]);
});
} else {
addFilesDeferred.resolveWith(null, [_ternWorker]);
}
isDocumentDirty = false;
return;
}
isDocumentDirty = false;
pendingTernRequests = [];
resolvedFiles = {};
@@ -851,18 +906,29 @@ define(function (require, exports, module) {
*
* @param {Session} session - the active hinting session
* @param {Document} document - the document of the editor that has changed
* @param {Document} previousDocument - the document of the editor is changing from
* @param {boolean} shouldPrimePump - true if the pump should be primed.
*/
function handleEditorChange(session, document, shouldPrimePump) {
function handleEditorChange(session, document, previousDocument, shouldPrimePump) {
if (addFilesPromise === null) {
doEditorChange(session, document, shouldPrimePump);
doEditorChange(session, document, previousDocument, shouldPrimePump);
} else {
addFilesPromise.done(function () {
doEditorChange(session, document, shouldPrimePump);
doEditorChange(session, document, previousDocument, shouldPrimePump);
});
}
}
/*
* Called each time the file associated with the active editor changes.
* Marks the file as being dirty and refresh its outer scope.
*
* @param {Document} document - the document that has changed
*/
function handleFileChange(document) {
isDocumentDirty = true;
}
_ternWorker.addEventListener("message", function (e) {
var response = e.data,
type = response.type;
@@ -880,6 +946,8 @@ define(function (require, exports, module) {
handlePrimePumpCompletion(response);
} else if (type === HintUtils.TERN_GET_GUESSES_MSG) {
handleGetGuesses(response);
} else if (type === HintUtils.TERN_UPDATE_FILE_MSG) {
handleUpdateFile(response);
} else {
console.log("Worker: " + (response.log || response));
}
@@ -890,6 +958,7 @@ define(function (require, exports, module) {
exports.getTernHints = getTernHints;
exports.handleEditorChange = handleEditorChange;
exports.requestGuesses = requestGuesses;
exports.handleFileChange = handleFileChange;
exports.requestHints = requestHints;
exports.requestJumptoDef = requestJumptoDef;
});
@@ -186,7 +186,7 @@ define(function (require, exports, module) {
var cursor = session.getCursor(),
type = session.getType();
return !cachedHints ||
return !cachedHints || !cachedCursor || !cachedType ||
cachedCursor.line !== cursor.line ||
type.property !== cachedType.property ||
type.context !== cachedType.context ||
@@ -248,6 +248,20 @@ define(function (require, exports, module) {
return editor.getModeForSelection() === "javascript";
}
/**
* Create a new StringMatcher instance, if needed.
*
* @returns {StringMatcher} - a StringMatcher instance.
*/
function getStringMatcher() {
if (!matcher) {
matcher = new StringMatch.StringMatcher({
preferPrefixMatches: true
});
}
return matcher;
}
/**
* Common code to get the session hints. Will get guesses if there were
@@ -263,7 +277,7 @@ define(function (require, exports, module) {
*/
function getSessionHints(query, type, $deferredHints) {
var hintResults = session.getHints(query, matcher);
var hintResults = session.getHints(query, getStringMatcher());
if (hintResults.needGuesses) {
var guessesResponse = ScopeManager.requestGuesses(session,
session.editor.document);
@@ -274,7 +288,7 @@ define(function (require, exports, module) {
guessesResponse.done(function () {
query = session.getQuery();
hintResults = session.getHints(query, matcher);
hintResults = session.getHints(query, getStringMatcher());
cachedHints = hintResults.hints;
var hintResponse = getHintResponse(cachedHints, query, type);
@@ -367,10 +381,6 @@ define(function (require, exports, module) {
var $deferredHints = $.Deferred();
scopeResponse.promise.done(function () {
cachedType = session.getType();
matcher = new StringMatch.StringMatcher({
preferPrefixMatches: true
});
getSessionHints(session.getQuery(), type, $deferredHints);
}).fail(function () {
if ($deferredHints.state() === "pending") {
@@ -491,11 +501,14 @@ define(function (require, exports, module) {
* information, and reject any pending deferred requests.
*
* @param {Editor} editor - editor context to be initialized.
* @param {Editor} editor - the previous editor.
* @param {boolean} primePump - true if the pump should be primed.
*/
function initializeSession(editor, primePump) {
function initializeSession(editor, previousEditor, primePump) {
session = new Session(editor);
ScopeManager.handleEditorChange(session, editor.document, primePump);
ScopeManager.handleEditorChange(session, editor.document,
previousEditor ? previousEditor.document : null,
primePump);
cachedHints = null;
}
@@ -504,15 +517,20 @@ define(function (require, exports, module) {
*
* @param {Editor} editor - editor context on which to listen for
* changes
* @param {Editor} previousEditor - the previous editor
*/
function installEditorListeners(editor) {
function installEditorListeners(editor, previousEditor) {
// always clean up cached scope and hint info
cachedCursor = null;
cachedHints = null;
cachedType = null;
if (editor && HintUtils.isSupportedLanguage(LanguageManager.getLanguageForPath(editor.document.file.fullPath).getId())) {
initializeSession(editor, true);
initializeSession(editor, previousEditor, true);
$(editor)
.on(HintUtils.eventName("change"), function () {
ScopeManager.handleFileChange(editor.document);
});
} else {
session = null;
}
@@ -540,7 +558,7 @@ define(function (require, exports, module) {
*/
function handleActiveEditorChange(event, current, previous) {
uninstallEditorListeners(previous);
installEditorListeners(current);
installEditorListeners(current, previous);
}
/*
@@ -284,6 +284,24 @@ importScripts("thirdparty/requirejs/require.js");
});
}
/**
* Update the context of a file in tern.
*
* @param {string} path - full path of file.
* @param {string} text - content of the file.
*/
function handleUpdateFile(path, text) {
ternServer.addFile(path, text);
self.postMessage({type: HintUtils.TERN_UPDATE_FILE_MSG,
path: path
});
// reset to get the best hints with the updated file.
ternServer.reset();
}
/**
* Make a completions request to tern to force tern to resolve files
* and create a fast first lookup for the user.
@@ -345,6 +363,8 @@ importScripts("thirdparty/requirejs/require.js");
text = request.text;
offset = request.offset;
getTernProperties(dir, file, offset, text, HintUtils.TERN_GET_GUESSES_MSG);
} else if (type === HintUtils.TERN_UPDATE_FILE_MSG) {
handleUpdateFile(request.path, request.text);
} else {
_log("Unknown message: " + JSON.stringify(request));
}
@@ -684,7 +684,25 @@ define(function (require, exports, module) {
expect(testDoc.getLine(end.line).length).toEqual(8);
});
});
it("should replace property hints but not following delimiters", function () {
var start = { line: 6, ch: 0 },
middle = { line: 6, ch: 4 },
end = { line: 6, ch: 9 },
endplus = { line: 6, ch: 10 };
testDoc.replaceRange("(A1.prop)", start, start);
testEditor.setCursorPos(middle);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
selectHint(JSCodeHints.jsHintProvider, hintObj, "propA");
runs(function () {
expect(testEditor.getCursorPos()).toEqual(end);
expect(testDoc.getRange(start, endplus)).toEqual("(A1.propA)");
expect(testDoc.getLine(endplus.line).length).toEqual(10);
});
});
it("should list hints for string, as string assigned to 's', 's' assigned to 'r' and 'r' assigned to 't'", function () {
var start = { line: 26, ch: 0 },
middle = { line: 26, ch: 2 };
@@ -961,24 +979,6 @@ define(function (require, exports, module) {
});
});
it("should replace property hints but not following delimiters", function () {
var start = { line: 6, ch: 0 },
middle = { line: 6, ch: 4 },
end = { line: 6, ch: 9 },
endplus = { line: 6, ch: 10 };
testDoc.replaceRange("(A1.prop)", start, start);
testEditor.setCursorPos(middle);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
selectHint(JSCodeHints.jsHintProvider, hintObj, "propA");
runs(function () {
expect(testEditor.getCursorPos()).toEqual(end);
expect(testDoc.getRange(start, endplus)).toEqual("(A1.propA)");
expect(testDoc.getLine(endplus.line).length).toEqual(10);
});
});
it("should jump to var", function () {
var start = { line: 44, ch: 10 };