From 0ed32ee9206155a896aa302be7f0c56f2269ff09 Mon Sep 17 00:00:00 2001 From: Brian Hicks Date: Wed, 23 Jun 2021 10:29:09 -0500 Subject: [PATCH] add an outline jumper for Rust --- dotfiles/kakoune.nix | 2 + pkgs/kak-tree-grepper/rc/kak-tree-grepper.kak | 24 ++++++++++++ sample-projects/rust/src/main.rs | 37 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 sample-projects/rust/src/main.rs diff --git a/dotfiles/kakoune.nix b/dotfiles/kakoune.nix index 86fbd1a..0f97417 100644 --- a/dotfiles/kakoune.nix +++ b/dotfiles/kakoune.nix @@ -297,6 +297,8 @@ in { echo 'hook buffer BufWritePre .* format' fi } + + map buffer normal ': outline-jump-rust' } hook global WinSetOption filetype=ruby %{ diff --git a/pkgs/kak-tree-grepper/rc/kak-tree-grepper.kak b/pkgs/kak-tree-grepper/rc/kak-tree-grepper.kak index 94c753f..37cafca 100644 --- a/pkgs/kak-tree-grepper/rc/kak-tree-grepper.kak +++ b/pkgs/kak-tree-grepper/rc/kak-tree-grepper.kak @@ -74,3 +74,27 @@ define-command -override -docstring "jump somewhere in an Haskell file's definit printf "evaluate-commands -client %s edit %s\n" "$CLIENT" "$EDIT_LOCATION" | indiekak -p "$SESSION" } -- %opt{tree_grepper_path} %opt{tree_grepper_fzf_path} %val{bufname} %arg{1} %val{client} %val{session} } + +define-command -override -docstring "jump somewhere in an Rust file's definition outline" -params 0..1 outline-jump-rust %{ + tmux-terminal-horizontal sh -c %{ + set -euo pipefail + + # what tools do we have available? + TREE_GREPPER=${1:-tree-grepper} + FZF=${2:-fzf} + + # what do we care about? + FILE=$3 + FZF_QUERY=$4 + + # where do we return results? + CLIENT=$5 + SESSION=$6 + + # do the magic! + QUERY="(use_declaration)@use (function_item _ (_) @function) (struct_item (type_identifier) @out) (field_declaration)@field (parameter) (let_declaration)@let (enum_item (type_identifier)@enum) (enum_variant)" + + EDIT_LOCATION="$("$TREE_GREPPER" --language rust "$QUERY" "$FILE" | fzf --with-nth 4,5 --nth 2,1 --delimiter=: --query "$FZF_QUERY" --select-1 | cut -d : -f 1-3 | tr : ' ')" + printf "evaluate-commands -client %s edit %s\n" "$CLIENT" "$EDIT_LOCATION" | indiekak -p "$SESSION" + } -- %opt{tree_grepper_path} %opt{tree_grepper_fzf_path} %val{bufname} %arg{1} %val{client} %val{session} +} diff --git a/sample-projects/rust/src/main.rs b/sample-projects/rust/src/main.rs new file mode 100644 index 0000000..42337a9 --- /dev/null +++ b/sample-projects/rust/src/main.rs @@ -0,0 +1,37 @@ +use std::fs; +use std::path::PathBuf; + +fn main() { + println!("Hello, World!") +} + +struct Foo { + fieldName: String, + count: u8, +} + +impl Foo { + pub fn new(bar: String) -> Foo { + Foo { + fieldName: bar, + count: 0 + } + } + + pub fn boop(&mut self) { + let to_add = 1; + self.count += to_add + } +} + +impl Drop for Foo { + fn drop(&mut self) { + println!("Dropping foo!") + } +} + +enum MyOption { + TheresNothingHereMan, + WhoahTheresAThing(T) +} +