Introduce XmlTagNameSynchronizer.runWithoutCancellingSyncTagsEditing(), make tricky user data key private

This commit is contained in:
Alexander Doroshko
2018-07-19 19:15:17 +03:00
parent e3b52fd71e
commit f1380aabe7
2 changed files with 25 additions and 25 deletions
@@ -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<LookupElement> {
public void handleInsert(InsertionContext context, LookupElement item) {
Project project = context.getProject();
Editor editor = context.getEditor();
Document document = InjectedLanguageUtil.getTopLevelEditor(editor).getDocument();
Ref<PsiElement> currentElementRef = Ref.create();
// Need to insert " " to prevent creating tags like <tagThis is my text
InjectedLanguageUtil.getTopLevelEditor(editor).getDocument().putUserData(XmlTagNameSynchronizer.SKIP_COMMAND, Boolean.TRUE);
final int offset = editor.getCaretModel().getOffset();
editor.getDocument().insertString(offset, " ");
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
PsiElement current = context.getFile().findElementAt(context.getStartOffset());
editor.getDocument().deleteString(offset, offset + 1);
InjectedLanguageUtil.getTopLevelEditor(editor).getDocument().putUserData(XmlTagNameSynchronizer.SKIP_COMMAND, null);
XmlTagNameSynchronizer.runWithoutCancellingSyncTagsEditing(document, () -> {
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;
@@ -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<Boolean> SKIP_COMMAND = Key.create("tag.name.synchronizer.skip.command");
private static final Key<Boolean> SKIP_COMMAND = Key.create("tag.name.synchronizer.skip.command");
private static final Logger LOG = Logger.getInstance(XmlTagNameSynchronizer.class);
private static final Set<Language> 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<Couple<RangeMarker>> 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();