Skip to content

Commit faa8dc9

Browse files
committed
fix: Code editor input error
1 parent 44b1387 commit faa8dc9

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

ui/src/components/codemirror-editor/index.vue

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { python } from '@codemirror/lang-python'
4646
import { oneDark } from '@codemirror/theme-one-dark'
4747
import { linter, type Diagnostic } from '@codemirror/lint'
4848
import { loadSharedApi } from '@/utils/dynamics-api/shared-api'
49-
import debounce from 'lodash/debounce'
49+
import { throttle } from 'lodash'
5050
5151
defineOptions({ name: 'CodemirrorEditor' })
5252
@@ -79,22 +79,34 @@ const data = computed({
7979
8080
function getRangeFromLineAndColumn(state: any, line: number, column: number, end_column?: number) {
8181
const l = state.doc.line(line)
82-
const form = l.from + column
83-
const to_end_column = l.from + end_column
82+
const lineLength = l.length
83+
const safeColumn = Math.max(0, Math.min(column, lineLength))
84+
const fromPos = l.from + safeColumn
85+
let safeEndColumn
86+
if (end_column !== undefined) {
87+
safeEndColumn = Math.max(0, Math.min(end_column, lineLength))
88+
} else {
89+
safeEndColumn = lineLength
90+
}
91+
const toPos = l.from + safeEndColumn
92+
const finalFrom = Math.min(fromPos, toPos)
93+
const finalTo = Math.max(fromPos, toPos)
8494
return {
85-
form: form > l.to ? l.to : form,
86-
to: end_column && to_end_column < l.to ? to_end_column : l.to,
95+
from: finalFrom,
96+
to: finalTo,
8797
}
8898
}
89-
90-
const asyncLint = debounce(async (doc: string) => {
91-
const res = await loadSharedApi({ type: 'tool', systemType: apiType.value }).postPylint(doc)
99+
const asyncLint = throttle(async (view: any) => {
100+
const res = await loadSharedApi({ type: 'tool', systemType: apiType.value }).postPylint(
101+
view.state.doc.toString(),
102+
)
92103
return res.data
93104
}, 500)
94105
95106
const regexpLinter = linter(async (view) => {
107+
const currentstate = view.state
96108
const diagnostics: Diagnostic[] = []
97-
const lintResults = await asyncLint(view.state.doc.toString())
109+
const lintResults = await asyncLint(view)
98110
if (!lintResults || lintResults.length === 0) {
99111
return diagnostics
100112
}
@@ -105,15 +117,15 @@ const regexpLinter = linter(async (view) => {
105117
limitedResults.forEach((element: any) => {
106118
try {
107119
const range = getRangeFromLineAndColumn(
108-
view.state,
120+
currentstate,
109121
element.line,
110122
element.column,
111123
element.endColumn,
112124
)
113125
// 验证范围有效性
114-
if (range.form >= 0 && range.to >= range.form) {
126+
if (range.from >= 0 && range.to >= range.from) {
115127
diagnostics.push({
116-
from: range.form,
128+
from: range.from,
117129
to: range.to,
118130
severity: element.type === 'error' ? 'error' : 'warning',
119131
message: element.message,
@@ -125,7 +137,7 @@ const regexpLinter = linter(async (view) => {
125137
})
126138
return diagnostics
127139
})
128-
const extensions = [python(), regexpLinter, oneDark]
140+
const extensions = [python(), oneDark, regexpLinter]
129141
const codemirrorStyle = {
130142
height: '210px!important',
131143
width: '100%',

0 commit comments

Comments
 (0)