diff --git a/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java b/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java index 15968704f92f..b7c84c3f5970 100644 --- a/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java +++ b/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java @@ -21,10 +21,12 @@ import com.intellij.codeInspection.htmlInspections.XmlEntitiesInspection; import com.intellij.lang.ASTNode; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.command.undo.UndoManager; +import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.RangeMarker; import com.intellij.openapi.editor.ScrollType; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.text.StringUtil; import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.psi.PsiDocumentManager; @@ -56,16 +58,18 @@ public class XmlTagInsertHandler implements InsertHandler { public void handleInsert(InsertionContext context, LookupElement item) { Project project = context.getProject(); Editor editor = context.getEditor(); + Document document = InjectedLanguageUtil.getTopLevelEditor(editor).getDocument(); + Ref currentElementRef = Ref.create(); // Need to insert " " to prevent creating tags like { + final int offset = editor.getCaretModel().getOffset(); + editor.getDocument().insertString(offset, " "); + PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); + currentElementRef.set(context.getFile().findElementAt(context.getStartOffset())); + editor.getDocument().deleteString(offset, offset + 1); + }); - final XmlTag tag = PsiTreeUtil.getContextOfType(current, XmlTag.class, true); + final XmlTag tag = PsiTreeUtil.getContextOfType(currentElementRef.get(), XmlTag.class, true); if (tag == null) return; diff --git a/xml/impl/src/com/intellij/codeInsight/editorActions/XmlTagNameSynchronizer.java b/xml/impl/src/com/intellij/codeInsight/editorActions/XmlTagNameSynchronizer.java index ed2fbbc82f00..d0581dc90366 100644 --- a/xml/impl/src/com/intellij/codeInsight/editorActions/XmlTagNameSynchronizer.java +++ b/xml/impl/src/com/intellij/codeInsight/editorActions/XmlTagNameSynchronizer.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 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. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.editorActions; import com.intellij.application.options.editor.WebEditorOptions; @@ -67,7 +53,7 @@ import java.util.Set; * @author Dennis.Ushakov */ public class XmlTagNameSynchronizer implements NamedComponent, CommandListener { - public static final Key SKIP_COMMAND = Key.create("tag.name.synchronizer.skip.command"); + private static final Key SKIP_COMMAND = Key.create("tag.name.synchronizer.skip.command"); private static final Logger LOG = Logger.getInstance(XmlTagNameSynchronizer.class); private static final Set SUPPORTED_LANGUAGES = ContainerUtil.set(HTMLLanguage.INSTANCE, XMLLanguage.INSTANCE, @@ -132,6 +118,16 @@ public class XmlTagNameSynchronizer implements NamedComponent, CommandListener { } } + public static void runWithoutCancellingSyncTagsEditing(@NotNull Document document, @NotNull Runnable runnable) { + document.putUserData(SKIP_COMMAND, Boolean.TRUE); + try { + runnable.run(); + } + finally { + document.putUserData(SKIP_COMMAND, null); + } + } + private static class TagNameSynchronizer implements DocumentListener { public static final TagNameSynchronizer[] EMPTY = new TagNameSynchronizer[0]; private final PsiDocumentManagerBase myDocumentManager; @@ -143,7 +139,7 @@ public class XmlTagNameSynchronizer implements NamedComponent, CommandListener { private State myState = State.INITIAL; private final List> myMarkers = new SmartList<>(); - public TagNameSynchronizer(Editor editor, Project project, Language language) { + private TagNameSynchronizer(Editor editor, Project project, Language language) { myEditor = editor; myLanguage = language; final Disposable disposable = ((EditorImpl)editor).getDisposable();