Skip to content

Commit

Permalink
feat: add textobjects query
Browse files Browse the repository at this point in the history
  • Loading branch information
AbaoFromCUG committed Mar 25, 2024
1 parent 88787d2 commit e1ae92a
Show file tree
Hide file tree
Showing 18 changed files with 764 additions and 184 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ max_line_length = 120
[*.{json,json5,js,cjs,ts,vue,css}]
indent_size = 2

[*.scm]
indent_size = 2

[*.rs]
indent_size = 4

Expand Down
73 changes: 70 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,27 @@ With 💤lazy.nvim:
{
"SUSTech-data/neopyter",
opts = {
-- auto define autocmd
auto_attach = true,
-- auto connect server
auto_connect = true,
-- your jupyter host + neopyter port
remote_address = "127.0.0.1:9001",
file_pattern = { "*.ju.*" },
on_attach = function(bufnr)
end
end,

highlight = {
enable = true,
shortsighted = true,
}
},
}
```

#### Integration

**nvim-cmp**
##### nvim-cmp

- `nvim-cmp`
- `lspkind.nvim`
Expand Down Expand Up @@ -95,7 +103,7 @@ cmp.setup({
},
)}

-- menu item highlight
-- menu item highlight
vim.api.nvim_set_hl(0, "CmpItemKindMagic", { bg = "NONE", fg = "#D4D434" })
vim.api.nvim_set_hl(0, "CmpItemKindPath", { link = "CmpItemKindFolder" })
vim.api.nvim_set_hl(0, "CmpItemKindDictkey", { link = "CmpItemKindKeyword" })
Expand All @@ -106,6 +114,59 @@ vim.api.nvim_set_hl(0, "CmpItemKindStatement", { link = "CmpItemKindVariable" })

More information, see [nvim-cmp wiki](https://github.com/hrsh7th/nvim-cmp/wiki/Menu-Appearance)

##### nvim-treesitter-textobjects

Supported captures in `textobjects` query group

- @cell
- @cell.code
- @cell.magic
- @cell.markdown
- @cell.raw
- @cell.special
- @cellseparator
- @cellseparator.code
- @cellseparator.magic
- @cellseparator.markdown
- @cellseparator.raw
- @cellseparator.special
- @cellbody
- @cellbody.code
- @cellbody.magic
- @cellbody.markdown
- @cellbody.raw
- @cellbody.special
- @cellcontent
- @cellcontent.code
- @cellcontent.magic
- @cellcontent.markdown
- @cellcontent.raw
- @cellcontent.special
- @cellborder
- @cellborder.markdown
- @cellborder.raw
- @cellborder.special
- @linemagic

```lua
require'nvim-treesitter.configs'.setup {
textobjects = {
move = {
enable = true,
goto_next_start = {
["]j"] = "@cellseparator",
["]c"] = "@cellcontent",
},
goto_previous_start = {
["[j"] = "@cellseparator",
["[c"] = "@cellcontent",
},
},
},
}

```

## Quick Start

- Open JupyterLab `jupyter lab`, there is a sidebar named `Neopyter`, which display neopyter ip+port
Expand Down Expand Up @@ -167,6 +228,12 @@ More information, see [nvim-cmp wiki](https://github.com/hrsh7th/nvim-cmp/wiki/M
- [x] Magic completion item
- [x] Path completion item
- [ ] Disable others?
- Tree-sitter
- [x] Highlight
- Separator+non-code
- Shortsighted
- [x] Textobjects
- [ ] Fold
- Kernel manage
- [x] Restart kernel
- [x] Restart kernel and run all
Expand Down
101 changes: 62 additions & 39 deletions doc/example.ju.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,92 @@
print("hello")
# This is example for neopyter notebook format
# This will become first cell
print("I am first cell")

# %%
print("previous line is standard cell separator")

# %% Cell with title
print("previous line is standard cell separator, can include a title `Cell with title`")

# %% [md]
"""
```lua
print("First cell")
print("you can mark cell tyep with `[lang]` syntax, notice the space")
print("default cell type is [code]")
```
"""
# %% ------------------------parse as code cell---------------------------------------
# %%

# %%

# %% title

# next line is line magic in jupyter lab
# %sleep

# %% [title

# %% []

# %% [ ]


# %% [code] title

# %% ------------------------parse as magic cell---------------------------------------

# %%timeit

# %%timeit
"""
print("Hello")
"""


print("Second cell")

# %% ------------------------parse as markdown cell---------------------------------------

# %% [md]
"""
# First title
## Second color
### Second color
# this is first level
## this is second level
* 1 level
** 2 level
*** 3 level
```bash
$ echo "code in code"
$ echo $PATH
```
"""

**Bold**
# %% [markdown]

"""
# %% [markdown]
"""
# First title
# %% [md] title

"""
# %% [markdown] title

# %% [raw]
"""
raw cell content
# %% ------------------------parse as raw cell---------------------------------------

# %% [raw]
"""
this is raw cell content
"""

# %%
# %% [raw] title

# %% title
# %% ------------------------parse as special cell---------------------------------------


# %% [title
# %% [sql]

# %% []
# %% [sql] title

class A():
def __init__(self) -> None:
pass
# %% [js]

"""
not markdown
# %% [js] title

"""
# %% ------------------------parse as special cell---------------------------------------
# %%

# %% [md]
"""
#
"""
# %%timeit

# %% [markdown]
# %% [sql]
"""
select * from table
"""

# %% [sql]
12 changes: 8 additions & 4 deletions lua/neopyter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ local utils = require("neopyter.utils")
---@class neopyter.Option
---@field remote_address string
---@field file_pattern string[]
---@field auto_attach boolean
---@field auto_attach boolean Automatically attach to the Neopyter server when open file_pattern matched files
---@field auto_connect boolean # auto connect jupyter lab
---@field rpc_client
---| "'async'" # AsyncRpcClient, default
---| "'block'" # BlockRpcClient
Expand All @@ -26,8 +27,8 @@ M.config = {
return ipynb_path
end,

-- Automatically attach to the Neopyter server when open file_pattern matched files
auto_attach = true,
auto_connect = true,
rpc_client = "async",
jupyter = {
auto_activate_file = true,
Expand All @@ -44,7 +45,7 @@ M.config = {
highlight = {
enable = true,
-- Dim all cells except the current one
shortsighted = false,
shortsighted = true,
},
parse_option = {
line_magic = true,
Expand All @@ -66,7 +67,10 @@ function M.setup(config)
group = augroup,
pattern = M.config.file_pattern,
callback = function()
if not jupyter.jupyterlab:is_connecting() then
if M.config.auto_connect and not jupyter.jupyterlab:is_connecting() then
jupyter.jupyterlab:connect()
end
if not jupyter.jupyterlab:is_attached() then
jupyter.jupyterlab:attach()
end
end,
Expand Down
4 changes: 3 additions & 1 deletion lua/neopyter/asyncwrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ local function async_wrap(cls, ignored_methods)
logger.log(string.format("Call api [%s] from main thread directly", key))
return a.run(function()
return value(unpack(params))
end, function()
end, function(result)
print(string.format("Call api [%s] complete from main thread directly:%s", key, result))
logger.log(string.format("Call api [%s] complete from main thread directly", key))
return "Bad"
end)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/neopyter/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end
---Return whether this source is available in the current context or not (optional).
---@return boolean
function source:is_available()
return jupyter.jupyterlab:is_connecting() and jupyter.notebook ~= nil and jupyter.notebook:is_attached()
return jupyter.notebook ~= nil and jupyter.notebook:is_attached() and jupyter.jupyterlab.client:is_connecting()
end

function source:get_debug_name()
Expand Down
Loading

0 comments on commit e1ae92a

Please sign in to comment.