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 node = e.getNode()!!
val start = findBlockContentStart(node) 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()!!)) result.addAll(ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, e.getTextRange()!!))
return result return result
} }
private fun findBlockContentStart(parentNode: ASTNode): Int { private fun findBlockContentStart(blockNode: ASTNode): Int {
val node = parentNode.getChildren(null) val node = blockNode.getChildren(null)
.stream() .stream()
.dropWhile { it.getElementType() != JetTokens.LBRACE } // search for '{' .dropWhile { it.getElementType() != JetTokens.LBRACE } // search for '{'
.drop(1) // skip it .drop(1) // skip it
.dropWhile { it is PsiWhiteSpace } // and skip all whitespaces .dropWhile { it is PsiWhiteSpace } // and skip all whitespaces
.firstOrNull() ?: parentNode .firstOrNull() ?: blockNode
return node.getTextRange()!!.getStartOffset() return node.getTextRange()!!.getStartOffset()
} }
private fun findBlockContentEnd(parentNode: ASTNode, startOffset: Int): Int { private fun findBlockContentEnd(blockNode: ASTNode): Int {
val node = parentNode.getChildren(null) val node = blockNode.getChildren(null)
.reverse() .reverse()
.stream() .stream()
.dropWhile { it.getElementType() != JetTokens.RBRACE } // search for '}' .dropWhile { it.getElementType() != JetTokens.RBRACE } // search for '}'
.drop(1) // skip it .drop(1) // skip it
.dropWhile { it is PsiWhiteSpace && (it: PsiWhiteSpace).getTextRange()!!.getStartOffset() > startOffset } // and skip all whitespaces .dropWhile { it is PsiWhiteSpace } // and skip all whitespaces
.firstOrNull() ?: parentNode.getLastChildNode()!! .firstOrNull() ?: blockNode.getLastChildNode()!!
return node.getTextRange()!!.getEndOffset() return node.getTextRange()!!.getEndOffset()
} }
} }
@@ -21,16 +21,15 @@ import com.intellij.psi.PsiElement
import org.jetbrains.jet.JetNodeTypes.* import org.jetbrains.jet.JetNodeTypes.*
import org.jetbrains.jet.lang.psi.JetContainerNode import org.jetbrains.jet.lang.psi.JetContainerNode
import org.jetbrains.jet.lang.psi.JetElement import org.jetbrains.jet.lang.psi.JetElement
import org.jetbrains.jet.plugin.JetLanguage
public class KotlinWordSelectionFilter : Condition<PsiElement>{ public class KotlinWordSelectionFilter : Condition<PsiElement>{
override fun value(e: PsiElement): Boolean { override fun value(e: PsiElement): Boolean {
if (e.getLanguage() != JetLanguage.INSTANCE) return true
if (KotlinListSelectioner.canSelect(e)) return false if (KotlinListSelectioner.canSelect(e)) return false
if (e is JetContainerNode) return false if (e is JetContainerNode) return false
if (e.getParent().getFirstChild().getNextSibling() == null) return false // skip nodes with the same range as their parent
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
return when (e.getNode().getElementType()) { return when (e.getNode().getElementType()) {
BLOCK, LITERAL_STRING_TEMPLATE_ENTRY -> false BLOCK, LITERAL_STRING_TEMPLATE_ENTRY -> false