<commit_before>package org.jetbrains.plugins.scala
package lang
package psi
package impl
package toplevel
package imports

import api.toplevel.imports.{ScImportSelectors, ScImportExpr, ScImportSelector}
import com.intellij.lang.ASTNode
import com.intellij.psi.tree.IElementType
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.PsiElement
import lexer.ScalaTokenTypes
import org.jetbrains.plugins.scala.lang.psi.api.base.ScStableCodeReferenceElement
import parser.ScalaElementTypes
import stubs.ScImportSelectorStub

/** 
* @author <NAME>
* Date: 20.02.2008
*/

class ScImportSelectorImpl extends ScalaStubBasedElementImpl[ScImportSelector] with ScImportSelector {
  def this(node: ASTNode) = {this(); setNode(node)}
  def this(stub: ScImportSelectorStub) = {this(); setStub(stub); setNode(null)}

  override def toString: String = "ImportSelector"

  def importedName: String = {
    val stub = getStub
    if (stub != null) {
      return stub.asInstanceOf[ScImportSelectorStub].importedName
    }
    val id = findChildByType(TokenSets.ID_SET)
    if (id == null) reference.refName else id.getText
  }

  def reference: ScStableCodeReferenceElement = {
    val stub = getStub
    if (stub != null) {
      return stub.asInstanceOf[ScImportSelectorStub].reference
    }
    (getFirstChild match {case s: ScStableCodeReferenceElement => s case _ => null})
  }

  def deleteSelector() {
    val expr: ScImportExpr = PsiTreeUtil.getParentOfType(this, classOf[ScImportExpr])
    if (expr.selectors.length + (if (expr.singleWildcard) 1 else 0) == 1) {
      expr.deleteExpr()
    }
    val forward: Boolean = expr.selectors.apply(0) == this
    var node = this.getNode
    var prev = if (forward) node.getTreeNext else node.getTreePrev
    var t: IElementType = null
    do {
      node.getTreeParent.removeChild(node)
      node = prev
      if (node != null) {
        prev = if (forward) node.getTreeNext else node.getTreePrev
        t = node.getElementType
      }
    } while (node != null && !(t == ScalaElementTypes.IMPORT_SELECTOR || t == ScalaTokenTypes.tUNDER))
  }

  def isAliasedImport: Boolean = {
    getStub match {
      case stub: ScImportSelectorStub => stub.isAliasedImport
      case _ =>
        PsiTreeUtil.getParentOfType(this, classOf[ScImportExpr]).selectors.length > 0 &&
                !getLastChild.isInstanceOf[ScStableCodeReferenceElement]
    }
  }
}<commit_msg>Remove braces from import expr if only one selector left #SCL-5036 fixed<commit_after>package org.jetbrains.plugins.scala
package lang
package psi
package impl
package toplevel
package imports

import api.toplevel.imports.{ScImportExpr, ScImportSelector}
import com.intellij.lang.ASTNode
import com.intellij.psi.tree.IElementType
import com.intellij.psi.util.PsiTreeUtil
import lexer.ScalaTokenTypes
import org.jetbrains.plugins.scala.lang.psi.api.base.ScStableCodeReferenceElement
import parser.ScalaElementTypes
import stubs.ScImportSelectorStub

/** 
* @author <NAME>
* Date: 20.02.2008
*/

class ScImportSelectorImpl extends ScalaStubBasedElementImpl[ScImportSelector] with ScImportSelector {
  def this(node: ASTNode) = {this(); setNode(node)}
  def this(stub: ScImportSelectorStub) = {this(); setStub(stub); setNode(null)}

  override def toString: String = "ImportSelector"

  def importedName: String = {
    val stub = getStub
    if (stub != null) {
      return stub.asInstanceOf[ScImportSelectorStub].importedName
    }
    val id = findChildByType(TokenSets.ID_SET)
    if (id == null) reference.refName else id.getText
  }

  def reference: ScStableCodeReferenceElement = {
    val stub = getStub
    if (stub != null) {
      return stub.asInstanceOf[ScImportSelectorStub].reference
    }
    getFirstChild match {case s: ScStableCodeReferenceElement => s case _ => null}
  }

  def deleteSelector() {
    val expr: ScImportExpr = PsiTreeUtil.getParentOfType(this, classOf[ScImportExpr])
    if (expr.selectors.length + (if (expr.singleWildcard) 1 else 0) == 1) {
      expr.deleteExpr()
    }
    val forward: Boolean = expr.selectors.apply(0) == this
    var node = this.getNode
    var prev = if (forward) node.getTreeNext else node.getTreePrev
    var t: IElementType = null
    do {
      node.getTreeParent.removeChild(node)
      node = prev
      if (node != null) {
        prev = if (forward) node.getTreeNext else node.getTreePrev
        t = node.getElementType
      }
    } while (node != null && !(t == ScalaElementTypes.IMPORT_SELECTOR || t == ScalaTokenTypes.tUNDER))

    expr.selectors match {
      case Seq(sel: ScImportSelector) if !sel.isAliasedImport =>
        val withoutBracesText = expr.qualifier.getText + "." + sel.reference.getText
        val newImportExpr = ScalaPsiElementFactory.createImportExprFromText(withoutBracesText, expr.getManager)
        expr.replace(newImportExpr)
      case _ =>
    }
  }

  def isAliasedImport: Boolean = {
    getStub match {
      case stub: ScImportSelectorStub => stub.isAliasedImport
      case _ =>
        PsiTreeUtil.getParentOfType(this, classOf[ScImportExpr]).selectors.length > 0 &&
                !getLastChild.isInstanceOf[ScStableCodeReferenceElement]
    }
  }
}
{'fraction_non_alphanumeric': 0.09928391716663441, 'fraction_numerical': 0.005806077027288562, 'mean_word_length': 3.7543698252069917, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2774542', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1614, 'n_words': 361}