fix(editor): Skip error line highlighting if out of range (#6721)

This commit is contained in:
Iván Ovejero 2023-07-24 11:12:52 +02:00 committed by GitHub
parent 4029386349
commit a62d00a479
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 7 deletions

View file

@ -24,7 +24,7 @@ import type { PropType } from 'vue';
import { mapStores } from 'pinia';
import type { LanguageSupport } from '@codemirror/language';
import type { Extension } from '@codemirror/state';
import type { Extension, Line } from '@codemirror/state';
import { Compartment, EditorState } from '@codemirror/state';
import type { ViewUpdate } from '@codemirror/view';
import { EditorView } from '@codemirror/view';
@ -154,18 +154,29 @@ export default defineComponent({
changes: { from: 0, to: this.content.length, insert: this.placeholder },
});
},
highlightLine(line: number | 'final') {
line(lineNumber: number): Line | null {
try {
return this.editor?.state.doc.line(lineNumber) ?? null;
} catch {
return null;
}
},
highlightLine(lineNumber: number | 'final') {
if (!this.editor) return;
if (line === 'final') {
if (lineNumber === 'final') {
this.editor.dispatch({
selection: { anchor: this.content.length },
});
return;
}
const line = this.line(lineNumber);
if (!line) return;
this.editor.dispatch({
selection: { anchor: this.editor.state.doc.line(line).from },
selection: { anchor: line.from },
});
},
trackCompletion(viewUpdate: ViewUpdate) {

View file

@ -17,6 +17,7 @@ import { acceptCompletion, autocompletion, ifNotIn } from '@codemirror/autocompl
import { indentWithTab, history, redo, toggleComment } from '@codemirror/commands';
import { bracketMatching, foldGutter, indentOnInput, LanguageSupport } from '@codemirror/language';
import { EditorState } from '@codemirror/state';
import type { Line } from '@codemirror/state';
import type { Extension } from '@codemirror/state';
import {
dropCursor,
@ -189,18 +190,29 @@ export default defineComponent({
onBlur() {
this.isFocused = false;
},
highlightLine(line: number | 'final') {
line(lineNumber: number): Line | null {
try {
return this.editor?.state.doc.line(lineNumber) ?? null;
} catch {
return null;
}
},
highlightLine(lineNumber: number | 'final') {
if (!this.editor) return;
if (line === 'final') {
if (lineNumber === 'final') {
this.editor.dispatch({
selection: { anchor: this.query.length },
});
return;
}
const line = this.line(lineNumber);
if (!line) return;
this.editor.dispatch({
selection: { anchor: this.editor.state.doc.line(line).from },
selection: { anchor: line.from },
});
},
},