Skip to content

Commit

Permalink
better diagnostic errors
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Oct 9, 2023
1 parent 2a8c83a commit ed80920
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
4 changes: 2 additions & 2 deletions mdxts/components/client/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Diagnostic, DiagnosticMessageChain } from 'ts-morph'

export function hasDiagnosticsForToken(
export function getDiagnosticForToken(
token: any,
tokenIndex: number,
lineIndex: number,
Expand Down Expand Up @@ -31,7 +31,7 @@ export function hasDiagnosticsForToken(
(diagnosticEnd >= tokenStart && diagnosticEnd <= tokenEnd) ||
(diagnosticStart <= tokenStart && diagnosticEnd >= tokenEnd)
) {
return true
return diagnostic
}
}

Expand Down
30 changes: 12 additions & 18 deletions mdxts/components/client/highlighter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getHighlighter as shikiGetHighlighter } from 'shiki'
import type { Diagnostic } from 'ts-morph'
import { hasDiagnosticsForToken } from './diagnostics'
import { getDiagnosticForToken } from './diagnostics'

export type Theme = Parameters<typeof shikiGetHighlighter>[0]['theme']

Expand Down Expand Up @@ -51,15 +51,7 @@ export function processToken(token: Token, diagnostic?: Diagnostic): Tokens {
const diagnosticStart = diagnostic?.getStart()
const diagnosticEnd = diagnosticStart + diagnostic?.getLength()

if (token.start >= diagnosticStart && token.end <= diagnosticEnd) {
// If the whole token is an error
return [
{
...token,
hasError: true,
},
]
} else if (diagnosticStart > token.start && diagnosticEnd < token.end) {
if (diagnosticStart > token.start && diagnosticEnd < token.end) {
// If only a part of the token is an error, split it
return [
{
Expand All @@ -81,6 +73,14 @@ export function processToken(token: Token, diagnostic?: Diagnostic): Tokens {
hasError: false,
},
]
} else if (token.start >= diagnosticStart && token.end <= diagnosticEnd) {
// If the whole token is an error
return [
{
...token,
hasError: true,
},
]
} else {
// No error in this token
return [
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function getHighlighter(options: any): Promise<Highlighter> {
}

if (diagnostics) {
const hasError = hasDiagnosticsForToken(
const diagnostic = getDiagnosticForToken(
token,
tokenIndex,
lineIndex,
Expand All @@ -133,14 +133,8 @@ export async function getHighlighter(options: any): Promise<Highlighter> {
code
)

if (hasError) {
const diagnostic = diagnostics.find((diagnostic) => {
const diagnosticStart = diagnostic.getStart()
const diagnosticEnd = diagnosticStart + diagnostic.getLength()
return diagnosticStart <= tokenEnd && diagnosticEnd >= tokenStart
})
if (diagnostic) {
const subTokens = processToken(processedToken, diagnostic)

return subTokens
}
}
Expand Down

0 comments on commit ed80920

Please sign in to comment.