Skip to content

RFC: Add session option#27

Open
laburnumT wants to merge 2 commits into
gpanders:masterfrom
laburnumT:master
Open

RFC: Add session option#27
laburnumT wants to merge 2 commits into
gpanders:masterfrom
laburnumT:master

Conversation

@laburnumT

Copy link
Copy Markdown

Adds a session option to codeblocks so previous results can be used.

To check that a command is done running, we wait for an eof_token to appear. This allows us to not cut off commands with output early, but also doesn't rely on using sleep.

@laburnumT

Copy link
Copy Markdown
Author

This is an RFC for adding session support to vim-medieval. It's currently quite
hacky, because repl's aren't always accessed the same way. But that's why it's a
first draft.

It currently has some checks in there for python (because that's what I use in
markdown for the most part).

This should probably be integrated into g:medieval_langs, or a new config
variable, but I just wanted to know if there is any interest in functionality
like this before I do that work.

@laburnumT

Copy link
Copy Markdown
Author

The main problem this is trying to address is where something is either costly
to run or not idempotent. As a rather stupid example:

<!-- session: example -->
```python
import random
import time

time.sleep(5)
x = 42
```

<!-- session: example -->
```python
print(random.randint(1, x))
```

Using require here would make the print block always take 5 seconds, whereas
with a session it would run instantly (as long as the first code block has been
run at least once)

laburnumT added 2 commits July 9, 2026 09:59
Adds a session option to codeblocks so previous results can be used.

To check that a command is done running, we wait for an eof_token to
appear. This allows us to not cut off commands with output early, but
also doesn't rely on using sleep.

**This commit does not support neovim**
Splits up retrieving the data from s:session_read_cb into separate
helper functions for vim and nvim.
@gpanders

Copy link
Copy Markdown
Owner

The main problem this is trying to address is where something is either costly to run or not idempotent. As a rather stupid example:

<!-- session: example -->
```python
import random
import time

time.sleep(5)
x = 42
print(random.randint(1, x))

Using require here would make the print block always take 5 seconds, whereas with a session it would run instantly (as long as the first code block has been run at least once)

The use case makes sense to me. Though I think you should already be able to solve this today with no changes.

For example, something like this:

<!-- target: a -->
```python
import random
import time

time.sleep(5)
x = 42
print(f"x={x}")
```

<!-- name: a
```python
```
-->

<!-- require: a -->
```python
import random
print(random.randint(1, x))
```

I understand this is a contrived example, but the basic idea is: in the first (expensive) block, write code to a temporary block (a in this case) that re-uses the expensive results of the computation. Then you can require that temporary block in the final block.

Note that the a block is wrapped inside the HTML comment so that it won't be visible in any rendered HTML output.

@laburnumT

Copy link
Copy Markdown
Author

That would probably solve a lot of cases. But I feel like it might run into
problems with something like Haskell or some other language. But even in python
this does run into problems with classes that aren't printable (don't have a
dedicated str method).

Something like the following wouldn't work:

<!-- target: a -->
```python
class Example:
    def __init__(self, a, b):
        self.a = a
        self.b = b


example = Example(1, 2)
print(f"example={example}")
```

<!-- name: a
```python
example=<__main__.Example object at 0x723d8c290d70>
```
-->

<!-- require: a, target: out -->
```python
print(example.a)
```

<!-- name: out -->
```python
  File "/tmp/vqFx8sb/13", line 1
    example=<__main__.Example object at 0x723d8c290d70>
            ^
SyntaxError: invalid syntax
```

It also seems rather cumbersome to have to add all the print statements one
might need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants