Search in sources :

Example 1 with GoBlock

use of com.goide.psi.GoBlock in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoWithBlockSurrounder method surroundStatements.

@Nullable
@Override
protected TextRange surroundStatements(@NotNull Project project, @NotNull PsiElement container, @NotNull PsiElement[] statements) throws IncorrectOperationException {
    GoBlock block = GoElementFactory.createBlock(project);
    PsiElement first = ArrayUtil.getFirstElement(statements);
    PsiElement last = ArrayUtil.getLastElement(statements);
    block = (GoBlock) container.addAfter(block, last);
    block.addRangeAfter(first, last, block.getLbrace());
    CodeEditUtil.markToReformat(block.getNode(), true);
    container.deleteChildRange(first, last);
    int offset = block.getTextRange().getEndOffset();
    return new TextRange(offset, offset);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) GoBlock(com.goide.psi.GoBlock) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GoBlock

use of com.goide.psi.GoBlock in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoAddFunctionBlockIntention method invoke.

@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);
            }
        }
    }
}
Also used : GoFunctionOrMethodDeclaration(com.goide.psi.GoFunctionOrMethodDeclaration) GoBlock(com.goide.psi.GoBlock) PsiElement(com.intellij.psi.PsiElement) GoSmartEnterProcessor(com.goide.editor.smart.GoSmartEnterProcessor)

Aggregations

GoBlock (com.goide.psi.GoBlock)2 PsiElement (com.intellij.psi.PsiElement)2 GoSmartEnterProcessor (com.goide.editor.smart.GoSmartEnterProcessor)1 GoFunctionOrMethodDeclaration (com.goide.psi.GoFunctionOrMethodDeclaration)1 TextRange (com.intellij.openapi.util.TextRange)1 Nullable (org.jetbrains.annotations.Nullable)1