@@ -107,12 +107,10 @@ internal class CCTemplateEditingListener : TemplateEditingAdapter() {
107107
108108 if (newOffset <= bodyOrFooterTypeRange.endOffset) {
109109 val editor = templateState.editor
110- val action = {
110+ WriteCommandAction .runWriteCommandAction(editor.project, " Reposition cursor after the subject " , " " , {
111111 editor.document.deleteString(newOffset, bodyOrFooterTypeRange.endOffset)
112112 editor.moveCaretToOffset(newOffset)
113- }
114-
115- WriteCommandAction .runWriteCommandAction(editor.project, " Reposition cursor after the subject" , " " , action)
113+ })
116114 }
117115 }
118116
@@ -124,12 +122,14 @@ internal class CCTemplateEditingListener : TemplateEditingAdapter() {
124122 val editor = templateState.editor
125123 val document = editor.document
126124 val startOffset = max(scopeStart - 1 , 0 )
127- val endOffset = min(scopeEnd + 1 , document.textLength)
128- val action = {
129- document.deleteString(startOffset, endOffset)
130- }
131125
132- WriteCommandAction .runWriteCommandAction(editor.project, " Delete scope's parentheses" , " " , action)
126+ // In some cases it seems the segment range is not yet up to date with document changes
127+ if (startOffset <= document.textLength) {
128+ val endOffset = min(scopeEnd + 1 , document.textLength)
129+ WriteCommandAction .runWriteCommandAction(editor.project, " Delete scope's parentheses" , " " , {
130+ document.deleteString(startOffset, endOffset)
131+ })
132+ }
133133 }
134134 }
135135
0 commit comments