Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add motions and textobjects #21

Merged
merged 11 commits into from
Feb 2, 2025
Merged

Conversation

hat0uma
Copy link
Owner

@hat0uma hat0uma commented Jan 18, 2025

This PR introduces new motion- and textobject-related APIs designed to enhance CSV navigation and selection in csvview.nvim.
The two primary additions are:

  1. A flexible field() function in jump.lua, enabling cursor movements based on CSV row/column coordinates (both relative and absolute).
  2. A convenient field() textobject in textobject.lua, allowing you to select the current CSV field (with optional delimiter inclusion).

Additionally, several supporting changes are included:

  • New jump-based APIs in jump.lua (e.g., next_field_end, prev_field_end, etc.) that behave like Vim's native motions (w, b, e, ge) but specifically for CSV fields.
  • A new get_cursor() function in util.lua that retrieves the cursor’s position with CSV-awareness (i.e., which row/column in the CSV, whether on a delimiter, etc.).

1. field() in jump.lua

-- Basic usage:
require("csvview.jump").field(0, {
  pos = { 1, 2 },      -- Move to row=1, column=2
  mode = "absolute",   -- "relative" or "absolute"
  anchor = "start",    -- "start" or "end" of the field
  col_wrap = true,     -- Wrap horizontally to next/previous row if needed
})
  • Relative Movements (default): pos = { row_offset, col_offset } from the current field.
  • Absolute Movements: mode = "absolute" interprets pos as 1-based row/column.
  • Anchor: If "start", place the cursor at field start; "end" jumps to field end.
  • Column Wrapping: Wrap to the previous/next row when exceeding column bounds.

For convenience, there are also shorthand motions such as:

next_field_start(bufnr?) -- like `w` motion.
prev_field_start(bufnr?) -- like `b` motion.
next_field_end(bufnr?) -- like `e` motion.
prev_field_end(bufnr?) -- like `ge` motion.

2. field() textobject in textobject.lua

-- Basic usage in an operator-pending or visual context:
require("csvview.textobject").field(0, {
  include_delimiter = false
})
  • Select Current Field: Selects the entire CSV field under the cursor.
  • Delimiter Inclusion: Set include_delimiter = true if you want to include the trailing or preceding comma in your selection.

References: #16

- Created `get_cursor` function to retrieve cursor
  information for the current position in the buffer.
- The `get_cursor` function checks if the current line is a comment or
  a valid CSV row and returns cursor information including CSV row/column
  and an "anchor" state.
- Added `is_valid_row` method to check if the row index is valid.
- Added `is_empty_field` method to check if the column is empty.
- Added `is_last_col` method to check if the cursor is at the last column.
- Added `col_idx_to_byte` method to get byte offset from column index.
- Added `byte_to_col_idx` method to get column index from byte position.
- Implemented motion module with functions for navigating CSV fields.
- Added textobject module to select current field in visual mode.
- Included utility functions for clamping values and checking row validity.
- Enhanced cursor movement with options for relative and absolute positioning.
- Improved user experience with customizable jump options.
Add unit tests for the textobject module in csvview. These tests
cover various scenarios including selecting fields with and without
delimiters, handling comments, and empty lines.
Add tests for the motion module to ensure correct cursor movements
within CSV files. These tests cover various scenarios including
relative and absolute positioning, skipping comments, and handling
empty lines.
The previous implementation had an issue where the cursor would not
correctly jump to the previous end of the field. This change ensures
that the cursor moves accurately by adjusting the column increases
appropriately.
@hat0uma hat0uma force-pushed the feat/add-motions-and-textobjects branch from cedcec2 to b36f251 Compare January 29, 2025 16:58
- Added keymaps and actions support to csvview.
- Renamed motion.lua to jump.lua for better clarity.
- Updated tests to reflect the changes in function names.
- Added new fields to `CsvView.Options` class.
- Updated `CsvView.Options` to `CsvView.InternalOptions` in various functions and classes.
Updated README.md to include new keymaps for text objects and
Excel-like navigation. Added detailed configuration options and
examples for better customization and usage.
@hat0uma hat0uma marked this pull request as ready for review February 2, 2025 16:38
@hat0uma hat0uma force-pushed the feat/add-motions-and-textobjects branch 3 times, most recently from f2ec35f to 3dd22d3 Compare February 2, 2025 18:59
@hat0uma hat0uma force-pushed the feat/add-motions-and-textobjects branch from 3dd22d3 to 70e0410 Compare February 2, 2025 19:14
@hat0uma hat0uma merged commit 22eb36a into main Feb 2, 2025
4 checks passed
@hat0uma hat0uma deleted the feat/add-motions-and-textobjects branch February 2, 2025 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant