<commit_before>package org.jetbrains.plugins.scala
package lang.languageLevel

import com.intellij.psi.{PsiFile, PsiElement}
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.ProjectFileIndex
import config.ScalaFacet

/**
 * @author Alefas
 * @since 24.10.12
 */
object ScalaLanguageLevel extends Enumeration {
  type ScalaLanguageLevel = ScalaLanguageLevel.Value

  val SCALA2_9 = Value("Scala 2.9")
  val SCALA2_10 = Value("Scala 2.10")
  val SCALA2_10_VIRTUALIZED = Value("Scala 2.10 virtualized")
  val SCALA2_11 = Value("Scala 2.11")
  val SCALA2_11_VIRTUALIZED = Value("Scala 2.11 virtualized")

  val DEFAULT_LANGUAGE_LEVEL = SCALA2_10

  def isVirtualized(languageLevel: ScalaLanguageLevel): Boolean = {
    languageLevel match {
      case SCALA2_10_VIRTUALIZED | SCALA2_11_VIRTUALIZED => true
      case _ => false
    }
  }

  def isThoughScala2_10(languageLevel: ScalaLanguageLevel): Boolean = {
    languageLevel match {
      case SCALA2_10 | SCALA2_10_VIRTUALIZED | SCALA2_11 | SCALA2_11_VIRTUALIZED => true
      case _ => false
    }
  }

  def isThoughScala2_11(languageLevel: ScalaLanguageLevel): Boolean = {
    languageLevel match {
      case SCALA2_11 | SCALA2_11_VIRTUALIZED => true
      case _ => false
    }
  }

  def valuesArray(): Array[ScalaLanguageLevel] = values.toArray

  implicit class ValueImpl(val languageLevel: ScalaLanguageLevel) extends AnyVal {
    def isVirtualized: Boolean = {
      languageLevel match {
        case SCALA2_10_VIRTUALIZED => true
        case SCALA2_11_VIRTUALIZED => true
        case _ => false
      }
    }

    def isThoughScala2_10: Boolean = {
      languageLevel match {
        case SCALA2_10 | SCALA2_10_VIRTUALIZED | SCALA2_11 | SCALA2_11_VIRTUALIZED => true
        case _ => false
      }
    }

    def isThoughScala2_11(languageLevel: ScalaLanguageLevel): Boolean = {
      languageLevel match {
        case SCALA2_11 | SCALA2_11_VIRTUALIZED => true
        case _ => false
      }
    }
  }

  def getLanguageLevel(element: PsiElement): ScalaLanguageLevel = {
    val file: PsiFile = element.getContainingFile
    if (file == null || file.getVirtualFile == null) return DEFAULT_LANGUAGE_LEVEL
    val module: Module = ProjectFileIndex.SERVICE.getInstance(element.getProject).getModuleForFile(file.getVirtualFile)
    if (module == null) return DEFAULT_LANGUAGE_LEVEL
    ScalaFacet.findIn(module).map(_.languageLevel).getOrElse(DEFAULT_LANGUAGE_LEVEL)
  }
}
<commit_msg>Better language level detection.<commit_after>package org.jetbrains.plugins.scala
package lang.languageLevel

import com.intellij.psi.{PsiFile, PsiElement}
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.ProjectFileIndex
import config.ScalaFacet
import annotation.tailrec

/**
 * @author Alefas
 * @since 24.10.12
 */
object ScalaLanguageLevel extends Enumeration {
  type ScalaLanguageLevel = ScalaLanguageLevel.Value

  val SCALA2_9 = Value("Scala 2.9")
  val SCALA2_10 = Value("Scala 2.10")
  val SCALA2_10_VIRTUALIZED = Value("Scala 2.10 virtualized")
  val SCALA2_11 = Value("Scala 2.11")
  val SCALA2_11_VIRTUALIZED = Value("Scala 2.11 virtualized")

  val DEFAULT_LANGUAGE_LEVEL = SCALA2_10

  def isVirtualized(languageLevel: ScalaLanguageLevel): Boolean = {
    languageLevel match {
      case SCALA2_10_VIRTUALIZED | SCALA2_11_VIRTUALIZED => true
      case _ => false
    }
  }

  def isThoughScala2_10(languageLevel: ScalaLanguageLevel): Boolean = {
    languageLevel match {
      case SCALA2_10 | SCALA2_10_VIRTUALIZED | SCALA2_11 | SCALA2_11_VIRTUALIZED => true
      case _ => false
    }
  }

  def isThoughScala2_11(languageLevel: ScalaLanguageLevel): Boolean = {
    languageLevel match {
      case SCALA2_11 | SCALA2_11_VIRTUALIZED => true
      case _ => false
    }
  }

  def valuesArray(): Array[ScalaLanguageLevel] = values.toArray

  implicit class ValueImpl(val languageLevel: ScalaLanguageLevel) extends AnyVal {
    def isVirtualized: Boolean = {
      languageLevel match {
        case SCALA2_10_VIRTUALIZED => true
        case SCALA2_11_VIRTUALIZED => true
        case _ => false
      }
    }

    def isThoughScala2_10: Boolean = {
      languageLevel match {
        case SCALA2_10 | SCALA2_10_VIRTUALIZED | SCALA2_11 | SCALA2_11_VIRTUALIZED => true
        case _ => false
      }
    }

    def isThoughScala2_11(languageLevel: ScalaLanguageLevel): Boolean = {
      languageLevel match {
        case SCALA2_11 | SCALA2_11_VIRTUALIZED => true
        case _ => false
      }
    }
  }

  def getLanguageLevel(element: PsiElement): ScalaLanguageLevel = {
    @tailrec
    def getContainingFileByContext(element: PsiElement): PsiFile = {
      element match {
        case file: PsiFile => file
        case null => null
        case elem => getContainingFileByContext(elem.getContext)
      }
    }
    val file: PsiFile = getContainingFileByContext(element)
    if (file == null || file.getVirtualFile == null) return DEFAULT_LANGUAGE_LEVEL
    val module: Module = ProjectFileIndex.SERVICE.getInstance(element.getProject).getModuleForFile(file.getVirtualFile)
    if (module == null) return DEFAULT_LANGUAGE_LEVEL
    ScalaFacet.findIn(module).map(_.languageLevel).getOrElse(DEFAULT_LANGUAGE_LEVEL)
  }
}

{'fraction_non_alphanumeric': 0.08319055777650866, 'fraction_numerical': 0.03693127736531506, 'mean_word_length': 3.6128182616330116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 32, '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': '4557861', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1778, 'n_words': 344}