2023-05-16 08:21:10 -04:00
|
|
|
import { EditorView, ViewUpdate } from '@codemirror/view'
|
2023-04-13 04:21:25 -04:00
|
|
|
import { Diagnostic, linter, lintGutter } from '@codemirror/lint'
|
|
|
|
import {
|
|
|
|
Compartment,
|
2023-06-08 04:35:51 -04:00
|
|
|
Extension,
|
2023-04-13 04:21:25 -04:00
|
|
|
RangeSet,
|
|
|
|
RangeValue,
|
|
|
|
StateEffect,
|
|
|
|
StateField,
|
|
|
|
Text,
|
|
|
|
} from '@codemirror/state'
|
|
|
|
import { Annotation } from '../../../../../types/annotation'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2024-01-23 08:00:32 -05:00
|
|
|
import { sendMB } from '@/infrastructure/event-tracking'
|
2024-06-17 06:34:09 -04:00
|
|
|
import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
|
|
|
|
|
|
|
|
interface CompileLogDiagnostic extends Diagnostic {
|
|
|
|
compile?: true
|
|
|
|
ruleId?: string
|
|
|
|
id?: string
|
|
|
|
entryIndex: number
|
|
|
|
firstOnLine?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
type RenderedDiagnostic = Pick<
|
|
|
|
CompileLogDiagnostic,
|
|
|
|
| 'message'
|
|
|
|
| 'severity'
|
|
|
|
| 'ruleId'
|
|
|
|
| 'compile'
|
|
|
|
| 'source'
|
|
|
|
| 'id'
|
|
|
|
| 'firstOnLine'
|
|
|
|
>
|
|
|
|
|
|
|
|
export type DiagnosticAction = (
|
|
|
|
diagnostic: RenderedDiagnostic
|
|
|
|
) => HTMLButtonElement | null
|
|
|
|
|
|
|
|
const diagnosticActions = importOverleafModules('diagnosticActions') as {
|
|
|
|
import: { default: DiagnosticAction }
|
|
|
|
}[]
|
2023-04-13 04:21:25 -04:00
|
|
|
|
|
|
|
const compileLintSourceConf = new Compartment()
|
|
|
|
|
|
|
|
export const annotations = () => [
|
|
|
|
compileDiagnosticsState,
|
|
|
|
compileLintSourceConf.of(compileLogLintSource()),
|
2023-06-08 04:35:51 -04:00
|
|
|
/**
|
|
|
|
* The built-in lint gutter extension, configured with zero hover delay.
|
|
|
|
*/
|
2023-04-13 04:21:25 -04:00
|
|
|
lintGutter({
|
|
|
|
hoverTime: 0,
|
|
|
|
}),
|
2024-02-15 04:45:50 -05:00
|
|
|
annotationsTheme,
|
2023-04-13 04:21:25 -04:00
|
|
|
]
|
|
|
|
|
2024-02-15 04:45:50 -05:00
|
|
|
/**
|
|
|
|
* A theme which moves the lint gutter outside the line numbers.
|
|
|
|
*/
|
|
|
|
const annotationsTheme = EditorView.baseTheme({
|
|
|
|
'.cm-gutter-lint': {
|
|
|
|
order: -1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-04-13 04:21:25 -04:00
|
|
|
export const lintSourceConfig = {
|
|
|
|
delay: 100,
|
|
|
|
// Show highlights only for errors
|
|
|
|
markerFilter(diagnostics: readonly Diagnostic[]) {
|
|
|
|
return diagnostics.filter(d => d.severity === 'error')
|
|
|
|
},
|
|
|
|
// Do not show any tooltips for highlights within the editor content
|
|
|
|
tooltipFilter() {
|
|
|
|
return []
|
|
|
|
},
|
2023-05-16 08:21:10 -04:00
|
|
|
needsRefresh(update: ViewUpdate) {
|
|
|
|
return update.selectionSet
|
|
|
|
},
|
2023-04-13 04:21:25 -04:00
|
|
|
}
|
|
|
|
|
2023-06-08 04:35:51 -04:00
|
|
|
/**
|
|
|
|
* A lint source using the compile log diagnostics
|
|
|
|
*/
|
|
|
|
const compileLogLintSource = (): Extension =>
|
2023-04-13 04:21:25 -04:00
|
|
|
linter(view => {
|
2024-06-17 06:34:09 -04:00
|
|
|
const items: CompileLogDiagnostic[] = []
|
|
|
|
// NOTE: iter() changes the order of diagnostics on the same line
|
2023-04-13 04:21:25 -04:00
|
|
|
const cursor = view.state.field(compileDiagnosticsState).iter()
|
|
|
|
while (cursor.value !== null) {
|
2024-01-23 08:00:32 -05:00
|
|
|
const { diagnostic } = cursor.value
|
2023-04-13 04:21:25 -04:00
|
|
|
items.push({
|
2024-01-23 08:00:32 -05:00
|
|
|
...diagnostic,
|
2023-04-13 04:21:25 -04:00
|
|
|
from: cursor.from,
|
|
|
|
to: cursor.to,
|
2024-01-23 08:00:32 -05:00
|
|
|
renderMessage: () => renderMessage(diagnostic),
|
2023-04-13 04:21:25 -04:00
|
|
|
})
|
|
|
|
cursor.next()
|
|
|
|
}
|
2024-06-17 06:34:09 -04:00
|
|
|
// restore the original order of items
|
|
|
|
items.sort((a, b) => a.from - b.from || a.entryIndex - b.entryIndex)
|
2023-04-13 04:21:25 -04:00
|
|
|
return items
|
|
|
|
}, lintSourceConfig)
|
|
|
|
|
2024-01-23 08:00:32 -05:00
|
|
|
class CompileLogDiagnosticRangeValue extends RangeValue {
|
|
|
|
constructor(public diagnostic: CompileLogDiagnostic) {
|
2023-04-13 04:21:25 -04:00
|
|
|
super()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 08:00:32 -05:00
|
|
|
const setCompileDiagnosticsEffect = StateEffect.define<CompileLogDiagnostic[]>()
|
2023-04-13 04:21:25 -04:00
|
|
|
|
2023-06-08 04:35:51 -04:00
|
|
|
/**
|
|
|
|
* A state field for the compile log diagnostics
|
|
|
|
*/
|
2023-04-13 04:21:25 -04:00
|
|
|
export const compileDiagnosticsState = StateField.define<
|
2024-01-23 08:00:32 -05:00
|
|
|
RangeSet<CompileLogDiagnosticRangeValue>
|
2023-04-13 04:21:25 -04:00
|
|
|
>({
|
|
|
|
create() {
|
|
|
|
return RangeSet.empty
|
|
|
|
},
|
|
|
|
update(value, transaction) {
|
|
|
|
for (const effect of transaction.effects) {
|
|
|
|
if (effect.is(setCompileDiagnosticsEffect)) {
|
|
|
|
return RangeSet.of(
|
|
|
|
effect.value.map(diagnostic =>
|
2024-01-23 08:00:32 -05:00
|
|
|
new CompileLogDiagnosticRangeValue(diagnostic).range(
|
2023-04-13 04:21:25 -04:00
|
|
|
diagnostic.from,
|
|
|
|
diagnostic.to
|
|
|
|
)
|
|
|
|
),
|
|
|
|
true
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transaction.docChanged) {
|
|
|
|
value = value.map(transaction.changes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
export const setAnnotations = (doc: Text, annotations: Annotation[]) => {
|
2024-06-17 06:34:09 -04:00
|
|
|
const diagnostics: CompileLogDiagnostic[] = []
|
2023-04-13 04:21:25 -04:00
|
|
|
|
|
|
|
for (const annotation of annotations) {
|
|
|
|
// ignore "whole document" (row: -1) annotations
|
|
|
|
if (annotation.row !== -1) {
|
|
|
|
try {
|
|
|
|
diagnostics.push(convertAnnotationToDiagnostic(doc, annotation))
|
|
|
|
} catch (error) {
|
|
|
|
// ignore invalid annotations
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.debug('invalid annotation position', error)
|
2023-04-13 04:21:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
effects: setCompileDiagnosticsEffect.of(diagnostics),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const showCompileLogDiagnostics = (show: boolean) => {
|
|
|
|
return {
|
|
|
|
effects: [
|
|
|
|
// reconfigure the compile log lint source
|
|
|
|
compileLintSourceConf.reconfigure(show ? compileLogLintSource() : []),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const convertAnnotationToDiagnostic = (
|
|
|
|
doc: Text,
|
|
|
|
annotation: Annotation
|
2024-01-23 08:00:32 -05:00
|
|
|
): CompileLogDiagnostic => {
|
2023-04-13 04:21:25 -04:00
|
|
|
if (annotation.row < 0) {
|
|
|
|
throw new Error(`Invalid annotation row ${annotation.row}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const line = doc.line(annotation.row + 1)
|
|
|
|
|
|
|
|
return {
|
|
|
|
from: line.from,
|
|
|
|
to: line.to, // NOTE: highlight whole line as synctex doesn't output column number
|
|
|
|
severity: annotation.type,
|
|
|
|
message: annotation.text,
|
2024-01-23 08:00:32 -05:00
|
|
|
ruleId: annotation.ruleId,
|
|
|
|
compile: true,
|
2024-06-17 06:34:09 -04:00
|
|
|
id: annotation.id,
|
|
|
|
entryIndex: annotation.entryIndex,
|
|
|
|
source: annotation.source,
|
|
|
|
firstOnLine: annotation.firstOnLine,
|
2023-04-13 04:21:25 -04:00
|
|
|
}
|
|
|
|
}
|
2024-01-23 08:00:32 -05:00
|
|
|
|
2024-06-17 06:34:09 -04:00
|
|
|
export const renderMessage = (diagnostic: RenderedDiagnostic) => {
|
2024-01-23 08:00:32 -05:00
|
|
|
const { message, severity, ruleId, compile = false } = diagnostic
|
|
|
|
|
|
|
|
const div = document.createElement('div')
|
2024-06-17 06:34:09 -04:00
|
|
|
div.classList.add('ol-cm-diagnostic-message')
|
|
|
|
|
|
|
|
div.append(message)
|
|
|
|
|
|
|
|
const activeDiagnosticActions = diagnosticActions
|
|
|
|
.map(m => m.import.default(diagnostic))
|
|
|
|
.filter(Boolean) as HTMLButtonElement[]
|
|
|
|
|
|
|
|
if (activeDiagnosticActions.length) {
|
|
|
|
const actions = document.createElement('div')
|
|
|
|
actions.classList.add('ol-cm-diagnostic-actions')
|
|
|
|
actions.append(...activeDiagnosticActions)
|
|
|
|
div.append(actions)
|
|
|
|
}
|
2024-01-23 08:00:32 -05:00
|
|
|
|
|
|
|
window.setTimeout(() => {
|
|
|
|
if (div.isConnected) {
|
|
|
|
sendMB('lint-gutter-marker-view', { severity, ruleId, compile })
|
|
|
|
}
|
|
|
|
}, 500) // 500ms delay to indicate intention, rather than accidental hover
|
|
|
|
|
|
|
|
return div
|
|
|
|
}
|