Skip to content

Commit 7585c89

Browse files
committed
Do not complain about missing function bodies + make AddFunctionBlock an intention
1 parent 8019364 commit 7585c89

File tree

10 files changed

+142
-6
lines changed

10 files changed

+142
-6
lines changed

resources/META-INF/gogland.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@
175175

176176
<lang.inspectionSuppressor language="go" implementationClass="com.goide.inspections.suppression.GoInspectionSuppressor"/>
177177

178+
<!-- intentions -->
179+
<intentionAction>
180+
<bundleName>com.goide.GoBundle</bundleName>
181+
<categoryKey>go.intentions.category</categoryKey>
182+
<className>com.goide.intentions.GoAddFunctionBlockIntention</className>
183+
</intentionAction>
178184
<!-- unused inspections -->
179185
<localInspection language="go" displayName="Unused import inspection" groupPath="Go"
180186
groupName="Declaration redundancy" enabledByDefault="true" level="ERROR"

resources/com/goide/GoBundle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616

1717

1818
# suppress inspection "UnusedProperty" – the property is required for showing list of links to nested configurables on root Go-configurable
19-
go.settings.description=
19+
go.settings.description=
20+
go.intentions.category=Go
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func functionName() <spot>{
2+
3+
}</spot>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
func functionName()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!--
2+
~ Copyright 2013-2016 Sergey Ignatov, Alexander Zolotov, Florin Patan
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+
This intention adds braces to function or method without a block.<br>
20+
</body>
21+
</html>

src/com/goide/editor/smart/GoSmartEnterProcessor.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ public void apply(@NotNull Editor editor, @NotNull SmartEnterProcessorWithFixers
107107
}
108108
}
109109

110-
private static class PlainEnterProcessor extends FixEnterProcessor {
110+
public static class PlainEnterProcessor extends FixEnterProcessor {
111111
@Nullable
112112
private static GoBlock findBlock(@Nullable PsiElement element) {
113-
element = PsiTreeUtil.getParentOfType(element, GoStatement.class, GoBlock.class, GoFunctionOrMethodDeclaration.class,
114-
GoFunctionLit.class);
113+
element = PsiTreeUtil.getNonStrictParentOfType(element, GoStatement.class, GoBlock.class, GoFunctionOrMethodDeclaration.class,
114+
GoFunctionLit.class);
115115
if (element instanceof GoSimpleStatement && element.getParent() instanceof GoStatement) {
116116
element = element.getParent();
117117
}
@@ -127,8 +127,7 @@ private static GoBlock findBlock(@Nullable PsiElement element) {
127127
public boolean doEnter(PsiElement psiElement, PsiFile file, @NotNull Editor editor, boolean modified) {
128128
GoBlock block = findBlock(psiElement);
129129
if (block != null) {
130-
int offset = block.getTextOffset() + 1;
131-
editor.getCaretModel().moveToOffset(offset);
130+
editor.getCaretModel().moveToOffset(block.getLbrace().getTextRange().getEndOffset());
132131
}
133132
plainEnter(editor);
134133
return true;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2013-2016 Sergey Ignatov, Alexander Zolotov, Florin Patan
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.goide.intentions;
18+
19+
import com.goide.editor.smart.GoSmartEnterProcessor;
20+
import com.goide.psi.GoBlock;
21+
import com.goide.psi.GoFunctionOrMethodDeclaration;
22+
import com.goide.psi.impl.GoElementFactory;
23+
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction;
24+
import com.intellij.openapi.editor.Editor;
25+
import com.intellij.openapi.project.Project;
26+
import com.intellij.psi.PsiDocumentManager;
27+
import com.intellij.psi.PsiElement;
28+
import com.intellij.util.IncorrectOperationException;
29+
import com.intellij.util.ObjectUtils;
30+
import org.jetbrains.annotations.Nls;
31+
import org.jetbrains.annotations.NotNull;
32+
33+
public class GoAddFunctionBlockIntention extends BaseElementAtCaretIntentionAction {
34+
public static final String NAME = "Add function body";
35+
36+
public GoAddFunctionBlockIntention() {
37+
setText(NAME);
38+
}
39+
40+
@Nls
41+
@NotNull
42+
@Override
43+
public String getFamilyName() {
44+
return NAME;
45+
}
46+
47+
@Override
48+
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
49+
PsiElement parent = element.getParent();
50+
return parent instanceof GoFunctionOrMethodDeclaration && ((GoFunctionOrMethodDeclaration)parent).getBlock() == null;
51+
}
52+
53+
@Override
54+
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
55+
PsiElement parent = element.getParent();
56+
if (parent instanceof GoFunctionOrMethodDeclaration) {
57+
GoBlock block = ((GoFunctionOrMethodDeclaration)parent).getBlock();
58+
if (block == null) {
59+
GoBlock newBlock = ObjectUtils.tryCast(parent.add(GoElementFactory.createBlock(project)), GoBlock.class);
60+
if (newBlock != null) {
61+
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
62+
new GoSmartEnterProcessor.PlainEnterProcessor().doEnter(newBlock, newBlock.getContainingFile(), editor, false);
63+
}
64+
}
65+
}
66+
}
67+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
func _() int {
4+
<caret>
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package main
2+
3+
func _<caret>() int
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2013-2016 Sergey Ignatov, Alexander Zolotov, Florin Patan
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.goide.intentions;
18+
19+
import com.goide.quickfix.GoQuickFixTestBase;
20+
21+
public class GoAddFunctionBlockIntentionTest extends GoQuickFixTestBase {
22+
public void testSimple() {
23+
doTest(GoAddFunctionBlockIntention.NAME, true);
24+
}
25+
26+
@Override
27+
protected String getBasePath() {
28+
return "intentions/add-missing-body";
29+
}
30+
}

0 commit comments

Comments
 (0)