Implement requests with partial file text to reduce memory usage for large files.

Add large comment to file1.js test file so it will be considered a large file and partial text updates can be tested.
For now, move two failing tests apart so they pass. Partial text updates are notas good a full text updates but save memory.
This commit is contained in:
dloverin committed 2013-05-30 14:47:58 -04:00
1 parent ae950bdfce
commit cb8a0e9746
6 files changed
+480 -182

No files matched your search

@@ -38,6 +38,12 @@ define(function (require, exports, module) {
TERN_GET_GUESSES_MSG = "GetGuesses",
TERN_WORKER_READY = "WorkerReady";
// Message parameter constants
var TERN_FILE_INFO_TYPE_PART = "part",
TERN_FILE_INFO_TYPE_FULL = "full",
TERN_FILE_INFO_TYPE_EMPTY = "empty";
exports.TERN_ADD_FILES_MSG = TERN_ADD_FILES_MSG;
exports.TERN_JUMPTODEF_MSG = TERN_JUMPTODEF_MSG;
exports.TERN_COMPLETIONS_MSG = TERN_COMPLETIONS_MSG;
@@ -48,6 +54,9 @@ define(function (require, exports, module) {
exports.TERN_GET_GUESSES_MSG = TERN_GET_GUESSES_MSG;
exports.TERN_UPDATE_FILE_MSG = TERN_UPDATE_FILE_MSG;
exports.TERN_WORKER_READY = TERN_WORKER_READY;
exports.TERN_FILE_INFO_TYPE_PART = TERN_FILE_INFO_TYPE_PART;
exports.TERN_FILE_INFO_TYPE_FULL = TERN_FILE_INFO_TYPE_FULL;
exports.TERN_FILE_INFO_TYPE_EMPTY = TERN_FILE_INFO_TYPE_EMPTY;
});
@@ -29,7 +29,7 @@
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets, $, Worker, setTimeout */
/*global define, brackets, CodeMirror, $, Worker, setTimeout */
define(function (require, exports, module) {
"use strict";
@@ -55,13 +55,17 @@ define(function (require, exports, module) {
isDocumentDirty = false,
_hintCount = 0,
_lastPrimePump = false,
currentWorker = null;
currentWorker = null,
documentChanges = null; // bounds of document changes
var MAX_TEXT_LENGTH = 1000000, // about 1MB
MAX_FILES_IN_DIR = 100,
var MAX_TEXT_LENGTH = 1000000, // about 1MB
MAX_FILES_IN_DIR = 100,
MAX_FILES_IN_PROJECT = 100,
// how often to reset the tern server
MAX_HINTS = 30;
MAX_HINTS = 30,
LARGE_LINE_CHANGE = 100,
LARGE_LINE_COUNT = 250,
OFFSET_ZERO = {line: 0, ch: 0};
/**
* An array of library names that contain JavaScript builtins definitions.
@@ -199,13 +203,13 @@ define(function (require, exports, module) {
* Add a pending request waiting for the tern-worker to complete.
*
* @param {string} file - the name of the file
* @param {number} offset - the offset into the file the request is for
* @param {{line: number, ch: number}} offset - the offset into the file the request is for
* @param {string} type - the type of request
* @return {jQuery.Promise} - the promise for the request
*/
function addPendingRequest(file, offset, type) {
var requests,
key = file + "@" + offset,
key = file + "@" + offset.line + "@" + offset.ch,
$deferredRequest;
if (CollectionUtils.hasProperty(pendingTernRequests, key)) {
requests = pendingTernRequests[key];
@@ -225,12 +229,12 @@ define(function (require, exports, module) {
/**
* Get any pending $.Deferred object waiting on the specified file and request type
* @param {string} file - the file
* @param {number} offset - the offset in the file the request was at
* @param {{line: number, ch: number}} offset - the offset into the file the request is for
* @param {string} type - the type of request
* @return {jQuery.Deferred} - the $.Deferred for the request
*/
function getPendingRequest(file, offset, type) {
var key = file + "@" + offset;
var key = file + "@" + offset.line + "@" + offset.ch;
if (CollectionUtils.hasProperty(pendingTernRequests, key)) {
var requests = pendingTernRequests[key],
requestType = requests[type];
@@ -255,19 +259,23 @@ define(function (require, exports, module) {
/**
* Get a Promise for the definition from TernJS, for the file & offset passed in.
* @param {{type: string, name: string, offsetLines: number, text: string}} fileInfo
* - type of update, name of file, and the text of the update.
* For "full" updates, the whole text of the file is present. For "part" updates,
* the changed portion of the text. For "empty" updates, the file has not been modified
* and the text is empty.
* @param {{line: number, ch: number}} offset - the offset in the file the hints should be calculate at
* @return {jQuery.Promise} - a promise that will resolve to definition when
* it is done
*/
function getJumptoDef(dir, file, offset, text) {
function getJumptoDef(fileInfo, offset) {
postMessage({
type: MessageIds.TERN_JUMPTODEF_MSG,
dir: dir,
file: file,
offset: offset,
text: text
fileInfo: fileInfo,
offset: offset
});
return addPendingRequest(file, offset, MessageIds.TERN_JUMPTODEF_MSG);
return addPendingRequest(fileInfo.name, offset, MessageIds.TERN_JUMPTODEF_MSG);
}
/**
@@ -275,17 +283,18 @@ define(function (require, exports, module) {
*
* @param {session} session - the session
* @param {Document} document - the document
* @param {number} offset - the offset into the document
* @param {{line: number, ch: 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;
fileInfo = {type: MessageIds.TERN_FILE_INFO_TYPE_FULL,
name: path,
offsetLines: 0,
text: session.getJavascriptText()};
var ternPromise = getJumptoDef(dir, path, offset, session.getJavascriptText());
var ternPromise = getJumptoDef(fileInfo, offset);
return {promise: ternPromise};
}
@@ -311,50 +320,174 @@ define(function (require, exports, module) {
/**
* Get a Promise for the completions from TernJS, for the file & offset passed in.
* @param {string} dir - the directory the file is in
* @param {string} file - the name of the file
* @param {number} offset - the offset in the file the hints should be calculate at
* @param {string} text - the text of the file
*
* @param {{type: string, name: string, offsetLines: number, text: string}} fileInfo
* - type of update, name of file, and the text of the update.
* For "full" updates, the whole text of the file is present. For "part" updates,
* the changed portion of the text. For "empty" updates, the file has not been modified
* and the text is empty.
* @param {{line: number, ch: number}} offset - the offset in the file the hints should be calculate at
* @param {boolean} isProperty - true if getting a property hint,
* otherwise getting an identifier hint.
* @return {jQuery.Promise} - a promise that will resolve to an array of completions when
* it is done
*/
function getTernHints(dir, file, offset, text, isProperty) {
function getTernHints(fileInfo, offset, isProperty) {
/**
* If the document is large and we have modified a small portions of it that
* we are asking hints for, then send a partial document.
*/
postMessage({
type: MessageIds.TERN_COMPLETIONS_MSG,
dir: dir,
file: file,
fileInfo: fileInfo,
offset: offset,
text: text,
isProperty: isProperty
});
return addPendingRequest(file, offset, MessageIds.TERN_COMPLETIONS_MSG);
return addPendingRequest(fileInfo.name, offset, MessageIds.TERN_COMPLETIONS_MSG);
}
/**
* Get a Promise for the function type from TernJS.
* @param {string} dir - the directory the file is in
* @param {string} file - the name of the file
* @param {{line:number, ch:number}} pos - the line, column info for what we want the function type of.
* Unfortunately tern requires line/col for this request instead of offset, but we cache all the request
* promises by file & offset, so we need the pos and offset for this method
* @param {number} offset - the offset in the file the hints should be calculate at
* @param {string} text - the text of the file
* @param {{type: string, name: string, offsetLines: number, text: string}} fileInfo
* - type of update, name of file, and the text of the update.
* For "full" updates, the whole text of the file is present. For "part" updates,
* the changed portion of the text. For "empty" updates, the file has not been modified
* and the text is empty.
* @param {{line:number, ch:number}} offset - the line, column info for what we want the function type of.
* @return {jQuery.Promise} - a promise that will resolve to the function type of the function being called.
*/
function getTernFunctionType(dir, file, pos, offset, text) {
function getTernFunctionType(fileInfo, offset) {
postMessage({
type: MessageIds.TERN_CALLED_FUNC_TYPE_MSG,
dir: dir,
file: file,
pos: pos,
offset: offset,
text: text
fileInfo: fileInfo,
offset: offset
});
return addPendingRequest(file, offset, MessageIds.TERN_CALLED_FUNC_TYPE_MSG);
return addPendingRequest(fileInfo.name, offset, MessageIds.TERN_CALLED_FUNC_TYPE_MSG);
}
/**
* Given a starting and ending position, get a code fragment that is self contained
* enough to be compiled.
*
* @param {!Session} session - the current session
* @param {{line: number, ch: number}} start - the starting position of the changes
* @returns {{type: string, name: string, offsetLines: number, text: string}}
*/
function getFragmentAround(session, start) {
var minIndent = null,
minLine = null,
endLine,
cm = session.editor._codeMirror,
tabSize = cm.getOption("tabSize"),
document = session.editor.document,
p,
min,
indent;
// expand range backwards
for (p = start.line - 1, min = Math.max(0, p - 50); p >= min; --p) {
var line = session.getLine(p),
fn = line.search(/\bfunction\b/);
if (fn >= 0) {
indent = CodeMirror.countColumn(line, null, tabSize);
if (minIndent === null || minIndent > indent) {
if (session.getToken({line: p, ch: fn + 1}).type === "keyword") {
minIndent = indent;
minLine = p;
}
}
}
}
if (minIndent === null) {
minIndent = 0;
}
if (minLine === null) {
minLine = min;
}
var max = Math.min(cm.lastLine(), start.line + 90);
for (endLine = start.line + 1; endLine < max; ++endLine) {
indent = CodeMirror.countColumn(cm.getLine(endLine), null, tabSize);
if (indent <= minIndent) {
break;
}
}
var from = {line: minLine, ch: 0},
to = {line: endLine, ch: 0};
return {type: MessageIds.TERN_FILE_INFO_TYPE_PART,
name: document.file.fullPath,
offsetLines: from.line,
text: document.getRange(from, to)};
}
/**
* Get an object that describes what tern needs to know about the updated
* file to produce a hint. As a side-effect of this calls the document
* changes are reset.
*
* @param {!Session} session - the current session
* @returns {{type: string, name: {string}, offsetLines: {number}, text: {string}}
*/
function getFileInfo(session) {
var start = session.getCursor(),
end = start,
document = session.editor.document,
path = document.file.fullPath,
isHtmlFile = LanguageManager.getLanguageForPath(path).getId() === "html",
result;
if (isHtmlFile) {
result = {type: MessageIds.TERN_FILE_INFO_TYPE_FULL,
name: path,
text: session.getJavascriptText()};
} else if (!documentChanges) {
result = {type: MessageIds.TERN_FILE_INFO_TYPE_EMPTY,
name: path,
text: ""};
} else if (session.editor.lineCount() > LARGE_LINE_COUNT &&
(documentChanges.to - documentChanges.from < LARGE_LINE_CHANGE) &&
documentChanges.from <= start.line &&
documentChanges.to > end.line) {
result = getFragmentAround(session, start);
} else {
result = {type: MessageIds.TERN_FILE_INFO_TYPE_FULL,
name: path,
text: document.getText()};
}
documentChanges = null;
return result;
}
/**
* Get the current offset. The offset is adjusted for "part" updates.
*
* @param {!Session} session - the current session
* @param {{type: string, name: string, offsetLines: number, text: string}} fileInfo
* - type of update, name of file, and the text of the update.
* For "full" updates, the whole text of the file is present. For "part" updates,
* the changed portion of the text. For "empty" updates, the file has not been modified
* and the text is empty.
* @param {{line: number, ch: number}=} offset - the default offset (optional). Will
* use the cursor if not provided.
* @returns {{line: number, ch: number}}
*/
function getOffset(session, fileInfo, offset) {
var newOffset = offset || session.getCursor();
if (fileInfo.type === MessageIds.TERN_FILE_INFO_TYPE_PART) {
newOffset.line = Math.max(0, newOffset.line - fileInfo.offsetLines);
}
return newOffset;
}
/**
@@ -367,20 +500,17 @@ define(function (require, exports, module) {
* request has completed.
*/
function requestGuesses(session, document) {
var path = document.file.fullPath,
text = session.getJavascriptText(),
offset = session.getOffset(),
$deferred = $.Deferred();
var $deferred = $.Deferred(),
fileInfo = getFileInfo(session),
offset = getOffset(session, fileInfo);
postMessage({
type: MessageIds.TERN_GET_GUESSES_MSG,
dir: "",
file: path,
offset: offset,
text: text
fileInfo: fileInfo,
offset: offset
});
var promise = addPendingRequest(path, offset, MessageIds.TERN_GET_GUESSES_MSG);
var promise = addPendingRequest(fileInfo.name, offset, MessageIds.TERN_GET_GUESSES_MSG);
promise.done(function (guesses) {
session.setGuesses(guesses);
$deferred.resolve();
@@ -393,7 +523,7 @@ define(function (require, exports, module) {
* Handle the response from the tern web worker when
* it responds with the list of completions
*
* @param {{dir:string, file:string, offset:number, completions:Array.<string>,
* @param {{file: string, offset: {line: number, ch: number}, completions:Array.<string>,
* properties:Array.<string>}} response - the response from the worker
*/
function handleTernCompletions(response) {
@@ -421,7 +551,8 @@ define(function (require, exports, module) {
* Handle the response from the tern web worker when
* it responds to the get guesses message.
*
* @param {{file:string, type: string, offset: number, properties: Array.<string>}} response -
* @param {{file: string, type: string, offset: {line: number, ch: number},
* properties: Array.<string>}} response -
* the response from the worker contains the guesses for a
* property lookup.
*/
@@ -446,7 +577,7 @@ define(function (require, exports, module) {
var path = response.path,
type = response.type,
$deferredHints = getPendingRequest(path, 0, type);
$deferredHints = getPendingRequest(path, OFFSET_ZERO, type);
if ($deferredHints) {
$deferredHints.resolve();
@@ -518,16 +649,16 @@ define(function (require, exports, module) {
*/
function updateTernFile(document) {
var path = document.file.fullPath;
_postMessageByPass({
type : MessageIds.TERN_UPDATE_FILE_MSG,
path : path,
text : document.getText()
});
return addPendingRequest(path, 0, MessageIds.TERN_UPDATE_FILE_MSG);
return addPendingRequest(path, OFFSET_ZERO, MessageIds.TERN_UPDATE_FILE_MSG);
}
/**
* Handle a request from the worker for text of a file
*
@@ -613,36 +744,34 @@ define(function (require, exports, module) {
* Prime the pump for a fast first lookup.
*
* @param {string} path - full path of file
* @param {string} text - text of file
* @return {jQuery.Promise} - the promise for the request
*/
function primePump(path, text) {
function primePump(path) {
_postMessageByPass({
type : MessageIds.TERN_PRIME_PUMP_MSG,
path : path,
text : text
path : path
});
return addPendingRequest(path, 0, MessageIds.TERN_PRIME_PUMP_MSG);
return addPendingRequest(path, OFFSET_ZERO, MessageIds.TERN_PRIME_PUMP_MSG);
}
/**
* Handle the response from the tern web worker when
* it responds to the prime pump message.
*
* @param {{path:string, type: string}} response - the response from the worker
* @param {{path: string, type: string}} response - the response from the worker
*/
function handlePrimePumpCompletion(response) {
var path = response.path,
type = response.type,
$deferredHints = getPendingRequest(path, 0, type);
$deferredHints = getPendingRequest(path, OFFSET_ZERO, type);
if ($deferredHints) {
$deferredHints.resolve();
}
}
/**
* Add new files to tern, keeping any previous files.
* The tern server must be initialized before making
@@ -839,7 +968,7 @@ define(function (require, exports, module) {
* @param {Document} previousDocument - the document the editor has changed from
* @param {boolean} shouldPrimePump - true if the pump should be primed.
*/
function doEditorChange(session, document, previousDocument, shouldPrimePump, isReset) {
function doEditorChange(session, document, previousDocument, shouldPrimePump) {
var path = document.file.fullPath,
split = HintUtils.splitPath(path),
dir = split.dir,
@@ -861,7 +990,7 @@ define(function (require, exports, module) {
if (isDocumentDirty && previousDocument) {
var updateFilePromise = updateTernFile(previousDocument);
updateFilePromise.done(function () {
primePump(path, document.getText());
primePump(path);
addFilesDeferred.resolveWith(null, [_ternWorker]);
});
} else {
@@ -881,7 +1010,7 @@ define(function (require, exports, module) {
initTernServer(dir, files);
if (shouldPrimePump) {
var hintsPromise = primePump(path, document.getText());
var hintsPromise = primePump(path);
hintsPromise.done(function () {
if (!usingModules()) {
// Read the subdirectories of the new file's directory.
@@ -896,7 +1025,7 @@ define(function (require, exports, module) {
addAllFilesAndSubdirectories(projectRoot, function () {
// prime the pump again but this time don't wait
// for completion.
primePump(path, document.getText());
primePump(path);
addFilesDeferred.resolveWith(null, [_ternWorker]);
});
@@ -916,7 +1045,7 @@ define(function (require, exports, module) {
addFilesDeferred.resolveWith(null);
});
}
/**
* Called each time a new editor becomes active.
*
@@ -934,7 +1063,7 @@ define(function (require, exports, module) {
});
}
}
/**
* Do some cleanup when a project is closed.
*
@@ -978,6 +1107,7 @@ define(function (require, exports, module) {
}
var reseting = false;
/**
* reset the tern worker thread, if necessary.
*
@@ -1006,6 +1136,7 @@ define(function (require, exports, module) {
}
}
}
/**
* Request hints from Tern.
*
@@ -1021,25 +1152,21 @@ define(function (require, exports, module) {
* hints have completed.
*/
function requestHints(session, document) {
var path = document.file.fullPath,
split = HintUtils.splitPath(path),
dir = split.dir,
file = split.file;
var $deferredHints = $.Deferred(),
hintPromise,
fnTypePromise,
text = session.getJavascriptText(),
offset = session.getOffset();
sessionType = session.getType(),
fileInfo = getFileInfo(session),
offset = getOffset(session, fileInfo,
sessionType.showFunctionType ? sessionType.functionCallPos : null);
maybeReset(session, document);
var sessionType = session.getType();
hintPromise = getTernHints(dir, path, offset, text, sessionType.property);
hintPromise = getTernHints(fileInfo, offset, sessionType.property);
if (sessionType.showFunctionType) {
// Show function sig
fnTypePromise = getTernFunctionType(dir, path, sessionType.functionCallPos, offset, text);
fnTypePromise = getTernFunctionType(fileInfo, offset);
} else {
var $fnTypeDeferred = $.Deferred();
fnTypePromise = $fnTypeDeferred.promise();
@@ -1062,15 +1189,43 @@ define(function (require, exports, module) {
);
return {promise: $deferredHints.promise()};
}
/**
* Track the update area of the current document so we can tell if we can send
* partial updates to tern or not.
*
* @param {{from: {line:number, ch: number}, to: {line:number, ch: number},
* text: Array<string>}} changeList - the document changes (since last change or cumlative?)
*/
function trackChange(changeList) {
var changed = documentChanges;
if (changed === null) {
documentChanges = changed = {from: changeList.from.line, to: changeList.from.line};
}
var end = changeList.from.line + (changeList.text.length - 1);
if (changeList.from.line < changed.to) {
changed.to = changed.to - (changeList.to.line - end);
}
if (end >= changed.to) {
changed.to = end + 1;
}
if (changed.from > changeList.from.line) {
changed.from = changeList.from.line;
}
}
/*
* Called each time the file associated with the active editor changes.
* Marks the file as being dirty.
*
* @param {Document} document - the document that has changed
* @param {from: {line:number, ch: number}, to: {line:number, ch: number}}
*/
function handleFileChange(document) {
function handleFileChange(changeList) {
isDocumentDirty = true;
trackChange(changeList);
}
/**
@@ -54,7 +54,8 @@ define(function (require, exports, module) {
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
matcher = null, // string matcher for hints
ignoreChange; // can ignore next "change" event if true;
/**
* Get the value of current session.
@@ -377,6 +378,8 @@ define(function (require, exports, module) {
// Compute fresh hints if none exist, or if the session
// type has changed since the last hint computation
if (this.needNewHints(session)) {
ScopeManager.handleFileChange({from: cursor, to: cursor, text: [key]});
ignoreChange = true;
var scopeResponse = ScopeManager.requestHints(session, session.editor.document);
if (scopeResponse.hasOwnProperty("promise")) {
@@ -530,8 +533,11 @@ define(function (require, exports, module) {
if (editor && HintUtils.isSupportedLanguage(LanguageManager.getLanguageForPath(editor.document.file.fullPath).getId())) {
initializeSession(editor, previousEditor, true);
$(editor)
.on(HintUtils.eventName("change"), function () {
ScopeManager.handleFileChange(editor.document);
.on(HintUtils.eventName("change"), function (event, editor, changeList) {
if (!ignoreChange) {
ScopeManager.handleFileChange(changeList);
}
ignoreChange = false;
});
} else {
session = null;
@@ -567,7 +573,7 @@ define(function (require, exports, module) {
* Handle JumptoDefiniton menu/keyboard command.
*/
function handleJumpToDefinition() {
var offset = session.getOffset(),
var offset = session.getCursor(),
response = ScopeManager.requestJumptoDef(session, session.editor.document, offset);
if (response.hasOwnProperty("promise")) {
@@ -597,7 +603,7 @@ define(function (require, exports, module) {
* Helper for QuickEdit jump-to-definition request.
*/
function quickEditHelper() {
var offset = session.getOffset(),
var offset = session.getCursor(),
response = ScopeManager.requestJumptoDef(session, session.editor.document, offset);
return response;
@@ -77,10 +77,9 @@ importScripts("thirdparty/requirejs/require.js");
*
* @param {Object} env - an Object with the environment, as read in from
* the json files in thirdparty/tern/defs
* @param {string} dir - the current directory
* @param {Array.<string>} files - a list of filenames tern should be aware of
*/
function initTernServer(env, dir, files) {
function initTernServer(env, files) {
var ternOptions = {
defs: env,
async: true,
@@ -94,33 +93,70 @@ importScripts("thirdparty/requirejs/require.js");
});
}
/**
* Build an object that can be used as a request to tern
* @param {string} dir - the current directory
* @param {string} file - the filename the request is in
* @param {string} query - the type of request being made
* @param {number} offset - the offset in the file the request is at
* @param {string} text - the text of the file
* Create a "full" update object.
*
* @param {string} path - full path of the file.
* @param {string} text - full text of the file.
* @return {{type: string, name: string, offsetLines: number, text: string}} -
* "full" update.
*/
function buildRequest(dir, file, query, offset, text) {
function createFullUpdate(path, text) {
return {type: MessageIds.TERN_FILE_INFO_TYPE_FULL,
name: path,
offsetLines: 0,
text: text};
}
/**
* Create a "empty" update object.
*
* @param {string} path - full path of the file.
* @return {{type: string, name: string, offsetLines: number, text: string}} -
* "empty" update.
*/
function createEmptyUpdate(path) {
return {type: MessageIds.TERN_FILE_INFO_TYPE_EMPTY,
name: path,
offsetLines: 0,
text: ""};
}
/**
* Build an object that can be used as a request to tern.
*
* @param {{type: string, name: string, offsetLines: number, text: string}} fileInfo
* - type of update, name of file, and the text of the update.
* For "full" updates, the whole text of the file is present. For "part" updates,
* the changed portion of the text. For "empty" updates, the file has not been modified
* and the text is empty.
* @param {string} query - the type of request being made
* @param {{line: number, ch: number}} offset -
*/
function buildRequest(fileInfo, query, offset) {
query = {type: query};
query.start = offset;
query.end = offset;
query.file = file;
query.file = (fileInfo.type === MessageIds.TERN_FILE_INFO_TYPE_PART) ? "#0" : fileInfo.name;
query.filter = false;
query.sort = false;
query.depths = true;
query.guess = true;
query.origins = true;
query.expandWordForward = false;
query.lineCharPositions = true;
var request = {query: query, files: [], offset: offset};
request.files.push({type: "full", name: file, text: text});
if (fileInfo.type !== MessageIds.TERN_FILE_INFO_TYPE_EMPTY) {
request.files.push(fileInfo);
}
return request;
}
/**
* Send a log message back from the worker to the main thread
*
@@ -132,26 +168,27 @@ importScripts("thirdparty/requirejs/require.js");
/**
* 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
* @param {{type: string, name: string, offsetLines: number, text: string}} fileInfo
* - type of update, name of file, and the text of the update.
* For "full" updates, the whole text of the file is present. For "part" updates,
* the changed portion of the text. For "empty" updates, the file has not been modified
* and the text is empty.
* @param {{line: number, ch: number}} offset - the offset into the
* file for cursor
*/
function getJumptoDef(dir, file, offset, text) {
var request = buildRequest(dir, file, "definition", offset, text);
request.query.lineCharPositions = true;
function getJumptoDef(fileInfo, offset) {
var request = buildRequest(fileInfo, "definition", offset);
// request.query.typeOnly = true; // FIXME: tern doesn't work exactly right yet.
ternServer.request(request, function (error, data) {
if (error) {
_log("Error returned from Tern 'definition' request: " + error);
self.postMessage({type: MessageIds.TERN_JUMPTODEF_MSG});
self.postMessage({type: MessageIds.TERN_JUMPTODEF_MSG, file: fileInfo.name, offset: offset});
return;
}
// Post a message back to the main thread with the definition
self.postMessage({type: MessageIds.TERN_JUMPTODEF_MSG,
file: file,
file: fileInfo.name,
resultFile: data.file,
offset: offset,
start: data.start,
@@ -163,15 +200,18 @@ importScripts("thirdparty/requirejs/require.js");
/**
* Get all the known properties for guessing.
*
* @param {string} dir - the directory
* @param {string} file - the file name
* @param {number} offset - the offset into the file where we want completions for
* @param {string} text - the text of the file
* @param {{type: string, name: string, offsetLines: number, text: string}} fileInfo
* - type of update, name of file, and the text of the update.
* For "full" updates, the whole text of the file is present. For "part" updates,
* the changed portion of the text. For "empty" updates, the file has not been modified
* and the text is empty.
* @param {{line: number, ch: number}} offset -
* the offset into the file where we want completions for
* @param {string} type - the type of the message to reply with.
*/
function getTernProperties(dir, file, offset, text, type) {
function getTernProperties(fileInfo, offset, type) {
var request = buildRequest(dir, file, "properties", undefined, text),
var request = buildRequest(fileInfo, "properties", offset),
i;
//_log("tern properties: request " + request.type + dir + " " + file);
ternServer.request(request, function (error, data) {
@@ -188,8 +228,7 @@ importScripts("thirdparty/requirejs/require.js");
// Post a message back to the main thread with the completions
self.postMessage({type: type,
dir: dir,
file: file,
file: fileInfo.name,
offset: offset,
properties: properties
});
@@ -198,16 +237,20 @@ importScripts("thirdparty/requirejs/require.js");
/**
* Get the completions for the given offset
* @param {string} dir - the directory
* @param {string} file - the file name
* @param {number} offset - the offset into the file where we want completions for
* @param {string} text - the text of the file
*
* @param {{type: string, name: string, offsetLines: number, text: string}} fileInfo
* - type of update, name of file, and the text of the update.
* For "full" updates, the whole text of the file is present. For "part" updates,
* the changed portion of the text. For "empty" updates, the file has not been modified
* and the text is empty.
* @param {{line: number, ch: number}} offset -
* the offset into the file where we want completions for
* @param {boolean} isProperty - true if getting a property hint,
* otherwise getting an identifier hint.
*/
function getTernHints(dir, file, offset, text, isProperty) {
function getTernHints(fileInfo, offset, isProperty) {
var request = buildRequest(dir, file, "completions", offset, text),
var request = buildRequest(fileInfo, "completions", offset),
i;
//_log("request " + dir + " " + file + " " + offset /*+ " " + text */);
@@ -227,28 +270,31 @@ importScripts("thirdparty/requirejs/require.js");
if (completions.length > 0 || !isProperty) {
// Post a message back to the main thread with the completions
self.postMessage({type: MessageIds.TERN_COMPLETIONS_MSG,
dir: dir,
file: file,
file: fileInfo.name,
offset: offset,
completions: completions
});
} else {
// if there are no completions, then get all the properties
getTernProperties(dir, file, offset, text, MessageIds.TERN_COMPLETIONS_MSG);
getTernProperties(fileInfo, offset, MessageIds.TERN_COMPLETIONS_MSG);
}
});
}
/**
* Get the function type for the given offset
* @param {string} dir - the directory
* @param {string} file - the file name
* @param {number} offset - the offset into the file where we want completions for
* @param {string} text - the text of the file
*
* @param {{type: string, name: string, offsetLines: number, text: string}} fileInfo
* - type of update, name of file, and the text of the update.
* For "full" updates, the whole text of the file is present. For "part" updates,
* the changed portion of the text. For "empty" updates, the file has not been modified
* and the text is empty.
* @param {{line: number, ch: number}} offset -
* the offset into the file where we want completions for
*/
function handleFunctionType(dir, file, pos, offset, text) {
function handleFunctionType(fileInfo, offset) {
var request = buildRequest(dir, file, "type", pos, text);
var request = buildRequest(fileInfo, "type", offset);
request.preferFunction = true;
@@ -263,8 +309,7 @@ importScripts("thirdparty/requirejs/require.js");
// Post a message back to the main thread with the completions
self.postMessage({type: MessageIds.TERN_CALLED_FUNC_TYPE_MSG,
dir: dir,
file: file,
file: fileInfo.name,
offset: offset,
fnType: fnType
});
@@ -305,10 +350,10 @@ importScripts("thirdparty/requirejs/require.js");
* Make a completions request to tern to force tern to resolve files
* and create a fast first lookup for the user.
* @param {string} path - the path of the file
* @param {string} text - the text of the file
*/
function handlePrimePump(path, text) {
var request = buildRequest("", path, "completions", 0, text);
function handlePrimePump(path) {
var fileInfo = createEmptyUpdate(path);
var request = buildRequest(fileInfo, "completions", {line: 0, ch: 0});
ternServer.request(request, function (error, data) {
// Post a message back to the main thread
@@ -319,49 +364,35 @@ importScripts("thirdparty/requirejs/require.js");
}
self.addEventListener("message", function (e) {
var dir, file, text, offset,
var file, text, offset,
request = e.data,
type = request.type;
if (type === MessageIds.TERN_INIT_MSG) {
dir = request.dir;
var env = request.env,
files = request.files;
initTernServer(env, dir, files);
initTernServer(env, files);
} else if (type === MessageIds.TERN_COMPLETIONS_MSG) {
dir = request.dir;
file = request.file;
text = request.text;
offset = request.offset;
getTernHints(dir, file, offset, text, request.isProperty);
getTernHints(request.fileInfo, offset, request.isProperty);
} else if (type === MessageIds.TERN_GET_FILE_MSG) {
file = request.file;
text = request.text;
handleGetFile(file, text);
} else if (type === MessageIds.TERN_CALLED_FUNC_TYPE_MSG) {
dir = request.dir;
file = request.file;
text = request.text;
offset = request.offset;
var pos = request.pos;
handleFunctionType(dir, file, pos, offset, text);
handleFunctionType(request.fileInfo, offset);
} else if (type === MessageIds.TERN_JUMPTODEF_MSG) {
file = request.file;
dir = request.dir;
text = request.text;
offset = request.offset;
getJumptoDef(dir, file, offset, text);
getJumptoDef(request.fileInfo, offset);
} else if (type === MessageIds.TERN_ADD_FILES_MSG) {
handleAddFiles(request.files);
} else if (type === MessageIds.TERN_PRIME_PUMP_MSG) {
handlePrimePump(request.path, request.text);
handlePrimePump(request.path);
} else if (type === MessageIds.TERN_GET_GUESSES_MSG) {
dir = request.dir;
file = request.file;
text = request.text;
offset = request.offset;
getTernProperties(dir, file, offset, text, MessageIds.TERN_GET_GUESSES_MSG);
getTernProperties(request.fileInfo, offset, MessageIds.TERN_GET_GUESSES_MSG);
} else if (type === MessageIds.TERN_UPDATE_FILE_MSG) {
handleUpdateFile(request.path, request.text);
} else {
@@ -154,3 +154,99 @@ function testNonAscii() {
hope["frenchçProp"] = "";
}
/* Add large comment to make this test over 250 lines which will trigger
* partial updates to be used.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* end of large comment
*/
@@ -808,7 +808,7 @@ define(function (require, exports, module) {
hintsPresentExact(hintObj, ["getAmountDue", "getName", "name", "setAmountDue"]);
});
});
it("should show argument from from .prototype.Method", function () {
var start = { line: 80, ch: 0 },
testPos = { line: 80, ch: 24 };
@@ -821,18 +821,6 @@ define(function (require, exports, module) {
});
});
it("should show guessed argument type from current passing parameter", function () {
var start = { line: 80, ch: 0 },
testPos = { line: 80, ch: 24 };
testDoc.replaceRange("myCustomer.setAmountDue(10)", start);
testEditor.setCursorPos(testPos);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentOrdered(hintObj, ["setAmountDue(amountDue: number)"]);
});
});
it("should show inner function type", function () {
var testPos = { line: 96, ch: 23 };
@@ -1083,6 +1071,19 @@ define(function (require, exports, module) {
// check we have a properties that start with "shift"
hintsPresentOrdered(hintObj, ["frenchçProp"]);
});
it("should show guessed argument type from current passing parameter", function () {
var start = { line: 80, ch: 0 },
testPos = { line: 80, ch: 24 };
console.log("should show guessed...");
testDoc.replaceRange("myCustomer.setAmountDue(10)", start);
testEditor.setCursorPos(testPos);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentOrdered(hintObj, ["setAmountDue(amountDue: number)"]);
});
});
});
describe("JavaScript Code Hinting in a HTML file", function () {