Home Explore Blog CI



nushell

11th chunk of `contributor-book/plugin_protocol_reference.md`
60c311fa97bc7a59e9852e7d1e122e81809197c5a36b760900000001000010cd
This call responds with [`Empty` pipeline data](#empty-header-variant) on success when no action is required by the plugin. On Unix-like operating systems, if the response is [`Value` pipeline data](#value-header-variant), it contains an [`Int`](#int) which is the process group ID the plugin must join using `setpgid()` in order to be in the foreground.

This call will fail with an error if the plugin is already in the foreground.

The plugin **should** call [`LeaveForeground`](#leaveforeground-engine-call) when it no longer needs to be in the foreground. Note that the plugin will also automatically be removed from the foreground when the plugin call response is received, even if the plugin call returns a stream.

Example:

```json
{
  "EngineCall": {
    "context": 0,
    "id": 0,
    "call": "EnterForeground"
  }
}
```

#### `LeaveForeground` engine call

Resets the state set by [`EnterForeground`](#enterforeground-engine-call).

If the plugin had been requested to change process groups by the response of `EnterForeground`, it should also reset that state by calling `setpgid(0)`, since plugins are normally in their own process group.

This call responds with [`Empty` pipeline data](#empty-header-variant) on success.

Example:

```json
{
  "EngineCall": {
    "context": 0,
    "id": 0,
    "call": "LeaveForeground"
  }
}
```

#### `GetSpanContents` engine call

Get the contents of a [`Span`](#span) from the engine. This can be used for viewing the source code that generated a value. The argument is a [`Span`](#span). The response on success is [`Value` pipeline data](3value-header-variant) containing a [`Binary`](#binary) value, as the result is not guaranteed to be valid UTF-8.

Example:

```json
{
  "EngineCall": {
    "id": 72,
    "call": {
      "GetSpanContents": {
        "start": 38881,
        "end": 39007
      }
    }
  }
}
```

#### `EvalClosure` engine call

Pass a [`Closure`](#closure) and arguments to the engine to be evaluated. Returns a [`PipelineData` response](#pipelinedata-engine-call-response) if successful with the output of the closure, which may be a stream.

| Field               | Type                                        | Description                                                            |
| ------------------- | ------------------------------------------- | ---------------------------------------------------------------------- |
| **closure**         | spanned [`Closure`](#closure)               | The closure to call, generally from a [`Value`](#value).               |
| **positional**      | [`Value`](#value) array                     | Positional arguments for the closure.                                  |
| **input**           | [`PipelineDataHeader`](#pipelinedataheader) | Input for the closure.                                                 |
| **redirect_stdout** | boolean                                     | Whether to redirect stdout if the closure ends in an external command. |
| **redirect_stderr** | boolean                                     | Whether to redirect stderr if the closure ends in an external command. |

The `Closure` is not wrapped as a `Value` - i.e., it doesn't have `{"Closure": ...}` around it.

Example:

```json
{
  "EngineCall": {
    "context": 7,
    "id": 40,
    "call": {
      "EvalClosure": {
        "closure": {
          "item": {
            "block_id": 72,
            "captures": []
          },
          "span": {
            "start": 780,
            "end": 812
          }
        },
        "positional": [
          {
            "Int": {
              "val": 7,
              "span": {
                "start": 3080,
                "end": 3081
              }
            }
          }
        ],
        "input": "Empty",
        "redirect_stdout": true,
        "redirect_stderr": false
      }
    }
  }
}
```

#### `FindDecl` engine call

Find the declaration ID for a command in scope. The body is the name of the desired command, as a string. Returns an [`Identifier` response](#identifier-engine-call-response) if successful with the ID of the declared command, or an [empty](#empty-header-variant) [`PipelineData` response](#pipelinedata-engine-call-response) if the command with the given name couldn't be found in the scope of the plugin call.

Title: Engine Calls: Foreground Management, Span Contents, Closure Evaluation, and Declaration Lookup
Summary
This section describes additional `EngineCall` options. It details `EnterForeground` and `LeaveForeground` calls for managing a plugin's foreground state for terminal interaction. It covers `GetSpanContents` for retrieving source code snippets based on a span. It discusses `EvalClosure` for evaluating closures with arguments, including details on positional arguments, input, and stdout/stderr redirection. Finally, it presents `FindDecl` for locating the declaration ID of a command within the plugin's scope.