What is context?
The circumstances that form the setting for an event, statement, or idea, and in terms of which it can be fully understood.
What does CodeRush use context for?
CodeRush uses context (in addition to user input) to decide which functionality to provide you with.
For example: The <Tab> key is used for indenting text, and also for invoking the Tab to Next Reference (TtNR) feature. When you hit the <Tab> key, CodeRush performs several context checks.
The context settings for TtNR are fairly complicated, but boils down to IsTheCaretWithinAnIdentifier. If CodeRush judges that this is the case, then it chooses the TtNR functionality, otherwise it chooses the indentation functionality.
CodeRush Templates and Shortcuts often overload each other in this way, and CodeRush uses the context to determine which overload to use.
How do I use a context?
You don’t use a context directly. Instead you choose a feature which is sensitive to context (a shortcut or template will do nicely) and configure it to only work within a given context (or set of contexts)
Features can be configured to work with a specific context where ever you see this UI.
Each node in the tree is a context. The contexts will either be true or false at a given moment.
When being tested, a ticked context will be tested at the moment of invocation and the feature\function will only be allowed if that context is found to be true. Alternatively if the context has a cross against it, then the function\feature will only be allowed to proceed if the context is explicitly found to be false.
What if I can’t find the context I need?
Well first of all I recommend contacting support@devexpress.com since it’s entirely possible that the context you’re after is there, but perhaps not named or located as you would expect. However, failing that, it is very easy to create a brand new context in a plugin.
How do I create a context?
Contexts are analogous to functions which return a boolean value. You check anything you like, (Source code, Project structure, color of the moon) and then set the context to true or false accordingly.
Creating a context is really really simple… just use the NewContext template. Note This template does not ship with CodeRush. See instructions later in this post for how to acquire the template.
Once you have the template installed, simply position your caret in the body of your plugin and type NewContext<space>
The Template will expand like so…
You will then need to fill out a few details.
- A Component Name
This first name is used to identify the metric component itself within your code. You might be about to create several of these and they all need to be distinct from one another and their methods not confused. - A Display Name
The display name is a short identifier used whenever a group of contexts are listed together to distinguish them from each other. - A Description
Explains to the user ticking or crossing the context, what the context is looking for in order to be considered satisfied. - A Provider Name
This is the path at which the context will be listed. A backslash separated set of strings is required here. ie Editor\XML\InOpenTag
With these properties filled out, all you need to do is…
- Implement the ContextSatisfied method
Perform whatever calculations and discovery you like and then set ea.Satisfied = true; when your context is satisfied - Call Register
Call your newly generated Register method from within your InitializePlugin method.
Where can I get this template?
As usual this template is available from within the CodeRush Plugin Templates repository on github
Download and Import this file using these instructions
Or if you’ve installed the Template Importer, simply point it at this webaddress and click import.