mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
generate xml tag in the right position
This commit is contained in:
@@ -52,13 +52,10 @@ public class XmlSmartCompletionProvider {
|
||||
if (subTag == tag) {
|
||||
break;
|
||||
}
|
||||
XmlElementDescriptor childDescriptor = subTag.getDescriptor();
|
||||
if (childDescriptor != null) {
|
||||
dfa.transition(childDescriptor, subTag);
|
||||
}
|
||||
dfa.transition(subTag);
|
||||
}
|
||||
List<XmlElementDescriptor> elements = dfa.getPossibleElements();
|
||||
for (XmlElementDescriptor elementDescriptor : elements) {
|
||||
for (XmlElementDescriptor elementDescriptor: elements) {
|
||||
addElementToResult(elementDescriptor, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public abstract class XmlContentDFA {
|
||||
|
||||
public abstract List<XmlElementDescriptor> getPossibleElements();
|
||||
|
||||
public abstract void transition(@NotNull XmlElementDescriptor element, XmlTag xmlTag);
|
||||
public abstract void transition(XmlTag xmlTag);
|
||||
|
||||
@Nullable
|
||||
public static XmlContentDFA getContentDFA(XmlTag parentTag) {
|
||||
|
||||
@@ -90,8 +90,11 @@ class XmlContentDFAImpl extends XmlContentDFA {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transition(@NotNull XmlElementDescriptor element, XmlTag xmlTag) {
|
||||
doTransition(element);
|
||||
public void transition(XmlTag xmlTag) {
|
||||
XmlElementDescriptor descriptor = xmlTag.getDescriptor();
|
||||
if (descriptor != null) {
|
||||
doTransition(descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
private Result doTransition(@NotNull XmlElementDescriptor element) {
|
||||
|
||||
@@ -119,7 +119,7 @@ class XsContentDFA extends XmlContentDFA {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transition(@NotNull XmlElementDescriptor element, XmlTag xmlTag) {
|
||||
public void transition(XmlTag xmlTag) {
|
||||
myContentModel.oneTransition(createQName(xmlTag), myState, myHandler);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.XmlElementFactory;
|
||||
import com.intellij.psi.impl.source.tree.Factory;
|
||||
import com.intellij.psi.impl.source.tree.LeafElement;
|
||||
import com.intellij.psi.impl.source.xml.XmlContentDFA;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.xml.*;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
@@ -91,16 +92,9 @@ public class GenerateXmlTagAction extends SimpleCodeInsightAction {
|
||||
protected void run() {
|
||||
if (selected == null) return;
|
||||
XmlTag newTag = createTag(contextTag, selected);
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
PsiElement element = file.findElementAt(offset);
|
||||
if (element != null) {
|
||||
XmlText xmlText = PsiTreeUtil.getParentOfType(element, XmlText.class);
|
||||
if (xmlText != null) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
newTag = contextTag.addSubTag(newTag, false);
|
||||
PsiElement anchor = getAnchor(contextTag, editor, selected);
|
||||
newTag = anchor == null ? contextTag.addSubTag(newTag, true) : (XmlTag)contextTag.addAfter(newTag, anchor);
|
||||
generateTag(newTag);
|
||||
}
|
||||
}.execute();
|
||||
@@ -135,6 +129,34 @@ public class GenerateXmlTagAction extends SimpleCodeInsightAction {
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiElement getAnchor(XmlTag contextTag, Editor editor, XmlElementDescriptor selected) {
|
||||
XmlContentDFA contentDFA = XmlContentDFA.getContentDFA(contextTag);
|
||||
PsiElement anchor = null;
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
if (contentDFA != null) {
|
||||
for (XmlTag subTag : contextTag.getSubTags()) {
|
||||
if (contentDFA.getPossibleElements().contains(selected)) {
|
||||
if (subTag.getTextOffset() > offset) {
|
||||
break;
|
||||
}
|
||||
anchor = subTag;
|
||||
}
|
||||
contentDFA.transition(subTag);
|
||||
}
|
||||
}
|
||||
if (anchor == null) { // insert it at caret position
|
||||
PsiElement[] children = contextTag.getChildren();
|
||||
for (PsiElement child : children) {
|
||||
if (child.getTextOffset() > offset) {
|
||||
break;
|
||||
}
|
||||
anchor = child;
|
||||
}
|
||||
}
|
||||
return anchor;
|
||||
}
|
||||
|
||||
public static void generateTag(XmlTag newTag) {
|
||||
newTag = generateRaw(newTag);
|
||||
newTag = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(newTag);
|
||||
|
||||
Reference in New Issue
Block a user