Skip to content

Commit

Permalink
feat!: use queries for determining context (PLEASE READ)
Browse files Browse the repository at this point in the history
This plugin has been significantly rewritten to use Treesitter
queries instead of patterns for determining context regions for
languages.

The main benefits of this change are:
- it is a much simpler implementation since we can leverage core APIs.
- it fits in more generally with the Treesitter eco-system.
- it allows configuration of contexts to be provided from multiples sources.
- it allows more sophisticated configuration of contexts since queries
  (with directives and predicates) are much more powerful than patterns.
- in theory the query format should be usuable for other editors.

The major downside of this new implementation is that it requires each
language to provide it's own query as opposed to using the general
purpose patterns. This means that some languages which had contexts
before may not have them now. If this is the case then please raise an
issue. Adding queries for a specific language is fairly simple but too
much work to implement for all 170+ parsers that exist.

This commit also drops explicit support for Nvim 0.7. If you still need
support for this version then you can use the `compat/0.7` release.
  • Loading branch information
lewis6991 committed Mar 3, 2023
1 parent 895ec44 commit 214c428
Show file tree
Hide file tree
Showing 24 changed files with 1,103 additions and 507 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: true
matrix:
neovim_branch: ['v0.7.0']
neovim_branch: ['v0.8.2']
runs-on: ubuntu-latest
env:
NEOVIM_BRANCH: ${{ matrix.neovim_branch }}
Expand Down
17 changes: 6 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,21 @@ $(NEOVIM):
nvim-treesitter:
git clone --depth 1 https://github.com/nvim-treesitter/nvim-treesitter

nvim-treesitter/parser/lua.so: nvim-treesitter $(NEOVIM)
nvim-treesitter/parser/%.so: nvim-treesitter $(NEOVIM)
VIMRUNTIME=$(NEOVIM)/runtime $(NEOVIM)/build/bin/nvim \
--headless \
--clean \
--cmd 'set rtp+=./nvim-treesitter' \
-c "TSInstallSync lua" \
-c "q"

nvim-treesitter/parser/rust.so: nvim-treesitter $(NEOVIM)
VIMRUNTIME=$(NEOVIM)/runtime $(NEOVIM)/build/bin/nvim \
--headless \
--clean \
--cmd 'set rtp+=./nvim-treesitter' \
-c "TSInstallSync rust" \
-c "TSInstallSync $*" \
-c "q"

export VIMRUNTIME=$(PWD)/$(NEOVIM)/runtime

.PHONY: test
test: $(NEOVIM) nvim-treesitter nvim-treesitter/parser/lua.so nvim-treesitter/parser/rust.so
test: $(NEOVIM) nvim-treesitter \
nvim-treesitter/parser/lua.so \
nvim-treesitter/parser/rust.so \
nvim-treesitter/parser/typescript.so
$(NEOVIM)/.deps/usr/bin/busted \
-v \
--lazy \
Expand Down
271 changes: 177 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ implemented with [nvim-treesitter](https://github.com/nvim-treesitter/nvim-trees

## Requirements

Neovim >= v0.7.x
Neovim >= v0.8.2

Note: if you need support for Neovim 0.6.x please use the tag `compat/0.6`.

Expand All @@ -29,106 +29,189 @@ use 'nvim-treesitter/nvim-treesitter-context'

![theme](./static/demo.gif)

### Notes

This plugins uses the new neovim `WinScrolled` event when available to update its
context window. Make sure to have a recent neovim build to get this behavior. The fallback
behavior is to update its content on `CursorMoved`.
## Supported languages

- [x] `bash`
- [x] `c`
- [x] `cpp`
- [x] `typescript`
- [x] `rust`
- [x] `json`
- [x] `lua`
- [x] `markdown`
- [x] `python`
- [x] `teal`
- [x] `yaml`
- [x] `php`
- [x] `scala`
- [x] `teal`
- [x] `toml`
- [x] `vim`
- [ ] `ada`
- [ ] `agda`
- [ ] `arduino`
- [ ] `astro`
- [ ] `beancount`
- [ ] `bibtex`
- [ ] `bicep`
- [ ] `blueprint`
- [ ] `c_sharp`
- [ ] `capnp`
- [ ] `chatito`
- [ ] `clojure`
- [ ] `cmake`
- [ ] `commonlisp`
- [ ] `cooklang`
- [ ] `cpon`
- [ ] `css`
- [ ] `cuda`
- [ ] `d`
- [ ] `dart`
- [ ] `devicetree`
- [ ] `dhall`
- [ ] `dockerfile`
- [ ] `dot`
- [ ] `ebnf`
- [ ] `ecma`
- [ ] `eex`
- [ ] `elixir`
- [ ] `elm`
- [ ] `elsa`
- [ ] `elvish`
- [ ] `embedded_template`
- [ ] `erlang`
- [ ] `fennel`
- [ ] `fish`
- [ ] `foam`
- [ ] `fsh`
- [ ] `func`
- [ ] `fusion`
- [ ] `gdscript`
- [ ] `git_rebase`
- [ ] `gleam`
- [ ] `glimmer`
- [ ] `glsl`
- [ ] `go`
- [ ] `godot_resource`
- [ ] `gomod`
- [ ] `gosum`
- [ ] `gowork`
- [ ] `graphql`
- [ ] `hack`
- [ ] `haskell`
- [ ] `hcl`
- [ ] `heex`
- [ ] `hjson`
- [ ] `hlsl`
- [ ] `hocon`
- [ ] `html`
- [ ] `html_tags`
- [ ] `htmldjango`
- [ ] `http`
- [ ] `ini`
- [ ] `java`
- [ ] `javascript`
- [ ] `jq`
- [ ] `jsdoc`
- [ ] `json5`
- [ ] `jsonc`
- [ ] `jsonnet`
- [ ] `jsx`
- [ ] `julia`
- [ ] `kdl`
- [ ] `kotlin`
- [ ] `lalrpop`
- [ ] `latex`
- [ ] `ledger`
- [ ] `llvm`
- [ ] `m68k`
- [ ] `make`
- [ ] `matlab`
- [ ] `menhir`
- [ ] `mermaid`
- [ ] `meson`
- [ ] `nickel`
- [ ] `ninja`
- [ ] `nix`
- [ ] `ocaml`
- [ ] `ocaml_interface`
- [ ] `ocamllex`
- [ ] `pascal`
- [ ] `passwd`
- [ ] `perl`
- [ ] `phpdoc`
- [ ] `pioasm`
- [ ] `po`
- [ ] `poe_filter`
- [ ] `prisma`
- [ ] `proto`
- [ ] `prql`
- [ ] `pug`
- [ ] `ql`
- [ ] `qmldir`
- [ ] `qmljs`
- [ ] `query`
- [ ] `r`
- [ ] `racket`
- [ ] `rasi`
- [ ] `regex`
- [ ] `rego`
- [ ] `rnoweb`
- [ ] `ron`
- [ ] `rst`
- [ ] `ruby`
- [ ] `scheme`
- [ ] `scss`
- [ ] `slint`
- [ ] `smali`
- [ ] `smithy`
- [ ] `solidity`
- [ ] `sparql`
- [ ] `sql`
- [ ] `starlark`
- [ ] `supercollider`
- [ ] `surface`
- [ ] `svelte`
- [ ] `swift`
- [ ] `sxhkdrc`
- [ ] `t32`
- [ ] `terraform`
- [ ] `thrift`
- [ ] `tiger`
- [ ] `tlaplus`
- [ ] `todotxt`
- [ ] `tsx`
- [ ] `turtle`
- [ ] `twig`
- [ ] `ungrammar`
- [ ] `v`
- [ ] `vala`
- [ ] `verilog`
- [ ] `vhs`
- [ ] `vue`
- [ ] `wgsl`
- [ ] `wgsl_bevy`
- [ ] `yang`
- [ ] `yuck`
- [ ] `zig`

## Configuration

(Default values are shown below)

```lua
require'treesitter-context'.setup{
enable = true, -- Enable this plugin (Can be enabled/disabled later via commands)
max_lines = 0, -- How many lines the window should span. Values <= 0 mean no limit.
trim_scope = 'outer', -- Which context lines to discard if `max_lines` is exceeded. Choices: 'inner', 'outer'
min_window_height = 0, -- Minimum editor window height to enable context. Values <= 0 mean no limit.
patterns = { -- Match patterns for TS nodes. These get wrapped to match at word boundaries.
-- For all filetypes
-- Note that setting an entry here replaces all other patterns for this entry.
-- By setting the 'default' entry below, you can control which nodes you want to
-- appear in the context window.
default = {
'class',
'function',
'method',
'for',
'while',
'if',
'switch',
'case',
'interface',
'struct',
'enum',
},
-- Patterns for specific filetypes
-- If a pattern is missing, *open a PR* so everyone can benefit.
tex = {
'chapter',
'section',
'subsection',
'subsubsection',
},
haskell = {
'adt'
},
rust = {
'impl_item',

},
terraform = {
'block',
'object_elem',
'attribute',
},
scala = {
'object_definition',
},
vhdl = {
'process_statement',
'architecture_body',
'entity_declaration',
},
markdown = {
'section',
},
elixir = {
'anonymous_function',
'arguments',
'block',
'do_block',
'list',
'map',
'tuple',
'quoted_content',
},
json = {
'pair',
},
typescript = {
'export_statement',
},
yaml = {
'block_mapping_pair',
},
},
exact_patterns = {
-- Example for a specific filetype with Lua patterns
-- Treat patterns.rust as a Lua pattern (i.e "^impl_item$" will
-- exactly match "impl_item" only)
-- rust = true,
},

-- [!] The options below are exposed but shouldn't require your attention,
-- you can safely ignore them.

zindex = 20, -- The Z-index of the context window
mode = 'cursor', -- Line used to calculate context. Choices: 'cursor', 'topline'
-- Separator between context and content. Should be a single character string, like '-'.
-- When separator is set, the context will only show up when there are at least 2 lines above cursorline.
separator = nil,
enable = true, -- Enable this plugin (Can be enabled/disabled later via commands)
max_lines = 0, -- How many lines the window should span. Values <= 0 mean no limit.
min_window_height = 0, -- Minimum editor window height to enable context. Values <= 0 mean no limit.
line_numbers = true,
multiline_threshold = 20, -- Maximum number of lines to collapse for a single context line
trim_scope = 'outer', -- Which context lines to discard if `max_lines` is exceeded. Choices: 'inner', 'outer'
mode = 'cursor', -- Line used to calculate context. Choices: 'cursor', 'topline'
-- Separator between context and content. Should be a single character string, like '-'.
-- When separator is set, the context will only show up when there are at least 2 lines above cursorline.
separator = nil,
zindex = 20, -- The Z-index of the context window
}
```

Expand Down
Loading

0 comments on commit 214c428

Please sign in to comment.