Skip to content

fix(code-editor): adaptLSDiagnosticRange fails to expand empty range at EOF #2905

@xgopilot

Description

@xgopilot

Problem

In spx-gui/src/components/editor/code-editor/xgo-code-editor/diagnostics.ts, the adaptLSDiagnosticRange function is supposed to expand a zero-length diagnostic range so that it can be displayed as an inline decoration. However, when the diagnostic position is at or near EOF, the expansion logic fails:

// If no non-newline character is found forward, fall back to backward scan:
for (let i = offsetStart; i >= 0; i--) {
  if (code[i] !== '\n') {
    offsetStart = i
    break
  }
}

When offsetStart === code.length (EOF), the backward scan starts at index code.length, but code[code.length] is undefined (not '\n'), so offsetStart = i is set to code.length — leaving start === end. The expanded range is still empty.

Expected Behavior

adaptLSDiagnosticRange should always return a non-empty range (i.e. start !== end) when given a zero-length input range, including at EOF.

Suggested Fix

  • Start the backward scan from offsetStart - 1 (not offsetStart) to avoid the off-by-one issue.
  • When no non-newline character exists anywhere (e.g. file is all newlines or empty), ensure offsetEnd is advanced to at least offsetStart + 1 as a last resort.

Location

spx-gui/src/components/editor/code-editor/xgo-code-editor/diagnostics.tsadaptLSDiagnosticRange method

Notes

This bug is pre-existing (not introduced by PR #2858). Tracked here per review comment in that PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions