Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use arg string value and arg name to guessType #496

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,15 @@ project(":") {
doLast {
val rev = getRev()
// reset
exec {
executable = "git"
args("reset", "HEAD", "--hard")
}
//exec {
//executable = "git"
//args("reset", "HEAD", "--hard")
//}
// clean untracked files
exec {
executable = "git"
args("clean", "-d", "-f")
}
//exec {
//executable = "git"
//args("clean", "-d", "-f")
//}
// switch
exec {
executable = if (isWin) "bunch/bin/bunch.bat" else "bunch/bin/bunch"
Expand Down Expand Up @@ -253,9 +253,9 @@ project(":") {
untilBuild.set(buildVersionData.untilBuild)
}

instrumentCode {
compilerVersion.set(buildVersionData.instrumentCodeCompilerVersion)
}
//instrumentCode {
// compilerVersion.set(buildVersionData.instrumentCodeCompilerVersion)
//}

publishPlugin {
token.set(System.getenv("IDEA_PUBLISH_TOKEN"))
Expand Down
84 changes: 83 additions & 1 deletion gen/com/tang/intellij/lua/psi/impl/LuaCallExprImpl.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ abstract class EmmyDebugProcessBase(session: XDebugSession) : LuaDebugProcess(se

override fun sessionInitialized() {
super.sessionInitialized()
session.setPauseActionSupported(true)
ApplicationManager.getApplication().executeOnPooledThread {
setupTransporter()
}
Expand All @@ -69,6 +70,7 @@ abstract class EmmyDebugProcessBase(session: XDebugSession) : LuaDebugProcess(se
registerBreakpoint(position, breakpoint)
}
}
session.setPauseActionSupported(true);
// send ready
transporter?.send(Message(MessageCMD.ReadyReq))
}
Expand Down Expand Up @@ -208,7 +210,6 @@ abstract class EmmyDebugProcessBase(session: XDebugSession) : LuaDebugProcess(se
override fun getEditorsProvider(): XDebuggerEditorsProvider {
return editorsProvider
}

fun addEvalResultHandler(handler: IEvalResultHandler) {
evalHandlers.add(handler)
}
Expand Down
174 changes: 167 additions & 7 deletions src/main/java/com/tang/intellij/lua/psi/LuaPsiImplUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.tang.intellij.lua.comment.psi.LuaDocTagVararg
import com.tang.intellij.lua.comment.psi.api.LuaComment
import com.tang.intellij.lua.lang.LuaIcons
import com.tang.intellij.lua.lang.type.LuaString
import com.tang.intellij.lua.psi.impl.*
import com.tang.intellij.lua.search.SearchContext
import com.tang.intellij.lua.stubs.LuaClassMemberStub
import com.tang.intellij.lua.stubs.LuaFuncBodyOwnerStub
Expand Down Expand Up @@ -188,26 +189,137 @@ fun guessParentType(callExpr: LuaCallExpr, context: SearchContext): ITy {
return callExpr.expr.guessType(context)
}

fun getStringValue(valueExpr: PsiElement): String {
if (valueExpr is LuaLiteralExprImpl)
{
return valueExpr.stringValue
}
else
{
if (valueExpr is LuaNameExpr)
{
val declaration = resolve(valueExpr as LuaNameExpr, SearchContext.get(valueExpr.project))
if (declaration is LuaNameExprImpl)
{
return declaration.text
}
else if(declaration is LuaNameDefImpl)
{
val exp = declaration?.parent?.parent?.lastChild?.lastChild
if (exp != null)
{
return getStringValue(exp)
}
}
}
else if (valueExpr is LuaIndexExpr)
{
val declaration = resolve(valueExpr as LuaIndexExpr, SearchContext.get(valueExpr.project))
if (declaration is LuaTableFieldImpl)
{
val strExp = declaration?.lastChild
if (strExp != null)
{
return getStringValue(strExp)
}
}
else if (declaration is LuaIndexExprImpl)
{
val exp = declaration?.parent?.parent?.lastChild?.lastChild
if (exp != null)
{
return getStringValue(exp)
}
}
}
}

return "";
}

fun getParamStringValue(valueExpr: PsiElement): String {
if (valueExpr is LuaNameExpr)
{
return valueExpr.text;
}
else if(valueExpr is LuaIndexExpr)
{
return valueExpr.lastChild.text;
}
return "";
}

fun getParamAllStringValue(valueExpr: PsiElement): String {
if (valueExpr is LuaNameExpr)
{
return valueExpr.text;
}
else if(valueExpr is LuaIndexExpr)
{
return valueExpr.text;
}
return "";
}

/**
* 获取第n个字符串参数
* @param callExpr callExpr
* *
* @return String PsiElement
*/
fun getStringArgByIndex(callExpr: LuaCallExpr, index: Int): PsiElement? {
val args = callExpr.args
var path: PsiElement? = null

when (args) {
is LuaSingleArg -> {
val expr = args.expr
if (expr is LuaLiteralExpr && index == 0) path = expr
}
is LuaListArgs -> args.exprList.let { list ->
if (list.isNotEmpty() && list.size > index) {
if (list[index] is LuaLiteralExpr) {
val valueExpr = list[index] as LuaLiteralExpr
if (valueExpr.kind == LuaLiteralKind.String)
path = valueExpr
}
else {
val context = SearchContext.get(callExpr.project)
if (list[index].guessType((context)).displayName == "string")
{
path = list[index]
}
}
}
}
}
return path
}

/**
* 获取第一个字符串参数
* 获取第n个参数的名字
* @param callExpr callExpr
* *
* @return String PsiElement
*/
fun getFirstStringArg(callExpr: LuaCallExpr): PsiElement? {
fun getParamNameByIndex(callExpr: LuaCallExpr, index: Int): PsiElement? {
val args = callExpr.args
var path: PsiElement? = null

when (args) {
is LuaSingleArg -> {
val expr = args.expr
if (expr is LuaLiteralExpr) path = expr
if (expr is LuaNameExpr && index == 0) path = expr
if (expr is LuaIndexExpr && index == 0) path = expr
}
is LuaListArgs -> args.exprList.let { list ->
if (list.isNotEmpty() && list[0] is LuaLiteralExpr) {
val valueExpr = list[0] as LuaLiteralExpr
if (valueExpr.kind == LuaLiteralKind.String)
path = valueExpr
if (list.isNotEmpty() && list.size > index) {
if (list[index] is LuaNameExpr) {
path = list[index]
}
else if (list[index] is LuaIndexExpr) {
path = list[index]
}
}
}
}
Expand Down Expand Up @@ -257,6 +369,54 @@ fun guessTypeAt(list: LuaExprList, context: SearchContext): ITy {
return Ty.UNKNOWN
}

fun getStringValue(typeName: LuaLiteralExpr): String {
return typeName.stringValue;
}

fun getNameExprStringValue(valueExpr: PsiElement): String {
val tree = LuaDeclarationTree.get(valueExpr.containingFile)
val declaration = tree.find(valueExpr as LuaExpr)?.firstDeclaration?.psi
val exp = declaration?.parent?.parent
if (exp != null)
{
val strExp = exp.lastChild.lastChild
if(strExp is LuaLiteralExprImpl)
{
val str = strExp.text
return str.substring(1, str.length - 1)
}
else
{
return getNameExprStringValue(strExp)
}

}
return "";
}

fun newType(typeName: String, ty: ITy, sourceStr: String, targetStr: String): ITy {
if (ty is TyArray) {
val t = typeName.substringBefore('[').trim()
val ty = TyLazyClass(t)
return TyArray(ty);
}
else if(ty is TySerializedGeneric){
val list = mutableListOf<ITy>();
ty.params.forEach {
var name = it.displayName
if(name.contains(sourceStr))
{
name = name.replace(sourceStr, targetStr)
}
list.add(newType(name, it, sourceStr, targetStr))
}
return TySerializedGeneric(list.toTypedArray(), ty.base)
}
else {
return TyLazyClass(typeName);
}
}

fun guessParentType(indexExpr: LuaIndexExpr, context: SearchContext): ITy {
val expr = PsiTreeUtil.getStubChildOfType(indexExpr, LuaExpr::class.java)
return expr?.guessType(context) ?: Ty.UNKNOWN
Expand Down