diff --git a/platform/core-api/src/com/intellij/openapi/editor/rangeMarkers.kt b/platform/core-api/src/com/intellij/openapi/editor/rangeMarkers.kt new file mode 100644 index 000000000000..6c863748bdd2 --- /dev/null +++ b/platform/core-api/src/com/intellij/openapi/editor/rangeMarkers.kt @@ -0,0 +1,18 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.openapi.editor + +import com.intellij.openapi.util.TextRange + +val RangeMarker.asTextRange: TextRange? + get() { + if (!isValid) return null + val start = startOffset + val end = endOffset + return if (start in 0..end) { + TextRange(start, end) + } + else { + // Probably a race condition had happened and range marker is invalidated + null + } + } \ No newline at end of file diff --git a/platform/core-api/src/com/intellij/psi/smartPointers.kt b/platform/core-api/src/com/intellij/psi/smartPointers.kt new file mode 100644 index 000000000000..67c6e1e1902b --- /dev/null +++ b/platform/core-api/src/com/intellij/psi/smartPointers.kt @@ -0,0 +1,5 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.psi + +fun E.createSmartPointer(): SmartPsiElementPointer = + SmartPointerManager.getInstance(project).createSmartPsiElementPointer(this) diff --git a/platform/core-api/src/com/intellij/psi/util/psiTreeUtil.kt b/platform/core-api/src/com/intellij/psi/util/psiTreeUtil.kt index 184f0a7c7c4c..1c7d0111755a 100644 --- a/platform/core-api/src/com/intellij/psi/util/psiTreeUtil.kt +++ b/platform/core-api/src/com/intellij/psi/util/psiTreeUtil.kt @@ -14,6 +14,12 @@ import org.jetbrains.annotations.ApiStatus import java.util.* import kotlin.reflect.KClass +val PsiElement.startOffset: Int + get() = textRange.startOffset + +val PsiElement.endOffset: Int + get() = textRange.endOffset + fun PsiElement.validOrNull(): PsiElement? = if (isValid) this else null // ----------- Walking children/siblings/parents ------------------------------------------------------------------------------------------- diff --git a/platform/lang-api/src/com/intellij/refactoring/suggested/Utils.kt b/platform/lang-api/src/com/intellij/refactoring/suggested/Utils.kt index 3105ca3ed034..50e8eeb3740e 100644 --- a/platform/lang-api/src/com/intellij/refactoring/suggested/Utils.kt +++ b/platform/lang-api/src/com/intellij/refactoring/suggested/Utils.kt @@ -3,37 +3,39 @@ package com.intellij.refactoring.suggested import com.intellij.openapi.editor.RangeMarker +import com.intellij.openapi.editor.asTextRange import com.intellij.openapi.editor.event.DocumentEvent import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElement import com.intellij.psi.SmartPointerManager import com.intellij.psi.SmartPsiElementPointer +@Suppress("DeprecatedCallableAddReplaceWith") +@Deprecated("Use TextRange(offset, offset + DocumentEvent.oldLength) directly") val DocumentEvent.oldRange: TextRange get() = TextRange(offset, offset + oldLength) +@Suppress("DeprecatedCallableAddReplaceWith") +@Deprecated("Use TextRange(offset, offset + DocumentEvent.newLength) directly") val DocumentEvent.newRange: TextRange get() = TextRange(offset, offset + newLength) +@Deprecated("Use range from com.intellij.openapi.editor", + replaceWith = ReplaceWith("this.asTextRange", "com.intellij.openapi.editor.range.asTextRange")) val RangeMarker.range: TextRange? - get() { - if (!isValid) return null - val start = startOffset - val end = endOffset - return if (start in 0..end) { - TextRange(start, end) - } - else { - // Probably a race condition had happened and range marker is invalidated - null - } - } + get() = this.asTextRange +@Deprecated("Use PsiElement.startOffset from com.intellij.psi", + replaceWith = ReplaceWith("this.startOffset", "com.intellij.psi.util.startOffset")) val PsiElement.startOffset: Int get() = textRange.startOffset +@Deprecated("Use PsiElement.endOffset from com.intellij.psi", + replaceWith = ReplaceWith("this.endOffset", "com.intellij.psi.util.endOffset")) val PsiElement.endOffset: Int get() = textRange.endOffset +@Deprecated("Use alternative method from com.intellij.psi package", + ReplaceWith("this.createSmartPointer()", "com.intellij.psi.createSmartPointer")) fun E.createSmartPointer(): SmartPsiElementPointer = SmartPointerManager.getInstance(project).createSmartPsiElementPointer(this)