JQuery IntelliSense in Visual Studio 2008

ScottGu recently announced that VS2008 JavaScript IntelliSense no longer fails completely when it runs into JQuery, which is fantastic news - and an impressive testament to the flexibility of VS2008's JavaScript parsing abilities. Previously, merely referencing JQuery was enough to disable all JavaScript IntelliSense in VS2008, which meant that JQuery addicts like me had simply never seen how impressive this system really can be.

But even though they've got over the crashing problem, the basic IntelliSense experience is still a little sparse, especially since JQuery employs minimally descriptive one-or-two character parameter names in an effort to keep source size down. JQuery also frequently employs method chaining (for more on which, see blogs passim) but VS IntelliSense is unable - against raw JQuery - to determine the return type and generate chained method and parameter hints.

So visual Studio IntelliSense starts off promisingly:

plain.1

but then falls flat as soon as you've hit the dot:

plain.2

This has led to a spate of efforts to annotate JQuery with IntelliSense-friendly documentation comments. Unfortunately, there's a limit to how much you can do with adding annotations directly to the JQuery code, and it's a time-consuming - and manually intensive - operation that will be blown away by the next release of JQuery.

One of the key problems is that JQuery - in classic JavaScript space-conscious style - creates a number of its prototype functions by repeatedly using the same function declaration in a loop, relying on JavaScript closures to make each function behave differently. Here's a sample where JQuery sets up a series of event hook methods:

jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
	"mousedown,mouseup,mousemove,mouseover,mouseout,change,select," + 
	"submit,keydown,keypress,keyup,error").split(","), function(i, name){
	
	// Handle event binding
	jQuery.fn[name] = function(fn){
		return fn ? this.bind(name, fn) : this.trigger(name);
	};
});

Stick a documentation comment in the top of that function, and you'll end up with the same IntelliSense documentation on 20 JQuery methods. Hardly ideal.

There has to be a better way; we use JQuery intensively at work, and so, with the encouragement of my colleague Duncan Smart, I've set out to produce better JQuery IntelliSense, using an automated process rather than manual annotation.

Duncan also pointed me towards the JQuery machine-readable documentation at http://jquery.com/api/. Sadly it's a little out of date, but hopefully it will be updated at some point, and when it is, the automated process should be able to employ the updated data and rebuild the annotated JQuery.

Visual Studio figures out external script references, such as to JQuery, by following special reference comments it finds at the top of .js files:

/// <reference path="jquery-1.2.3.js" />

In practice, this means that you can reference any JavaScript file through this mechanism, not just those that you're actually going to run at runtime. Most sites deploy a minified version of JQuery to the browser, but you could reference instead an annotated JQuery file here to get IntelliSense support.

There is a different resolution employed to provide IntelliSense from within <script> tags, where VS attempts to follow <script src=""> links to external includes, and even traces includes it finds in the master page. But we can fool the design-time engine by including <script> tags that won't be output to the browser - for example wrapping the tag inside an <asp:PlaceHolder> with its Visible property set permanently to false. If it overrides the definitions that will really be imported at runtime with its own design-time definitions, we get IntelliSense - in ASPX pages, HTML pages, and so on.

In which case, the question arises, why does the file you're referencing here need to contain anything other than the annotations to drive IntelliSense? Why make VS2008 work so hard to understand JQuery in the first place, when you're already lying to it about which JavaScript source file you're using?

So the approach I've ended up with is not to annotate the JQuery source code, which has the limitations mentioned before, but instead to generate completely different JavaScript that defines a stub that looks exactly the same as JQuery from the point of view of IntelliSense, but which contains no actual functionality. It's a design-time 'header file' that contains all the documentation and structure of JQuery but none of the code. The result is fully-functional method and parameter IntelliSense. Where previously VS drew a blank, now it can continue to help.

intel.1

 intel.2

 intel.3

intel.4 

Employing the script-hiding technique mentioned above, here it is functioning in an ASPX page:

aspx

To produce the script, I turned, naturally, to JavaScript. Using JavaScript, it's possible to examine the actual JQuery API and object model as it exists in the web browser, and construct a script that outputs the same structure. Along the way, I can also generate the appropriate documentation comments from the XML source data at http://jquery.com/api/data/jquery-docs-xml.xml, and include IntelliSense comments. Here's a sample of the output it ends up producing:

jQuery = $ = function (expr, context) {
	/// <summary>
	/// 1: $(expr, context) - This function accepts a string containing a CSS or basic XPath selector which is then used to match a set of elements.
	/// 2: $(html) - Create DOM elements on-the-fly from the provided String of raw HTML.
	/// 3: $(elems) - Wrap jQuery functionality around a single or multiple DOM Element(s).
	/// 4: $(fn) - A shorthand for $(document).
	/// </summary>
	/// <returns type="jQuery"></returns>
	/// <param name="expr" />
	/// 1: expr - An expression to search with
	/// 2: html - A string of HTML to create on the fly.
	/// 3: elems - DOM element(s) to be encapsulated by a jQuery object.
	/// 4: fn - The function to execute when the DOM is ready.
	/// </param>
	/// <param name="context" optional="true" />
	/// 1: context - (optional) A DOM Element, Document or jQuery to use as context
	/// </param>
	/// <field type="String" name="jquery">The current version of jQuery.</field>
	/// <field type="Number" name="length">The number of elements currently matched.</field>
};

You can run the JQuery IntelliSense Header Generator in your browser, copy and paste the resulting script into a .js file, and then you can use it as an IntelliSense source.

We've posted the header generator up here. Or, you can download a copy of the header file from here. Let me know in the comments if you find it useful, or if there's anything you think needs changing about it.

UPDATED: I just noticed that Brennan Stehling has produced something similar to this stub file - apparently manually. Hopefully he won't be too annoyed I've made a tool that does the same thing.

Print | posted on Friday, February 15, 2008 2:59 PM

Feedback


Gravatar

# re: JQuery IntelliSense in Visual Studio 2008 2/15/2008 4:48 PM Lance Fisher

Hi James,

I really like this approach. Commenting the whole library by hand was good for getting familiar with jQuery, but I don't know that I want to spend an afternoon on it everytime that a new version is released.

It would be really nice if Visual Studio directly supported stub-files and overloads. I noticed that you did the same thing that I did with including all the overloads in the summary. It would be nice if you could cycle through them like you can with C# and get hints on the different parameters too.

The only downside I can think of with using a stub is debugging. With the actual file, you can see comments when you step into the source, but really, I don't see this as much of a problem. You can still use the "development" version of jQuery. Brennan mentioned that he was working on an automated tool too. This might make a good project on codeplex. I created one a few days ago at www.codeplex.com/jsintellisense, but I don't know that I'm going to be able to spend much time on it right now.


 re: JQuery IntelliSense in Visual Studio 2008 2/16/2008 3:20 PM Steve

How do I use this in VS ?

ie. where does it go?


# re: JQuery IntelliSense in Visual Studio 2008 2/16/2008 5:33 PM James

@Steve - good question, I guess I didn't make this absolutely clear. You need to place the generated JQuery stub file in your project, alongside your real JQuery javascript file. Then, wherever you reference the JQuery library you need to augment or replace that reference with a reference to the intellisense-commented stub file instead.

So, if you've got a JavaScript .js file that uses JQuery functions, you need to put

/// <reference path="jquery.intellisense.js"/>

as the very first line - this will tell the intellisense system to use the stub file.

In an ASPX page or masterpage where you're currently importing the jquery library with a <script src="jquery.js"> tag, you need to use a technique like the one I mention above of embedding an additional <script src="jquery.intellisense.js"> tag after that tag within an <asp:Placeholder Visible="false">:

<asp:PlaceHolder Runat="server" Visible="false">
<script src="jquery.intellisense.js"></script>
</asp:PlaceHolder>

Hope that clarifies things...


# Scott Hanselman - ComputerZen 2/16/2008 9:06 PM

Scott Hanselman - ComputerZen


# Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET 2/17/2008 7:01 PM ScottGu's Blog

Here is the latest in my link-listing series .&amp;#160; Also check out my ASP.NET Tips, Tricks and Tutorials


# 2月17日链接篇: ASP.NET, ASP.NET AJAX, Visual Studio, .NET 2/17/2008 10:10 PM Joycode@Ab110.com

【原文地址】 Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET 【原文发表日期】 Sunday, February 17, 2008


# Colegamenti del 17 Febbraio: ASP.NET, ASP.NET AJAX, Visual Studio, .NET 2/18/2008 1:18 PM Scott Guthrie Italian WebLog

Colegamenti del 17 Febbraio: ASP.NET, ASP.NET AJAX, Visual Studio, .NET


# Brennan's Blog 2/18/2008 8:46 PM

As I work on JavaScript I often consult with Google to find documentation on which functions are available and how they work. Sometimes I find the answer right away and other times it takes more effort to find the right answers. I really want it to be easier and faster to find the right answers quickly so I started work on a Tool Window that I call JavaScript Browser that integrates within Visual Studio 2008....


# Enlaces 17 de Febrero: ASP.NET, ASP.NET AJAX, Visual Studio, .NET 2/19/2008 5:26 PM

Enlaces 17 de Febrero: ASP.NET, ASP.NET AJAX, Visual Studio, .NET


Gravatar

 up-to-date machine-readable jQuery documentation 2/21/2008 10:45 PM Axel Kollmorgen

you can get "jQuery machine-readable documentation" for any jQuery version with the wikiapi2xml tool: http://dev.jquery.com/browser/trunk/tools/wikiapi2xml. the .py scripts produce the same xml as the out of date http://jquery.com/api/ . they are supposed to run cgi, so you have to fiddle a bit to get them run command line. or just google for "createjQueryXMLDocs.py" and you may find a running cgi version which spares you from this (http://www.exfer.net/jquery/createjQueryXMLDocs.py). you could even modify this script to generate your stub file. or incorporate it in your script ...

that other tool in the tools dir also looks interesting: http://dev.jquery.com/view/trunk/tools/api-browser/.

thanks for the nice work!


# re: JQuery IntelliSense in Visual Studio 2008 2/22/2008 2:47 PM James

@Axel - thanks for the pointers. I've had a play with that python script and trying to get it running under IronPython in a simulated CGI environment (you can run any CGI program from the command line if you just set the rgith environment variables).

If nothing else, it's been a good excuse for playing with IronPython (I have managed to run it successfully under native Python, but I love a challenge).

It does generate output in a slightly different format than the file I worked from (I think I was using the output from createjQueryXMLDocsOldFormat.py) but it's definitely what we're looking for.


# (在Visual Studio 2008中让jQuery支持智能感知) 2/22/2008 2:49 PM

(在Visual Studio 2008中让jQuery支持智能感知)


Gravatar

 re: JQuery IntelliSense in Visual Studio 2008 2/23/2008 12:27 AM Axel Kollmorgen

yes, the output from both createjQueryXMLDocs.py and createjQueryXMLDocsOldFormat.py is different to the old 1.1.2 API xml docs, the one from createjQueryXMLDocs.py more than the one from createjQueryXMLDocsOldFormat.py. i noticed when i tried to generate the stub file for jquery 1.2.3 with your script. but yes, it should be possible to adapt that script to generate a complete jquery 1.2.3 documentation stub.

another thought: as the input is xml, it might be easier to generate the output / the documentation stub with xsl. gonna give it a try.


# re: JQuery IntelliSense in Visual Studio 2008 2/25/2008 12:43 PM James

@Axel - indeed, if the only data I was using was the XML documentation, XSL would be a great choice; but the goal of the JavaScript tool is to examine the complete structure of a live JQuery object using JS property reflection, and generate its stub file based on that, annotated with appropriate documentation comments.

For instance, based on the documentation alone, you can determine that there is a property called '$.browser'; but you can't determine that there's a '$.browser.msie' member on it.

By reflecting over a live JQuery instance, the JavaScript code finds all the properties that actually exist and simulates them, which is why I ended up with this approach.


# jQuery Intellisense in Visual Studio 2/25/2008 4:58 PM

jQuery Intellisense in Visual Studio


Gravatar

# re: JQuery IntelliSense in Visual Studio 2008 2/26/2008 7:08 AM Axel Kollmorgen

jQuery.browser is documented quite thoroughly in the api wiki and the xml docs based on it. it mentions all the members / available flags, incl. 'msie'. it also mentions that jQuery.browser.version, which is a member of jQuery.browser, too, is *not* a browser flag but something different. so it actually knows *more* than pure introspection with a live jquery instance. judging from this, i think that the api xml doc should be all you need for generating a complete vstudio doc stub, without the need for additional jquery introspection.

i won't find time for testing this, though. not soon, anyway.


# re: JQuery IntelliSense in Visual Studio 2008 2/26/2008 1:32 PM James

@Axel - indeed, the documentation 'mentions' all the properties, as part of the descriptive text and examples. But a 'mention' isn't especially easy to parse out of the documentation, and the XML files I'm looking at don't have specific elements corresponding to those subproperties at all.


Gravatar

# re: JQuery IntelliSense in Visual Studio 2008 2/29/2008 9:03 PM Chris

I like you're automated version, but it's a little out of date to be usable. 1.2 introduced lots of changes. Maybe they will update that documentation


# Вчера пытался заставить заработать intellisense в VS2008 для jQuery... 3/10/2008 1:32 PM

Вчера пытался заставить заработать intellisense в VS2008 для jQuery...


 re: JQuery IntelliSense in Visual Studio 2008 3/11/2008 3:09 PM Andrei

Excellent work. I am using it for jQuery 1.2.3. Thanks!


# The Agileer: ASP.NET MVC Framework, Ajax, & jQuery 3/17/2008 10:16 PM

... In order to have some useful intellisense we add a second reference to a "dummy" jQuery script, which contains only the information required for jQuery intellisense, basically empty shells with XML documentation comments. You can get the jQuery intellisense script from here....


# Nabble: jQuery intellisense in VS2008 (including chaining) 3/17/2008 10:20 PM

At first, intellisense doesn’t work on chained methods, but James Hart figured out a way to solve that: He wrote an automated script that extracts the jQuery XML documentation into a js file, which is used by VS in the intellisense, showing methods, parameters, etc. Unfortunately, the XML documentation he had was pretty old (1.1.2)

I used the excellent python script by David Serduke (http://www.exfer.net/jquery/createjQueryXMLDocs.py) to extract the latest documentation straight out of the jQuery wiki and export it as XML, then updated James’ script to work with the new XML schema and re-generated the file.


# fordie's blog 3/20/2008 5:45 PM

One thing that was missing until now though was a way of getting jQuery intellisense in VS2008. Yesterday I spotted this article by James Hart which describes how this missing functionality can be be added.


# JQuery IntelliSense in VS2008 4/28/2008 7:50 PM BradVin's .Net Blog

I know, I know, this has been done before. The reason I'm writing this post, however, is to improve on


 re: JQuery IntelliSense in Visual Studio 2008 5/13/2008 1:13 PM Steve

Thank you. Still no luck?

I get an error that 'Element reference is not supported' and I do not get any intellisense

I'm running VS 2008 with the fix. I have the following on my ms mvc site master:

/// <reference path="/BistroWeb/Content/Scripts/jQuery.intellisense.js" />
<script src="/BistroWeb/Content/scripts/jQuery.intellisense.js" type="text/javascript"></script>

Sorry to be a pain, I'd just like to see this work :) I use jQuery all day :)


# Visual Studio 2008 Intellisense for jQuery 1.2.5 5/21/2008 8:34 PM lancefisher.net

Visual Studio 2008 Intellisense for jQuery 1.2.5


# Always 英文技术文章参照( 二 ){ UpdateTime:2008-7-2; } My article in the cnblogs 7/2/2008 3:10 AM cnblogs.com

1. ASP.NET Popup Control http://www.codeproject.com/KB/custom-controls/asppopup.aspx 2. Componentart

Title  
Name  
Email
Url
Comments   
Please add 7 and 6 and type the answer here: