Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Add missing tree-sitter navigation directions.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadronized committed Apr 23, 2024
1 parent 6e46a5e commit 258a31a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
37 changes: 29 additions & 8 deletions kak-tree-sitter/rc/text-objects.kak
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ map global tree-sitter F ':enter-user-mode tree-sitter-find-extend<ret>'
map global tree-sitter <a-F> ':enter-user-mode tree-sitter-find-extend-rev<ret>' -docstring 'find(extend) prev'
map global tree-sitter s ":kak-tree-sitter-req-nav '""parent""'<ret>" -docstring 'select parent'
map global tree-sitter t ":kak-tree-sitter-req-nav '""first_child""'<ret>" -docstring 'select first child'
map global tree-sitter <c-t> ":kak-tree-sitter-req-nav '""last_child""'<ret>" -docstring 'select last child'
map global tree-sitter c ":kak-tree-sitter-req-nav '{ ""prev_sibling"": { ""cousin"": false } }'<ret>" -docstring 'select previous sibling'
map global tree-sitter r ":kak-tree-sitter-req-nav '{ ""next_sibling"": { ""cousin"": false } }'<ret>" -docstring 'select next sibling'
map global tree-sitter C ":kak-tree-sitter-req-nav '{ ""prev_sibling"": { ""cousin"": true } }'<ret>" -docstring 'select previous sibling (cousin)'
map global tree-sitter R ":kak-tree-sitter-req-nav '{ ""next_sibling"": { ""cousin"": true } }'<ret>" -docstring 'select next sibling (cousin)'
map global tree-sitter ( ":kak-tree-sitter-req-nav '""first_sibling""'<ret>" -docstring 'select first sibling'
map global tree-sitter ) ":kak-tree-sitter-req-nav '""last_sibling""'<ret>" -docstring 'select last sibling'
map global tree-sitter T ':enter-user-mode tree-sitter-nav-sticky<ret>' -docstring 'sticky tree navigation'

map global tree-sitter-search f ':kak-tree-sitter-req-text-objects function.around search_next<ret>' -docstring 'function'
Expand Down Expand Up @@ -83,11 +86,16 @@ define-command -hidden kak-tree-sitter-nav-sticky-parent %{
enter-user-mode tree-sitter-nav-sticky
}

define-command -hidden kak-tree-sitter-nav-sticky-1st-child %{
define-command -hidden kak-tree-sitter-nav-sticky-first-child %{
kak-tree-sitter-req-nav '"first_child"'
enter-user-mode tree-sitter-nav-sticky
}

define-command -hidden kak-tree-sitter-nav-sticky-last-child %{
kak-tree-sitter-req-nav '"last_child"'
enter-user-mode tree-sitter-nav-sticky
}

define-command -hidden kak-tree-sitter-nav-sticky-prev-sibling -params 1 %{
kak-tree-sitter-req-nav "{ ""prev_sibling"": { ""cousin"": %arg{1} }}"
enter-user-mode tree-sitter-nav-sticky
Expand All @@ -98,10 +106,23 @@ define-command -hidden kak-tree-sitter-nav-sticky-next-sibling -params 1 %{
enter-user-mode tree-sitter-nav-sticky
}

map global tree-sitter-nav-sticky s ':kak-tree-sitter-nav-sticky-parent<ret>' -docstring 'select parent'
map global tree-sitter-nav-sticky t ':kak-tree-sitter-nav-sticky-1st-child<ret>' -docstring 'select first child'
map global tree-sitter-nav-sticky C ':kak-tree-sitter-nav-sticky-prev-sibling true<ret>' -docstring 'select previous sibling (cousin)'
map global tree-sitter-nav-sticky R ':kak-tree-sitter-nav-sticky-next-sibling true<ret>' -docstring 'select next sibling (cousin)'
map global tree-sitter-nav-sticky c ':kak-tree-sitter-nav-sticky-prev-sibling false<ret>' -docstring 'select previous sibling'
map global tree-sitter-nav-sticky r ':kak-tree-sitter-nav-sticky-next-sibling false<ret>' -docstring 'select next sibling'
map global tree-sitter-nav-sticky u ':kak-tree-sitter-nav-sticky-undo<ret>' -docstring 'undo selection'
define-command -hidden kak-tree-sitter-nav-sticky-first-sibling %{
kak-tree-sitter-req-nav '"first_sibling"'
enter-user-mode tree-sitter-nav-sticky
}

define-command -hidden kak-tree-sitter-nav-sticky-last-sibling %{
kak-tree-sitter-req-nav '"last_sibling"'
enter-user-mode tree-sitter-nav-sticky
}

map global tree-sitter-nav-sticky s ':kak-tree-sitter-nav-sticky-parent<ret>' -docstring 'select parent'
map global tree-sitter-nav-sticky t ':kak-tree-sitter-nav-sticky-first-child<ret>' -docstring 'select first child'
map global tree-sitter-nav-sticky <c-t> ':kak-tree-sitter-nav-sticky-first-child<ret>' -docstring 'select last child'
map global tree-sitter-nav-sticky C ':kak-tree-sitter-nav-sticky-prev-sibling true<ret>' -docstring 'select previous sibling (cousin)'
map global tree-sitter-nav-sticky R ':kak-tree-sitter-nav-sticky-next-sibling true<ret>' -docstring 'select next sibling (cousin)'
map global tree-sitter-nav-sticky c ':kak-tree-sitter-nav-sticky-prev-sibling false<ret>' -docstring 'select previous sibling'
map global tree-sitter-nav-sticky r ':kak-tree-sitter-nav-sticky-next-sibling false<ret>' -docstring 'select next sibling'
map global tree-sitter-nav-sticky ( ':kak-tree-sitter-nav-sticky-first-sibling<ret>' -docstring 'select first sibling'
map global tree-sitter-nav-sticky ) ':kak-tree-sitter-nav-sticky-last-sibling<ret>' -docstring 'select last sibling'
map global tree-sitter-nav-sticky u ':kak-tree-sitter-nav-sticky-undo<ret>' -docstring 'undo selection'
12 changes: 6 additions & 6 deletions kak-tree-sitter/src/nav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ pub enum Dir {
/// First child of the current node, if any.
FirstChild,

// NOTE: currently not available due to tree-sitter version
// /// Last child of the current node, if any.
// LastChild,
// Last child of the current node, if any.
LastChild,

/// First sibling of the current node, if any.
FirstSibling,

// NOTE: currently not available due to tree-sitter version
// /// Last sibling of the current node if any.
// LastSibling,
/// Last sibling of the current node if any.
LastSibling,

/// Previous sibiling of the current node, if any.
PrevSibling {
/// Should we take cousins into account?
Expand Down
12 changes: 11 additions & 1 deletion kak-tree-sitter/src/tree_sitter_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,17 @@ impl TreeState {
let res = match dir {
Dir::Parent => node.parent(),
Dir::FirstChild => node.child(0),
Dir::FirstSibling => node.parent().and_then(|node| node.child(0)),
Dir::LastChild => node
.child_count()
.checked_sub(1)
.and_then(|i| node.child(i)),
Dir::FirstSibling => node.parent().and_then(|parent| parent.child(0)),
Dir::LastSibling => node.parent().and_then(|parent| {
parent
.child_count()
.checked_sub(1)
.and_then(|i| parent.child(i))
}),
Dir::PrevSibling { cousin } if cousin => Self::find_prev_sibling_or_cousin(&node),
Dir::NextSibling { cousin } if cousin => Self::find_next_sibling_or_cousin(&node),
Dir::PrevSibling { .. } => node.prev_sibling(),
Expand Down

0 comments on commit 258a31a

Please sign in to comment.