<commit_before>/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.intellij.plugins.hcl.terraform

import com.intellij.codeInsight.completion.CompletionParameters
import com.intellij.codeInsight.completion.CompletionProvider
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.completion.CompletionType
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.lang.injection.InjectedLanguageManager
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.patterns.ElementPattern
import com.intellij.patterns.PatternCondition
import com.intellij.patterns.PlatformPatterns.psiElement
import com.intellij.patterns.PlatformPatterns.psiFile
import com.intellij.patterns.PsiElementPattern
import com.intellij.patterns.StandardPatterns.not
import com.intellij.patterns.StandardPatterns.or
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiLanguageInjectionHost
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.impl.DebugUtil
import com.intellij.util.ProcessingContext
import org.intellij.plugins.hcl.HCLElementTypes
import org.intellij.plugins.hcl.codeinsight.HCLCompletionProvider
import org.intellij.plugins.hcl.psi.*
import org.intellij.plugins.hcl.terraform.config.TerraformLanguage
import org.intellij.plugins.hcl.terraform.config.model.*
import org.intellij.plugins.hcl.terraform.il.ILFileType
import java.util.*

public class TerraformConfigCompletionProvider : HCLCompletionProvider() {
  init {
    val WhiteSpace = psiElement(PsiWhiteSpace::class.java)
    val ID = psiElement(HCLElementTypes.ID)

    val Identifier = psiElement(HCLIdentifier::class.java)
    val File = psiElement(HCLFile::class.java)
    val Block = psiElement(HCLBlock::class.java)
    val Property = psiElement(HCLProperty::class.java)
    val Object = psiElement(HCLObject::class.java)

    val TerraformConfigFile = psiFile(HCLFile::class.java).withLanguage(TerraformLanguage)

    extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
        .inFile(TerraformConfigFile)
        .withParent(File)
        .andNot(psiElement().afterSiblingSkipping2(WhiteSpace, or(ID, Identifier))),
        BlockKeywordCompletionProvider);
    extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
        .inFile(TerraformConfigFile)
        .withParent(Identifier)
        .withSuperParent(2, Block)
        .withSuperParent(3, File)
        .withParent(not(psiElement(HCLIdentifier::class.java).afterSiblingSkipping2(WhiteSpace, or(ID, Identifier)))),
        BlockKeywordCompletionProvider);

    // TODO: Provide data from all resources in folder (?)

    if (MODEL_BASED_COMPLETION_ENABLED) {

      extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
          .inFile(TerraformConfigFile)
          .withParent(not(Identifier))
          .andOr(psiElement().withSuperParent(1, File), psiElement().withSuperParent(1, Block))
          .afterSiblingSkipping2(WhiteSpace, or(ID, Identifier))
          , BlockTypeOrNameCompletionProvider);
      extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
          .inFile(TerraformConfigFile)
          .withParent(psiElement(HCLIdentifier::class.java).afterSiblingSkipping2(WhiteSpace, or(ID, Identifier)))
          .andOr(psiElement().withSuperParent(2, File), psiElement().withSuperParent(2, Block))
          , BlockTypeOrNameCompletionProvider);


      extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
          .inFile(TerraformConfigFile)
          .withParent(Object)
          .withSuperParent(2, Block)
          , ResourcePropertiesCompletionProvider);

      extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
          .inFile(TerraformConfigFile)
          .withParent(Identifier)
          .withSuperParent(2, Property)
          .withSuperParent(3, Object)
          .withSuperParent(4, Block)
          , ResourcePropertiesCompletionProvider);
      extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
          .inFile(TerraformConfigFile)
          .withParent(Identifier)
          .withSuperParent(2, Block)
          .withSuperParent(3, Object)
          .withSuperParent(4, Block)
          , ResourcePropertiesCompletionProvider);

    }
  }

  companion object {
    public val BLOCK_KEYWORDS: TreeSet<String> = sortedSetOf(
        "atlas",
        "module",
        "output",
        "provider",
        "resource",
        "variable"
    )

    val MODEL_BASED_COMPLETION_ENABLED: Boolean by lazy {
      val application = ApplicationManager.getApplication()
      application.isUnitTestMode || application.isInternal
    }

    public val COMMON_RESOURCE_PROPERTIES: SortedSet<String> = DefaultResourceTypeProperties.map { it.name }.toSortedSet()

    private val LOG = Logger.getInstance(TerraformConfigCompletionProvider::class.java)
    fun DumpPsiFileModel(element: PsiElement): () -> String {
      return { DebugUtil.psiToString(element.containingFile, true) }
    }
  }

  private abstract class OurCompletionProvider : CompletionProvider<CompletionParameters>() {<commit_msg>Make terraform completion work not only in internal mode<commit_after>/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.intellij.plugins.hcl.terraform

import com.intellij.codeInsight.completion.CompletionParameters
import com.intellij.codeInsight.completion.CompletionProvider
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.completion.CompletionType
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.lang.injection.InjectedLanguageManager
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.patterns.ElementPattern
import com.intellij.patterns.PatternCondition
import com.intellij.patterns.PlatformPatterns.psiElement
import com.intellij.patterns.PlatformPatterns.psiFile
import com.intellij.patterns.PsiElementPattern
import com.intellij.patterns.StandardPatterns.not
import com.intellij.patterns.StandardPatterns.or
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiLanguageInjectionHost
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.impl.DebugUtil
import com.intellij.util.ProcessingContext
import org.intellij.plugins.hcl.HCLElementTypes
import org.intellij.plugins.hcl.codeinsight.HCLCompletionProvider
import org.intellij.plugins.hcl.psi.*
import org.intellij.plugins.hcl.terraform.config.TerraformLanguage
import org.intellij.plugins.hcl.terraform.config.model.*
import org.intellij.plugins.hcl.terraform.il.ILFileType
import java.util.*

public class TerraformConfigCompletionProvider : HCLCompletionProvider() {
  init {
    val WhiteSpace = psiElement(PsiWhiteSpace::class.java)
    val ID = psiElement(HCLElementTypes.ID)

    val Identifier = psiElement(HCLIdentifier::class.java)
    val File = psiElement(HCLFile::class.java)
    val Block = psiElement(HCLBlock::class.java)
    val Property = psiElement(HCLProperty::class.java)
    val Object = psiElement(HCLObject::class.java)

    val TerraformConfigFile = psiFile(HCLFile::class.java).withLanguage(TerraformLanguage)

    extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
        .inFile(TerraformConfigFile)
        .withParent(File)
        .andNot(psiElement().afterSiblingSkipping2(WhiteSpace, or(ID, Identifier))),
        BlockKeywordCompletionProvider);
    extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
        .inFile(TerraformConfigFile)
        .withParent(Identifier)
        .withSuperParent(2, Block)
        .withSuperParent(3, File)
        .withParent(not(psiElement(HCLIdentifier::class.java).afterSiblingSkipping2(WhiteSpace, or(ID, Identifier)))),
        BlockKeywordCompletionProvider);

    // TODO: Provide data from all resources in folder (?)

    if (MODEL_BASED_COMPLETION_ENABLED) {

      extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
          .inFile(TerraformConfigFile)
          .withParent(not(Identifier))
          .andOr(psiElement().withSuperParent(1, File), psiElement().withSuperParent(1, Block))
          .afterSiblingSkipping2(WhiteSpace, or(ID, Identifier))
          , BlockTypeOrNameCompletionProvider);
      extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
          .inFile(TerraformConfigFile)
          .withParent(psiElement(HCLIdentifier::class.java).afterSiblingSkipping2(WhiteSpace, or(ID, Identifier)))
          .andOr(psiElement().withSuperParent(2, File), psiElement().withSuperParent(2, Block))
          , BlockTypeOrNameCompletionProvider);


      extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
          .inFile(TerraformConfigFile)
          .withParent(Object)
          .withSuperParent(2, Block)
          , ResourcePropertiesCompletionProvider);

      extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
          .inFile(TerraformConfigFile)
          .withParent(Identifier)
          .withSuperParent(2, Property)
          .withSuperParent(3, Object)
          .withSuperParent(4, Block)
          , ResourcePropertiesCompletionProvider);
      extend(CompletionType.BASIC, psiElement(HCLElementTypes.ID)
          .inFile(TerraformConfigFile)
          .withParent(Identifier)
          .withSuperParent(2, Block)
          .withSuperParent(3, Object)
          .withSuperParent(4, Block)
          , ResourcePropertiesCompletionProvider);

    }
  }

  companion object {
    public val BLOCK_KEYWORDS: TreeSet<String> = sortedSetOf(
        "atlas",
        "module",
        "output",
        "provider",
        "resource",
        "variable"
    )

    val MODEL_BASED_COMPLETION_ENABLED: Boolean by lazy {
      val application = ApplicationManager.getApplication()
      true || application.isUnitTestMode || application.isInternal
    }

    public val COMMON_RESOURCE_PROPERTIES: SortedSet<String> = DefaultResourceTypeProperties.map { it.name }.toSortedSet()

    private val LOG = Logger.getInstance(TerraformConfigCompletionProvider::class.java)
    fun DumpPsiFileModel(element: PsiElement): () -> String {
      return { DebugUtil.psiToString(element.containingFile, true) }
    }
  }

  private abstract class OurCompletionProvider : CompletionProvider<CompletionParameters>() {
{'fraction_non_alphanumeric': 0.09219919153693988, 'fraction_numerical': 0.004988389094349359, 'mean_word_length': 4.193389906208129, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1466215', 'n_tokens_mistral': 3422, 'n_tokens_neox': 3386, 'n_words': 609}