|
| 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 | +} |
0 commit comments