public static boolean onStatementBeginning(@NotNull PsiElement psiElement) { PsiElement prevLeaf = prevVisibleLeafOrNewLine(psiElement); if (prevLeaf == null) { return false; } if (prevLeaf instanceof PsiWhiteSpace) { return true; } IElementType type = prevLeaf.getNode().getElementType(); return type == GoTypes.SEMICOLON || type == GoTypes.LBRACE || type == GoTypes.RBRACE || type == GoTypes.COLON; }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement start, @NotNull PsiElement end) { if (start.isValid() && end.isValid()) { PsiElement parent = start.getParent(); if (parent != null && parent.equals(end.getParent())) { parent.getNode().removeRange(start.getNode(), end.getNode().getTreeNext()); } } } }
@Nullable private static PsiElement getFirstCompositeElement(@Nullable PsiElement at) { if (at instanceof PsiComment || at instanceof LeafPsiElement && ((LeafPsiElement)at).getElementType() == GoTypes.STRING) return at; PsiElement result = at; while (result != null && (result instanceof PsiWhiteSpace || result.getNode().getChildren(null).length == 0)) { result = result.getParent(); } return result; }
@Override public void addElementToExtract(PsiElement e) { super.addElementToExtract(e); CodeEditUtil.markToReformat(e.getNode(), true); }
@Override public boolean process(@NotNull PsiElement o) { IElementType type = o.getNode().getElementType(); if (GoParserDefinition.COMMENTS.contains(type) || GoParserDefinition.WHITESPACES.contains(type)) { return true; } myIsLineBreakpointAvailable = true; return false; }
@NotNull @Override public FormattingModel createModel(@NotNull PsiElement element, @NotNull CodeStyleSettings settings) { Block block = new GoFormattingBlock(element.getNode(), null, Indent.getNoneIndent(), null, settings, createSpacingBuilder(settings)); return FormattingModelProvider.createFormattingModelForPsiFile(element.getContainingFile(), block, settings); }
private static Couple<PsiElement> getElementRange(@NotNull Editor editor, @NotNull PsiFile file) { Pair<PsiElement, PsiElement> primeElementRangePair = getElementRange(editor, file, getLineRangeFromSelection(editor)); if (primeElementRangePair == null) return null; ASTNode firstNode = TreeUtil.findFirstLeaf(primeElementRangePair.first.getNode()); ASTNode lastNode = TreeUtil.findLastLeaf(primeElementRangePair.second.getNode()); if (firstNode == null || lastNode == null) return null; return Couple.of(firstNode.getPsi(), lastNode.getPsi()); }
@Nullable private static Couple<PsiElement> getElementRange(@NotNull GoFile file, int startOffset, int endOffset) { PsiElement startElement = findNotWhiteSpaceElementAtOffset(file, startOffset, true); PsiElement endElement = findNotWhiteSpaceElementAtOffset(file, endOffset - 1, false); if (startElement == null || endElement == null) return null; ASTNode startNode = TreeUtil.findFirstLeaf(startElement.getNode()); ASTNode endNode = TreeUtil.findLastLeaf(endElement.getNode()); if (startNode == null || endNode == null) return null; startElement = startNode.getPsi(); endElement = endNode.getPsi(); if (startElement == null || endElement == null) return null; return Couple.of(startElement, endElement); }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { WriteCommandAction.runWriteCommandAction(project, () -> { PsiElement element = ObjectUtils.tryCast(descriptor.getStartElement(), myClazz); if (element != null && (myElementType == null || element.getNode().getElementType() == myElementType)) { element.delete(); } }); } }
/** * Shorthand method for removing CRLF element. * * @param startElement working PSI element */ private void removeCrlf(PsiElement startElement) { ASTNode node = TreeUtil.findSibling(startElement.getNode(), IgnoreTypes.CRLF); if (node == null) { node = TreeUtil.findSiblingBackward(startElement.getNode(), IgnoreTypes.CRLF); } if (node != null) { node.getPsi().delete(); } }
@Override protected boolean isInContext(@NotNull PsiElement element) { if (element.getNode().getElementType() == GoTypes.IDENTIFIER) { if (isInsideFieldTypeDeclaration(element)) { return true; } if (isInsideFieldTypeDeclaration(prevVisibleLeafOrNewLine(element))) { return true; } } return false; }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { assert startElement == endElement; com.intellij.lang.ASTNode parentNode = startElement.getNode(); com.intellij.lang.ASTNode whiteSpace = parentNode.findChildByType(TokenType.WHITE_SPACE); BlockSupport blockSupport = BlockSupport.getInstance(project); final int startOffset = whiteSpace.getStartOffset(); final int endOffset = startOffset + whiteSpace.getTextLength(); blockSupport.reparseRange(file, startOffset, endOffset, ""); } }
private static void addCommentFolds(@NotNull PsiElement comment, @NotNull Set<PsiElement> processedComments, @NotNull List<FoldingDescriptor> result) { if (processedComments.contains(comment)) return; PsiElement end = null; for (PsiElement current = comment.getNextSibling(); current != null; current = current.getNextSibling()) { ASTNode node = current.getNode(); if (node == null) break; IElementType elementType = node.getElementType(); if (elementType == GoParserDefinition.LINE_COMMENT) { end = current; processedComments.add(current); continue; } if (elementType == TokenType.WHITE_SPACE) continue; break; } if (end != null) { int startOffset = comment.getTextRange().getStartOffset(); int endOffset = end.getTextRange().getEndOffset(); result.add(new NamedFoldingDescriptor(comment, startOffset, endOffset, null, "/.../")); } }
@Nullable @Override public Info getInfo(PsiElement e) { if (e != null && e.getNode().getElementType() == GoTypes.IDENTIFIER) { PsiElement parent = e.getParent(); PsiFile file = e.getContainingFile(); if (GoRunUtil.isMainGoFile(file) && parent instanceof GoFunctionDeclaration) { if (GoConstants.MAIN.equals(((GoFunctionDeclaration)parent).getName())) { return new Info(AllIcons.RunConfigurations.TestState.Run, TOOLTIP_PROVIDER, ExecutorAction.getActions(0)); } } } return null; } }
@Override public boolean isPublic() { T stub = getStub(); if (stub != null && stub.isPublic()) { return true; } PsiElement firstChild = getFirstChild(); return firstChild.getNode().getElementType() == BallerinaTypes.PUBLIC; }
private static void deleteImportSpec(@Nullable GoImportSpec importSpec) { GoImportDeclaration importDeclaration = PsiTreeUtil.getParentOfType(importSpec, GoImportDeclaration.class); if (importSpec != null && importDeclaration != null) { PsiElement startElementToDelete = importSpec; PsiElement endElementToDelete = importSpec; if (importDeclaration.getImportSpecList().size() == 1) { startElementToDelete = importDeclaration; endElementToDelete = importDeclaration; PsiElement nextSibling = endElementToDelete.getNextSibling(); if (nextSibling != null && nextSibling.getNode().getElementType() == GoTypes.SEMICOLON) { endElementToDelete = nextSibling; } } // todo: delete after proper formatter implementation PsiElement nextSibling = endElementToDelete.getNextSibling(); if (nextSibling instanceof PsiWhiteSpace && nextSibling.textContains('\n')) { endElementToDelete = nextSibling; } startElementToDelete.getParent().deleteChildRange(startElementToDelete, endElementToDelete); } }
@Override public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) { PsiElement position = parameters.getPosition(); PsiFile file = parameters.getOriginalFile(); ASTNode node = position.getNode(); if (file instanceof GoFile && position.getParent() instanceof GoPackageClause && node.getElementType() == GoTypes.IDENTIFIER) { boolean isTestFile = GoTestFinder.isTestFile(file); PsiDirectory directory = file.getParent(); String currentPackageName = ((GoFile)file).getPackageName(); Collection<String> packagesInDirectory = GoPackageUtil.getAllPackagesInDirectory(directory, null, true); for (String packageName : packagesInDirectory) { if (!packageName.equals(currentPackageName)) { result.addElement(packageLookup(packageName, GoCompletionUtil.PACKAGE_PRIORITY - 1)); } if (isTestFile) { result.addElement(packageLookup(packageName + GoConstants.TEST_SUFFIX, GoCompletionUtil.PACKAGE_PRIORITY)); } } if (directory != null && ContainerUtil.filter(directory.getFiles(), Conditions.instanceOf(GoFile.class)).size() == 1) { String packageFromDirectory = GoPsiImplUtil.getLocalPackageName(directory.getName()); if (!packageFromDirectory.isEmpty()) { result.addElement(packageLookup(packageFromDirectory, GoCompletionUtil.PACKAGE_PRIORITY - 1)); } } result.addElement(packageLookup(GoConstants.MAIN, GoCompletionUtil.PACKAGE_PRIORITY - 2)); } super.fillCompletionVariants(parameters, result); }
@Nullable @Override public Info getInfo(PsiElement e) { if (e != null && e.getNode().getElementType() == GoTypes.IDENTIFIER) { PsiElement parent = e.getParent(); PsiFile file = e.getContainingFile(); if (!GoTestFinder.isTestFile(file)) { return null; } if (GoRunUtil.isPackageContext(e)) { return new Info(AllIcons.RunConfigurations.TestState.Run_run, TOOLTIP_PROVIDER, ExecutorAction.getActions(0)); } else if (parent instanceof GoFunctionOrMethodDeclaration) { GoTestFunctionType functionType = GoTestFunctionType.fromName(((GoFunctionOrMethodDeclaration)parent).getName()); if (functionType != null) { if (parent instanceof GoFunctionDeclaration) { return getInfo(GoTestLocator.PROTOCOL + "://" + ((GoFunctionDeclaration)parent).getName(), e.getProject()); } else if (parent instanceof GoMethodDeclaration) { GoReceiver receiver = ((GoMethodDeclaration)parent).getReceiver(); PsiElement receiverIdentifier = receiver != null ? receiver.getIdentifier() : null; String receiverText = receiverIdentifier != null ? receiverIdentifier.getText() + "." : ""; return getInfo(GoTestLocator.PROTOCOL + "://" + receiverText + ((GoMethodDeclaration)parent).getName(), e.getProject()); } } } } return null; }