Skip to content

Commit a9f6850

Browse files
committed
clean code
1 parent 4d55984 commit a9f6850

13 files changed

+207
-232
lines changed

src/main/java/com/tang/intellij/lua/codeInsight/intention/CreateFieldFromParameterIntention.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.intellij.psi.PsiElement
1313
import com.intellij.psi.PsiFile
1414
import com.intellij.psi.util.PsiTreeUtil
1515
import com.intellij.util.IncorrectOperationException
16+
import com.tang.intellij.lua.codeInsight.template.macro.SuggestTypeMacro
1617
import com.tang.intellij.lua.psi.*
1718
import com.tang.intellij.lua.search.SearchContext
1819
import com.tang.intellij.lua.stubs.index.LuaClassIndex

src/main/java/com/tang/intellij/lua/codeInsight/intention/CreateFunctionDocIntention.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.template.TemplateManager
2020
import com.intellij.codeInsight.template.impl.MacroCallNode
2121
import com.intellij.codeInsight.template.impl.TextExpression
2222
import com.intellij.openapi.editor.Editor
23+
import com.tang.intellij.lua.codeInsight.template.macro.SuggestTypeMacro
2324
import com.tang.intellij.lua.psi.LuaCommentOwner
2425
import com.tang.intellij.lua.psi.LuaFuncBodyOwner
2526
import org.jetbrains.annotations.Nls

src/main/java/com/tang/intellij/lua/codeInsight/intention/CreateFunctionReturnAnnotationIntention.kt

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package com.tang.intellij.lua.codeInsight.intention
1818

19-
import com.intellij.codeInsight.template.TemplateManager
2019
import com.intellij.codeInsight.template.impl.MacroCallNode
2120
import com.intellij.codeInsight.template.impl.TextExpression
2221
import com.intellij.openapi.editor.Editor
23-
import com.intellij.psi.PsiElement
2422
import com.intellij.psi.util.PsiTreeUtil
23+
import com.tang.intellij.lua.codeInsight.template.macro.SuggestTypeMacro
24+
import com.tang.intellij.lua.comment.LuaCommentUtil
2525
import com.tang.intellij.lua.comment.psi.LuaDocReturnDef
2626
import com.tang.intellij.lua.psi.LuaCommentOwner
2727
import com.tang.intellij.lua.psi.LuaFuncBodyOwner
@@ -44,24 +44,11 @@ class CreateFunctionReturnAnnotationIntention : FunctionIntention() {
4444

4545
override fun invoke(bodyOwner: LuaFuncBodyOwner, editor: Editor) {
4646
if (bodyOwner is LuaCommentOwner) {
47-
val comment = bodyOwner.comment
48-
val funcBody = bodyOwner.funcBody
49-
if (funcBody != null) {
50-
val templateManager = TemplateManager.getInstance(editor.project)
51-
val template = templateManager.createTemplate("", "")
52-
if (comment != null) template.addTextSegment("\n")
47+
LuaCommentUtil.insertTemplate(bodyOwner, editor) { _, template ->
5348
template.addTextSegment("---@return ")
5449
val typeSuggest = MacroCallNode(SuggestTypeMacro())
5550
template.addVariable("returnType", typeSuggest, TextExpression("table"), false)
5651
template.addEndVariable()
57-
if (comment != null) {
58-
editor.caretModel.moveToOffset(comment.textOffset + comment.textLength)
59-
} else {
60-
template.addTextSegment("\n")
61-
val e: PsiElement = bodyOwner
62-
editor.caretModel.moveToOffset(e.node.startOffset)
63-
}
64-
templateManager.startTemplate(editor, template)
6552
}
6653
}
6754
}

src/main/java/com/tang/intellij/lua/codeInsight/intention/CreateParameterAnnotationIntention.kt

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
package com.tang.intellij.lua.codeInsight.intention
1818

1919
import com.intellij.codeInsight.intention.impl.BaseIntentionAction
20-
import com.intellij.codeInsight.template.TemplateManager
2120
import com.intellij.codeInsight.template.impl.MacroCallNode
2221
import com.intellij.codeInsight.template.impl.TextExpression
2322
import com.intellij.openapi.editor.Editor
2423
import com.intellij.openapi.project.Project
2524
import com.intellij.psi.PsiFile
2625
import com.intellij.psi.util.PsiTreeUtil
2726
import com.intellij.util.IncorrectOperationException
27+
import com.tang.intellij.lua.codeInsight.template.macro.SuggestTypeMacro
28+
import com.tang.intellij.lua.comment.LuaCommentUtil
2829
import com.tang.intellij.lua.psi.LuaCommentOwner
2930
import com.tang.intellij.lua.psi.LuaParamNameDef
3031
import org.jetbrains.annotations.Nls
@@ -62,25 +63,12 @@ class CreateParameterAnnotationIntention : BaseIntentionAction() {
6263

6364
val owner = PsiTreeUtil.getParentOfType(parDef, LuaCommentOwner::class.java)
6465
if (owner != null) {
65-
val comment = owner.comment
66-
67-
val templateManager = TemplateManager.getInstance(project)
68-
val template = templateManager.createTemplate("", "")
69-
if (comment != null)
70-
template.addTextSegment("\n")
71-
template.addTextSegment(String.format("---@param %s ", parDef.name))
72-
val name = MacroCallNode(SuggestTypeMacro())
73-
template.addVariable("type", name, TextExpression("table"), true)
74-
template.addEndVariable()
75-
76-
if (comment != null) {
77-
editor.caretModel.moveToOffset(comment.textOffset + comment.textLength)
78-
} else {
79-
editor.caretModel.moveToOffset(owner.node.startOffset)
80-
template.addTextSegment("\n")
66+
LuaCommentUtil.insertTemplate(owner, editor) { _, template ->
67+
template.addTextSegment(String.format("---@param %s ", parDef.name))
68+
val name = MacroCallNode(SuggestTypeMacro())
69+
template.addVariable("type", name, TextExpression("table"), true)
70+
template.addEndVariable()
8171
}
82-
83-
templateManager.startTemplate(editor, template)
8472
}
8573
}
8674
}

src/main/java/com/tang/intellij/lua/codeInsight/intention/CreateTypeAnnotationIntention.java

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.codeInsight.intention
18+
19+
import com.intellij.codeInsight.intention.impl.BaseIntentionAction
20+
import com.intellij.codeInsight.template.impl.MacroCallNode
21+
import com.intellij.codeInsight.template.impl.TextExpression
22+
import com.intellij.openapi.editor.Editor
23+
import com.intellij.openapi.project.Project
24+
import com.intellij.psi.PsiFile
25+
import com.tang.intellij.lua.codeInsight.template.macro.SuggestTypeMacro
26+
import com.tang.intellij.lua.comment.LuaCommentUtil
27+
import com.tang.intellij.lua.psi.LuaLocalDef
28+
import com.tang.intellij.lua.psi.LuaPsiTreeUtil
29+
30+
/**
31+
*
32+
* Created by TangZX on 2016/12/16.
33+
*/
34+
class CreateTypeAnnotationIntention : BaseIntentionAction() {
35+
36+
override fun getFamilyName(): String {
37+
return text
38+
}
39+
40+
override fun getText(): String {
41+
return "Create type annotation"
42+
}
43+
44+
override fun isAvailable(project: Project, editor: Editor, psiFile: PsiFile): Boolean {
45+
val localDef = LuaPsiTreeUtil.findElementOfClassAtOffset(psiFile, editor.caretModel.offset, LuaLocalDef::class.java, false)
46+
if (localDef != null) {
47+
val comment = localDef.comment
48+
return comment?.typeDef == null
49+
}
50+
return false
51+
}
52+
53+
override fun invoke(project: Project, editor: Editor, psiFile: PsiFile) {
54+
val localDef = LuaPsiTreeUtil.findElementOfClassAtOffset(psiFile, editor.caretModel.offset, LuaLocalDef::class.java, false)
55+
if (localDef != null) {
56+
LuaCommentUtil.insertTemplate(localDef, editor) { _, template ->
57+
template.addTextSegment("---@type ")
58+
val name = MacroCallNode(SuggestTypeMacro())
59+
template.addVariable("type", name, TextExpression("table"), true)
60+
template.addEndVariable()
61+
}
62+
}
63+
}
64+
}

src/main/java/com/tang/intellij/lua/codeInsight/intention/SetVisibilityIntention.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.intellij.codeInsight.template.impl.MacroCallNode
2121
import com.intellij.codeInsight.template.impl.TextExpression
2222
import com.intellij.openapi.editor.Editor
2323
import com.intellij.psi.PsiElement
24+
import com.tang.intellij.lua.codeInsight.template.macro.NamesMacro
2425
import com.tang.intellij.lua.comment.psi.LuaDocAccessModifier
2526
import com.tang.intellij.lua.psi.LuaClassMethodDef
2627
import com.tang.intellij.lua.psi.LuaCommentOwner

src/main/java/com/tang/intellij/lua/codeInsight/intention/SuggestTypeMacro.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/main/java/com/tang/intellij/lua/codeInsight/intention/NamesMacro.kt renamed to src/main/java/com/tang/intellij/lua/codeInsight/template/macro/NamesMacro.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.tang.intellij.lua.codeInsight.intention
17+
package com.tang.intellij.lua.codeInsight.template.macro
1818

1919
import com.intellij.codeInsight.lookup.LookupElement
2020
import com.intellij.codeInsight.lookup.LookupElementBuilder

src/main/java/com/tang/intellij/lua/codeInsight/template/macro/SuggestFirstLuaVarNameMacro.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)