Context key expressions
Context key expressions are a way to express values dynamically in the fields of a Sourcegraph extension’s manifest.
With string interpolation, you can insert these expressions directly into string fields.
Context keys are like variables that you can use inside of these interpolated
expressions. Context keys give you access to values that are available to your
extension dynamically, such as
resource for the currently viewed resource, or
config for the configuration settings.
Context key expressions can be interpolated inside of strings. In manifest
fields that support interpolated expressions, you can interpolate an expression
by surrounding it with
Supported fields in the manifest
These fields in the manifest support context key expressions.
In contributed actions
String fields that accept interpolated expressions, in contributed actions:
commandArguments: each string item in the array accepts interpolated expressions.
Fields that expect a context key expression:
actionItem.pressed: renders the action in a pressed state when this expression evaluates to true.
In menu contributions
Fields that expect a context key expression, in menu contributions:
when: enables the menu contribution when this expression evaluates to true.
Available context keys
Context keys are the variables that you can use inside of expressions.
config: a namespace containing all the settings that are available. For example,
sourcegraphBaseUrlvalue from Sourcegraph settings.
resource: the current resource being viewed, such as a file.
component: true if a component is open, such as a panel, directory view, or file view.
component.selections: an object representing the current selections.
panel: the panel component, if a panel is open.
Returns the value of a property named
object or return undefined if
either the object or the property doesn’t exist.
Returns the object converted to a JSON string using
The expression syntax is simple and isn’t intended to be a full programming language, so there are some limitations.
- Lack of operator precedence. Because of the simplicity of the parser, operators do not have any precedence and are simply evaluated left-to-right. Use parentheses to specify precedence.
- Lack of a ternary operator. Instead of a ternary operator, you can use
||operators to achieve a similar result.