Skip to content

Commit 4d55984

Browse files
committed
intentions
1 parent 00df449 commit 4d55984

File tree

8 files changed

+435
-284
lines changed

8 files changed

+435
-284
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.openapi.editor.Editor
21+
import com.intellij.openapi.project.Project
22+
import com.intellij.psi.PsiDocumentManager
23+
import com.intellij.psi.PsiFile
24+
import com.intellij.psi.codeStyle.CodeStyleManager
25+
import com.tang.intellij.lua.psi.LuaCallExpr
26+
import com.tang.intellij.lua.psi.LuaPsiTreeUtil
27+
import com.tang.intellij.lua.psi.LuaTypes
28+
29+
class AppendCallParenIntention : BaseIntentionAction() {
30+
override fun getFamilyName() = "Append call paren"
31+
32+
override fun getText() = familyName
33+
34+
override fun isAvailable(project: Project, editor: Editor, psiFile: PsiFile): Boolean {
35+
val callExpr = LuaPsiTreeUtil.findElementOfClassAtOffset(psiFile, editor.caretModel.offset, LuaCallExpr::class.java, false)
36+
if (callExpr != null) {
37+
val childByType = callExpr.args.node.findChildByType(LuaTypes.LPAREN)
38+
return childByType == null
39+
}
40+
return false
41+
}
42+
43+
override fun invoke(project: Project, editor: Editor, psiFile: PsiFile) {
44+
val callExpr = LuaPsiTreeUtil.findElementOfClassAtOffset(psiFile, editor.caretModel.offset, LuaCallExpr::class.java, false)
45+
if (callExpr != null) {
46+
val argsNode = callExpr.args.node
47+
editor.document.insertString(argsNode.startOffset + argsNode.textLength, ")")
48+
editor.document.insertString(argsNode.startOffset, "(")
49+
50+
PsiDocumentManager.getInstance(project).commitDocument(editor.document)
51+
CodeStyleManager.getInstance(project).reformat(callExpr)
52+
}
53+
}
54+
}
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.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.tang.intellij.lua.psi.LuaCommentOwner
24+
import com.tang.intellij.lua.psi.LuaFuncBodyOwner
25+
import org.jetbrains.annotations.Nls
26+
27+
28+
class CreateFunctionDocIntention : FunctionIntention() {
29+
override fun isAvailable(bodyOwner: LuaFuncBodyOwner, editor: Editor): Boolean {
30+
if (bodyOwner is LuaCommentOwner) {
31+
return bodyOwner.comment == null || bodyOwner.funcBody == null
32+
}
33+
return false
34+
}
35+
36+
@Nls
37+
override fun getFamilyName() = text
38+
39+
override fun getText() = "Create LuaDoc"
40+
41+
override fun invoke(bodyOwner: LuaFuncBodyOwner, editor: Editor) {
42+
val funcBody = bodyOwner.funcBody
43+
if (funcBody != null) {
44+
val templateManager = TemplateManager.getInstance(bodyOwner.project)
45+
val template = templateManager.createTemplate("", "")
46+
template.addTextSegment("---" + bodyOwner.name!!)
47+
val typeSuggest = MacroCallNode(SuggestTypeMacro())
48+
49+
// params
50+
val parDefList = funcBody.paramNameDefList
51+
for (parDef in parDefList) {
52+
template.addTextSegment(String.format("\n---@param %s ", parDef.name))
53+
template.addVariable(parDef.name, typeSuggest, TextExpression("table"), false)
54+
}
55+
56+
template.addEndVariable()
57+
template.addTextSegment("\n")
58+
59+
val textOffset = bodyOwner.node.startOffset
60+
editor.caretModel.moveToOffset(textOffset)
61+
templateManager.startTemplate(editor, template)
62+
}
63+
}
64+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.intellij.psi.util.PsiTreeUtil
25+
import com.tang.intellij.lua.comment.psi.LuaDocReturnDef
26+
import com.tang.intellij.lua.psi.LuaCommentOwner
27+
import com.tang.intellij.lua.psi.LuaFuncBodyOwner
28+
import org.jetbrains.annotations.Nls
29+
30+
31+
class CreateFunctionReturnAnnotationIntention : FunctionIntention() {
32+
override fun isAvailable(bodyOwner: LuaFuncBodyOwner, editor: Editor): Boolean {
33+
if (bodyOwner is LuaCommentOwner) {
34+
val comment = bodyOwner.comment
35+
return comment == null || PsiTreeUtil.getChildrenOfType(comment, LuaDocReturnDef::class.java) == null
36+
}
37+
return false
38+
}
39+
40+
@Nls
41+
override fun getFamilyName() = text
42+
43+
override fun getText() = "Create return annotation"
44+
45+
override fun invoke(bodyOwner: LuaFuncBodyOwner, editor: Editor) {
46+
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")
53+
template.addTextSegment("---@return ")
54+
val typeSuggest = MacroCallNode(SuggestTypeMacro())
55+
template.addVariable("returnType", typeSuggest, TextExpression("table"), false)
56+
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)
65+
}
66+
}
67+
}
68+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.TemplateManager
21+
import com.intellij.codeInsight.template.impl.MacroCallNode
22+
import com.intellij.codeInsight.template.impl.TextExpression
23+
import com.intellij.openapi.editor.Editor
24+
import com.intellij.openapi.project.Project
25+
import com.intellij.psi.PsiFile
26+
import com.intellij.psi.util.PsiTreeUtil
27+
import com.intellij.util.IncorrectOperationException
28+
import com.tang.intellij.lua.psi.LuaCommentOwner
29+
import com.tang.intellij.lua.psi.LuaParamNameDef
30+
import org.jetbrains.annotations.Nls
31+
32+
class CreateParameterAnnotationIntention : BaseIntentionAction() {
33+
@Nls
34+
override fun getFamilyName() = text
35+
36+
override fun getText() = "Create parameter annotation"
37+
38+
override fun isAvailable(project: Project, editor: Editor, psiFile: PsiFile): Boolean {
39+
val offset = editor.caretModel.offset
40+
val name = findParamName(psiFile, offset) ?: findParamName(psiFile, offset - 1)
41+
return name != null //&& name.funcBodyOwner !is LuaClosureExpr
42+
}
43+
44+
private fun findParamName(psiFile: PsiFile, offset:Int): LuaParamNameDef? {
45+
var element = psiFile.findElementAt(offset)
46+
if (element != null) {
47+
element = element.parent
48+
if (element is LuaParamNameDef) {
49+
val commentOwner = PsiTreeUtil.getParentOfType(element, LuaCommentOwner::class.java)
50+
val comment = commentOwner?.comment
51+
comment?.getParamDef(element.name) ?: return element
52+
}
53+
}
54+
return null
55+
}
56+
57+
@Throws(IncorrectOperationException::class)
58+
override fun invoke(project: Project, editor: Editor, psiFile: PsiFile) {
59+
val offset = editor.caretModel.offset
60+
val parDef = findParamName(psiFile, offset) ?: findParamName(psiFile, offset - 1)
61+
parDef ?: return
62+
63+
val owner = PsiTreeUtil.getParentOfType(parDef, LuaCommentOwner::class.java)
64+
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")
81+
}
82+
83+
templateManager.startTemplate(editor, template)
84+
}
85+
}
86+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.openapi.editor.Editor
21+
import com.intellij.openapi.project.Project
22+
import com.intellij.psi.PsiFile
23+
import com.intellij.util.IncorrectOperationException
24+
import com.tang.intellij.lua.psi.LuaClosureExpr
25+
import com.tang.intellij.lua.psi.LuaFuncBodyOwner
26+
import com.tang.intellij.lua.psi.LuaPsiTreeUtil
27+
28+
abstract class FunctionIntention : BaseIntentionAction() {
29+
override fun isAvailable(project: Project, editor: Editor, psiFile: PsiFile): Boolean {
30+
val bodyOwner = LuaPsiTreeUtil.findElementOfClassAtOffset(psiFile, editor.caretModel.offset, LuaFuncBodyOwner::class.java, false)
31+
//不对Closure生效
32+
if (bodyOwner == null || bodyOwner is LuaClosureExpr)
33+
return false
34+
35+
//不在body内
36+
val contains = bodyOwner.funcBody?.textRange?.contains(editor.caretModel.offset)
37+
if (contains != null && contains)
38+
return false
39+
40+
return isAvailable(bodyOwner, editor)
41+
}
42+
43+
abstract fun isAvailable(bodyOwner: LuaFuncBodyOwner, editor: Editor): Boolean
44+
45+
@Throws(IncorrectOperationException::class)
46+
override fun invoke(project: Project, editor: Editor, psiFile: PsiFile) {
47+
val bodyOwner = LuaPsiTreeUtil.findElementOfClassAtOffset(psiFile, editor.caretModel.offset, LuaFuncBodyOwner::class.java, false)
48+
if (bodyOwner != null) invoke(bodyOwner, editor)
49+
}
50+
51+
abstract fun invoke(bodyOwner: LuaFuncBodyOwner, editor: Editor)
52+
}

0 commit comments

Comments
 (0)