mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Automatically adjust line indent for closed tag (WEB-418)
This commit is contained in:
@@ -19,6 +19,7 @@ import com.intellij.application.options.editor.WebEditorOptions;
|
||||
import com.intellij.ide.highlighter.XmlLikeFileType;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.lang.xml.XMLLanguage;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.EditorModificationUtil;
|
||||
import com.intellij.openapi.editor.ScrollType;
|
||||
@@ -26,6 +27,7 @@ import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.impl.source.tree.TreeUtil;
|
||||
import com.intellij.psi.templateLanguages.OuterLanguageElement;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
@@ -113,6 +115,7 @@ public class XmlSlashTypedHandler extends TypedHandlerDelegate {
|
||||
}
|
||||
}
|
||||
EditorModificationUtil.insertStringAtCaret(editor, tag.getName() + ">", false);
|
||||
autoIndent(editor);
|
||||
return Result.STOP;
|
||||
}
|
||||
}
|
||||
@@ -165,4 +168,15 @@ public class XmlSlashTypedHandler extends TypedHandlerDelegate {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void autoIndent(@NotNull Editor editor) {
|
||||
Project project = editor.getProject();
|
||||
if (project != null) {
|
||||
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
|
||||
Document document = editor.getDocument();
|
||||
documentManager.commitDocument(document);
|
||||
int lineOffset = document.getLineStartOffset(document.getLineNumber(editor.getCaretModel().getOffset()));
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(document, lineOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,8 +48,8 @@ public class XmlTypedHandlersTest extends LightPlatformCodeInsightFixtureTestCas
|
||||
"<foo><<caret>\n" +
|
||||
"<foo><<caret>\n" +
|
||||
"</bar>", '/', "<bar>\n" +
|
||||
"<foo></foo><caret>\n" +
|
||||
"<foo></foo><caret>\n" +
|
||||
" <foo></foo><caret>\n" +
|
||||
" <foo></foo><caret>\n" +
|
||||
"</bar>");
|
||||
}
|
||||
|
||||
@@ -68,8 +68,8 @@ public class XmlTypedHandlersTest extends LightPlatformCodeInsightFixtureTestCas
|
||||
"<foo><<caret>\n" +
|
||||
"<fiz><<caret>\n" +
|
||||
"</bar>", '/', "<bar>\n" +
|
||||
"<foo></foo><caret>\n" +
|
||||
"<fiz></fiz><caret>\n" +
|
||||
" <foo></foo><caret>\n" +
|
||||
" <fiz></fiz><caret>\n" +
|
||||
"</bar>");
|
||||
}
|
||||
|
||||
@@ -167,6 +167,24 @@ public class XmlTypedHandlersTest extends LightPlatformCodeInsightFixtureTestCas
|
||||
doTest("<<caret> ?>", '?', "<?<caret> ?>");
|
||||
}
|
||||
|
||||
public void testAutoindentEndTag() throws Exception {
|
||||
doTest(
|
||||
"<div>\n" +
|
||||
" <p>\n" +
|
||||
" Some text\n" +
|
||||
" </p>\n" +
|
||||
" <<caret>",
|
||||
|
||||
'/',
|
||||
|
||||
"<div>\n" +
|
||||
" <p>\n" +
|
||||
" Some text\n" +
|
||||
" </p>\n" +
|
||||
"</div><caret>"
|
||||
);
|
||||
}
|
||||
|
||||
private void doTest(String text, char c, String result) {
|
||||
myFixture.configureByText(XmlFileType.INSTANCE, text);
|
||||
myFixture.type(c);
|
||||
|
||||
Reference in New Issue
Block a user