use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class AntCreatePropertyFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement psiElement = descriptor.getPsiElement();
final PsiFile containingFile = psiElement.getContainingFile();
final FileModificationService modificationService = FileModificationService.getInstance();
Navigatable result = null;
if (myPropFile != null) {
final VirtualFile vFile = myPropFile.getVirtualFile();
boolean canModify = true;
if (myPropFile instanceof PsiFile) {
canModify = modificationService.prepareFileForWrite((PsiFile) myPropFile);
} else if (vFile != null) {
canModify = modificationService.prepareVirtualFilesForWrite(project, Collections.singleton(vFile));
}
if (canModify) {
final IProperty generatedProperty = myPropFile.addProperty(myCanonicalText, "");
result = vFile != null ? new OpenFileDescriptor(project, vFile, generatedProperty.getPsiElement().getTextRange().getEndOffset()) : generatedProperty;
}
} else {
if (containingFile instanceof XmlFile) {
final XmlFile xmlFile = (XmlFile) containingFile;
final XmlTag rootTag = xmlFile.getRootTag();
if (rootTag != null && modificationService.prepareFileForWrite(xmlFile)) {
final XmlTag propTag = rootTag.createChildTag(PROPERTY, rootTag.getNamespace(), null, false);
propTag.setAttribute(NAME_ATTR, myCanonicalText);
propTag.setAttribute(VALUE_ATTR, "");
final DomElement contextElement = DomUtil.getDomElement(descriptor.getPsiElement());
PsiElement generated;
if (contextElement == null) {
generated = rootTag.addSubTag(propTag, true);
} else {
final AntDomTarget containingTarget = contextElement.getParentOfType(AntDomTarget.class, false);
final DomElement anchor = containingTarget != null ? containingTarget : contextElement;
final XmlTag tag = anchor.getXmlTag();
if (!rootTag.equals(tag)) {
generated = tag.getParent().addBefore(propTag, tag);
} else {
generated = rootTag.addSubTag(propTag, true);
}
}
if (generated instanceof XmlTag) {
final XmlAttribute valueAttrib = ((XmlTag) generated).getAttribute(VALUE_ATTR);
if (valueAttrib != null) {
final XmlAttributeValue valueElement = valueAttrib.getValueElement();
if (valueElement instanceof Navigatable) {
result = (Navigatable) valueElement;
}
}
}
if (result == null && generated instanceof Navigatable) {
result = (Navigatable) generated;
}
}
}
}
if (result != null) {
result.navigate(true);
}
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class AntRenameHandler method getElements.
@Nullable
private static PsiElement[] getElements(DataContext dataContext) {
final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(dataContext);
if (!(psiFile instanceof XmlFile && AntDomFileDescription.isAntFile((XmlFile) psiFile))) {
return null;
}
final Editor editor = LangDataKeys.EDITOR.getData(dataContext);
if (editor == null) {
return null;
}
return getPsiElementsIn(editor, psiFile);
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class AntHectorConfigurable method applyItem.
private void applyItem(final String contextStr) {
XmlFile context = null;
if (!NONE.equals(contextStr)) {
context = myPathToFileMap.get(contextStr);
assert context != null;
}
AntConfigurationBase.getInstance(myProject).setContextFile(myFile, context);
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class AntHectorConfigurable method createComponent.
public JComponent createComponent() {
final JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(IdeBorderFactory.createTitledBorder("File Context", false));
myCombo = new ComboBox();
myCombo.putClientProperty(CONTEXTS_COMBO_KEY, Boolean.TRUE);
panel.add(new JLabel("Included into:"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, JBUI.insets(5, 0), 0, 0));
panel.add(myCombo, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, JBUI.insets(5, 5, 5, 0), 0, 0));
final PsiManager psiManager = PsiManager.getInstance(myProject);
final FileBasedIndex fbi = FileBasedIndex.getInstance();
final Collection<VirtualFile> antFiles = fbi.getContainingFiles(AntImportsIndex.INDEX_NAME, AntImportsIndex.ANT_FILES_WITH_IMPORTS_KEY, myFileFilter);
for (VirtualFile file : antFiles) {
final PsiFile psiFile = psiManager.findFile(file);
if (!(psiFile instanceof XmlFile)) {
continue;
}
final XmlFile xmlFile = (XmlFile) psiFile;
if (!xmlFile.equals(myFile) && AntDomFileDescription.isAntFile(xmlFile)) {
final String path = PathUtil.getLocalPath(file);
final XmlFile previous = myPathToFileMap.put(path, xmlFile);
assert previous == null;
}
}
final List<String> paths = new ArrayList<>(myPathToFileMap.keySet());
Collections.sort(paths, (o1, o2) -> o1.compareTo(o2));
myCombo.addItem(NONE);
for (String path : paths) {
myCombo.addItem(path);
}
final AntConfigurationBase antConfig = AntConfigurationBase.getInstance(myProject);
final XmlFile currentContext = antConfig.getContextFile(myFile);
if (currentContext != null) {
final VirtualFile vFile = currentContext.getVirtualFile();
assert vFile != null;
final String path = PathUtil.getLocalPath(vFile);
if (!FileUtil.pathsEqual(path, myLocalPath)) {
myOriginalContext = path;
}
}
myCombo.setSelectedItem(myOriginalContext);
return panel;
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class XmlSurroundWithRangeAdjuster method adjustSurroundWithRange.
@Override
public TextRange adjustSurroundWithRange(final PsiFile file, final TextRange selectedRange) {
if (!(file instanceof XmlFile))
return selectedRange;
int startOffset = selectedRange.getStartOffset();
int endOffset = selectedRange.getEndOffset();
PsiElement element1 = file.findElementAt(startOffset);
PsiElement element2 = file.findElementAt(endOffset - 1);
Language lang1 = getLanguage(element1);
Language lang2 = getLanguage(element2);
if (element1 instanceof PsiWhiteSpace && isLanguageWithWSSignificant(lang1)) {
startOffset = element1.getTextRange().getEndOffset();
element1 = file.findElementAt(startOffset);
}
if (element2 instanceof PsiWhiteSpace && isLanguageWithWSSignificant(lang2)) {
endOffset = element2.getTextRange().getStartOffset();
element2 = file.findElementAt(endOffset);
}
lang1 = getLanguage(element1);
lang2 = getLanguage(element2);
if (lang1 != lang2)
return null;
TextRange.assertProperRange(startOffset, endOffset, "Wrong offsets for " + selectedRange.substring(file.getText()));
return new TextRange(startOffset, endOffset);
}
Aggregations