use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class InputExpressionDialog method prepareShow.
@SuppressWarnings({ "unchecked" })
private void prepareShow(XmlElement contextElement) {
final NamespaceCollector.CollectedInfo collectedInfo;
if (contextElement != null) {
collectedInfo = NamespaceCollector.collectInfo((XmlFile) contextElement.getContainingFile());
myNamespaceCache = collectedInfo.namespaces;
} else {
collectedInfo = NamespaceCollector.empty();
myNamespaceCache = null;
}
myContextProvider = new InteractiveContextProvider(contextElement, collectedInfo, myModel);
myContextProvider.attachTo(myXPathFile);
final HistoryElement historyElement = myModel.getSelectedItem();
if (historyElement != null) {
myContextProvider.getNamespaceContext().setMap(asMap(historyElement.namespaces));
} else {
myContextProvider.getNamespaceContext().setMap(asMap(null));
}
updateOkAction();
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class RenameXmlAttributeProcessor method doRenameXmlAttributeValue.
private static void doRenameXmlAttributeValue(@NotNull XmlAttributeValue value, String newName, UsageInfo[] infos, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
LOG.assertTrue(value.isValid());
renameAll(value, infos, newName, value.getValue());
PsiManager psiManager = value.getManager();
LOG.assertTrue(psiManager != null);
XmlFile file = (XmlFile) PsiFileFactory.getInstance(psiManager.getProject()).createFileFromText("dummy.xml", XMLLanguage.INSTANCE, "<a attr=\"" + newName + "\"/>");
@SuppressWarnings("ConstantConditions") PsiElement element = value.replace(file.getRootTag().getAttributes()[0].getValueElement());
if (listener != null) {
listener.elementRenamed(element);
}
}
use of com.intellij.psi.xml.XmlFile 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;
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class ExtendedTagInsertHandler method handleInsert.
@Override
public void handleInsert(final InsertionContext context, final LookupElement item) {
final XmlFile contextFile = (XmlFile) context.getFile();
final XmlExtension extension = XmlExtension.getExtension(contextFile);
final XmlFile file = extension.getContainingFile(contextFile);
final Project project = context.getProject();
assert file != null;
final PsiElement psiElement = file.findElementAt(context.getStartOffset());
assert psiElement != null;
if (isNamespaceBound(psiElement)) {
doDefault(context, item);
return;
}
final Editor editor = context.getEditor();
final Document document = editor.getDocument();
PsiDocumentManager.getInstance(project).commitDocument(document);
final int caretOffset = editor.getCaretModel().getOffset();
final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
caretMarker.setGreedyToRight(true);
final XmlNamespaceHelper.Runner<String, IncorrectOperationException> runAfter = new XmlNamespaceHelper.Runner<String, IncorrectOperationException>() {
@Override
public void run(final String namespacePrefix) {
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiElement element = file.findElementAt(context.getStartOffset());
if (element != null) {
qualifyWithPrefix(namespacePrefix, element, document);
PsiDocumentManager.getInstance(project).commitDocument(document);
}
editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
doDefault(context, item);
}
};
try {
final String prefixByNamespace = getPrefixByNamespace(file, myNamespace);
if (myNamespacePrefix != null || StringUtil.isEmpty(prefixByNamespace)) {
final String nsPrefix = myNamespacePrefix == null ? suggestPrefix(file, myNamespace) : myNamespacePrefix;
XmlNamespaceHelper.getHelper(file).insertNamespaceDeclaration(file, editor, Collections.singleton(myNamespace), nsPrefix, runAfter);
FeatureUsageTracker.getInstance().triggerFeatureUsed(XmlCompletionContributor.TAG_NAME_COMPLETION_FEATURE);
} else {
// qualify && complete
runAfter.run(prefixByNamespace);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class XmlAttributeInsertHandler method handleInsert.
@Override
public void handleInsert(final InsertionContext context, final LookupElement item) {
final Editor editor = context.getEditor();
final Document document = editor.getDocument();
final int caretOffset = editor.getCaretModel().getOffset();
final PsiFile file = context.getFile();
final CharSequence chars = document.getCharsSequence();
final String quote = getAttributeQuote(HtmlUtil.hasHtml(file) || HtmlUtil.supportsXmlTypedHandlers(file));
final boolean insertQuotes = WebEditorOptions.getInstance().isInsertQuotesForAttributeValue() && StringUtil.isNotEmpty(quote);
final boolean hasQuotes = CharArrayUtil.regionMatches(chars, caretOffset, "=\"") || CharArrayUtil.regionMatches(chars, caretOffset, "='");
if (!hasQuotes) {
PsiElement fileContext = file.getContext();
String toInsert = null;
if (fileContext != null) {
if (fileContext.getText().startsWith("\""))
toInsert = "=''";
if (fileContext.getText().startsWith("\'"))
toInsert = "=\"\"";
}
if (toInsert == null) {
toInsert = "=" + quote + quote;
}
if (!insertQuotes)
toInsert = "=";
if (caretOffset >= document.getTextLength() || "/> \n\t\r".indexOf(document.getCharsSequence().charAt(caretOffset)) < 0) {
document.insertString(caretOffset, toInsert + " ");
} else {
document.insertString(caretOffset, toInsert);
}
if ('=' == context.getCompletionChar()) {
// IDEA-19449
context.setAddCompletionChar(false);
}
}
editor.getCaretModel().moveToOffset(caretOffset + (insertQuotes || hasQuotes ? 2 : 1));
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
AutoPopupController.getInstance(editor.getProject()).scheduleAutoPopup(editor);
if (myNamespaceToInsert != null && file instanceof XmlFile) {
final PsiElement element = file.findElementAt(context.getStartOffset());
final XmlTag tag = element != null ? PsiTreeUtil.getParentOfType(element, XmlTag.class) : null;
if (tag != null) {
String prefix = ExtendedTagInsertHandler.suggestPrefix((XmlFile) file, myNamespaceToInsert);
if (prefix != null) {
prefix = makePrefixUnique(prefix, tag);
final XmlNamespaceHelper helper = XmlNamespaceHelper.getHelper(context.getFile());
if (helper != null) {
final Project project = context.getProject();
PsiDocumentManager.getInstance(project).commitDocument(document);
qualifyWithPrefix(prefix, element);
helper.insertNamespaceDeclaration((XmlFile) file, editor, Collections.singleton(myNamespaceToInsert), prefix, null);
}
}
}
}
}
Aggregations