DS-5554 Caret access is moved under read action to avoid read access exceptions

GitOrigin-RevId: 2a252a71cd6d5deacebb4f7aa7115cb0eab2473c
This commit is contained in:
Anton Bragin
2023-09-15 12:26:54 +02:00
committed by intellij-monorepo-bot
parent 2b641a7f92
commit 24521d56f2

View File

@@ -1,5 +1,6 @@
package org.jetbrains.plugins.notebooks.visualization
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.Editor
@@ -60,26 +61,28 @@ class CaretBasedCellSelectionModel(private val editor: Editor) : NotebookCellSel
}
private fun Document.getSelectionLines(caret: Caret): IntRange {
val selectionStart = caret.selectionStart
val selectionEnd = caret.selectionEnd
val lastLine = getLineNumber(selectionEnd)
return ReadAction.compute<IntRange, Throwable> {
val selectionStart = caret.selectionStart
val selectionEnd = caret.selectionEnd
val lastLine = getLineNumber(selectionEnd)
// See: DS-3659 Context menu action "Delete cell" deletes wrong cell
if (caret.offset < selectionStart || caret.offset > selectionEnd) {
val caretLine = getLineNumber(caret.offset)
return IntRange(caretLine, caretLine)
// See: DS-3659 Context menu action "Delete cell" deletes wrong cell
if (caret.offset < selectionStart || caret.offset > selectionEnd) {
val caretLine = getLineNumber(caret.offset)
return@compute IntRange(caretLine, caretLine)
}
if (caret.offset < selectionEnd && getLineStartOffset(lastLine) == selectionEnd) {
// for example, after triple click on line1
// #%%
// <selection><caret>line1
// </selection>#%%
// line2
return@compute IntRange(getLineNumber(caret.selectionStart), lastLine - 1)
}
return@compute IntRange(getLineNumber(caret.selectionStart), lastLine)
}
if (caret.offset < selectionEnd && getLineStartOffset(lastLine) == selectionEnd) {
// for example, after triple click on line1
// #%%
// <selection><caret>line1
// </selection>#%%
// line2
return IntRange(getLineNumber(caret.selectionStart), lastLine - 1)
}
return IntRange(getLineNumber(caret.selectionStart), lastLine)
}
private val NotebookCellLines.Interval.startLogicalPosition: LogicalPosition