Skip to content

Commit

Permalink
feat(templates): avanterules filetype support (closes yetone#254) (ye…
Browse files Browse the repository at this point in the history
…tone#466)

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
  • Loading branch information
aarnphm authored Sep 3, 2024
1 parent 054695c commit 4ad9134
Show file tree
Hide file tree
Showing 31 changed files with 960 additions and 263 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
* text=auto eol=lf
**/*.lock linguist-generated=true
*.avanterules linguist-language=jinja
syntax/jinja.vim linguist-vendored
6 changes: 4 additions & 2 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ function Build-FromSource($feature) {

cargo build --release --features=$feature

$targetFile = "avante_tokenizers.dll"
Copy-Item (Join-Path "target\release\libavante_tokenizers.dll") (Join-Path $BuildDir $targetFile)
$targetTokenizerFile = "avante_tokenizers.dll"
$targetTemplatesFile = "avante_templates.dll"
Copy-Item (Join-Path "target\release\libavante_tokenizers.dll") (Join-Path $BuildDir $targetTokenizerFile)
Copy-Item (Join-Path "target\release\libavante_templates.dll") (Join-Path $BuildDir $targetTemplatesFile)

Remove-Item -Recurse -Force "target"
}
Expand Down
58 changes: 58 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ version = "0.1.0"

[workspace.dependencies]
avante-tokenizers = { path = "crates/avante-tokenizers" }
avante-templates = { path = "crates/avante-templates" }
minijinja = { version = "2.2.0", features = [
"loader",
"json",
"fuel",
"unicode",
"speedups",
"custom_syntax",
"loop_controls",
] }
mlua = { version = "0.10.0-beta.1", features = [
"module",
"serialize",
], git = "https://github.com/mlua-rs/mlua.git", branch = "main" }
tiktoken-rs = { version = "0.5.9" }
tokenizers = { version = "0.20.0", features = [
"esaxx_fast",
"http",
"unstable_wasm",
"onig",
], default-features = false }
serde = { version = "1.0.209", features = ["derive"] }

[workspace.lints.rust]
unsafe_code = "warn"
Expand Down
44 changes: 26 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,40 @@ else
$(error Unsupported operating system: $(UNAME))
endif

LUA_VERSIONS := luajit lua51
LUA_VERSIONS := luajit lua51 lua52 lua53 lua54

BUILD_DIR := build
TARGET_LIBRARY ?= all

all: luajit

luajit: $(BUILD_DIR)/libavante_tokenizers.$(EXT)
lua51: $(BUILD_DIR)/libavante_tokenizers-lua51.$(EXT)
lua52: $(BUILD_DIR)/libavante_tokenizers-lua52.$(EXT)
lua53: $(BUILD_DIR)/libavante_tokenizers-lua53.$(EXT)
lua54: $(BUILD_DIR)/libavante_tokenizers-lua54.$(EXT)
define make_definitions
ifeq ($(TARGET_LIBRARY), all)
$1: $(BUILD_DIR)/libAvanteTokenizers-$1.$(EXT) $(BUILD_DIR)/libAvanteTemplates-$1.$(EXT)
else ifeq ($(TARGET_LIBRARY), tokenizers)
$1: $(BUILD_DIR)/libAvanteTokenizers-$1.$(EXT)
else ifeq ($(TARGET_LIBRARY), templates)
$1: $(BUILD_DIR)/libAvanteTemplates-$1.$(EXT)
else
$$(error TARGET_LIBRARY must be one of all, tokenizers, templates)
endif
endef

$(foreach lua_version,$(LUA_VERSIONS),$(eval $(call make_definitions,$(lua_version))))

define build_from_source
cargo build --release --features=$1
cp target/release/libavante_tokenizers.$(EXT) $(BUILD_DIR)/avante_tokenizers.$(EXT)
cargo build --release --features=$1 -p avante-$2
cp target/release/libavante_$2.$(EXT) $(BUILD_DIR)/avante_$2.$(EXT)
endef

define build_targets
$(BUILD_DIR)/libAvanteTokenizers-$1.$(EXT): $(BUILD_DIR)
$$(call build_from_source,$1,tokenizers)
$(BUILD_DIR)/libAvanteTemplates-$1.$(EXT): $(BUILD_DIR)
$$(call build_from_source,$1,templates)
endef

$(BUILD_DIR)/libavante_tokenizers.$(EXT): $(BUILD_DIR)
$(call build_from_source,luajit)
$(BUILD_DIR)/libavante_tokenizers-lua51.$(EXT): $(BUILD_DIR)
$(call build_from_source,lua51)
$(BUILD_DIR)/libavante_tokenizers-lua52.$(EXT): $(BUILD_DIR)
$(call build_from_source,lua52)
$(BUILD_DIR)/libavante_tokenizers-lua53.$(EXT): $(BUILD_DIR)
$(call build_from_source,lua53)
$(BUILD_DIR)/libavante_tokenizers-lua54.$(EXT): $(BUILD_DIR)
$(call build_from_source,lua54)
$(foreach lua_version,$(LUA_VERSIONS),$(eval $(call build_targets,$(lua_version))))

$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
Expand Down
71 changes: 63 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ https://github.com/user-attachments/assets/86140bfd-08b4-483d-a887-1b701d9e37dd
opts = {
-- add any opts here
},
build = ":AvanteBuild", -- This is optional, recommended tho. Also note that this will block the startup for a bit since we are compiling bindings in Rust.
build = ":AvanteBuild", -- Also note that this will block the startup for a bit since we are compiling bindings in Rust.
dependencies = {
"stevearc/dressing.nvim",
"nvim-lua/plenary.nvim",
Expand Down Expand Up @@ -93,7 +93,7 @@ Plug 'yetone/avante.nvim', { 'branch': 'main', 'do': { -> avante#build() }, 'on'

> [!important]
>
> For `avante.tokenizers` to work, make sure to call `require('avante_lib').load()` somewhere when entering the editor.
> For `avante.tokenizers` and templates to work, make sure to call `require('avante_lib').load()` somewhere when entering the editor.
> We will leave the users to decide where it fits to do this, as this varies among configurations. (But we do recommend running this after where you set your colorscheme)
</details>
Expand Down Expand Up @@ -147,6 +147,7 @@ require('copilot').setup ({
require('render-markdown').setup ({
-- use recommended settings from above
})
require('avante_lib').load()
require('avante').setup ({
-- Your config here!
})
Expand Down Expand Up @@ -341,14 +342,67 @@ The following key bindings are available for use with `avante.nvim`:
See [highlights.lua](./lua/avante/highlights.lua) for more information
## Custom prompts
By default, `avante.nvim` provides three different modes to interact with: `planning`, `editing`, and `suggesting`, followed with three different prompts per mode.
- `planning`: Used with `require("avante").toggle()` on sidebar
- `editing`: Used with `require("avante").edit()` on selection codeblock
- `suggesting`: Used with `require("avante").get_suggestion():suggest()` on Tab flow.
Users can customize the system prompts via `Config.system_prompt`. We recommend calling this in a custom Autocmds depending on your need:
```lua
vim.api.nvim_create_autocmd("User", {
pattern = "ToggleMyPrompt"
callback = function() require("avante.config").override({system_prompt = "MY CUSTOM SYSTEM PROMPT"}) end,
})
vim.keymap.set("n", "<leader>am", function() vim.api.nvim_exec_autocmds("User", { pattern = "ToggleMyPrompt" }) end, { desc = "avante: toggle my prompt" })
```
If one wish to custom prompts for each mode, `avante.nvim` will check for project root based on the given buffer whether it contains
the following patterns: `*.{mode}.avanterules`.
The rules for root hierarchy:
- lsp workspace folders
- lsp root_dir
- root pattern of filename of the current buffer
- root pattern of cwd
<details>
<summary>Example folder structure for custom prompt</summary>
If you have the following structure:
```bash
.
├── .git/
├── typescript.planning.avanterules
├── snippets.editing.avanterules
└── src/
```
- `typescript.planning.avanterules` will be used for `planning` mode
- `snippets.editing.avanterules`` will be used for `editing` mode
- the default `suggesting` prompt from `avante.nvim` will be used for `suggesting` mode.
</details>
> [!important]
>
> `*.avanterules` is a jinja template file, in which will be rendered using [minijinja](https://github.com/mitsuhiko/minijinja). See [templates](https://github.com/yetone/avante.nvim/blob/main/lua/avante/templates) for example on how to extend current templates.
## TODOs
- [x] Chat with current file
- [x] Apply diff patch
- [x] Chat with the selected block
- [x] Slash commands
- [x] Edit the selected block
- [ ] Smart Tab (Cursor Flow)
- [x] Smart Tab (Cursor Flow)
- [ ] Chat with project
- [ ] Chat with selected files
Expand All @@ -367,12 +421,13 @@ See [wiki](https://github.com/yetone/avante.nvim/wiki) for more recipes and tric
We would like to express our heartfelt gratitude to the contributors of the following open-source projects, whose code has provided invaluable inspiration and reference for the development of avante.nvim:
| Nvim Plugin | License | Functionality | Where did we use |
| Nvim Plugin | License | Functionality | Location |
| --- | --- | --- | --- |
| [git-conflict.nvim](https://github.com/akinsho/git-conflict.nvim) | No License | Diff comparison functionality | https://github.com/yetone/avante.nvim/blob/main/lua/avante/diff.lua |
| [ChatGPT.nvim](https://github.com/jackMort/ChatGPT.nvim) | Apache 2.0 License | Calculation of tokens count | https://github.com/yetone/avante.nvim/blob/main/lua/avante/utils/tokens.lua |
| [img-clip.nvim](https://github.com/HakonHarnes/img-clip.nvim) | MIT License | Clipboard image support | https://github.com/yetone/avante.nvim/blob/main/lua/avante/clipboard.lua |
| [copilot.lua](https://github.com/zbirenbaum/copilot.lua) | MIT License | Copilot support | https://github.com/yetone/avante.nvim/blob/main/lua/avante/providers/copilot.lua |
| [git-conflict.nvim](https://github.com/akinsho/git-conflict.nvim) | No License | Diff comparison functionality | [lua/avante/diff.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/diff.lua) |
| [ChatGPT.nvim](https://github.com/jackMort/ChatGPT.nvim) | Apache 2.0 License | Calculation of tokens count | [avante/utils/tokens.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/utils/tokens.lua) |
| [img-clip.nvim](https://github.com/HakonHarnes/img-clip.nvim) | MIT License | Clipboard image support | [avante/clipboard.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/clipboard.lua) |
| [copilot.lua](https://github.com/zbirenbaum/copilot.lua) | MIT License | Copilot support | [avante/providers/copilot.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/providers/copilot.lua) |
| [jinja.vim](https://github.com/HiPhish/jinja.vim) | MIT License | Template filetype support | [syntax/jinja.vim](https://github.com/yetone/avante.nvim/blob/main/syntax/jinja.vim) |
The high quality and ingenuity of these projects' source code have been immensely beneficial throughout our development process. We extend our sincere thanks and respect to the authors and contributors of these projects. It is the selfless dedication of the open-source community that drives projects like avante.nvim forward.
Expand Down
24 changes: 24 additions & 0 deletions crates/avante-templates/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[lib]
crate-type = ["cdylib"]

[package]
name = "avante-templates"
edition.workspace = true
rust-version.workspace = true
license.workspace = true
version.workspace = true

[dependencies]
mlua = { workspace = true }
minijinja = { workspace = true }
serde = { workspace = true, features = ["derive"] }

[lints]
workspace = true

[features]
lua51 = ["mlua/lua51"]
lua52 = ["mlua/lua52"]
lua53 = ["mlua/lua53"]
lua54 = ["mlua/lua54"]
luajit = ["mlua/luajit"]
Loading

0 comments on commit 4ad9134

Please sign in to comment.