@Override public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { if (!WeexFileUtil.isOnWeexFile(element)) { return false; } int offset = editor.getCaretModel().getOffset(); Document document = editor.getDocument(); if (!element.isWritable() || element.getContext() == null || !element.getContext().isWritable()) { return false; } if (element instanceof XmlToken && ((XmlToken) element).getTokenType().toString().equals("XML_END_TAG_START")) { String next = document.getText(new TextRange(offset, offset + 1)); if (next != null && next.equals("<")) { return true; } } return available(element); }
private static PsiElement adjustSectionElement(PsiFile file, PsiElement element){ PsiElement psiElement = element; if(file instanceof PsiJavaFile) { if (psiElement instanceof PsiJavaToken) { if (((PsiJavaToken) psiElement).getTokenType() == ElementType.SEMICOLON) { psiElement = psiElement.getPrevSibling(); if (psiElement instanceof PsiLiteralExpression) { return psiElement; } } else if (((PsiJavaToken) psiElement).getTokenType() == ElementType.STRING_LITERAL) { return psiElement; } } }else if(file instanceof XmlFile) { if(psiElement instanceof XmlToken){ psiElement = psiElement.getParent(); if(psiElement instanceof XmlAttributeValue) { return psiElement; } } } return null; }
@Override public Optional<String> extractTextFromElement(PsiElement element, boolean concatString, boolean stripWhitespace) { // maybe its xml then try that if (element instanceof XmlAttributeValue) { return Optional.ofNullable(((XmlAttributeValue) element).getValue()); } else if (element instanceof XmlText) { return Optional.ofNullable(((XmlText) element).getValue()); } else if (element instanceof XmlToken) { // it may be a token which is a part of an combined attribute if (concatString) { XmlAttributeValue xml = PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class); if (xml != null) { return Optional.ofNullable(getInnerText(xml.getValue())); } } else { String returnText = element.getText(); final PsiElement prevSibling = element.getPrevSibling(); if (prevSibling != null && prevSibling.getText().equalsIgnoreCase("&")) { returnText = prevSibling.getText() + returnText; } return Optional.ofNullable(getInnerText(returnText)); } } return Optional.empty(); }
public void testCamelGutterForToD() { myFixture.configureByFiles("XmlCamelRouteLineMarkerProviderToDTestData.xml"); List<GutterMark> gutters = myFixture.findAllGutters(); assertNotNull(gutters); assertEquals("Should contain 1 Camel gutter", 1, gutters.size()); assertSame("Gutter should have the Camel icon", ServiceManager.getService(CamelPreferenceService.class).getCamelIcon(), gutters.get(0).getIcon()); assertEquals("Camel route", gutters.get(0).getTooltipText()); LineMarkerInfo.LineMarkerGutterIconRenderer gutter = (LineMarkerInfo.LineMarkerGutterIconRenderer) gutters.get(0); assertTrue(gutter.getLineMarkerInfo().getElement() instanceof XmlToken); assertEquals("The navigation start element doesn't match", "file:inbox", PsiTreeUtil.getParentOfType(gutter.getLineMarkerInfo().getElement(), XmlTag.class).getAttribute("uri").getValue()); List<GotoRelatedItem> gutterTargets = getGutterNavigationDestinationElements(gutter); assertEquals("Navigation should have one target", 1, gutterTargets.size()); assertEquals("The navigation target route doesn't match", "file:inbox", gutterTargets.get(0).getElement().getText()); assertEquals("The navigation target tag name doesn't match", "toD", getGuttersWithXMLTarget(gutterTargets).get(0).getLocalName()); }
public static boolean canSelectBasic(final PsiElement e) { return !(e instanceof PsiWhiteSpace) && !(e instanceof PsiComment) && !(e instanceof PsiCodeBlock) && !(e instanceof PsiArrayInitializerExpression) && !(e instanceof PsiParameterList) && !(e instanceof PsiExpressionList) && !(e instanceof PsiBlockStatement) && !(e instanceof PsiJavaCodeReferenceElement) && !(e instanceof PsiJavaToken && !(e instanceof PsiKeyword)) && !(e instanceof XmlToken) && !(e instanceof XmlElement) && !(e instanceof PsiDocTag); }
@Override public void annotate(@NotNull PsiElement psiElement, @NotNull final AnnotationHolder holder) { psiElement.accept(new XmlElementVisitor() { @Override public void visitXmlTag(XmlTag tag) { XmlAttribute attribute = tag.getAttribute("aaa", ""); if (attribute != null) { holder.createWarningAnnotation(attribute, "AAATTR"); } } @Override public void visitXmlToken(XmlToken token) { if (token.getTokenType() == XmlTokenType.XML_ENTITY_REF_TOKEN) { holder.createWarningAnnotation(token, "ENTITY"); } } }); }
@Override @NotNull public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) { return new XmlElementVisitor() { @Override public void visitXmlToken(final XmlToken token) { if (token.getTokenType() == XmlTokenType.XML_NAME) { PsiElement element = token.getPrevSibling(); while(element instanceof PsiWhiteSpace) element = element.getPrevSibling(); if (element instanceof XmlToken && ((XmlToken)element).getTokenType() == XmlTokenType.XML_START_TAG_START) { PsiElement parent = element.getParent(); if (parent instanceof XmlTag && !(token.getNextSibling() instanceof OuterLanguageElement)) { XmlTag tag = (XmlTag)parent; checkTag(tag, holder, isOnTheFly); } } } } @Override public void visitXmlAttribute(final XmlAttribute attribute) { checkAttribute(attribute, holder, isOnTheFly); } }; }
private static void doFix(@NotNull final PsiElement element) throws IncorrectOperationException { final XmlToken endNameToken = (XmlToken)element; final PsiElement tagElement = endNameToken.getParent(); if (!(tagElement instanceof XmlTag) && !(tagElement instanceof PsiErrorElement)) return; if (tagElement instanceof PsiErrorElement) { tagElement.delete(); } else { final ASTNode astNode = tagElement.getNode(); if (astNode != null) { final ASTNode endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(astNode); if (endTagStart != null) { final Document document = PsiDocumentManager.getInstance(element.getProject()).getDocument(tagElement.getContainingFile()); if (document != null) { document.deleteString(endTagStart.getStartOffset(), tagElement.getLastChild().getTextRange().getEndOffset()); } } } } }
private static void registerProblemStart(@NotNull final AnnotationHolder holder, @NotNull final XmlTag tag, @NotNull final XmlToken start, @NotNull final XmlToken end) { PsiElement context = tag.getContainingFile().getContext(); if (context != null) { ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(context.getLanguage()); if (parserDefinition != null) { ASTNode contextNode = context.getNode(); if (contextNode != null && contextNode.getChildren(parserDefinition.getStringLiteralElements()) != null) { // TODO: we should check for concatenations here return; } } } final String tagName = tag.getName(); final String endTokenText = end.getText(); final RenameTagBeginOrEndIntentionAction renameEndAction = new RenameTagBeginOrEndIntentionAction(tagName, endTokenText, false); final RenameTagBeginOrEndIntentionAction renameStartAction = new RenameTagBeginOrEndIntentionAction(endTokenText, tagName, true); final Annotation annotation = holder.createErrorAnnotation(start, XmlErrorMessages.message("tag.has.wrong.closing.tag.name")); annotation.registerFix(renameEndAction); annotation.registerFix(renameStartAction); }
private static void registerProblemEnd(@NotNull final AnnotationHolder holder, @NotNull final XmlTag tag, @NotNull final XmlToken end) { PsiElement context = tag.getContainingFile().getContext(); if (context != null) { ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(context.getLanguage()); if (parserDefinition != null) { ASTNode contextNode = context.getNode(); if (contextNode != null && contextNode.getChildren(parserDefinition.getStringLiteralElements()) != null) { // TODO: we should check for concatenations here return; } } } final String tagName = tag.getName(); final String endTokenText = end.getText(); final RenameTagBeginOrEndIntentionAction renameEndAction = new RenameTagBeginOrEndIntentionAction(tagName, endTokenText, false); final RenameTagBeginOrEndIntentionAction renameStartAction = new RenameTagBeginOrEndIntentionAction(endTokenText, tagName, true); final Annotation annotation = holder.createErrorAnnotation(end, XmlErrorMessages.message("wrong.closing.tag.name")); annotation.registerFix(new RemoveExtraClosingTagIntentionAction()); annotation.registerFix(renameEndAction); annotation.registerFix(renameStartAction); }
@Nullable static XmlToken findEndTagName(@Nullable final PsiErrorElement element) { if (element == null) return null; final ASTNode astNode = element.getNode(); if (astNode == null) return null; ASTNode current = astNode.getLastChildNode(); ASTNode prev = current; while (current != null) { final IElementType elementType = prev.getElementType(); if ((elementType == XmlTokenType.XML_NAME || elementType == XmlTokenType.XML_TAG_NAME) && current.getElementType() == XmlTokenType.XML_END_TAG_START) { return (XmlToken)prev.getPsi(); } prev = current; current = current.getTreePrev(); } return null; }
@Override public boolean shouldHighlightErrorElement(@NotNull final PsiErrorElement element) { final PsiFile psiFile = element.getContainingFile(); if (psiFile == null || psiFile.getViewProvider().getBaseLanguage() != HTMLLanguage.INSTANCE && HTMLLanguage.INSTANCE != element.getLanguage()) return true; final PsiElement[] children = element.getChildren(); if (children.length > 0) { if (children[0] instanceof XmlToken && XmlTokenType.XML_END_TAG_START == ((XmlToken)children[0]).getTokenType()) { if (XmlErrorMessages.message("xml.parsing.closing.tag.matches.nothing").equals(element.getErrorDescription())) { return false; } } } return true; }
private static void addMessagesForTreeChild(final XmlToken childByRole, final Validator.ValidationHost.ErrorType type, final String message, AnnotationHolder myHolder, IntentionAction... actions) { if (childByRole != null) { Annotation annotation; if (type == Validator.ValidationHost.ErrorType.ERROR) { annotation = myHolder.createErrorAnnotation(childByRole, message); } else { annotation = myHolder.createWarningAnnotation(childByRole, message); } appendFixes(annotation, actions); } }
@Override @NotNull public PsiReference[] getReferences() { final IElementType elementType = getElementType(); if (elementType == XmlTokenType.XML_DATA_CHARACTERS || elementType == XmlTokenType.XML_CHAR_ENTITY_REF) { return ReferenceProvidersRegistry.getReferencesFromProviders(this); } else if (elementType == XmlTokenType.XML_NAME && getParent() instanceof PsiErrorElement) { final PsiElement element = getPrevSibling(); if (element instanceof XmlToken && ((XmlToken)element).getTokenType() == XmlTokenType.XML_END_TAG_START) { return new PsiReference[] {TagNameReference.createTagNameReference(this, getNode(), false)}; } } return super.getReferences(); }
public static Quantifier getQuantifierImpl(PsiElement element) { PsiElement nextSibling = element.getNextSibling(); while (nextSibling instanceof PsiWhiteSpace) { nextSibling = nextSibling.getNextSibling(); } if (nextSibling instanceof XmlToken) { IElementType tokenType = ((XmlToken)nextSibling).getTokenType(); if (tokenType == XML_PLUS) { return Quantifier.ONE_OR_MORE; } else if (tokenType == XML_STAR) { return Quantifier.ZERO_OR_MORE; } else if (tokenType == XML_QUESTION) { return Quantifier.OPTIONAL; } } return Quantifier.REQUIRED; }
@Override @NotNull public TextRange getDeclarationRange(@NotNull final PsiElement container) { XmlTag xmlTag = (XmlTag)container; int endOffset = xmlTag.getTextRange().getStartOffset(); for (PsiElement child = xmlTag.getFirstChild(); child != null; child = child.getNextSibling()) { endOffset = child.getTextRange().getEndOffset(); if (child instanceof XmlToken) { XmlToken token = (XmlToken)child; IElementType tokenType = token.getTokenType(); if (tokenType == XmlTokenType.XML_EMPTY_ELEMENT_END || tokenType == XmlTokenType.XML_TAG_END) break; } } return new TextRange(xmlTag.getTextRange().getStartOffset(), endOffset); }
@Override public String findPrefix(PsiElement insertedElement, int offset) { String prefix = super.findPrefix(insertedElement, offset); boolean searchForEntities = insertedElement instanceof XmlToken && ( ((XmlToken)insertedElement).getTokenType() == XmlTokenType.XML_DATA_CHARACTERS || ((XmlToken)insertedElement).getTokenType() == XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN ); if (searchForEntities) { if (prefix.startsWith("&")) { prefix = prefix.substring(1); } else if (prefix.contains("&")) { prefix = prefix.substring(prefix.indexOf("&") + 1); } } return prefix; }
private static void handleUserSelection(InsertionContext context, @NotNull Function<Color, String> colorToStringConverter) { Color myColorAtCaret = null; Editor selectedTextEditor = context.getEditor(); PsiElement element = context.getFile().findElementAt(selectedTextEditor.getCaretModel().getOffset()); if (element instanceof XmlToken) { myColorAtCaret = getColorFromElement(element); } context.getDocument().deleteString(context.getStartOffset(), context.getTailOffset()); List<ColorPickerListener> listeners = ColorPickerListenerFactory.createListenersFor(element); Color color = ColorChooser.chooseColor(WindowManager.getInstance().suggestParentWindow(context.getProject()), XmlBundle.message("choose.color.dialog.title"), myColorAtCaret, true, listeners, true); if (color != null) { String colorString = colorToStringConverter.fun(color); context.getDocument().insertString(context.getStartOffset(), colorString); context.getEditor().getCaretModel().moveToOffset(context.getTailOffset()); } }
@Override @NotNull public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) { final Pair<XmlTagChild, XmlTagChild> childrenInRange = XmlUtil.findTagChildrenInRange(file, startOffset, endOffset); if (childrenInRange == null) { final PsiElement elementAt = file.findElementAt(startOffset); if (elementAt instanceof XmlToken && ((XmlToken)elementAt).getTokenType() == XmlTokenType.XML_DATA_CHARACTERS) { return new PsiElement[] {elementAt}; } return PsiElement.EMPTY_ARRAY; } List<PsiElement> result = new ArrayList<PsiElement>(); PsiElement first = childrenInRange.getFirst(); PsiElement last = childrenInRange.getSecond(); while(true) { result.add(first); if (first == last) break; first = first.getNextSibling(); } return PsiUtilCore.toPsiElementArray(result); }
@Nullable public static XmlToken getEndTagNameElement(@NotNull XmlTag tag) { final ASTNode node = tag.getNode(); if (node == null) return null; ASTNode current = node.getLastChildNode(); ASTNode prev = current; while (current != null) { final IElementType elementType = prev.getElementType(); if ((elementType == XmlTokenType.XML_NAME || elementType == XmlTokenType.XML_TAG_NAME) && current.getElementType() == XmlTokenType.XML_END_TAG_START) { return (XmlToken)prev.getPsi(); } prev = current; current = current.getTreePrev(); } return null; }
private static TextRange getTag(XmlToken tagName, IElementType tagStart) { if (tagName != null) { PsiElement s = tagName.getPrevSibling(); while (s != null && s.getNode().getElementType() != tagStart) { s = s.getPrevSibling(); } PsiElement f = tagName.getNextSibling(); while (f != null && !(f.getNode().getElementType() == XmlTokenType.XML_TAG_END || f.getNode().getElementType() == XmlTokenType.XML_EMPTY_ELEMENT_END)) { f = f.getNextSibling(); } if (s != null && f != null) { return new TextRange(s.getTextRange().getStartOffset(), f.getTextRange().getEndOffset()); } } return null; }
@Override public void visitXmlTag(XmlTag tag) { super.visitXmlTag(tag); if (tag.getNamespace().isEmpty()) { final XmlElementDescriptor descriptor = tag.getDescriptor(); if (descriptor instanceof AndroidAnyTagDescriptor) { final XmlToken startTagNameElement = XmlTagUtil.getStartTagNameElement(tag); if (startTagNameElement != null && !isUnknownCustomView(tag)) { myResult.add(myInspectionManager.createProblemDescriptor(startTagNameElement, XmlErrorMessages.message( "element.is.not.allowed.here", tag.getName()), myOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING)); } final XmlToken endTagNameElement = XmlTagUtil.getEndTagNameElement(tag); if (endTagNameElement != null && !isUnknownCustomView(tag)) { myResult.add(myInspectionManager.createProblemDescriptor(endTagNameElement, XmlErrorMessages.message( "element.is.not.allowed.here", tag.getName()), myOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING)); } } } }
@Nullable private static LayoutUsageData getLayoutUsageDataFromContext(Editor editor) { if (editor == null) { return null; } final PsiElement element = PsiUtilBase.getElementAtCaret(editor); if (!(element instanceof XmlToken) || AndroidFacet.getInstance(element) == null) { return null; } final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class); return tag != null ? AndroidInlineUtil.getLayoutUsageData(tag) : null; }
@Override public PsiElement getChangeHighlightingDirtyScopeFor(@NotNull PsiElement changedElement) { try { if (changedElement instanceof XmlToken && changedElement.getNode().getElementType() == XmlElementType.XML_ATTRIBUTE_VALUE_TOKEN) { final PsiElement grandParent = changedElement.getParent().getParent(); if (grandParent instanceof XmlAttribute) { if (XsltSupport.isXPathAttribute((XmlAttribute)grandParent)) { return grandParent; } } } else if (changedElement instanceof XmlTag && XsltSupport.isTemplate((XmlTag)changedElement, false)) { return changedElement; } } catch (NullPointerException e) { // sth was null } return null; }
@Override public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { if (element instanceof XmlToken && ((XmlToken)element).getTokenType() == XmlTokenType.XML_NAME && element.getParent() instanceof XmlTag) { final XmlTag tag = (XmlTag)element.getParent(); for (XmlTag xmlTag : tag.getSubTags()) { if (xmlTag.getAttribute(FxmlConstants.FX_VALUE) == null) return false; } final XmlTag parentTag = tag.getParentTag(); if (parentTag != null && tag.getDescriptor() instanceof JavaFxPropertyElementDescriptor && parentTag.getDescriptor() instanceof JavaFxClassBackedElementDescriptor) { setText("Collapse tag '" + tag.getName() + "' to attribute"); return true; } } return false; }
@NotNull public PsiReference[] getReferences() { final IElementType elementType = getElementType(); if (elementType == XmlTokenType.XML_DATA_CHARACTERS || elementType == XmlTokenType.XML_CHAR_ENTITY_REF) { return ReferenceProvidersRegistry.getReferencesFromProviders(this); } else if (elementType == XmlTokenType.XML_NAME && getParent() instanceof PsiErrorElement) { final PsiElement element = getPrevSibling(); if (element instanceof XmlToken && ((XmlToken)element).getTokenType() == XmlTokenType.XML_END_TAG_START) { return new PsiReference[] {TagNameReference.createTagNameReference(this, getNode(), false)}; } } return super.getReferences(); }
@Nullable static XmlToken findEndTagName(@Nullable final PsiErrorElement element) { if (element == null) return null; final ASTNode astNode = element.getNode(); if (astNode == null) return null; ASTNode current = astNode.getLastChildNode(); ASTNode prev = current; while (current != null) { final IElementType elementType = prev.getElementType(); if ((elementType == XmlElementType.XML_NAME || elementType == XmlElementType.XML_TAG_NAME) && current.getElementType() == XmlElementType.XML_END_TAG_START) { return (XmlToken)prev.getPsi(); } prev = current; current = current.getTreePrev(); } return null; }
@NotNull public TextRange getDeclarationRange(@NotNull final PsiElement container) { XmlTag xmlTag = (XmlTag)container; int endOffset = xmlTag.getTextRange().getStartOffset(); for (PsiElement child = xmlTag.getFirstChild(); child != null; child = child.getNextSibling()) { endOffset = child.getTextRange().getEndOffset(); if (child instanceof XmlToken) { XmlToken token = (XmlToken)child; IElementType tokenType = token.getTokenType(); if (tokenType == XmlTokenType.XML_EMPTY_ELEMENT_END || tokenType == XmlTokenType.XML_TAG_END) break; } } return new TextRange(xmlTag.getTextRange().getStartOffset(), endOffset); }
public boolean shouldHighlightErrorElement(@NotNull final PsiErrorElement element) { final PsiFile psiFile = element.getContainingFile(); if (psiFile == null || (psiFile.getViewProvider().getBaseLanguage() != HTMLLanguage.INSTANCE && HTMLLanguage.INSTANCE != element.getLanguage())) return true; final PsiElement[] children = element.getChildren(); if (children.length > 0) { if (children[0] instanceof XmlToken && XmlTokenType.XML_END_TAG_START == ((XmlToken)children[0]).getTokenType()) { if (XmlErrorMessages.message("xml.parsing.closing.tag.matches.nothing").equals(element.getErrorDescription())) { return false; } } } return true; }
private static void handleUserSelection(InsertionContext context, @NotNull Function<Color, String> colorToStringConverter) { Color myColorAtCaret = null; Editor selectedTextEditor = context.getEditor(); PsiElement element = context.getFile().findElementAt(selectedTextEditor.getCaretModel().getOffset()); if (element instanceof XmlToken) { myColorAtCaret = getColorFromElement(element); } context.getDocument().deleteString(context.getStartOffset(), context.getTailOffset()); ColorPickerListener[] listeners = ColorPickerListenerFactory.createListenersFor(element); Color color = ColorChooser.chooseColor(WindowManager.getInstance().suggestParentWindow(context.getProject()), XmlBundle.message("choose.color.dialog.title"), myColorAtCaret, true, listeners, true); if (color != null) { String colorString = colorToStringConverter.fun(color); context.getDocument().insertString(context.getStartOffset(), colorString); context.getEditor().getCaretModel().moveToOffset(context.getTailOffset()); } }
@NotNull public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) { final Pair<XmlTagChild, XmlTagChild> childrenInRange = XmlUtil.findTagChildrenInRange(file, startOffset, endOffset); if (childrenInRange == null) { final PsiElement elementAt = file.findElementAt(startOffset); if (elementAt instanceof XmlToken && ((XmlToken)elementAt).getTokenType() == XmlTokenType.XML_DATA_CHARACTERS) { return new PsiElement[] {elementAt}; } return PsiElement.EMPTY_ARRAY; } List<PsiElement> result = new ArrayList<PsiElement>(); PsiElement first = childrenInRange.getFirst(); PsiElement last = childrenInRange.getSecond(); while(true) { result.add(first); if (first == last) break; first = first.getNextSibling(); } return PsiUtilCore.toPsiElementArray(result); }