mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Emmet: move calculating template text into hint alarm
This commit is contained in:
@@ -37,6 +37,7 @@ import com.intellij.ui.LightweightHint;
|
||||
import com.intellij.ui.components.JBPanel;
|
||||
import com.intellij.util.Alarm;
|
||||
import com.intellij.util.DocumentUtil;
|
||||
import com.intellij.util.Producer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -81,16 +82,17 @@ public class EmmetPreviewHint extends LightweightHint implements Disposable {
|
||||
HintManagerImpl.getInstanceImpl().showEditorHint(this, myParentEditor, position.first, hintFlags, 0, false, hintHint);
|
||||
}
|
||||
|
||||
public void updateText(@Nullable final String newText) {
|
||||
if (StringUtil.isEmpty(newText)) {
|
||||
hide();
|
||||
}
|
||||
else {
|
||||
myAlarm.cancelAllRequests();
|
||||
myAlarm.addRequest(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!isDisposed && !myEditor.getDocument().getText().equals(newText)) {
|
||||
public void updateText(@NotNull final Producer<String> contentProducer) {
|
||||
myAlarm.cancelAllRequests();
|
||||
myAlarm.addRequest(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!isDisposed) {
|
||||
final String newText = contentProducer.produce();
|
||||
if (StringUtil.isEmpty(newText)) {
|
||||
hide();
|
||||
}
|
||||
else if (!myEditor.getDocument().getText().equals(newText)) {
|
||||
DocumentUtil.writeInRunUndoTransparentAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -101,8 +103,8 @@ public class EmmetPreviewHint extends LightweightHint implements Disposable {
|
||||
HintManagerImpl.adjustEditorHintPosition(EmmetPreviewHint.this, myParentEditor, position.first, position.second);
|
||||
}
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -35,6 +35,7 @@ import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiFileFactory;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.xml.XmlFile;
|
||||
import com.intellij.util.Producer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -75,7 +76,7 @@ public class EmmetPreviewUtil {
|
||||
public void documentChanged(@NotNull DocumentEvent e) {
|
||||
EmmetPreviewHint existingHint = EmmetPreviewHint.getExistingHint(editor);
|
||||
if (existingHint != null) {
|
||||
existingHint.updateText(calculateTemplateText(editor, file, expandPrimitiveAbbreviations));
|
||||
existingHint.updateText(new TemplateTextProducer(editor, file, expandPrimitiveAbbreviations));
|
||||
}
|
||||
else {
|
||||
e.getDocument().removeDocumentListener(this);
|
||||
@@ -88,7 +89,7 @@ public class EmmetPreviewUtil {
|
||||
public void caretPositionChanged(@NotNull CaretEvent e) {
|
||||
EmmetPreviewHint existingHint = EmmetPreviewHint.getExistingHint(e.getEditor());
|
||||
if (existingHint != null) {
|
||||
existingHint.updateText(calculateTemplateText(editor, file, expandPrimitiveAbbreviations));
|
||||
existingHint.updateText(new TemplateTextProducer(editor, file, expandPrimitiveAbbreviations));
|
||||
}
|
||||
else {
|
||||
e.getEditor().getCaretModel().removeCaretListener(this);
|
||||
@@ -124,4 +125,22 @@ public class EmmetPreviewUtil {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static class TemplateTextProducer implements Producer<String> {
|
||||
@NotNull private final Editor myEditor;
|
||||
@NotNull private final PsiFile myFile;
|
||||
private final boolean myExpandPrimitiveAbbreviations;
|
||||
|
||||
public TemplateTextProducer(@NotNull Editor editor, @NotNull PsiFile file, boolean expandPrimitiveAbbreviations) {
|
||||
myEditor = editor;
|
||||
myFile = file;
|
||||
myExpandPrimitiveAbbreviations = expandPrimitiveAbbreviations;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String produce() {
|
||||
return calculateTemplateText(myEditor, myFile, myExpandPrimitiveAbbreviations);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user