08 May 2007

Code Completion with ReSharper

Completion with ReSharper is very different from what you have in Visual Studio. Some people refer to it as "broken". Indeed, when you start using ReSharper's intellisense, you may feel that something is "wrong". But it's just a little different and much better. I will describe completion features in ReSharper and you'll see how superior they are to what Visual Studio offers.

Look
Visually, completion list is not that different with default ReSharper settings. Here is original Visual Studio completion for type System.String, without any prefix:



Here is what ReSharper displays:



First evident difference is usage of bold font face. It emphasizes immediate members of type, those declared exactly in type for which completion was called. Note how GetType() is not bold, because it was inherited from System.Object. In large hierarchies, like windows controls, it is huge timesaver to be able to spot members specific to current type.

Another obvious difference is ability to show all overloads for a member in a signature popup, not just some random one with cryptic message (+2 overload(s)). You can instantly see that you can test end of line against string in a case-insensitive way and that it would be second boolean parameter.

You can also see, that ReSharper can suggest indexer (and correctly remove dot when inserting brackets). We also have some custom icons for parameters and local variables to distinguish them from fields.

If you wish, you can set completion font to match font in Editor and thus align completion items with what you type. You can tweak it on ReSharper/Options/IntelliSense/Code Completion page.

Narrow Down
More differences become visible when you start typing while completion list is open.



As you can see, list has become smaller and contains only items with the specified prefix, which is highlighted. For a large completion list, such as when you have windows control, it helps you quickly find what you need. It is even more important for type completion, but later about it.

Replace and Insert
The most confusing part and most difficult thing to adopt in ReSharper completion behavior is choosing between replacing and inserting. When you write new code, it doesn't matter, but if you are using completion inside existing expression it makes big difference. Consider the following example: we have method that replaces all spaces with underscores. Imagine that you need to modify the body to remove leading and trailing spaces before doing replacement.

        public string QuoteSpaces(string text)
        {
            return text.Replace(' ', '_');
        }


You position caret right after "text." and hit Ctrl-Space, then type few letters to locate Trim method.



Now, if you hit "Tab" key to complete the identifier it will remove "Replace" and insert "Trim", then put caret into parenthesis. Very useful when you need to change existing identifier to another one. Note the light red highlighting which denotes text to be removed, if you hit Tab. If you hit "Enter" key, it will just insert "Trim()" and let you specify parameters, if needed.

Name Suggestion
One more thing that ReSharper does for you is suggesting variable names when typing declaration.



It works for fields, parameters, variables, members and alike. Saves a lot of typing, I have to say. Name suggestion completion takes naming conventions into account, so be sure to set them in ReSharper/Options/Code Style/ C# or Visual Basic / Naming Convention.

Type Completion
Whenever you need a type at the caret position and you only have basic completion at hands, you have to type full type name and then import type via some sort of fix, e.g. Visual Studio smart-tag action. You also have to match case, and can you remember which one is right - DESCryptoServiceProvider or DesCryptoServiceProvider?

ReSharper comes to rescue with Type Completion, which shows all types matching prefix regardless of (un-)imported namespaces and inserts required using directives when you select a type.



Default shortcut is Ctrl-Alt-Space. Start using it now!

Smart Completion
ReSharper has deep knowledge about your code. Really! When you see variable declaration in gray, it is ReSharper who analysed execution flow and figured out that particular variable is not used. Some of this knowledge is available to you in completion. When it is known what type is expected at specific point, you may get filtered list of available symbols by invoking Smart Complete (default shortcut Ctrl-Shift-Space):



You can see that ReSharper suggested some static members of System.String and parameter "text". Well, you were going to return something of string type, so here you are!

Smart Completion is also smart enough to suggest anonymous and regular method creation:



Or even local variable creation in place of out parameter:



Conclusion
When you type code, completion is your best friend. Master all three completion kinds, practice for second-nature and you can achieve marvellous code generation speed.

Note: Some or all of the features mentioned in this article may be available only in latest EAP versions of ReSharper.

No comments: