More clear code

This commit is contained in:
Valentin Kipyatkov
2014-12-18 19:00:48 +03:00
parent 7893b1d83f
commit eb35827e9a
2 changed files with 14 additions and 13 deletions
@@ -42,32 +42,34 @@ public class KotlinCodeBlockSelectioner : ExtendWordSelectionHandlerBase() {
val node = e.getNode()!!
val start = findBlockContentStart(node)
val end = findBlockContentEnd(node, start)
val end = findBlockContentEnd(node)
if (end > start) {
result.addAll(ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, TextRange(start, end)))
}
result.addAll(ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, TextRange(start, end)))
result.addAll(ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, e.getTextRange()!!))
return result
}
private fun findBlockContentStart(parentNode: ASTNode): Int {
val node = parentNode.getChildren(null)
private fun findBlockContentStart(blockNode: ASTNode): Int {
val node = blockNode.getChildren(null)
.stream()
.dropWhile { it.getElementType() != JetTokens.LBRACE } // search for '{'
.drop(1) // skip it
.dropWhile { it is PsiWhiteSpace } // and skip all whitespaces
.firstOrNull() ?: parentNode
.firstOrNull() ?: blockNode
return node.getTextRange()!!.getStartOffset()
}
private fun findBlockContentEnd(parentNode: ASTNode, startOffset: Int): Int {
val node = parentNode.getChildren(null)
private fun findBlockContentEnd(blockNode: ASTNode): Int {
val node = blockNode.getChildren(null)
.reverse()
.stream()
.dropWhile { it.getElementType() != JetTokens.RBRACE } // search for '}'
.drop(1) // skip it
.dropWhile { it is PsiWhiteSpace && (it: PsiWhiteSpace).getTextRange()!!.getStartOffset() > startOffset } // and skip all whitespaces
.firstOrNull() ?: parentNode.getLastChildNode()!!
.dropWhile { it is PsiWhiteSpace } // and skip all whitespaces
.firstOrNull() ?: blockNode.getLastChildNode()!!
return node.getTextRange()!!.getEndOffset()
}
}
@@ -21,16 +21,15 @@ import com.intellij.psi.PsiElement
import org.jetbrains.jet.JetNodeTypes.*
import org.jetbrains.jet.lang.psi.JetContainerNode
import org.jetbrains.jet.lang.psi.JetElement
import org.jetbrains.jet.plugin.JetLanguage
public class KotlinWordSelectionFilter : Condition<PsiElement>{
override fun value(e: PsiElement): Boolean {
if (e.getLanguage() != JetLanguage.INSTANCE) return true
if (KotlinListSelectioner.canSelect(e)) return false
if (e is JetContainerNode) return false
val parent = e.getParent()
if (parent !is JetElement) return true
if (parent.getFirstChild().getNextSibling() == null) return false // skip nodes with the same range as their parent
if (e.getParent().getFirstChild().getNextSibling() == null) return false // skip nodes with the same range as their parent
return when (e.getNode().getElementType()) {
BLOCK, LITERAL_STRING_TEMPLATE_ENTRY -> false