Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: various issues with node ranges #89

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/otter.nvim.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*otter.nvim.txt* For Neovim >= 0.9.0 Last change: 2024 February 11
*otter.nvim.txt* For Neovim >= 0.9.0 Last change: 2024 February 12

==============================================================================
Table of Contents *otter.nvim-table-of-contents*
Expand Down
10 changes: 8 additions & 2 deletions lua/otter/keeper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,23 @@ M.get_current_language_context = function(main_nr)
local name = query.captures[id]

lang_capture = determine_language(main_nr, name, node, metadata, lang_capture)
local start_row, start_col, end_row, end_col = node:range()
end_row = end_row - 1
local last_line = vim.api.nvim_buf_get_lines(main_nr, end_row, end_row+1, true)
if last_line ~= nil and last_line[1] ~= nil then
end_col = last_line[1]:len()
end

if lang_capture and (name == "content" or name == "injection.content") then
-- chunks where the name of the injected language is dynamic
-- e.g. markdown code chunks
if ts.is_in_node_range(node, row, col) then
return lang_capture, node:range()
return lang_capture, start_row, start_col, end_row, end_col
end
-- chunks where the name of the language is the name of the capture
elseif fn.contains(injectable_languages, name) then
if ts.is_in_node_range(node, row, col) then
return name, node:range()
return name, start_row, start_col, end_row, end_col
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lua/otter/tools/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ M.is_otter_language_context = function(lang)
local current = require("otter.keeper").get_current_language_context()
if current == lang then
vim.b["quarto_is_" .. lang .. "_chunk"] = true
return true
else
return false
end
end

Expand Down
16 changes: 16 additions & 0 deletions tests/debug-example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#! /bin/env bash
# usage: ./tests/debug-example.sh $1

echo "choose an example file based on number of extension."
echo "e.g. ./tests/debug-example.sh 01"
echo "e.g. ./tests/debug-example.sh qmd"
echo "Options"
ls ./tests/examples/

if [ -z "$1" ]; then
echo "Please provide a file extension or a number"
echo "And call this from the project root directory"
exit 1
fi

nvim ./tests/examples/*$1* -c ":lua require'otter'.dev_setup()"
File renamed without changes.
7 changes: 7 additions & 0 deletions tests/examples/01.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ engine: markdown

# Hello

```{r}
print('hello world')

plot(1:10)
```


```{python}
print('hello world')
```
Expand Down
17 changes: 17 additions & 0 deletions tests/examples/03.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ engine: markdown

# Hello

```lua
print( "this should be formatted")
```

```lua
print( "this should be formatted")
```

```{python}
print('hello world')
```
Expand Down Expand Up @@ -43,3 +51,12 @@ def hello():
```{python}
hello()
```


```{python}
import numpy as np

np.zeros(10)
```


26 changes: 26 additions & 0 deletions tests/examples/05.norg
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@document.meta
title: Norg Example
description: This is an example of Norg document.
@end

* Norg Example

** Code
@code lua
print("Hello, Norg!")
@end


@code python
print("Hello world!")
import numpy as np
x = np.zeros(10)
@end

** Other things
This is a paragraph.

- This is a list.
- This is another list.

This is another paragraph.
16 changes: 16 additions & 0 deletions tests/examples/06.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const foo = html`
<div>
<p>hello</p>
</div>
`;

const bar = css`
.foo {
color: rgb(0, 0, 0);
}
`;

const lua = lua`
local foo = "bar"
print(foo)
`;
17 changes: 17 additions & 0 deletions tests/examples/07-just-r.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Hello World
format: html
engine: markdown
---

# Formatting

```{r}
print('hello world')
x = "Hello"
```

```{r}
print("hello world")
x <- "Hello"
```
Loading