@Override public String getDescription(PsiElement e) { String text = e.getText(); if (text.length() > 20) text = text.substring(0, 17) + "..."; return "Unwrap " + text; }
@Nullable private static String getImportPathForElement(@Nullable PsiElement element) { PsiFile file = element != null ? element.getContainingFile() : null; return file instanceof GoFile ? ((GoFile)file).getImportPath(false) : null; }
@Override public void collectElementsToIgnore(PsiElement element, Set<PsiElement> result) { PsiElement parent = element.getParent(); if (parent instanceof GoIfStatement) { result.add(parent); } }
private static boolean hasNewLineBefore(@NotNull PsiElement anchor) { PsiElement prevSibling = anchor.getPrevSibling(); while (prevSibling instanceof PsiWhiteSpace) { if (prevSibling.textContains('\n')) { return true; } prevSibling = prevSibling.getPrevSibling(); } return false; }
public static boolean processChildren(@NotNull PsiElement element, @NotNull PsiScopeProcessor processor, @NotNull ResolveState substitutor, @Nullable PsiElement lastParent, @NotNull PsiElement place) { PsiElement run = lastParent == null ? element.getLastChild() : lastParent.getPrevSibling(); while (run != null) { if (run instanceof GoCompositeElement && !run.processDeclarations(processor, substitutor, null, place)) return false; run = run.getPrevSibling(); } return true; }
@Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { myElement.replace(GoElementFactory.createIdentifierFromText(myElement.getProject(), newElementName)); return myElement; }
@Override public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { PsiElement parent = element.getParent(); if (element instanceof BallerinaNameReference) { if (parent instanceof BallerinaAnnotationAttachment) { PsiReference reference = ((BallerinaNameReference) element).getIdentifier().getReference(); if (reference != null) { PsiElement resolvedElement = reference.resolve(); if ((resolvedElement != null) && (resolvedElement .getParent() instanceof BallerinaGlobalVariableDefinition)) { Annotation annotation = holder.createInfoAnnotation(element, null); annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.GLOBAL_VARIABLE); annotateKeyword(element, holder, BallerinaSyntaxHighlightingColors.KEYWORD, false); } else if (elementType == BallerinaTypes.MAP || elementType == BallerinaTypes.FOREACH) { if (parent.getNode().getElementType() == BallerinaTypes.RESERVED_WORD) { if (parent.getParent() != null && parent.getParent().getNode().getElementType() == BallerinaTypes.ANY_IDENTIFIER_NAME) { annotateKeyword(element, holder, BallerinaSyntaxHighlightingColors.DEFAULT, false); annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.DOCUMENTATION); } else if (elementType == BallerinaTypes.MARKDOWN_DOCUMENTATION_LINE_START) { TextRange textRange = element.getTextRange(); annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.DOCUMENTATION); } else if (elementType == BallerinaTypes.PARAMETER_DOCUMENTATION_START) { TextRange textRange = element.getTextRange();
/** * Return element range which contains TextRange(start, end) of top level elements * common parent of elements is straight parent for each element */ @Nullable private static Couple<PsiElement> getTopmostElementRange(@NotNull Couple<PsiElement> elementRange, @NotNull PsiElement commonParent) { if (elementRange.first == null || elementRange.second == null) return null; int start = elementRange.first.getTextOffset(); int end = elementRange.second.getTextRange().getEndOffset(); TextRange range = commonParent.getTextRange(); PsiElement[] children = commonParent.getChildren(); if (commonParent.isEquivalentTo(elementRange.first) || commonParent.isEquivalentTo(elementRange.second) || range.getStartOffset() == start && (children.length == 0 || children[0].getTextRange().getStartOffset() > start) || range.getEndOffset() == end && (children.length == 0 || children[children.length - 1].getTextRange().getEndOffset() < end)) { return Couple.of(commonParent, commonParent); } PsiElement startElement = elementRange.first; PsiElement endElement = elementRange.second; for (PsiElement element : children) { range = element.getTextRange(); if (range.contains(start) && !range.contains(end)) { startElement = element; } if (range.contains(end - 1) && !range.contains(start - 1)) { endElement = element; } } return startElement.getParent().isEquivalentTo(endElement.getParent()) ? Couple.of(startElement, endElement) : null; }
private static void addNavigationElements(@NotNull TreeElement root, @NotNull TIntArrayList navigationOffsets, boolean start) { if (root instanceof PsiTreeElementBase) { PsiElement element = ((PsiTreeElementBase)root).getValue(); int offset; if (start) { offset = element.getTextRange().getStartOffset(); if (element.getLanguage().getID().equals("JAVA")) { // HACK: for Java classes and methods, we want to jump to the opening brace int textOffset = element.getTextOffset(); int braceIndex = element.getText().indexOf('{', textOffset - offset); if (braceIndex >= 0) { offset += braceIndex; } } } else { offset = element.getTextRange().getEndOffset() - 1; } if (!navigationOffsets.contains(offset)) { navigationOffsets.add(offset); } } for (TreeElement child : root.getChildren()) { addNavigationElements(child, navigationOffsets, start); } }
@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; } }
private void addMarker(@NotNull Collection<LineMarkerInfo> result, @NotNull Set<Integer> lines, @NotNull PsiElement definition, @NotNull PsiElement implementation) { // Get the document manager; PsiDocumentManager documentManager = PsiDocumentManager.getInstance(definition.getProject()); // Get the document. Document document = documentManager.getDocument(implementation.getContainingFile()); if (document == null) { return; } // Get the offset of the current element. int textOffset = implementation.getTextOffset(); // Get the line number of the current element. int lineNumber = document.getLineNumber(textOffset); if (!lines.contains(lineNumber)) { // Add the number to the set. lines.add(lineNumber); // Return a new line marker. result.add(new BallerinaImplementedFunctionMarkerInfo(definition, implementation)); result.add(new BallerinaImplementingFunctionMarkerInfo(implementation, definition)); } }
@Override public List<TextRange> select(@NotNull PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { PsiElement parent = e.getParent(); List<TextRange> result = super.select(e, editorText, cursorOffset, editor); if (parent instanceof GoImportString || parent instanceof GoStringLiteral) { result.add(ElementManipulators.getValueTextRange(parent).shiftRight(parent.getTextRange().getStartOffset())); result.addAll(expandToWholeLine(editorText, e.getTextRange())); if (e.getTextLength() > 2) { result.add(TextRange.create(e.getTextRange().getStartOffset() + 1, e.getTextRange().getEndOffset() - 1)); GoSignature signature = ((GoNamedSignatureOwner)parent).getSignature(); if (signature != null) { int nameStartOffset = parent.getTextOffset(); result.add(TextRange.create(nameStartOffset, signature.getParameters().getTextRange().getEndOffset())); result.add(TextRange.create(nameStartOffset, signature.getTextRange().getEndOffset()));
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 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()); } } } }
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, "/.../")); } }
@NotNull private static Collection<PsiComment> getCommentsToConsider(@NotNull GoFile file) { Collection<PsiComment> commentsToConsider = ContainerUtil.newArrayList(); PsiElement child = file.getFirstChild(); int lastEmptyLineOffset = 0; while (child != null) { if (child instanceof PsiComment) { commentsToConsider.add((PsiComment)child); } else if (child instanceof PsiWhiteSpace) { if (StringUtil.countChars(child.getText(), '\n') > 1) { lastEmptyLineOffset = child.getTextRange().getStartOffset(); } } else { break; } child = child.getNextSibling(); } int finalLastEmptyLineOffset = lastEmptyLineOffset; return ContainerUtil.filter(commentsToConsider, comment -> comment.getTextRange().getStartOffset() < finalLastEmptyLineOffset); } }
public void extractNewLine(PsiElement from) { PsiElement newLine = GoElementFactory.createNewLine(from.getProject()); if (myIsEffective) { newLine = from.getParent().addBefore(newLine, from); } if (newLine != null) { addElementToExtract(newLine); } }
private void visitDeclaration (PsiElement o) { if (o.getChildren().length == 0 && GoDocumentationProvider.getCommentsForElement(o instanceof GoImportDeclaration && o.getPrevSibling() == null ? o.getParent() : o).isEmpty() && PsiTreeUtil.findChildrenOfType(o, PsiComment.class).isEmpty()) { holder.registerProblem(o, "Empty declaration <code>#ref</code> #loc", ProblemHighlightType.LIKE_UNUSED_SYMBOL, new GoDeleteQuickFix(QUICK_FIX_NAME, o.getClass())); } } };
public static GlobalSearchScope goPathResolveScope(@NotNull PsiElement context) { // it's important to ask module on file, otherwise module won't be found for elements in libraries files [zolotov] Module module = ModuleUtilCore.findModuleForPsiElement(context.getContainingFile()); return GoPathResolveScope.create(context.getProject(), module, context); }
private RecursiveMethodCallMarkerInfo(@NotNull PsiElement methodCall) { super(methodCall, methodCall.getTextRange(), AllIcons.Gutter.RecursiveMethod, Pass.UPDATE_OVERRIDDEN_MARKERS, FunctionUtil.constant("Recursive call"), null, GutterIconRenderer.Alignment.RIGHT ); } }