Compare commits

...

1 Commits

Author SHA1 Message Date
29e1164ac0 WIP 2025-06-18 17:02:17 -05:00
2 changed files with 41 additions and 27 deletions

View File

@ -389,7 +389,7 @@ export class KclManager extends EventTarget {
if (err(result)) { if (err(result)) {
const kclError: KCLError = result as KCLError const kclError: KCLError = result as KCLError
this.diagnostics = kclErrorsToDiagnostics([kclError]) this.diagnostics = kclErrorsToDiagnostics([kclError], code)
this._astParseFailed = true this._astParseFailed = true
await this.checkIfSwitchedFilesShouldClear() await this.checkIfSwitchedFilesShouldClear()
@ -403,8 +403,8 @@ export class KclManager extends EventTarget {
this._kclErrorsCallBack([]) this._kclErrorsCallBack([])
this._logsCallBack([]) this._logsCallBack([])
this.addDiagnostics(compilationErrorsToDiagnostics(result.errors)) this.addDiagnostics(compilationErrorsToDiagnostics(result.errors, code))
this.addDiagnostics(compilationErrorsToDiagnostics(result.warnings)) this.addDiagnostics(compilationErrorsToDiagnostics(result.warnings, code))
if (result.errors.length > 0) { if (result.errors.length > 0) {
this._astParseFailed = true this._astParseFailed = true

View File

@ -290,6 +290,11 @@ export class KCLUndefinedValueError extends KCLError {
} }
} }
/** Convert this UTF-16 source range offset to UTF-8 as SourceRange is always a UTF-8*/
function toUtf8(utf16SourceRange: [number, number, number]): SourceRange {
throw new Error('TODO')
}
/** /**
* Maps the lsp diagnostic to an array of KclErrors. * Maps the lsp diagnostic to an array of KclErrors.
* Currently the diagnostics are all errors, but in the future they could include lints. * Currently the diagnostics are all errors, but in the future they could include lints.
@ -299,20 +304,24 @@ export function lspDiagnosticsToKclErrors(
diagnostics: LspDiagnostic[] diagnostics: LspDiagnostic[]
): KCLError[] { ): KCLError[] {
return diagnostics return diagnostics
.flatMap( .flatMap(({ range, message }) => {
({ range, message }) => const sourceRange = toUtf8([
new KCLError( posToOffset(doc, range.start)!,
'unexpected', posToOffset(doc, range.end)!,
message, 0,
[posToOffset(doc, range.start)!, posToOffset(doc, range.end)!, 0], ])
[], return new KCLError(
[], 'unexpected',
[], message,
defaultArtifactGraph(), sourceRange,
{}, [],
null [],
) [],
) defaultArtifactGraph(),
{},
null
)
})
.sort((a, b) => { .sort((a, b) => {
const c = a.sourceRange[0] const c = a.sourceRange[0]
const d = b.sourceRange[0] const d = b.sourceRange[0]
@ -331,7 +340,8 @@ export function lspDiagnosticsToKclErrors(
* Currently the diagnostics are all errors, but in the future they could include lints. * Currently the diagnostics are all errors, but in the future they could include lints.
* */ * */
export function kclErrorsToDiagnostics( export function kclErrorsToDiagnostics(
errors: KCLError[] errors: KCLError[],
sourceCode: string
): CodeMirrorDiagnostic[] { ): CodeMirrorDiagnostic[] {
let nonFatal: CodeMirrorDiagnostic[] = [] let nonFatal: CodeMirrorDiagnostic[] = []
const errs = errors const errs = errors
@ -350,8 +360,8 @@ export function kclErrorsToDiagnostics(
item.sourceRange[1] !== err.sourceRange[1] item.sourceRange[1] !== err.sourceRange[1]
) { ) {
diagnostics.push({ diagnostics.push({
from: item.sourceRange[0], from: toUtf16(item.sourceRange[0], sourceCode),
to: item.sourceRange[1], to: toUtf16(item.sourceRange[1], sourceCode),
message: 'Part of the error backtrace', message: 'Part of the error backtrace',
severity: 'hint', severity: 'hint',
}) })
@ -368,8 +378,8 @@ export function kclErrorsToDiagnostics(
nonFatal = nonFatal.concat(compilationErrorsToDiagnostics(err.nonFatal)) nonFatal = nonFatal.concat(compilationErrorsToDiagnostics(err.nonFatal))
} }
diagnostics.push({ diagnostics.push({
from: err.sourceRange[0], from: toUtf16(err.sourceRange[0], sourceCode),
to: err.sourceRange[1], to: toUtf16(err.sourceRange[1], sourceCode),
message, message,
severity: 'error', severity: 'error',
}) })
@ -379,7 +389,8 @@ export function kclErrorsToDiagnostics(
} }
export function compilationErrorsToDiagnostics( export function compilationErrorsToDiagnostics(
errors: CompilationError[] errors: CompilationError[],
sourceCode: string
): CodeMirrorDiagnostic[] { ): CodeMirrorDiagnostic[] {
return errors return errors
?.filter((err) => isTopLevelModule(err.sourceRange)) ?.filter((err) => isTopLevelModule(err.sourceRange))
@ -397,8 +408,8 @@ export function compilationErrorsToDiagnostics(
apply: (view: EditorView, from: number, to: number) => { apply: (view: EditorView, from: number, to: number) => {
view.dispatch({ view.dispatch({
changes: { changes: {
from: suggestion.source_range[0], from: toUtf16(suggestion.source_range[0], sourceCode),
to: suggestion.source_range[1], to: toUtf16(suggestion.source_range[1], sourceCode),
insert: suggestion.insert, insert: suggestion.insert,
}, },
annotations: [lspCodeActionEvent], annotations: [lspCodeActionEvent],
@ -408,8 +419,8 @@ export function compilationErrorsToDiagnostics(
] ]
} }
return { return {
from: err.sourceRange[0], from: toUtf16(err.sourceRange[0], sourceCode),
to: err.sourceRange[1], to: toUtf16(err.sourceRange[1], sourceCode),
message: err.message, message: err.message,
severity, severity,
actions, actions,
@ -447,3 +458,6 @@ export function kclErrorsByFilename(
return fileNameToError return fileNameToError
} }
function toUtf16(utf16Offset: number, sourceCode: string): number {
throw new Error('Function not implemented.')
}