Skip to content

Commit

Permalink
Handle errors spanning multiple tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
with-heart committed Mar 4, 2022
1 parent fbdf0a2 commit e501f8c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .changeset/tall-moons-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"shiki-twoslash": minor
---

Added support for highlighting errors that span multiple tokens.

Previously, error highlighting checked each token in a line to see if an error should be applied. This failed to apply an error highlight when the error spanned multiple tokens, like `T`+`[`+`Key`+`]` in the following example:

```ts
type MyPick<T, K> = {
[Key in K]: T[Key];
};
```

`T[Key]` will now be correctly highlighted as an error.
16 changes: 15 additions & 1 deletion packages/shiki-twoslash/src/renderers/twoslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,21 @@ export function twoslashRenderer(lines: Lines, options: HtmlRendererOptions & Tw
return result
}

const errorsInToken = errors.filter(findTokenFunc(tokenPos))
const isTokenWithinErrorRange = (start: number) => (e: any) =>
start >= e.character && start + token.content.length <= e.character + e.length

const isTokenWithinErrorRangeDebug = (start: number) => (e: any) => {
const result = start >= e.character && start + token.content.length <= e.character + e.length
// prettier-ignore
console.log(result, token.content ,start, '>=', e.character, '&&', start + token.content.length, '<=', e.character + e.length)
if (result) {
console.log("Found:", e)
console.log("Inside:", token)
}
return result
}

const errorsInToken = errors.filter(isTokenWithinErrorRange(tokenPos))
const lspResponsesInToken = lspValues.filter(findTokenFunc(tokenPos))
const queriesInToken = queries.filter(findTokenFunc(tokenPos))

Expand Down

0 comments on commit e501f8c

Please sign in to comment.