use of com.intellij.psi.xml.XmlToken in project intellij-community by JetBrains.
the class XmlTokenSelectioner method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
XmlToken token = (XmlToken) e;
if (shouldSelectToken(token)) {
List<TextRange> ranges = super.select(e, editorText, cursorOffset, editor);
SelectWordUtil.addWordSelection(editor.getSettings().isCamelWords(), editorText, cursorOffset, ranges);
return ranges;
} else {
List<TextRange> result = new ArrayList<>();
SelectWordUtil.addWordSelection(editor.getSettings().isCamelWords(), editorText, cursorOffset, result);
return result;
}
}
use of com.intellij.psi.xml.XmlToken in project intellij-community by JetBrains.
the class TagDeclarationRangeHandler method getDeclarationRange.
@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);
}
use of com.intellij.psi.xml.XmlToken in project intellij-community by JetBrains.
the class DtdSelectioner method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
PsiElement[] children = e.getChildren();
PsiElement first = null;
PsiElement last = null;
for (PsiElement child : children) {
if (child instanceof XmlToken) {
XmlToken token = (XmlToken) child;
if (token.getTokenType() == XmlTokenType.XML_TAG_END) {
last = token;
break;
}
if (token.getTokenType() == XmlTokenType.XML_ELEMENT_DECL_START || token.getTokenType() == XmlTokenType.XML_ATTLIST_DECL_START) {
first = token;
}
}
}
List<TextRange> result = new ArrayList<>(1);
if (first != null && last != null) {
final int offset = last.getTextRange().getEndOffset() + 1;
result.addAll(ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, new TextRange(first.getTextRange().getStartOffset(), offset < editorText.length() ? offset : editorText.length()), false));
}
return result;
}
use of com.intellij.psi.xml.XmlToken in project intellij-community by JetBrains.
the class XmlSurroundDescriptor method getElementsToSurround.
@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 first = childrenInRange.getFirst();
PsiElement last = childrenInRange.getSecond();
while (true) {
result.add(first);
if (first == last)
break;
first = first.getNextSibling();
}
return PsiUtilCore.toPsiElementArray(result);
}
use of com.intellij.psi.xml.XmlToken in project intellij-community by JetBrains.
the class DefaultXmlTagNameProvider method getRootTagsVariants.
private static List<LookupElement> getRootTagsVariants(final XmlTag tag, final List<LookupElement> elements) {
elements.add(LookupElementBuilder.create("?xml version=\"1.0\" encoding=\"\" ?>").withPresentableText("<?xml version=\"1.0\" encoding=\"\" ?>").withInsertHandler(new InsertHandler<LookupElement>() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
int offset = context.getEditor().getCaretModel().getOffset();
context.getEditor().getCaretModel().moveToOffset(offset - 4);
AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(context.getEditor());
}
}));
final FileBasedIndex fbi = FileBasedIndex.getInstance();
Collection<String> result = new ArrayList<>();
Processor<String> processor = Processors.cancelableCollectProcessor(result);
fbi.processAllKeys(XmlNamespaceIndex.NAME, processor, tag.getProject());
final GlobalSearchScope scope = new EverythingGlobalScope();
for (final String ns : result) {
if (ns.startsWith("file://"))
continue;
fbi.processValues(XmlNamespaceIndex.NAME, ns, null, new FileBasedIndex.ValueProcessor<XsdNamespaceBuilder>() {
@Override
public boolean process(final VirtualFile file, XsdNamespaceBuilder value) {
List<String> tags = value.getRootTags();
for (String s : tags) {
elements.add(LookupElementBuilder.create(s).withTypeText(ns).withInsertHandler(new XmlTagInsertHandler() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
final Editor editor = context.getEditor();
final Document document = context.getDocument();
final int caretOffset = editor.getCaretModel().getOffset();
final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
caretMarker.setGreedyToRight(true);
XmlFile psiFile = (XmlFile) context.getFile();
boolean incomplete = XmlUtil.getTokenOfType(tag, XmlTokenType.XML_TAG_END) == null && XmlUtil.getTokenOfType(tag, XmlTokenType.XML_EMPTY_ELEMENT_END) == null;
XmlNamespaceHelper.getHelper(psiFile).insertNamespaceDeclaration(psiFile, editor, Collections.singleton(ns), null, null);
editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
XmlTag rootTag = psiFile.getRootTag();
if (incomplete) {
XmlToken token = XmlUtil.getTokenOfType(rootTag, XmlTokenType.XML_EMPTY_ELEMENT_END);
// remove tag end added by smart attribute adder :(
if (token != null)
token.delete();
PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(document);
super.handleInsert(context, item);
}
}
}));
}
return true;
}
}, scope);
}
return elements;
}
Aggregations