mirror of
https://github.com/RustyRaptor/brackets.git
synced 2026-12-04 00:15:22 -07:00
Merge pull request #5 from dloverin/cleanup-esprima
Remove espirma and related scope info code
This commit is contained in:
commit
08f356c649
7 files changed
+14
-4635
No files matched your search
@@ -28,7 +28,6 @@ define(function (require, exports, module) {
|
||||
"use strict";
|
||||
|
||||
var MODE_NAME = "javascript",
|
||||
SCOPE_MSG_TYPE = "outerScope",
|
||||
SINGLE_QUOTE = "\'",
|
||||
DOUBLE_QUOTE = "\"",
|
||||
TERN_INIT_MSG = "ternInit",
|
||||
@@ -109,52 +108,9 @@ define(function (require, exports, module) {
|
||||
return name + "." + EVENT_TAG;
|
||||
}
|
||||
|
||||
/*
|
||||
* Annotate list of identifiers with their scope level.
|
||||
*
|
||||
* @param {Array.<Object>} identifiers - list of identifier tokens to be
|
||||
* annotated
|
||||
* @param {Scope} scope - scope object used to determine the scope level of
|
||||
* each identifier token in the previous list.
|
||||
* @return {Array.<Object>} - the input array; to each object in the array a
|
||||
* new level {number} property has been added to indicate its scope
|
||||
* level.
|
||||
*/
|
||||
function annotateWithScope(identifiers, scope) {
|
||||
return identifiers.map(function (t) {
|
||||
var level = scope.contains(t.value);
|
||||
|
||||
if (level >= 0) {
|
||||
t.level = level;
|
||||
} else {
|
||||
t.level = -1;
|
||||
}
|
||||
return t;
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Annotate a list of properties with their association level
|
||||
*
|
||||
* @param {Array.<Object>} properties - list of property tokens
|
||||
* @param {Association} association - an object that maps property
|
||||
* names to the number of times it occurs in a particular context
|
||||
* @return {Array.<Object>} - the input array; to each object in the array a
|
||||
* new level {number} property has been added to indicate the number
|
||||
* of times the property has occurred in the association context.
|
||||
*/
|
||||
function annotateWithAssociation(properties, association) {
|
||||
return properties.map(function (t) {
|
||||
if (association[t.value] > 0) {
|
||||
t.level = 0;
|
||||
}
|
||||
return t;
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Annotate a list of tokens as being global variables
|
||||
*
|
||||
*
|
||||
* @param {Array.<Object>} globals - list of identifier tokens
|
||||
* @return {Array.<Object>} - the input array; to each object in the array a
|
||||
* new global {boolean} property has been added to indicate that it is
|
||||
@@ -169,15 +125,15 @@ define(function (require, exports, module) {
|
||||
|
||||
/*
|
||||
* Annotate a list of tokens as literals of a particular kind;
|
||||
* if string literals, annotate with an appropriate delimiter.
|
||||
*
|
||||
* if string literals, annotate with an appropriate delimiter.
|
||||
*
|
||||
* @param {Array.<Object>} literals - list of hint tokens
|
||||
* @param {string} kind - the kind of literals in the list (e.g., "string")
|
||||
* @return {Array.<Object>} - the input array; to each object in the array a
|
||||
* new literal {boolean} property has been added to indicate that it
|
||||
* is a literal hint, and also a new kind {string} property to indicate
|
||||
* the literal kind. For string literals, a delimiter property is also
|
||||
* added to indicate what the default delimiter should be (viz. a
|
||||
* added to indicate what the default delimiter should be (viz. a
|
||||
* single or double quotation mark).
|
||||
*/
|
||||
function annotateLiterals(literals, kind) {
|
||||
@@ -195,7 +151,7 @@ define(function (require, exports, module) {
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* Annotate a list of tokens as keywords
|
||||
*
|
||||
* @param {Array.<Object>} keyword - list of keyword tokens
|
||||
@@ -210,27 +166,6 @@ define(function (require, exports, module) {
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Annotate a list of tokens with a path name
|
||||
*
|
||||
* @param {Array.<Object>} tokens - list of hint tokens
|
||||
* @param {string} dir - directory with which to annotate the hints
|
||||
* @param {string} file - file name with which to annotate the hints
|
||||
* @return {Array.<Object>} - the input array; to each object in the array a
|
||||
* new path {string} property has been added, equal to dir + file.
|
||||
*/
|
||||
function annotateWithPath(tokens, dir, file) {
|
||||
var path = dir + file;
|
||||
|
||||
return tokens.map(function (t) {
|
||||
t.path = path;
|
||||
return t;
|
||||
});
|
||||
}
|
||||
|
||||
// TODO 1: The definitions below should be defined in a separate JSON file.
|
||||
// TODO 2: Add properties and associations for the builtin globals.
|
||||
|
||||
var KEYWORD_NAMES = [
|
||||
"break", "case", "catch", "continue", "debugger", "default", "delete",
|
||||
"do", "else", "finally", "for", "function", "if", "in", "instanceof",
|
||||
@@ -250,141 +185,16 @@ define(function (require, exports, module) {
|
||||
}),
|
||||
LITERALS = annotateLiterals(LITERAL_TOKENS);
|
||||
|
||||
var JSL_GLOBAL_NAMES = [
|
||||
"clearInterval", "clearTimeout", "document", "event", "frames",
|
||||
"history", "Image", "location", "name", "navigator", "Option",
|
||||
"parent", "screen", "setInterval", "setTimeout", "window",
|
||||
"XMLHttpRequest", "alert", "confirm", "console", "Debug", "opera",
|
||||
"prompt", "WSH", "Buffer", "exports", "global", "module", "process",
|
||||
"querystring", "require", "__filename", "__dirname", "defineClass",
|
||||
"deserialize", "gc", "help", "load", "loadClass", "print", "quit",
|
||||
"readFile", "readUrl", "runCommand", "seal", "serialize", "spawn",
|
||||
"sync", "toint32", "version", "ActiveXObject", "CScript", "Enumerator",
|
||||
"System", "VBArray", "WScript"
|
||||
],
|
||||
JSL_GLOBAL_TOKENS = annotateGlobals(JSL_GLOBAL_NAMES.map(function (t) {
|
||||
return makeToken(t, []);
|
||||
})),
|
||||
JSL_GLOBALS = JSL_GLOBAL_TOKENS.reduce(function (prev, curr) {
|
||||
prev[curr.value] = curr;
|
||||
return prev;
|
||||
}, {}); // builds an object from the array of tokens.
|
||||
|
||||
// Predefined sets of globals as defined by JSLint
|
||||
var JSL_GLOBALS_BROWSER = [
|
||||
JSL_GLOBALS.clearInterval,
|
||||
JSL_GLOBALS.clearTimeout,
|
||||
JSL_GLOBALS.document,
|
||||
JSL_GLOBALS.event,
|
||||
JSL_GLOBALS.frames,
|
||||
JSL_GLOBALS.history,
|
||||
JSL_GLOBALS.Image,
|
||||
JSL_GLOBALS.location,
|
||||
JSL_GLOBALS.name,
|
||||
JSL_GLOBALS.navigator,
|
||||
JSL_GLOBALS.Option,
|
||||
JSL_GLOBALS.parent,
|
||||
JSL_GLOBALS.screen,
|
||||
JSL_GLOBALS.setInterval,
|
||||
JSL_GLOBALS.setTimeout,
|
||||
JSL_GLOBALS.window,
|
||||
JSL_GLOBALS.XMLHttpRequest
|
||||
],
|
||||
JSL_GLOBALS_DEVEL = [
|
||||
JSL_GLOBALS.alert,
|
||||
JSL_GLOBALS.confirm,
|
||||
JSL_GLOBALS.console,
|
||||
JSL_GLOBALS.Debug,
|
||||
JSL_GLOBALS.opera,
|
||||
JSL_GLOBALS.prompt,
|
||||
JSL_GLOBALS.WSH
|
||||
],
|
||||
JSL_GLOBALS_NODE = [
|
||||
JSL_GLOBALS.Buffer,
|
||||
JSL_GLOBALS.clearInterval,
|
||||
JSL_GLOBALS.clearTimeout,
|
||||
JSL_GLOBALS.console,
|
||||
JSL_GLOBALS.exports,
|
||||
JSL_GLOBALS.global,
|
||||
JSL_GLOBALS.module,
|
||||
JSL_GLOBALS.process,
|
||||
JSL_GLOBALS.querystring,
|
||||
JSL_GLOBALS.require,
|
||||
JSL_GLOBALS.setInterval,
|
||||
JSL_GLOBALS.setTimeout,
|
||||
JSL_GLOBALS.__filename,
|
||||
JSL_GLOBALS.__dirname
|
||||
],
|
||||
JSL_GLOBALS_RHINO = [
|
||||
JSL_GLOBALS.defineClass,
|
||||
JSL_GLOBALS.deserialize,
|
||||
JSL_GLOBALS.gc,
|
||||
JSL_GLOBALS.help,
|
||||
JSL_GLOBALS.load,
|
||||
JSL_GLOBALS.loadClass,
|
||||
JSL_GLOBALS.print,
|
||||
JSL_GLOBALS.quit,
|
||||
JSL_GLOBALS.readFile,
|
||||
JSL_GLOBALS.readUrl,
|
||||
JSL_GLOBALS.runCommand,
|
||||
JSL_GLOBALS.seal,
|
||||
JSL_GLOBALS.serialize,
|
||||
JSL_GLOBALS.spawn,
|
||||
JSL_GLOBALS.sync,
|
||||
JSL_GLOBALS.toint32,
|
||||
JSL_GLOBALS.version
|
||||
],
|
||||
JSL_GLOBALS_WINDOWS = [
|
||||
JSL_GLOBALS.ActiveXObject,
|
||||
JSL_GLOBALS.CScript,
|
||||
JSL_GLOBALS.Debug,
|
||||
JSL_GLOBALS.Enumerator,
|
||||
JSL_GLOBALS.System,
|
||||
JSL_GLOBALS.VBArray,
|
||||
JSL_GLOBALS.WScript,
|
||||
JSL_GLOBALS.WSH
|
||||
];
|
||||
|
||||
var JSL_GLOBAL_DEFS = {
|
||||
browser : JSL_GLOBALS_BROWSER,
|
||||
devel : JSL_GLOBALS_DEVEL,
|
||||
node : JSL_GLOBALS_NODE,
|
||||
rhino : JSL_GLOBALS_RHINO,
|
||||
windows : JSL_GLOBALS_WINDOWS
|
||||
};
|
||||
|
||||
var BUILTIN_GLOBAL_NAMES = [
|
||||
"Array", "Boolean", "Date", "Function", "Iterator", "Number", "Object",
|
||||
"RegExp", "String", "ArrayBuffer", "DataView", "Float32Array",
|
||||
"Float64Array", "Int16Array", "Int32Array", "Int8Array", "Uint16Array",
|
||||
"Uint32Array", "Uint8Array", "Uint8ClampedArray", "Error", "EvalError",
|
||||
"InternalError", "RangeError", "ReferenceError", "StopIteration",
|
||||
"SyntaxError", "TypeError", "URIError", "decodeURI",
|
||||
"decodeURIComponent", "encodeURI", "encodeURIComponent", "eval",
|
||||
"isFinite", "isNaN", "parseFloat", "parseInt", "uneval", "Infinity",
|
||||
"JSON", "Math", "NaN"
|
||||
],
|
||||
BUILTIN_GLOBAL_TOKENS = BUILTIN_GLOBAL_NAMES.map(function (t) {
|
||||
return makeToken(t, []);
|
||||
}),
|
||||
BUILTIN_GLOBALS = annotateGlobals(BUILTIN_GLOBAL_TOKENS);
|
||||
|
||||
exports.makeToken = makeToken;
|
||||
exports.hintable = hintable;
|
||||
exports.maybeIdentifier = maybeIdentifier;
|
||||
exports.splitPath = splitPath;
|
||||
exports.eventName = eventName;
|
||||
exports.annotateWithPath = annotateWithPath;
|
||||
exports.annotateLiterals = annotateLiterals;
|
||||
exports.annotateGlobals = annotateGlobals;
|
||||
exports.annotateWithScope = annotateWithScope;
|
||||
exports.annotateWithAssociation = annotateWithAssociation;
|
||||
exports.JSL_GLOBAL_DEFS = JSL_GLOBAL_DEFS;
|
||||
exports.BUILTIN_GLOBALS = BUILTIN_GLOBALS;
|
||||
exports.KEYWORDS = KEYWORDS;
|
||||
exports.LITERALS = LITERALS;
|
||||
exports.MODE_NAME = MODE_NAME;
|
||||
exports.SCOPE_MSG_TYPE = SCOPE_MSG_TYPE;
|
||||
exports.SINGLE_QUOTE = SINGLE_QUOTE;
|
||||
exports.DOUBLE_QUOTE = DOUBLE_QUOTE;
|
||||
exports.TERN_COMPLETIONS_MSG = TERN_COMPLETIONS_MSG;
|
||||
|
||||
@@ -105,9 +105,6 @@ define(function (require, exports, module) {
|
||||
if (file !== undefined) {
|
||||
if (!fileState[dir].hasOwnProperty(file)) {
|
||||
fileState[dir][file] = {
|
||||
// global scope object for this file
|
||||
scope : null,
|
||||
|
||||
// has the file changed since the scope was updated?
|
||||
dirtyFile : true,
|
||||
|
||||
@@ -115,22 +112,7 @@ define(function (require, exports, module) {
|
||||
dirtyScope : true,
|
||||
|
||||
// is the parser worker active for this file?
|
||||
active : false,
|
||||
|
||||
// all variable and parameter names defined in this file
|
||||
identifiers : null,
|
||||
|
||||
// all property names found in this file
|
||||
properties : null,
|
||||
|
||||
// all globals defined in this file
|
||||
globals : null,
|
||||
|
||||
// all string literals found in this file
|
||||
literals : null,
|
||||
|
||||
// all context-property associations found in this file
|
||||
associations : null
|
||||
active : false
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -158,118 +140,7 @@ define(function (require, exports, module) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a new outer scope object from the parser worker, if necessary
|
||||
*
|
||||
* @param {string} dir - the directory name for which the outer scope is to
|
||||
* be refreshed
|
||||
* @param {string} file - the file name for which the outer scope is to be
|
||||
* refreshed
|
||||
* @param {string} text - the text of the file for which the outer scope is
|
||||
* to be refreshed
|
||||
*/
|
||||
function refreshOuterScope(dir, file, text) {
|
||||
|
||||
if (text.length > MAX_TEXT_LENGTH) {
|
||||
return;
|
||||
}
|
||||
|
||||
var state = getFileState(dir, file);
|
||||
|
||||
// if there is not yet an outer scope or if the file has changed then
|
||||
// we might need to update the outer scope
|
||||
if (state.scope === null || state.dirtyFile) {
|
||||
if (!state.active) {
|
||||
var path = dir + file,
|
||||
entry = new NativeFileSystem.FileEntry(path);
|
||||
|
||||
// the outer scope worker is about to be active
|
||||
state.active = true;
|
||||
|
||||
// the file will be clean since the last outer scope request
|
||||
state.dirtyFile = false;
|
||||
}
|
||||
}
|
||||
state.text = text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get inner scope information for a given file and offset if a suitable
|
||||
* global scope object is availble; otherwise, return a promise for such
|
||||
* information, resolved when a suitable global scope object becomes
|
||||
* available.
|
||||
*
|
||||
* @param {string} dir - the directory name for which the inner scope is to
|
||||
* be refreshed
|
||||
* @param {string} file - the file name for which the inner scope is to be
|
||||
* refreshed
|
||||
* @param {number} offset - offset into the text at which the inner scope
|
||||
* is to be refreshed
|
||||
* @return {Object + jQuery.Promise} - inner scope information, or a promise
|
||||
* for such information, including the local scope object and lists of
|
||||
* identifiers, properties, globals, literals and associations.
|
||||
*/
|
||||
function refreshInnerScope(dir, file, offset) {
|
||||
|
||||
var state = getFileState(dir, file);
|
||||
|
||||
// If there is no outer scope, the inner scope request is deferred.
|
||||
if (!state.scope) {
|
||||
if (pendingRequest && pendingRequest.deferred.state() === "pending") {
|
||||
pendingRequest.deferred.reject();
|
||||
}
|
||||
|
||||
pendingRequest = {
|
||||
dir : dir,
|
||||
file : file,
|
||||
offset : offset,
|
||||
deferred : $.Deferred()
|
||||
};
|
||||
|
||||
// Request the outer scope from the parser worker.
|
||||
DocumentManager.getDocumentForPath(dir + file).done(function (document) {
|
||||
refreshOuterScope(dir, file, document.getText());
|
||||
});
|
||||
return { promise: pendingRequest.deferred.promise() };
|
||||
} else {
|
||||
// The inner scope will be clean after this
|
||||
state.dirtyScope = false;
|
||||
|
||||
// Try to find an inner scope from the current outer scope
|
||||
var innerScope = state.scope.findChild(offset);
|
||||
|
||||
if (!innerScope) {
|
||||
// we may have failed to find a child scope because a
|
||||
// character was added to the end of the file, outside of
|
||||
// the (now out-of-date and currently-being-updated)
|
||||
// outer scope. Hence, if offset is greater than the range
|
||||
// of the outerScope, we manually set innerScope to the
|
||||
// outerScope
|
||||
innerScope = state.scope;
|
||||
}
|
||||
|
||||
// FIXME: This could be more efficient if instead of filtering
|
||||
// the entire list of identifiers we just used the identifiers
|
||||
// in the scope of innerScope, but that list doesn't have the
|
||||
// accumulated position information.
|
||||
var identifiersForScope = filterByScope(state.identifiers, innerScope),
|
||||
propertiesForFile = mergeProperties(dir),
|
||||
associationsForFile = mergeAssociations(dir);
|
||||
|
||||
return {
|
||||
scope : innerScope,
|
||||
identifiers : identifiersForScope,
|
||||
globals : state.globals,
|
||||
literals : state.literals,
|
||||
properties : propertiesForFile,
|
||||
associations: associationsForFile
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new inner scope and related info, if possible. If there is no
|
||||
* outer scope for the given file, a promise will be returned instead.
|
||||
* (See refreshInnerScope above.)
|
||||
* Request hints from Tern.
|
||||
*
|
||||
* Note that successive calls to getScope may return the same objects, so
|
||||
* clients that wish to modify those objects (e.g., by annotating them based
|
||||
@@ -280,11 +151,10 @@ define(function (require, exports, module) {
|
||||
* desired
|
||||
* @param {number} offset - the offset into the document at which scope
|
||||
* info is desired
|
||||
* @return {jQuery.Promise} - A promise that will resolve to the inner scope info.
|
||||
* The promise will not complete until the scope info is refreshed, and tern has
|
||||
* computed completions.
|
||||
* @return {jQuery.Promise} - The promise will not complete until the tern
|
||||
* hints have completed.
|
||||
*/
|
||||
function getScopeInfo(session, document, offset) {
|
||||
function requestHints(session, document, offset) {
|
||||
var path = document.file.fullPath,
|
||||
split = HintUtils.splitPath(path),
|
||||
dir = split.dir,
|
||||
@@ -407,7 +277,6 @@ define(function (require, exports, module) {
|
||||
|
||||
markFileDirty(dir, file);
|
||||
|
||||
|
||||
reader.readEntries(function (entries) {
|
||||
entries.slice(0, MAX_FILES_IN_DIR).forEach(function (entry) {
|
||||
if (entry.isFile) {
|
||||
@@ -419,10 +288,7 @@ define(function (require, exports, module) {
|
||||
if (file.indexOf(".") > 1) { // ignore /.dotfiles
|
||||
var mode = LanguageManager.getLanguageForPath(entry.fullPath).getMode();
|
||||
if (mode === HintUtils.MODE_NAME) {
|
||||
DocumentManager.getDocumentForPath(path).done(function (document) {
|
||||
refreshOuterScope(dir, file, document.getText());
|
||||
});
|
||||
files.push(file);
|
||||
files.push(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -430,13 +296,12 @@ define(function (require, exports, module) {
|
||||
initTernServer(dir, files);
|
||||
}, function (err) {
|
||||
console.log("Unable to refresh directory: " + err);
|
||||
refreshOuterScope(dir, file, document.getText());
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Called each time the file associated with the active edtor changes.
|
||||
* 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
|
||||
@@ -448,61 +313,6 @@ define(function (require, exports, module) {
|
||||
file = split.file;
|
||||
|
||||
markFileDirty(dir, file);
|
||||
refreshOuterScope(dir, file, document.getText());
|
||||
}
|
||||
|
||||
/*
|
||||
* Receive an outer scope object from the parser worker and resolves any
|
||||
* deferred inner scope requests.
|
||||
*
|
||||
* @param {Object} response - the parser response object, which includes
|
||||
* the global scope and lists of identifiers, properties, globals,
|
||||
* literals and associations.
|
||||
*/
|
||||
function handleOuterScope(response) {
|
||||
var dir = response.dir,
|
||||
file = response.file,
|
||||
path = dir + file,
|
||||
state = getFileState(dir, file),
|
||||
scopeInfo;
|
||||
|
||||
if (state.active) {
|
||||
state.active = false;
|
||||
if (response.success) {
|
||||
state.scope = Scope.rebuild(response.scope);
|
||||
|
||||
// The outer scope should cover the entire file
|
||||
state.scope.range.start = 0;
|
||||
state.scope.range.end = response.length;
|
||||
|
||||
state.globals = response.globals;
|
||||
state.identifiers = response.identifiers;
|
||||
state.properties = response.properties;
|
||||
state.literals = response.literals;
|
||||
state.associations = response.associations;
|
||||
|
||||
state.dirtyScope = true;
|
||||
|
||||
if (state.dirtyFile) {
|
||||
DocumentManager.getDocumentForPath(path).done(function (document) {
|
||||
refreshOuterScope(dir, file, document.getText());
|
||||
});
|
||||
}
|
||||
|
||||
if (pendingRequest !== null && pendingRequest.dir === dir &&
|
||||
pendingRequest.file === file) {
|
||||
if (pendingRequest.deferred.state() === "pending") {
|
||||
scopeInfo = refreshInnerScope(dir, file, pendingRequest.offset);
|
||||
if (scopeInfo && !scopeInfo.deferred) {
|
||||
pendingRequest.deferred.resolveWith(null, [scopeInfo]);
|
||||
pendingRequest = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log("Expired scope request: " + path);
|
||||
}
|
||||
}
|
||||
|
||||
ternWorker.addEventListener("message", function (e) {
|
||||
@@ -551,7 +361,7 @@ define(function (require, exports, module) {
|
||||
|
||||
exports.handleEditorChange = handleEditorChange;
|
||||
exports.handleFileChange = handleFileChange;
|
||||
exports.getScopeInfo = getScopeInfo;
|
||||
exports.requestHints = requestHints;
|
||||
exports.isScopeDirty = isScopeDirty;
|
||||
exports.getTernHints = getTernHints;
|
||||
});
|
||||
@@ -43,22 +43,6 @@ define(function (require, exports, module) {
|
||||
this.ternHints = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the scope information assocated with the current session
|
||||
*
|
||||
* @param {Object} scopeInfo - scope information, including the scope and
|
||||
* lists of identifiers, globals, literals and properties, and a set
|
||||
* of associations
|
||||
*/
|
||||
Session.prototype.setScopeInfo = function (scopeInfo) {
|
||||
this.scope = scopeInfo.scope;
|
||||
this.identifiers = scopeInfo.identifiers;
|
||||
this.globals = scopeInfo.globals;
|
||||
this.literals = scopeInfo.literals;
|
||||
this.properties = scopeInfo.properties;
|
||||
this.associations = scopeInfo.associations;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the name of the file associated with the current session
|
||||
*
|
||||
|
||||
@@ -336,7 +336,7 @@ define(function (require, exports, module) {
|
||||
query.length == 0 ||
|
||||
(cachedHints.length == 0 && query.length == 2)) { // FIXME: length of 2 is special because that is when tern starts guessing
|
||||
var offset = session.getOffset(),
|
||||
scopeResponse = ScopeManager.getScopeInfo(session, session.editor.document, offset),
|
||||
scopeResponse = ScopeManager.requestHints(session, session.editor.document, offset),
|
||||
self = this;
|
||||
|
||||
if (scopeResponse.hasOwnProperty("promise")) {
|
||||
|
||||
@@ -1,311 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
|
||||
/*global self, importScripts, setTimeout */
|
||||
|
||||
/**
|
||||
* Loads a file that contains an AMD module definition using the web worker
|
||||
* importScripts global function.
|
||||
*
|
||||
* @param {string} url - the URL of the module to load
|
||||
* @return {Object} - the module imported from the aforementioned URL
|
||||
*/
|
||||
function require(url) {
|
||||
"use strict";
|
||||
|
||||
var exports = {},
|
||||
oldDefine = self.define;
|
||||
|
||||
/*
|
||||
* The following function is called by AMD modules when loaded with
|
||||
* importScripts. CommonJS-style modules will only pass a wrapper function
|
||||
* as a single argument; proper AMD-style modules may additionally pass an
|
||||
* array of bindings, which are ignored. In the former case, the wrapper
|
||||
* function expects require, exports and module arguments; in the latter
|
||||
* case, the wrapper function expects only an exports arguments.
|
||||
*
|
||||
* @param {?Array.<string>} bindings - an optional list of resources
|
||||
* required by the module
|
||||
* @param {Function} wrapper - the function that defines the module
|
||||
*/
|
||||
self.define = function (bindings, wrapper) {
|
||||
var module = { exports: exports },
|
||||
require = null;
|
||||
|
||||
if (typeof bindings === "function") {
|
||||
wrapper = bindings;
|
||||
} else {
|
||||
require = module.exports;
|
||||
}
|
||||
|
||||
wrapper(require, module.exports, module);
|
||||
exports = module.exports;
|
||||
};
|
||||
self.define.amd = true;
|
||||
importScripts(url);
|
||||
self.define = oldDefine;
|
||||
return exports;
|
||||
}
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
var Scope = require("Scope.js"),
|
||||
HintUtils = require("HintUtils.js"),
|
||||
esprima = require("thirdparty/esprima/esprima.js");
|
||||
|
||||
// maximum number of times a file will be reparsed before failing
|
||||
var MAX_RETRIES = 10;
|
||||
|
||||
/**
|
||||
* Send a log message back from the worker to the main thread
|
||||
*
|
||||
* @param {string} msg - the log message
|
||||
*/
|
||||
function _log(msg) {
|
||||
self.postMessage({log: msg });
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk the scope to find all the objects of a given type, along with a
|
||||
* list of their positions in the file.
|
||||
*
|
||||
* @param {Function} walk - the function used to walk over an implicit scope
|
||||
* @param {string} keyProp - the property of the implicit scope to walk over
|
||||
* @return {Array.<Object>} - a list of hint tokens for the implicit scope,
|
||||
* including the positions at which they occur.
|
||||
*/
|
||||
function siftPositions(walk, keyProp) {
|
||||
var occurrences,
|
||||
results = [],
|
||||
key,
|
||||
token,
|
||||
comparator = function (a, b) {
|
||||
return a - b;
|
||||
};
|
||||
|
||||
occurrences = walk(function (acc, token) {
|
||||
if (Object.prototype.hasOwnProperty.call(acc, token[keyProp])) {
|
||||
acc[token[keyProp]].push(token.range[0]);
|
||||
} else {
|
||||
acc[token[keyProp]] = [token.range[0]];
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
for (key in occurrences) {
|
||||
if (Object.prototype.hasOwnProperty.call(occurrences, key)) {
|
||||
token = HintUtils.makeToken(key, occurrences[key].sort(comparator));
|
||||
results.push(token);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk the scope to compute all available association objects
|
||||
*
|
||||
* @param {Function} walk - the function used to walk over an implicit scope
|
||||
* @return {Object} - a set of association objects: each property is a
|
||||
* property name; each value is an association, which indicates the
|
||||
* number of times the property occurs w.r.t. to a particular lookup
|
||||
* context.
|
||||
*/
|
||||
function siftAssociations(walk) {
|
||||
return walk(function (acc, assoc) {
|
||||
var obj = assoc.object,
|
||||
prop = assoc.property;
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(acc, obj.name)) {
|
||||
if (Object.prototype.hasOwnProperty.call(acc[obj.name], prop.name)) {
|
||||
acc[obj.name][prop.name]++;
|
||||
} else {
|
||||
acc[obj.name][prop.name] = 1;
|
||||
}
|
||||
} else {
|
||||
acc[obj.name] = {};
|
||||
acc[obj.name][prop.name] = 1;
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse JSLint globals annotations from an array of JavaScript comments
|
||||
*
|
||||
* @param {Array.<string>} comments - list of JavaScript comments
|
||||
* @return {Array.<Object>} - a list of global hint objects as described by
|
||||
* JSLint annotations found in the comments.
|
||||
*/
|
||||
function extractGlobals(comments) {
|
||||
var globals = [];
|
||||
|
||||
if (comments) {
|
||||
comments.forEach(function (comment) {
|
||||
if (comment.type === "Block" && comment.value) {
|
||||
var val = comment.value;
|
||||
if (val.indexOf("global") === 0) {
|
||||
val.substring(7).split(",").forEach(function (global) {
|
||||
var index = global.indexOf(":");
|
||||
|
||||
if (index >= 0) {
|
||||
global = global.substring(0, index);
|
||||
}
|
||||
globals.push(HintUtils.makeToken(global.trim()));
|
||||
});
|
||||
} else if (val.indexOf("jslint") === 0) {
|
||||
val.substring(7).split(",").forEach(function (ann) {
|
||||
var index = ann.indexOf(":"),
|
||||
aKey = (index >= 0) ? ann.substring(0, index).trim() : "",
|
||||
aVal = (index >= 0) ? ann.substring(index + 1, ann.length).trim() : "";
|
||||
|
||||
if (aVal === "true" && HintUtils.JSL_GLOBAL_DEFS.hasOwnProperty(aKey)) {
|
||||
globals = globals.concat(HintUtils.JSL_GLOBAL_DEFS[aKey]);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return globals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the scope and associated parse information back to the caller.
|
||||
* Called by the parse function below.
|
||||
*
|
||||
* @param {string} dir - the directory name of the parsed file
|
||||
* @param {string} file - the file name of the parsed file
|
||||
* @param {number} length - the length of the parsed file
|
||||
* @param {Object} parseObj - scope and token data from the parsed file
|
||||
*/
|
||||
function respond(dir, file, length, parseObj) {
|
||||
var success = !!parseObj,
|
||||
response = {
|
||||
type : HintUtils.SCOPE_MSG_TYPE,
|
||||
dir : dir,
|
||||
file : file,
|
||||
length : length,
|
||||
success : success
|
||||
};
|
||||
|
||||
if (success) {
|
||||
var scope = parseObj.scope,
|
||||
globals = parseObj.globals,
|
||||
identifiers = siftPositions(scope.walkDownIdentifiers.bind(scope), "name"),
|
||||
properties = siftPositions(scope.walkDownProperties.bind(scope), "name"),
|
||||
literals = siftPositions(scope.walkDownLiterals.bind(scope), "value"),
|
||||
associations = siftAssociations(scope.walkDownAssociations.bind(scope));
|
||||
|
||||
response.scope = scope;
|
||||
response.globals = HintUtils.annotateGlobals(globals);
|
||||
response.identifiers = identifiers;
|
||||
response.properties = HintUtils.annotateWithPath(properties, dir, file);
|
||||
response.literals = HintUtils.annotateLiterals(literals, "string");
|
||||
response.associations = associations;
|
||||
}
|
||||
|
||||
self.postMessage(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a JavaScript text with Esprima. This function is intended to be
|
||||
* called asynchronously; the respond function above is called with the
|
||||
* results of parsing.
|
||||
*
|
||||
* @param {string} dir - the directory name of the file to parse
|
||||
* @param {string} file - the file name of the file to parse
|
||||
* @param {string} text - the text of the file to parse
|
||||
* @param {number} retries - the number of times an unparseable text should
|
||||
* be retried, after blanking whatever lines are causing the parsing
|
||||
* errors.
|
||||
*/
|
||||
function parse(dir, file, text, retries) {
|
||||
try {
|
||||
var ast = esprima.parse(text, {
|
||||
range : true,
|
||||
tolerant : true,
|
||||
comment : true
|
||||
});
|
||||
|
||||
var scope = new Scope(ast),
|
||||
definedGlobals = extractGlobals(ast.comments),
|
||||
builtinGlobals = HintUtils.BUILTIN_GLOBALS,
|
||||
allGlobals = definedGlobals.concat(builtinGlobals),
|
||||
comparator = function (a, b) { return a.value < b.value; };
|
||||
|
||||
respond(dir, file, text.length, {
|
||||
scope : scope,
|
||||
globals : allGlobals.sort(comparator)
|
||||
});
|
||||
} catch (err) {
|
||||
// If parsing fails, we can try again after blanking out the line
|
||||
// that caused the parse error. This is unreliable though, because
|
||||
// the line number on which the parser fails is not necessarily the
|
||||
// best line to remove. Some errors will cause the entire remainder
|
||||
// of the file to be blanked out, never resulting in a parseable
|
||||
// file. Consequently, this is attempted only when necessary; i.e.,
|
||||
// when the request.force flag is set.
|
||||
// Inspired by fuckit.js: https://github.com/mattdiamond/fuckitjs
|
||||
|
||||
// _log("Parsing failed: " + err + " at " + err.index);
|
||||
if (retries > 0) {
|
||||
var lines = text.split("\n"),
|
||||
lineno = Math.min(lines.length, err.lineNumber) - 1,
|
||||
newline,
|
||||
removed;
|
||||
|
||||
// Blank the offending line and start over
|
||||
if (-1 < lineno < lines.length) {
|
||||
newline = lines[lineno].replace(/./g, " ");
|
||||
if (newline !== lines[lineno]) {
|
||||
removed = lines.splice(lineno, 1, newline);
|
||||
if (removed && removed.length > 0) {
|
||||
setTimeout(function () {
|
||||
parse(dir, file, lines.join("\n"), --retries);
|
||||
}, 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
respond(dir, file, text.length, null);
|
||||
}
|
||||
}
|
||||
|
||||
self.addEventListener("message", function (e) {
|
||||
var request = e.data,
|
||||
type = request.type;
|
||||
|
||||
if (type === HintUtils.SCOPE_MSG_TYPE) {
|
||||
var dir = request.dir,
|
||||
file = request.file,
|
||||
text = request.text,
|
||||
retries = request.force ? MAX_RETRIES : 0;
|
||||
setTimeout(function () { parse(dir, file, text, retries); }, 0);
|
||||
} else {
|
||||
_log("Unknown message: " + JSON.stringify(request));
|
||||
}
|
||||
});
|
||||
}());
|
||||
@@ -1,19 +0,0 @@
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user