neoclide/coc.nvim · error · Error

File range not found at lnum: ${lnum}

Error message

File range not found at lnum: ${lnum}

What it means

RefactorBuffer.getFileRange looks up a FileRange by its line number in the refactor result buffer and throws when no registered range occupies that lnum. Callers (infos, r) rely on the cursor being inside a listed file entry; an arbitrary line yields no range.

Source

Thrown at src/handler/refactor/buffer.ts:140

  public get fileItems(): FileItem[] {
    return this._fileItems
  }

  public getFileItem(uri: string): FileItem | undefined {
    let filepath = URI.parse(uri).fsPath
    return this._fileItems.find(o => sameFile(o.filepath, filepath))
  }

  public getFileRange(lnum: number): FileRange & { filepath: string } {
    for (let item of this._fileItems) {
      for (let r of item.ranges) {
        if (r.lnum == lnum) {
          return Object.assign(omit(r, ['highlights']), { filepath: item.filepath })
        }
      }
    }
    throw new Error(`File range not found at lnum: ${lnum}`)
  }

  public onChange(e: DidChangeTextDocumentParams): void {
    if (this.changing) return
    if (e.contentChanges.length === 0) {
      this.highlightLineNr()
      this.nvim.redrawVim()
      return
    }
    let { nvim } = this
    e = fixChangeParams(e)
    let change = e.contentChanges[0]
    let { original } = e
    if (change.range.end.line > 2) {
      nvim.call('setbufvar', [e.bufnr, '&modified', 1], true)
    }
    let { range, text } = change
    let lineChange = lineCountChange(TextEdit.replace(range, text))

View on GitHub (pinned to 50e974d969)

Solutions

  1. Move the cursor to a highlighted result line inside the refactor buffer before invoking the command.
  2. Do not manually edit the refactor buffer; regenerate it with a fresh `:CocCommand document.refactorSearch`.
  3. If ranges look stale (buffer edited after generation), close and redo the refactor search.
  4. Check your mapping targets the correct command/window.
Defensive patterns

Strategy: validation

Validate before calling

// check the cursor line is a recorded entry before invoking
const lnum = await vim.call('line', '.')
// inspect the refactor buffer's highlighted entry lines
if (!entryLnums.has(lnum)) return

Try / catch

try {
  const range = buffer.getFileRange(lnum)
} catch (e) {
  if (String(e?.message).startsWith('File range not found at lnum')) return
  throw e
}

Prevention

When it happens

Trigger: Calling `:CocCommand document.refactorSearch infos` or pressing the mapped key ('r' path) while the cursor sits on a line of the refactor panel that is not one of the recorded file-range lines (headers, separators, code lines not covered by ranges).

Common situations: Cursor on a file header or blank line instead of a result line; refactor panel was edited/reloaded so recorded lnums shifted; invoking the command from a window other than the refactor buffer.

Related errors


AI-assisted analysis of neoclide/coc.nvim@50e974d969 (2026-08-31). Data as JSON: /api/errors/6e0cabaabfe96b6e. Report an issue: GitHub.