Skip to content

Commit

Permalink
Max out highlight work
Browse files Browse the repository at this point in the history
* Add more fixtures / improve existing ones
  * These are really helpful for testing out the plugin while it's being
    written
* Use row checks rather than starts for coincident nodes
* Add more blacklist matchers
  * This here is the bummer. But since it's just the highlighting, with
    these the plugin is behaving more correctly than without. So maybe
    some day these can be sunset, but for now, they live on.
  • Loading branch information
aaronik committed Dec 16, 2024
1 parent 079f9f2 commit 60b6ff3
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 278 deletions.
27 changes: 20 additions & 7 deletions lua/treewalker/nodes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ local TARGET_BLACKLIST_TYPE_MATCHERS = {
}

local HIGHLIGHT_BLACKLIST_TYPE_MATCHERS = {
"chunk",
"body",
"block",
"program",
"module", -- python
"chunk", -- lua
"body", -- ruby
"block", -- ruby
"program", -- ruby
"haskell", -- guess which language starts their module tree with this node
"translation_unit", -- c module
"source_file", -- rust
}


Expand Down Expand Up @@ -52,6 +56,14 @@ function M.have_same_start(node1, node2)
scol1 == scol2
end

---Do the nodes have the same starting row
---@param node1 TSNode
---@param node2 TSNode
---@return boolean
function M.have_same_row(node1, node2)
return M.get_row(node1) == M.get_row(node2)
end

---Do the nodes have the same level of indentation
---@param node1 TSNode
---@param node2 TSNode
Expand Down Expand Up @@ -109,12 +121,13 @@ function M.get_descendants(node)
return descendants
end

-- Get farthest ancestor (or self) at the same starting coordinates
-- Get farthest ancestor (or self) at the same starting row
---@param node TSNode
---@return TSNode
function M.get_farthest_ancestor_with_same_srow(node)
function M.get_highest_coincident(node)
local parent = node:parent()
while parent and M.have_same_start(node, parent) do
-- prefer row over start on account of lisps / S-expressions, which start with (identifier, ..)
while parent and M.have_same_row(node, parent) do
if M.is_highlight_target(parent) then node = parent end
parent = parent:parent()
end
Expand Down
9 changes: 1 addition & 8 deletions lua/treewalker/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ end

local M = {}

---set cursor without throwing error
---@param row integer
---@param col integer
function M.safe_set_cursor(row, col)
pcall(vim.api.nvim_win_set_cursor, 0, { row, col }) -- catch any errors in nvim_win_set_cursor
end

---Flash a highlight over the given range
---@param range Range4
function M.highlight(range)
Expand Down Expand Up @@ -63,7 +56,7 @@ function M.jump(row, node)
vim.api.nvim_win_set_cursor(0, { row, 0 })
vim.cmd('normal! ^')
if require("treewalker").opts.highlight then
node = nodes.get_farthest_ancestor_with_same_srow(node)
node = nodes.get_highest_coincident(node)
M.highlight(nodes.range(node))
end
end
Expand Down
57 changes: 57 additions & 0 deletions tests/fixtures/c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>

// Structure to represent an account
typedef struct {
int accountNumber;
float balance;
} Account;

// Function to create a new account
Account* createAccount(int accountNumber, float initialBalance) {
Account* newAccount = (Account*)malloc(sizeof(Account));
if (!newAccount) {
printf("Memory error\n");
return NULL;
}
newAccount->accountNumber = accountNumber;
newAccount->balance = initialBalance;
return newAccount;
}

// Function to deposit money into an account
void deposit(Account* account, float amount) {
if (amount > 0.0f) {
account->balance += amount;
printf("Deposited $%.2f into account %d\n", amount, account->accountNumber);
} else {
printf("Invalid deposit amount: $%.2f\n", amount);
}
}

// Function to withdraw money from an account
void withdraw(Account* account, float amount) {
if (amount > 0.0f && amount <= account->balance) {
account->balance -= amount;
printf("Withdrawn $%.2f from account %d\n", amount, account->accountNumber);
} else {
printf("Invalid withdrawal amount: $%.2f\n", amount);
}
}

// Function to display account information
void printAccountInfo(Account* account) {
printf("Account Number: %d\nBalance: $%.2f\n", account->accountNumber, account->balance);
}

int main() {
Account* account = createAccount(12345, 1000.00f);

deposit(account, 500.00f);
withdraw(account, 200.00f);
printAccountInfo(account);

free(account);
return 0;
}

51 changes: 44 additions & 7 deletions tests/fixtures/haskell.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
import Data.List
-- Import the necessary modules
import Data.List (sort)
import Control.Monad (replicateM)

permutations :: [a] -> [[a]]
permutations [] = [[]]
permutations xs = do
x <- xs
xsRest <- permutations $ filter (/=x) xs
return $ map (x:) xsRest
-- Define a function to print out all even numbers in a list
printEvens :: [Int] -> IO ()
printEvens [] = return ()
printEvens (x : xs)
| x `mod` 2 == 0 = putStrLn (show x) >> printEvens xs
| otherwise = printEvens xs

-- Define a function to calculate the sum of all numbers in a list
sumNumbers :: [Int] -> Int
sumNumbers [] = 0
sumNumbers (x : xs) = x + sumNumbers xs

-- Define a function to generate a random list of numbers
randomList :: IO [Int]
randomList = do
n <- getLine
let n' = read n :: Int
replicateM n (getRandomR (-100, 100)) >>= return . sort

-- Define a function to calculate the median of a list of numbers
median :: [Double] -> Double
median xs = median' (sort xs)
where
median' [] = error "Empty list"
median' [_] = error "List contains single element"
median' xs
| odd len = fromIntegral $ xs !! (len `div` 2)
| otherwise = mean
where
len = length xs
mean = (sum xs) / fromIntegral len

-- Main function to run the program
main :: IO ()
main = do
printEvens [1, 3, 5, 7, 9]
print $ sumNumbers [-2, -4, 0, 10]
randomList >>= mapM_ putStrLn . map show
let xs = [-3.0, -1.0, 0.0, 1.0, 3.0]
ys = [5.5, 6.6]
print $ median xs
print $ sumNumbers ys
3 changes: 3 additions & 0 deletions tests/fixtures/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,6 @@ function M.get_node()
end

return M

-- This is a lua fixture. I thought I was being smart when I got it
-- from this plugin. But actually I was being dumb, and this is very confusing, l0lz
24 changes: 24 additions & 0 deletions tests/fixtures/python.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
class Person:
def __init__(self, name):
self.name = name

def greet(self):
print(f"Hello, my name is {self.name}!")

class Book:
def __init__(self, title, author):
self.title = title
self.author = author

def describe(self):
print(f"{self.title} by {self.author}")

class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

def display_info(self):
print(f"Make: {self.make}, Model: {self.model}, Year: {self.year}")

def main():
"""
This function demonstrates a nested structure.
Expand Down
Loading

0 comments on commit 60b6ff3

Please sign in to comment.