findrefs

package
v0.29.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package findrefs maps LSP textDocument/references results into a multibuffer fragment list.

The server returns a flat slice of (path, line, col) sites with no surrounding context. The multibuffer pane wants Fragments — each one a contiguous slice of one file with per-line annotations. findrefs reads each referenced file, slices a window of context around every hit, merges overlapping windows on the same file, sorts by (path, startLine), and returns the resulting Fragment slice.

Compute is pure (file reads go through a Reader callback so tests can inject an in-memory map). FindReferencesCmd wraps Compute with the actual LSP call.

Index

Constants

View Source
const DefaultContextLines = 3

DefaultContextLines is the half-window of source lines emitted around each hit. Three above + three below mirrors `git diff --unified=3` — the same window the user already sees when the multibuffer pane is loaded with diff fragments.

Variables

View Source
var ErrNoClient = errors.New("no language server attached")

ErrNoClient signals the LSP client was nil or not initialized. Wrapped in a FragmentsMsg so the host can bind alt+u unconditionally and surface a hint when the language server is still attaching.

View Source
var NoIdentifierError = errors.New("no identifier under cursor")

NoIdentifierError signals the cursor was not on an identifier. The host catches this before issuing the LSP call and shows it in the status bar.

Functions

func BuildFragments

func BuildFragments(locs []nooklsp.Location, contextLines int, reader Reader) []multibuffer.Fragment

BuildFragments converts a flat location list into Fragments suitable for the multibuffer pane. Steps:

  1. Stable-sort locations by (Path, Line, Col).
  2. Group adjacent same-path runs.
  3. For each file, read it via reader, then build windows of [hit-contextLines, hit+contextLines] clamped to file bounds. Windows that overlap or touch (gap of 0) are merged so a method called twice on lines 7 and 9 produces one fragment 4..12 rather than two overlapping ones.
  4. The hit row itself is marked multibuffer.Added so the pane paints it with the highlight color the diff loader uses for "+" lines. Context rows are marked multibuffer.Context.

A file that fails to read produces a single-line placeholder fragment carrying the error text, so the user still sees the hit and Enter still works to open the file (which will surface the real error in the editor pane).

func FindReferencesCmd

func FindReferencesCmd(client *nooklsp.Client, path string, row, col int, contextLines int, reader Reader) tea.Cmd

FindReferencesCmd is the tea.Cmd that calls client.References at (path, row, col) and returns a multibuffer.FragmentsMsg with the resulting Fragments. reader is exposed for tests; production callers pass nil for the OS-backed default.

The host pane title is set separately via Reset(...) before the command fires, so we don't carry symbol back through the message.

func OSReader

func OSReader(path string) (string, error)

OSReader is the default Reader implementation. Errors propagate.

func Symbol

func Symbol(source string, row, col int) string

Symbol returns the identifier at (row, col) in source. Identifiers are `[A-Za-z_][A-Za-z0-9_]*`; numeric-only spans return "". row and col are 0-indexed. The half-open contract is "cursor is on the identifier if col is anywhere within or at the right edge of the identifier" — i.e. col may equal len(ident) and still resolve, matching how editors place the caret after the last keystroke.

Types

type Reader

type Reader func(path string) (string, error)

Reader reads a file from disk as one string. Default is OSReader; tests inject map-backed readers via BuildFragments and Compute.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL