Shortcuts, Editing and Code Completion
Let's dive into the most important productivity shortcuts, editing, and code completion.
Get Going with ReSharper and Alt-Enter
As with any other new tool, it's very helpful to learn about its basic functionality first. ReSharper makes this very straightforward by providing Alt-Enter as the single entry point to find actions and options, or to apply fixes to our code.
After pressing Alt-Enter, we can start typing to find our desired action, which can include navigation methods, refactorings, or one of the several ReSharper tool windows. The list of actions always shows the assigned keyboard shortcut to help us learn faster.
Having Alt-Enter makes it very easy to keep your hands on the keyboard at all times, so you can avoid having to switch between input devices.
Editing Code and Code Completion
ReSharper can help you while writing code. Wherever your caret position may be, ReSharper has a good selection of code completion items for us!
For instance with basic completion, in a class body, we would get all possible access modifiers, whereas in function bodies we get all control flow keywords, imported types, parameters and variables available in that particular scope. Through smart completion we can even filter the list to show only relevant items, for instance to show only items of type bool
.
Code completion also works for other languages and file formats, and even inside strings! Using language injections, HTML, regular expressions, and JSON is highlighted in strings, and shows relevant tags, character classes, or property names when needed.
Replace that part with:
Code Templates
ReSharper's code templates can generate otherwise tedious code for you! The most basic templates are live/surround templates and file templates (even with multiple files). A lot of general-purpose templates come preinstalled, for instance the cw
template for Console.WriteLine
, the nguid
template to generate a new Guid
, or the try
surround template to generate a try-catch
block (even works by temporarily overwriting the selected code). Templates can have placeholders, which can be entered when applying a template. But these placeholders can also make use of the wide range of macros to give suggestions, including the current user, date and time, expected types for generic arguments, or transformation of other macro values (identifier name from text).
Another kind are postfix templates. These are typically written in a reverse order of thinking. For instance, instead of using the foreach
live template and then defining the numbers
collection as we go through the placeholders, with a postfix template we can write numbers.foreach
and ReSharper will complete what we want with much less effort. A full list of postfix templates is available from the options dialog. Postfix templates can't be defined in a simple way like live or surround templates (they require to use the ReSharper SDK), however, ReSharper also allows to define source templates alongside our code. These can be used in a similar manner with just some smartness limitations.