Skip to content

Commit

Permalink
Do not complain about missing function bodies + make AddFunctionBlock…
Browse files Browse the repository at this point in the history
… an intention
  • Loading branch information
zolotov committed Oct 13, 2016
1 parent 8019364 commit 7585c89
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 6 deletions.
6 changes: 6 additions & 0 deletions resources/META-INF/gogland.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@

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

<!-- intentions -->
<intentionAction>
<bundleName>com.goide.GoBundle</bundleName>
<categoryKey>go.intentions.category</categoryKey>
<className>com.goide.intentions.GoAddFunctionBlockIntention</className>
</intentionAction>
<!-- unused inspections -->
<localInspection language="go" displayName="Unused import inspection" groupPath="Go"
groupName="Declaration redundancy" enabledByDefault="true" level="ERROR"
Expand Down
3 changes: 2 additions & 1 deletion resources/com/goide/GoBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@


# suppress inspection "UnusedProperty" – the property is required for showing list of links to nested configurables on root Go-configurable
go.settings.description=
go.settings.description=
go.intentions.category=Go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func functionName() <spot>{

}</spot>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
func functionName()
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
~ Copyright 2013-2016 Sergey Ignatov, Alexander Zolotov, Florin Patan
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<html>
<body>
This intention adds braces to function or method without a block.<br>
</body>
</html>
9 changes: 4 additions & 5 deletions src/com/goide/editor/smart/GoSmartEnterProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ public void apply(@NotNull Editor editor, @NotNull SmartEnterProcessorWithFixers
}
}

private static class PlainEnterProcessor extends FixEnterProcessor {
public static class PlainEnterProcessor extends FixEnterProcessor {
@Nullable
private static GoBlock findBlock(@Nullable PsiElement element) {
element = PsiTreeUtil.getParentOfType(element, GoStatement.class, GoBlock.class, GoFunctionOrMethodDeclaration.class,
GoFunctionLit.class);
element = PsiTreeUtil.getNonStrictParentOfType(element, GoStatement.class, GoBlock.class, GoFunctionOrMethodDeclaration.class,
GoFunctionLit.class);
if (element instanceof GoSimpleStatement && element.getParent() instanceof GoStatement) {
element = element.getParent();
}
Expand All @@ -127,8 +127,7 @@ private static GoBlock findBlock(@Nullable PsiElement element) {
public boolean doEnter(PsiElement psiElement, PsiFile file, @NotNull Editor editor, boolean modified) {
GoBlock block = findBlock(psiElement);
if (block != null) {
int offset = block.getTextOffset() + 1;
editor.getCaretModel().moveToOffset(offset);
editor.getCaretModel().moveToOffset(block.getLbrace().getTextRange().getEndOffset());
}
plainEnter(editor);
return true;
Expand Down
67 changes: 67 additions & 0 deletions src/com/goide/intentions/GoAddFunctionBlockIntention.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2013-2016 Sergey Ignatov, Alexander Zolotov, Florin Patan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.goide.intentions;

import com.goide.editor.smart.GoSmartEnterProcessor;
import com.goide.psi.GoBlock;
import com.goide.psi.GoFunctionOrMethodDeclaration;
import com.goide.psi.impl.GoElementFactory;
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

public class GoAddFunctionBlockIntention extends BaseElementAtCaretIntentionAction {
public static final String NAME = "Add function body";

public GoAddFunctionBlockIntention() {
setText(NAME);
}

@Nls
@NotNull
@Override
public String getFamilyName() {
return NAME;
}

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
PsiElement parent = element.getParent();
return parent instanceof GoFunctionOrMethodDeclaration && ((GoFunctionOrMethodDeclaration)parent).getBlock() == null;
}

@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
PsiElement parent = element.getParent();
if (parent instanceof GoFunctionOrMethodDeclaration) {
GoBlock block = ((GoFunctionOrMethodDeclaration)parent).getBlock();
if (block == null) {
GoBlock newBlock = ObjectUtils.tryCast(parent.add(GoElementFactory.createBlock(project)), GoBlock.class);
if (newBlock != null) {
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
new GoSmartEnterProcessor.PlainEnterProcessor().doEnter(newBlock, newBlock.getContainingFile(), editor, false);
}
}
}
}
}
5 changes: 5 additions & 0 deletions testData/intentions/add-missing-body/simple-after.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func _() int {
<caret>
}
3 changes: 3 additions & 0 deletions testData/intentions/add-missing-body/simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

func _<caret>() int
30 changes: 30 additions & 0 deletions tests/com/goide/intentions/GoAddFunctionBlockIntentionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2013-2016 Sergey Ignatov, Alexander Zolotov, Florin Patan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.goide.intentions;

import com.goide.quickfix.GoQuickFixTestBase;

public class GoAddFunctionBlockIntentionTest extends GoQuickFixTestBase {
public void testSimple() {
doTest(GoAddFunctionBlockIntention.NAME, true);
}

@Override
protected String getBasePath() {
return "intentions/add-missing-body";
}
}

0 comments on commit 7585c89

Please sign in to comment.