Skip to content

Feature proposal: Accept OffsetOrPosition that denotes end of line #1038

@sealer3

Description

@sealer3

Clear and concise description of the problem

I would like to decorate (for example) lines 75 and 76 with Shiki. Currently that means I must also know the precise length of line 76, which means I have to split lines myself to determine what the line length is in order to produce a Position for the DecorationItem's start and end positions. This is something Shiki already has to do. Adding this feature would make things easier from a library user point of view.

Suggested solution

Can we have some way of describing "I want to highlight the whole line"? A simple solution would be to accept a negative character index as a Position and treat that as line.length + character. So if you want to highlight lines 75 and 76, you would use

...
start: {
    line: 75,
    character: 0,
  },
end: {
    line: 76,
    character: -1,
}

We would just need to modify this bit to accept it:

else {
const line = converter.lines[p.line]
if (line === undefined)
throw new ShikiError(`Invalid decoration position ${JSON.stringify(p)}. Lines length: ${converter.lines.length}`)
if (p.character < 0 || p.character > line.length)
throw new ShikiError(`Invalid decoration position ${JSON.stringify(p)}. Line ${p.line} length: ${line.length}`)
return {
...p,
offset: converter.posToIndex(p.line, p.character),
}
}

Alternative

Alternatively, we can derive some other object that accounts for an entire line.

Additional context

I'd be happy to make a PR for this simple change myself if I get the OK from a maintainer.

Validations

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