Skip to content

Commit 00df449

Browse files
committed
SetVisibilityIntention
1 parent bd4e86c commit 00df449

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.lookup.LookupElement
20+
import com.intellij.codeInsight.lookup.LookupElementBuilder
21+
import com.intellij.codeInsight.template.Expression
22+
import com.intellij.codeInsight.template.ExpressionContext
23+
import com.intellij.codeInsight.template.Macro
24+
import com.intellij.codeInsight.template.Result
25+
26+
class NamesMacro(vararg val args: String) : Macro() {
27+
override fun getPresentableName() = "LuaNamesMacro"
28+
29+
override fun getName() = "LuaNamesMacro"
30+
31+
override fun calculateResult(p0: Array<out Expression>, p1: ExpressionContext?): Result? {
32+
return null
33+
}
34+
35+
override fun calculateLookupItems(params: Array<out Expression>, context: ExpressionContext?): Array<LookupElement>? {
36+
val list = mutableListOf<LookupElement>()
37+
for (name in args) {
38+
list.add(LookupElementBuilder.create(name))
39+
}
40+
return list.toTypedArray()
41+
}
42+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.template.TemplateManager
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.psi.PsiElement
24+
import com.tang.intellij.lua.comment.psi.LuaDocAccessModifier
25+
import com.tang.intellij.lua.psi.LuaClassMethodDef
26+
import com.tang.intellij.lua.psi.LuaCommentOwner
27+
import com.tang.intellij.lua.psi.LuaFuncBodyOwner
28+
29+
class SetVisibilityIntention : FunctionIntention() {
30+
31+
override fun isAvailable(bodyOwner: LuaFuncBodyOwner, editor: Editor): Boolean {
32+
if (bodyOwner is LuaClassMethodDef) {
33+
return bodyOwner.comment?.findTag(LuaDocAccessModifier::class.java) == null
34+
}
35+
return false
36+
}
37+
38+
override fun invoke(bodyOwner: LuaFuncBodyOwner, editor: Editor) {
39+
if (bodyOwner is LuaCommentOwner) {
40+
val comment = bodyOwner.comment
41+
val funcBody = bodyOwner.funcBody
42+
if (funcBody != null) {
43+
val templateManager = TemplateManager.getInstance(editor.project)
44+
val template = templateManager.createTemplate("", "")
45+
if (comment != null) template.addTextSegment("\n")
46+
template.addTextSegment("---@")
47+
val typeSuggest = MacroCallNode(NamesMacro("public", "protected", "private"))
48+
template.addVariable("visibility", typeSuggest, TextExpression("private"), false)
49+
template.addEndVariable()
50+
if (comment != null) {
51+
editor.caretModel.moveToOffset(comment.textOffset + comment.textLength)
52+
} else {
53+
template.addTextSegment("\n")
54+
val e: PsiElement = bodyOwner
55+
editor.caretModel.moveToOffset(e.node.startOffset)
56+
}
57+
templateManager.startTemplate(editor, template)
58+
}
59+
}
60+
}
61+
62+
override fun getFamilyName() = text
63+
64+
override fun getText() = "Set Visibility (public/protected/private)"
65+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@
271271
<category>Lua</category>
272272
<className>com.tang.intellij.lua.codeInsight.intention.InvertBooleanIntention</className>
273273
</intentionAction>
274+
<intentionAction>
275+
<category>Lua</category>
276+
<className>com.tang.intellij.lua.codeInsight.intention.SetVisibilityIntention</className>
277+
</intentionAction>
274278

275279
<localInspection
276280
implementationClass="com.tang.intellij.lua.codeInsight.inspection.DuplicateClassDeclaration"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
<html>
18+
<body>
19+
20+
</body>
21+
</html>

0 commit comments

Comments
 (0)