Skip to content

refactor(tab_strip): improve structure and readability#218

Open
Gitkbc wants to merge 1 commit intolunarmodules:mainfrom
Gitkbc:refactor/tab-strip-cleanup
Open

refactor(tab_strip): improve structure and readability#218
Gitkbc wants to merge 1 commit intolunarmodules:mainfrom
Gitkbc:refactor/tab-strip-cleanup

Conversation

@Gitkbc
Copy link
Contributor

@Gitkbc Gitkbc commented Mar 9, 2026

Summary

Refactors tab_strip.lua to improve readability and maintainability by removing duplicated logic and extracting helper functions.

Changes

  • Extracted Logic : Item validation and processing moved to process_items.
  • Selection Handling : Extracted initial selection logic into resolve_initial_selection and introduced _find_selected_index to remove repeated tab lookup logic.
  • Modular Rendering: Split rendering logic into _render_tab_slice and _render_padding_slice to simplify _build_tab_line.
  • Clean Code: Added constants for the overflow indicator (ELLIPSIS) and improved variable naming (e.g. tab_index) for clarity.

All tests are passed. Please let me know if any further improvements are required — happy to update.

Fixes #217

-- Process and validate an items array into the internal format.
-- @tparam table|nil items Raw items array from caller.
-- @treturn table Processed items with validated labels and default ids.
local function process_items(items)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name doesn't describe what it does, it is overly generic. maybe validate_items? Also the parameter description is waste of bytes. This is what AI generated slob looks like.

The implementation is not lua-ish.

self._cache_valid = false
end


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the whitespace changes? this messes with git history, try to avoid changes like this.


-- Overflow indicator used when tabs exceed available width.
local ELLIPSIS = "…"
local ELLIPSIS_WIDTH = width.utf8swidth(ELLIPSIS)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not ok. You initialize the value here during loading the module, before the user has even initialized the terminal. During initialization the ambiguous-width might be configured differently and this value generated here might be wrong.


-- Find the index of the currently selected tab, or nil if not found.
-- @treturn number|nil The 1-based index into self.items.
function TabStrip:_find_selected_index()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method is rather similar to the resolve_initial_selection above. Yet that one is implemented as a function and this one as a method. That is inconsistent.

local visible_end = visible_start + effective_width
local rendered_width = has_left_overflow and ellipsis_width or 0
local visible_start = self._viewport_offset
local visible_end = visible_start + effective_width
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more whitespace changes that convolute the git history


return TabStrip

return TabStrip No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace; the final newline was removed. fyi; you can configure your editor most likely to honor the settings in .editorconfig

-- @tparam table processed_items Validated items array.
-- @tparam any|nil requested_id The caller-supplied initial selection id.
-- @treturn any|nil The resolved tab id, or nil if no items exist.
local function resolve_initial_selection(processed_items, requested_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some of these function have but a single caller. In that case the scope can be reduced by wrapping the function and its caller in a do...end block

@Gitkbc Gitkbc force-pushed the refactor/tab-strip-cleanup branch from b47a47d to c890b14 Compare March 10, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

refactor: tab_strip

2 participants