IDEA-112914 Insert closing tag after typing </

This commit is contained in:
Dmitry Avdeev
2013-08-30 15:27:26 +04:00
parent 6dc07e7673
commit b100cb37b7
2 changed files with 44 additions and 4 deletions
@@ -22,6 +22,7 @@ import com.intellij.openapi.editor.EditorModificationUtil;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
@@ -34,7 +35,7 @@ import com.intellij.psi.xml.*;
import com.intellij.xml.util.XmlUtil;
import org.jetbrains.annotations.NotNull;
public class XmlSlashTypedHandler extends TypedHandlerDelegate {
public class XmlSlashTypedHandler extends TypedHandlerDelegate implements XmlTokenType {
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile editedFile, final FileType fileType) {
if ((editedFile.getLanguage() instanceof XMLLanguage || editedFile.getViewProvider().getBaseLanguage() instanceof XMLLanguage) && c == '/') {
PsiDocumentManager.getInstance(project).commitAllDocuments();
@@ -80,10 +81,17 @@ public class XmlSlashTypedHandler extends TypedHandlerDelegate {
if (!(element.getLanguage() instanceof XMLLanguage)) return Result.CONTINUE;
ASTNode prevLeaf = element.getNode();
final String prevLeafText = prevLeaf != null ? prevLeaf.getText():null;
if (prevLeaf != null && !"/".equals(prevLeafText)) {
if (!"/".equals(prevLeafText.trim())) return Result.CONTINUE;
if (prevLeaf == null) return Result.CONTINUE;
final String prevLeafText = prevLeaf.getText();
if ("</".equals(prevLeafText) && prevLeaf.getElementType() == XML_END_TAG_START) {
XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
if (tag != null && StringUtil.isNotEmpty(tag.getName())) {
EditorModificationUtil.insertStringAtCaret(editor, tag.getName() + ">");
return Result.STOP;
}
}
if (!"/".equals(prevLeafText.trim())) return Result.CONTINUE;
while((prevLeaf = TreeUtil.prevLeaf(prevLeaf)) != null && prevLeaf.getElementType() == XmlTokenType.XML_WHITE_SPACE);
if(prevLeaf instanceof OuterLanguageElement) {
element = file.getViewProvider().findElementAt(offset - 1, file.getLanguage());
@@ -0,0 +1,32 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.completion;
import com.intellij.ide.highlighter.XmlFileType;
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
/**
* @author Dmitry Avdeev
* Date: 30.08.13
*/
public class XmlTypedHandlersTest extends LightPlatformCodeInsightFixtureTestCase {
public void testClosingTag() throws Exception {
myFixture.configureByText(XmlFileType.INSTANCE, "<foo><<caret>");
myFixture.type('/');
myFixture.checkResult("<foo></foo>");
}
}