<commit_before>/*
 * Copyright (c) 2017. tangzx(<EMAIL>)
 *
 * 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 com.tang.intellij.lua.stubs.types

import com.intellij.lang.ASTNode
import com.intellij.psi.stubs.*
import com.intellij.util.io.StringRef
import com.tang.intellij.lua.Constants
import com.tang.intellij.lua.lang.LuaLanguage
import com.tang.intellij.lua.psi.LuaFile
import com.tang.intellij.lua.psi.LuaFuncDef
import com.tang.intellij.lua.psi.LuaParamInfo
import com.tang.intellij.lua.psi.LuaPsiImplUtil
import com.tang.intellij.lua.psi.impl.LuaFuncDefImpl
import com.tang.intellij.lua.search.SearchContext
import com.tang.intellij.lua.stubs.LuaFuncStub
import com.tang.intellij.lua.stubs.LuaFuncStubImpl
import com.tang.intellij.lua.stubs.index.LuaClassMethodIndex
import com.tang.intellij.lua.stubs.index.LuaGlobalIndex
import com.tang.intellij.lua.stubs.index.LuaShortNameIndex
import com.tang.intellij.lua.stubs.index.StubKeys
import com.tang.intellij.lua.ty.Ty
import java.io.IOException

/**

 * Created by tangzx on 2016/11/26.
 */
class LuaFuncType : IStubElementType<LuaFuncStub, LuaFuncDef>("Global Function", LuaLanguage.INSTANCE) {

    override fun createPsi(luaGlobalFuncStub: LuaFuncStub): LuaFuncDef {
        return LuaFuncDefImpl(luaGlobalFuncStub, this)
    }

    override fun createStub(funcDef: LuaFuncDef, stubElement: StubElement<*>): LuaFuncStub {
        val nameRef = funcDef.nameIdentifier!!
        val searchContext = SearchContext(funcDef.project).setCurrentStubFile(funcDef.containingFile)
        val returnTypeSet = LuaPsiImplUtil.guessReturnTypeSetOriginal(funcDef, searchContext)
        val params = LuaPsiImplUtil.getParamsOriginal(funcDef)
        var moduleName = Constants.WORD_G
        val file = funcDef.containingFile
        if (file is LuaFile) moduleName = file.moduleName ?: Constants.WORD_G

        return LuaFuncStubImpl(nameRef.text, moduleName, params, returnTypeSet, stubElement)
    }

    override fun getExternalId() = "lua.global_func_def"

    override fun shouldCreateStub(node: ASTNode): Boolean {
        val element = node.psi
        if (element is LuaFuncDef) {
            return element.nameIdentifier != null
        }
        return false
    }

    @Throws(IOException::class)
    override fun serialize(luaGlobalFuncStub: LuaFuncStub, stubOutputStream: StubOutputStream) {
        stubOutputStream.writeName(luaGlobalFuncStub.name)
        stubOutputStream.writeName(luaGlobalFuncStub.module)

        // params
        val params = luaGlobalFuncStub.params
        stubOutputStream.writeByte(params.size)
        for (param in params) {
            LuaParamInfo.serialize(param, stubOutputStream)
        }

        Ty.serialize(luaGlobalFuncStub.returnTypeSet, stubOutputStream)<commit_msg>function forward declaration local f function f()end<commit_after>/*
 * Copyright (c) 2017. tangzx(<EMAIL>)
 *
 * 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 com.tang.intellij.lua.stubs.types

import com.intellij.lang.ASTNode
import com.intellij.psi.stubs.*
import com.intellij.util.io.StringRef
import com.tang.intellij.lua.Constants
import com.tang.intellij.lua.lang.LuaLanguage
import com.tang.intellij.lua.psi.*
import com.tang.intellij.lua.psi.impl.LuaFuncDefImpl
import com.tang.intellij.lua.search.SearchContext
import com.tang.intellij.lua.stubs.LuaFuncStub
import com.tang.intellij.lua.stubs.LuaFuncStubImpl
import com.tang.intellij.lua.stubs.index.LuaClassMethodIndex
import com.tang.intellij.lua.stubs.index.LuaGlobalIndex
import com.tang.intellij.lua.stubs.index.LuaShortNameIndex
import com.tang.intellij.lua.stubs.index.StubKeys
import com.tang.intellij.lua.ty.Ty
import java.io.IOException

/**

 * Created by tangzx on 2016/11/26.
 */
class LuaFuncType : IStubElementType<LuaFuncStub, LuaFuncDef>("Global Function", LuaLanguage.INSTANCE) {

    override fun createPsi(luaGlobalFuncStub: LuaFuncStub): LuaFuncDef {
        return LuaFuncDefImpl(luaGlobalFuncStub, this)
    }

    override fun createStub(funcDef: LuaFuncDef, stubElement: StubElement<*>): LuaFuncStub {
        val nameRef = funcDef.nameIdentifier!!
        val searchContext = SearchContext(funcDef.project).setCurrentStubFile(funcDef.containingFile)
        val returnTypeSet = LuaPsiImplUtil.guessReturnTypeSetOriginal(funcDef, searchContext)
        val params = LuaPsiImplUtil.getParamsOriginal(funcDef)
        var moduleName = Constants.WORD_G
        val file = funcDef.containingFile
        if (file is LuaFile) moduleName = file.moduleName ?: Constants.WORD_G

        return LuaFuncStubImpl(nameRef.text, moduleName, params, returnTypeSet, stubElement)
    }

    override fun getExternalId() = "lua.global_func_def"

    override fun shouldCreateStub(node: ASTNode): Boolean {
        val element = node.psi
        if (element is LuaFuncDef) {
            return element.nameIdentifier != null && element.forwardDeclaration == null
        }
        return false
    }

    @Throws(IOException::class)
    override fun serialize(luaGlobalFuncStub: LuaFuncStub, stubOutputStream: StubOutputStream) {
        stubOutputStream.writeName(luaGlobalFuncStub.name)
        stubOutputStream.writeName(luaGlobalFuncStub.module)

        // params
        val params = luaGlobalFuncStub.params
        stubOutputStream.writeByte(params.size)
        for (param in params) {
            LuaParamInfo.serialize(param, stubOutputStream)
        }

        Ty.serialize(luaGlobalFuncStub.returnTypeSet, stubOutputStream)
{'fraction_non_alphanumeric': 0.08777863182167564, 'fraction_numerical': 0.004919292851652575, 'mean_word_length': 3.9176114890400604, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3521521', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1999, 'n_words': 449}