|
| 1 | +/* |
| 2 | + * Copyright (c) 2017. tangzx([email protected]) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.tang.intellij.lua.psi |
| 18 | + |
| 19 | +import com.intellij.openapi.project.Project |
| 20 | +import com.intellij.psi.PsiElement |
| 21 | +import com.intellij.psi.PsiFileFactory |
| 22 | +import com.intellij.psi.util.PsiTreeUtil |
| 23 | +import com.tang.intellij.lua.comment.psi.LuaDocFieldDef |
| 24 | +import com.tang.intellij.lua.lang.LuaLanguage |
| 25 | + |
| 26 | +/** |
| 27 | + * |
| 28 | + * Created by TangZX on 2016/11/24. |
| 29 | + */ |
| 30 | +object LuaElementFactory { |
| 31 | + fun createFile(project: Project, content: String): LuaFile { |
| 32 | + val name = "dummy.lua" |
| 33 | + return PsiFileFactory.getInstance(project).createFileFromText(name, LuaLanguage.INSTANCE, content) as LuaFile |
| 34 | + } |
| 35 | + |
| 36 | + fun createIdentifier(project: Project, name: String): PsiElement { |
| 37 | + val content = "local $name = 0" |
| 38 | + val file = createFile(project, content) |
| 39 | + val def = PsiTreeUtil.findChildOfType(file, LuaNameDef::class.java)!! |
| 40 | + return def.firstChild |
| 41 | + } |
| 42 | + |
| 43 | + fun createLiteral(project: Project, value: String): LuaLiteralExpr { |
| 44 | + val content = "local a = " + value |
| 45 | + val file = createFile(project, content) |
| 46 | + return PsiTreeUtil.findChildOfType(file, LuaLiteralExpr::class.java)!! |
| 47 | + } |
| 48 | + |
| 49 | + fun createName(project: Project, name: String): PsiElement { |
| 50 | + val element = createWith(project, name + " = 1") |
| 51 | + return PsiTreeUtil.findChildOfType(element, LuaNameExpr::class.java)!! |
| 52 | + } |
| 53 | + |
| 54 | + fun newLine(project: Project): PsiElement { |
| 55 | + return createWith(project, "\n") |
| 56 | + } |
| 57 | + |
| 58 | + fun createWith(project: Project, code: String): PsiElement { |
| 59 | + val file = createFile(project, code) |
| 60 | + return file.firstChild |
| 61 | + } |
| 62 | + |
| 63 | + fun createDocIdentifier(project: Project, name: String): PsiElement { |
| 64 | + val element = createWith(project, "---@field $name string") |
| 65 | + val fieldDef = PsiTreeUtil.findChildOfType(element, LuaDocFieldDef::class.java)!! |
| 66 | + return fieldDef.id!! |
| 67 | + } |
| 68 | +} |
0 commit comments