<commit_before>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package git4idea.search

import com.intellij.ide.actions.searcheverywhere.FoundItemDescriptor
import com.intellij.ide.actions.searcheverywhere.SearchEverywhereContributorFactory
import com.intellij.ide.actions.searcheverywhere.WeightedSearchEverywhereContributor
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.NlsSafe
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.codeStyle.NameUtil
import com.intellij.ui.SimpleListCellRenderer
import com.intellij.ui.list.LeftRightRenderer
import com.intellij.util.Processor
import com.intellij.util.ui.EmptyIcon
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UI
import com.intellij.util.ui.UIUtil
import com.intellij.vcs.log.Hash
import com.intellij.vcs.log.VcsCommitMetadata
import com.intellij.vcs.log.VcsRef
import com.intellij.vcs.log.data.DataPack
import com.intellij.vcs.log.data.VcsLogData
import com.intellij.vcs.log.impl.VcsLogContentUtil
import com.intellij.vcs.log.impl.VcsProjectLog
import com.intellij.vcs.log.ui.render.LabelIcon
import com.intellij.vcs.log.util.VcsLogUtil
import com.intellij.vcs.log.util.containsAll
import com.intellij.vcs.log.visible.filters.VcsLogFilterObject
import git4idea.branch.GitBranchUtil
import git4idea.i18n.GitBundle
import git4idea.log.GitRefManager
import git4idea.repo.GitRepositoryManager
import javax.swing.JList
import javax.swing.ListCellRenderer

class GitSearchEverywhereContributor(private val project: Project) : WeightedSearchEverywhereContributor<Any>, DumbAware {

  // higher priority -> higher position
  private val COMMIT_BY_HASH_PRIORITY = 0 - 10
  private val LOCAL_BRANCH_PRIORITY = 0 - 20
  private val REMOTE_BRANCH_PRIORITY = 0 - 30
  private val TAG_PRIORITY = 0 - 40
  private val COMMIT_BY_MESSAGE_PRIORITY = 0 - 50

  override fun fetchWeightedElements(pattern: String,
                                     progressIndicator: ProgressIndicator,
                                     consumer: Processor<in FoundItemDescriptor<Any>>) {

    val logManager = VcsProjectLog.getInstance(project).logManager ?: return
    val dataManager = logManager.dataManager
    val storage = dataManager.storage
    val index = dataManager.index

    val dataPack = awaitFullLogDataPack(dataManager, progressIndicator) ?: return

    if (pattern.length >= 7 && VcsLogUtil.HASH_REGEX.matcher(pattern).matches()) {
      storage.findCommitId {
        progressIndicator.checkCanceled()
        it.hash.asString().startsWith(pattern, true) && dataPack.containsAll(listOf(it), storage)
      }?.let { commitId ->
        val id = storage.getCommitIndex(commitId.hash, commitId.root)
        dataManager.miniDetailsGetter.loadCommitsData(listOf(id), {
          consumer.process(FoundItemDescriptor(it, COMMIT_BY_HASH_PRIORITY))
        }, progressIndicator)
      }
    }

    val matcher = NameUtil.buildMatcher(pattern)
      .withCaseSensitivity(NameUtil.MatchingCaseSensitivity.NONE)
      .preferringStartMatches()
      .build()

    dataPack.refsModel.stream().forEach {
      progressIndicator.checkCanceled()
      if (matcher.matches(it.name)) {
        val weight = when (it.type) {
          GitRefManager.TAG -> TAG_PRIORITY
          GitRefManager.LOCAL_BRANCH -> LOCAL_BRANCH_PRIORITY
          GitRefManager.REMOTE_BRANCH -> REMOTE_BRANCH_PRIORITY
          GitRefManager.HEAD -> LOCAL_BRANCH_PRIORITY
          else -> REMOTE_BRANCH_PRIORITY
        }
        consumer.process(FoundItemDescriptor(it, weight))
      }
    }<commit_msg>[git] match git refs event if the pattern is in the middle<commit_after>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package git4idea.search

import com.intellij.ide.actions.searcheverywhere.FoundItemDescriptor
import com.intellij.ide.actions.searcheverywhere.SearchEverywhereContributorFactory
import com.intellij.ide.actions.searcheverywhere.WeightedSearchEverywhereContributor
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.NlsSafe
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.codeStyle.NameUtil
import com.intellij.ui.SimpleListCellRenderer
import com.intellij.ui.list.LeftRightRenderer
import com.intellij.util.Processor
import com.intellij.util.ui.EmptyIcon
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UI
import com.intellij.util.ui.UIUtil
import com.intellij.vcs.log.Hash
import com.intellij.vcs.log.VcsCommitMetadata
import com.intellij.vcs.log.VcsRef
import com.intellij.vcs.log.data.DataPack
import com.intellij.vcs.log.data.VcsLogData
import com.intellij.vcs.log.impl.VcsLogContentUtil
import com.intellij.vcs.log.impl.VcsProjectLog
import com.intellij.vcs.log.ui.render.LabelIcon
import com.intellij.vcs.log.util.VcsLogUtil
import com.intellij.vcs.log.util.containsAll
import com.intellij.vcs.log.visible.filters.VcsLogFilterObject
import git4idea.branch.GitBranchUtil
import git4idea.i18n.GitBundle
import git4idea.log.GitRefManager
import git4idea.repo.GitRepositoryManager
import javax.swing.JList
import javax.swing.ListCellRenderer

class GitSearchEverywhereContributor(private val project: Project) : WeightedSearchEverywhereContributor<Any>, DumbAware {

  // higher priority -> higher position
  private val COMMIT_BY_HASH_PRIORITY = 0 - 10
  private val LOCAL_BRANCH_PRIORITY = 0 - 20
  private val REMOTE_BRANCH_PRIORITY = 0 - 30
  private val TAG_PRIORITY = 0 - 40
  private val COMMIT_BY_MESSAGE_PRIORITY = 0 - 50

  override fun fetchWeightedElements(pattern: String,
                                     progressIndicator: ProgressIndicator,
                                     consumer: Processor<in FoundItemDescriptor<Any>>) {

    val logManager = VcsProjectLog.getInstance(project).logManager ?: return
    val dataManager = logManager.dataManager
    val storage = dataManager.storage
    val index = dataManager.index

    val dataPack = awaitFullLogDataPack(dataManager, progressIndicator) ?: return

    if (pattern.length >= 7 && VcsLogUtil.HASH_REGEX.matcher(pattern).matches()) {
      storage.findCommitId {
        progressIndicator.checkCanceled()
        it.hash.asString().startsWith(pattern, true) && dataPack.containsAll(listOf(it), storage)
      }?.let { commitId ->
        val id = storage.getCommitIndex(commitId.hash, commitId.root)
        dataManager.miniDetailsGetter.loadCommitsData(listOf(id), {
          consumer.process(FoundItemDescriptor(it, COMMIT_BY_HASH_PRIORITY))
        }, progressIndicator)
      }
    }

    val matcher = NameUtil.buildMatcher("*$pattern")
      .withCaseSensitivity(NameUtil.MatchingCaseSensitivity.NONE)
      .typoTolerant()
      .build()

    dataPack.refsModel.stream().forEach {
      progressIndicator.checkCanceled()
      if (matcher.matches(it.name)) {
        val weight = when (it.type) {
          GitRefManager.TAG -> TAG_PRIORITY
          GitRefManager.LOCAL_BRANCH -> LOCAL_BRANCH_PRIORITY
          GitRefManager.REMOTE_BRANCH -> REMOTE_BRANCH_PRIORITY
          GitRefManager.HEAD -> LOCAL_BRANCH_PRIORITY
          else -> REMOTE_BRANCH_PRIORITY
        }
        consumer.process(FoundItemDescriptor(it, weight))
      }
    }
{'fraction_non_alphanumeric': 0.085667215815486, 'fraction_numerical': 0.008363958940565202, 'mean_word_length': 4.046035805626598, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3836385', 'n_tokens_mistral': 2584, 'n_tokens_neox': 2498, 'n_words': 427}