diff --git a/require.intellisense.js b/require.intellisense.js
index 9e565d4..26d5e5d 100755
--- a/require.intellisense.js
+++ b/require.intellisense.js
@@ -1,91 +1,84 @@
///
-!function (window) {
- var defines = [],
- moduleUrls = [],
- oldDefine = window.define,
- oldRequire = window.require,
- oldLoad = requirejs.load;
+(function () {
+ var DEBUG = 3, WARN = 2, ERROR = 1, NONE = 0;
- var loadEvent = document.createEvent("event");
- loadEvent.type = "load";
+ //Set the logging level
+ var logLevel = DEBUG;
- // Ensure that we're only patching require/define
- // if RequireJS is the current AMD implementation
- if (window.require !== window.requirejs)
- return;
-
- intellisense.annotate(window, {
- define: function () {
- ///
- /// Defines a named module, with optional dependencies, whose value is determined by executing a callback.
- /// The name of the module
- /// An array of modules that this module depends on
- /// The callback that will be called when your module is asked to produce a value
- ///
- ///
- /// Defines an anonymous module, with no dependencies, whose value is determined by executing a callback.
- /// The callback that will be called when your module is asked to produce a value
- ///
- ///
- /// Defines an anonymous module, with no dependencies, whose value is an object literal.
- /// The object literal that represents the value of this module
- ///
- },
- require: function () {
- ///
- /// Defines a callback function that will be triggered after a set of dependency modules have been evaluated
- ///
- ///
- ///
- }
- });
-
- requirejs.load = function (context, moduleName, url) {
- moduleUrls.push(url);
- oldLoad.call(requirejs, context, moduleName, url);
+ function log(level) {
+ var msg = Array.prototype.slice.call(arguments, 1);
+ if (logLevel >= level) {
+ msg.splice(0, 0, level == DEBUG ? 'DEBUG' : level == WARN ?
+ 'WARN' : level == ERROR ? 'ERROR' : 'UNKNOWN');
+ intellisense.logMessage(msg.join(':'));
}
+ }
- window.define = function (name, deps, callback) {
- defines.push([name, deps, callback]);
+ log(DEBUG, "Re-read _references.js ");
- oldRequire.call(window, deps, callback);
-
- defines.forEach(function (define) {
- oldDefine.apply(window, define);
- });
+ //Redirect errors to intellisense log
+ requirejs.onError = function (e) {
+ var modules = e.requireModules && e.requireModules.join(',');
+ switch (e.requireType) {
+ case 'scripterror':
+ log(WARN, modules, "Dependency script not yet loaded, check the stated define name matches the require.");
+ break;
+ default:
+ log(ERROR, e.requireType, modules, e.toString());
+ break;
}
-
- window.define.amd = {
- multiversion: true,
- plugins: true,
- jQuery: true
- };
-
- window.require = function (deps, callback) {
- setTimeout(function () {
- // #1. Call the original require
- oldRequire.call(window, deps, callback);
-
- defines.forEach(function (define, index) {
- oldDefine.apply(window, define);
+ };
- var scriptElements = document.getElementsByTagName("script");
-
- for (var i = 0; i < scriptElements.length; i++) {
- var script = scriptElements[i];
- if (script.src == moduleUrls[index]) {
- loadEvent.currentTarget = script;
- requirejs.onScriptLoad(loadEvent);
- }
- }
- });
- }, 0);
+ var originalDefine = define;
+ var lastDefine;
+ var currentDocumentId = "_@ROOT";
+ define = function (name, deps, callback) {
+ //Disallow anonymous modules - unfortunately we can't handle them
+ //without turning everything to guesswork.
+ if (typeof name !== 'string') {
+ log(ERROR, "Intellisense not supported for anonymous modules");
+ }
+ //Make use of the fact that VS seemingly invokes the current document
+ //code twice. On the second call, rename the module to ensure this
+ //specific code will be evaluated and then require it to evaluate it.
+ if (lastDefine === name) {
+ log(DEBUG, "Define current document");
+ originalDefine(currentDocumentId, deps, callback);
+ log(DEBUG, "Require current document");
+ require([currentDocumentId], function () {
+ //This log message may never get printed as VS stops execution of
+ //the current document once it hits the node at which auto-
+ //completion has being invoked.
+ log(DEBUG, "Loaded current document and all dependencies");
+ });
+ } else {
+ log(DEBUG, "Define module", name);
+ originalDefine(name, deps, callback);
}
+ lastDefine = name;
+ };
- // Redirect all of the patched methods back to their originals
- // so Intellisense will use the previously defined annotations
- intellisense.redirectDefinition(requirejs.load, oldLoad);
- intellisense.redirectDefinition(window.define, oldDefine);
- intellisense.redirectDefinition(window.require, oldRequire);
-}(this);
\ No newline at end of file
+ intellisense.annotate(window, {
+ define: function () {
+ ///
+ /// Defines a named module, with optional dependencies, whose value is determined by executing a callback.
+ /// The name of the module
+ /// An array of modules that this module depends on
+ /// The callback that will be called when your module is asked to produce a value
+ ///
+ ///
+ /// Defines a named module, with optional dependencies, whose value is determined by executing a callback.
+ /// The name of the module
+ /// The callback that will be called when your module is asked to produce a value
+ ///
+ },
+ require: function () {
+ ///
+ /// Defines a callback function that will be triggered after a set of dependency modules have been evaluated
+ ///
+ ///
+ ///
+ }
+ });
+}());
\ No newline at end of file