shikijs / twoslash

You take some Shiki, add a hint of TypeScript compiler, and 🎉 incredible static code samples
https://shikijs.github.io/twoslash/
MIT License
1.08k stars 53 forks source link

Handle errors spanning multiple tokens #145

Closed with-heart closed 3 months ago

with-heart commented 2 years ago

This PR resolves #144 by creating a new function that checks whether each token is within the range of an error (e.character to e.character + e.length). Checking the range produces the correct results (T[Key] is highlighted as an error):

image

This function replaces findTokenFunc only when handling errors (lsp responses + queries still use findTokenFunc).

I also added isTokenWithinErrorRangeDebug which is similar to findTokenFuncDebug for any future debugging needs.

changeset-bot[bot] commented 2 years ago

🦋 Changeset detected

Latest commit: e501f8c955d4e800ca2206025beeb30e229e720c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages | Name | Type | | -------------------------------- | ----- | | shiki-twoslash | Minor | | eleventy-plugin-shiki-twoslash | Patch | | markdown-it-shiki-twoslash | Patch | | remark-shiki-twoslash | Patch | | docusaurus-preset-shiki-twoslash | Patch | | gatsby-remark-shiki-twoslash | Patch | | hexo-shiki-twoslash | Patch | | twoslash-cli | Patch | | vuepress-plugin-shiki-twoslash | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

with-heart commented 2 years ago

The accurateErrors fixture from remark-shiki-twoslash is failing, but I'm not entirely sure why. After removing all the noise, here's the diff:

- <data-lsp lsp='const todo: Readonly&lt;Todo>' >todo</data-lsp>.<data-err><data-lsp lsp='(property) title: any' >title</data-lsp></data-err>
+ <data-lsp lsp='const todo: Readonly&lt;Todo>' >todo</data-lsp>.<data-lsp lsp='(property) title: any' >title</data-lsp>

It shows that it's not rendering title in todo.title = 'Hello' as an error, but if I drop that fixture in a twoslash block in a local app using these these changes (with remark-shiki-twoslash), title is highlighted just fine:

image image

Any ideas?

orta commented 2 years ago

Oof, not sure when I'll find time to dig into this properly - this section of code is super gnarly and not an area I fully grok anymore