refactoring: extract XDebuggerInlayUtil (following IDEA-CR-14187)

This commit is contained in:
Dmitry Batrak
2016-10-05 11:26:53 +03:00
parent afd1d1b470
commit 6ddab52341
3 changed files with 117 additions and 90 deletions
@@ -0,0 +1,113 @@
/*
* Copyright 2000-2016 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.xdebugger.impl;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorCustomElementRenderer;
import com.intellij.openapi.editor.Inlay;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.FontPreferences;
import com.intellij.openapi.editor.impl.ComplementaryFontsRegistry;
import com.intellij.openapi.editor.impl.FontInfo;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ui.UIUtil;
import com.intellij.xdebugger.ui.DebuggerColors;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.util.List;
public class XDebuggerInlayUtil {
public static void createInlay(@NotNull Project project, @NotNull VirtualFile file, int offset, String inlayText) {
UIUtil.invokeLaterIfNeeded(() -> {
FileEditor editor = FileEditorManager.getInstance(project).getSelectedEditor(file);
if (editor instanceof TextEditor) {
Editor e = ((TextEditor)editor).getEditor();
CharSequence text = e.getDocument().getImmutableCharSequence();
int insertOffset = offset;
while (insertOffset < text.length() && Character.isJavaIdentifierPart(text.charAt(insertOffset))) insertOffset++;
List<Inlay> existing = e.getInlayModel().getInlineElementsInRange(insertOffset, insertOffset);
for (Inlay inlay : existing) {
if (inlay.getRenderer() instanceof MyRenderer) {
Disposer.dispose(inlay);
}
}
e.getInlayModel().addInlineElement(insertOffset, new MyRenderer(inlayText));
}
});
}
public static void clearInlays(@NotNull Project project) {
UIUtil.invokeLaterIfNeeded(() -> {
FileEditor[] editors = FileEditorManager.getInstance(project).getAllEditors();
for (FileEditor editor : editors) {
if (editor instanceof TextEditor) {
Editor e = ((TextEditor)editor).getEditor();
List<Inlay> existing = e.getInlayModel().getInlineElementsInRange(0, e.getDocument().getTextLength());
for (Inlay inlay : existing) {
if (inlay.getRenderer() instanceof MyRenderer) {
Disposer.dispose(inlay);
}
}
}
}
});
}
private static class MyRenderer implements EditorCustomElementRenderer {
private final String myText;
private MyRenderer(String text) {
myText = "(" + text + ")";
}
private static FontInfo getFontInfo(@NotNull Editor editor) {
EditorColorsScheme colorsScheme = editor.getColorsScheme();
FontPreferences fontPreferences = colorsScheme.getFontPreferences();
TextAttributes attributes = editor.getColorsScheme().getAttributes(DebuggerColors.INLINED_VALUES_EXECUTION_LINE);
int fontStyle = attributes == null ? Font.PLAIN : attributes.getFontType();
return ComplementaryFontsRegistry.getFontAbleToDisplay('a', fontStyle, fontPreferences);
}
@Override
public int calcWidthInPixels(@NotNull Editor editor) {
FontInfo fontInfo = getFontInfo(editor);
return fontInfo.fontMetrics().stringWidth(myText);
}
@Override
public void paint(@NotNull Editor editor, @NotNull Graphics g, @NotNull Rectangle r) {
TextAttributes attributes = editor.getColorsScheme().getAttributes(DebuggerColors.INLINED_VALUES_EXECUTION_LINE);
if (attributes == null) return;
Color fgColor = attributes.getForegroundColor();
if (fgColor == null) return;
g.setColor(fgColor);
FontInfo fontInfo = getFontInfo(editor);
g.setFont(fontInfo.getFont());
FontMetrics metrics = fontInfo.fontMetrics();
g.drawString(myText, r.x, r.y + metrics.getAscent());
}
}
}
@@ -17,23 +17,18 @@ package com.intellij.xdebugger.impl.frame;
import com.intellij.ide.dnd.DnDManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.Inlay;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.event.SelectionEvent;
import com.intellij.openapi.editor.event.SelectionListener;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx;
import com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ObjectLongHashMap;
import com.intellij.util.ui.UIUtil;
import com.intellij.xdebugger.XDebugSession;
import com.intellij.xdebugger.XDebuggerBundle;
import com.intellij.xdebugger.XSourcePosition;
@@ -42,6 +37,7 @@ import com.intellij.xdebugger.evaluation.XDebuggerEditorsProvider;
import com.intellij.xdebugger.evaluation.XDebuggerEvaluator;
import com.intellij.xdebugger.frame.XStackFrame;
import com.intellij.xdebugger.frame.XValueContainer;
import com.intellij.xdebugger.impl.XDebuggerInlayUtil;
import com.intellij.xdebugger.impl.actions.XDebuggerActions;
import com.intellij.xdebugger.impl.evaluate.quick.XValueHint;
import com.intellij.xdebugger.impl.evaluate.quick.common.ValueHintType;
@@ -51,13 +47,11 @@ import com.intellij.xdebugger.impl.ui.tree.XDebuggerTreeRestorer;
import com.intellij.xdebugger.impl.ui.tree.XDebuggerTreeState;
import com.intellij.xdebugger.impl.ui.tree.nodes.XStackFrameNode;
import com.intellij.xdebugger.impl.ui.tree.nodes.XValueContainerNode;
import com.intellij.xdebugger.impl.ui.tree.nodes.XValueNodeImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.List;
/**
* @author nik
@@ -98,21 +92,7 @@ public abstract class XVariablesViewBase extends XDebugView {
}
protected static void clearInlays(XDebuggerTree tree) {
if (!Registry.is("debugger.show.values.inplace")) return;
UIUtil.invokeLaterIfNeeded(() -> {
FileEditor[] editors = FileEditorManager.getInstance(tree.getProject()).getAllEditors();
for (FileEditor editor : editors) {
if (editor instanceof TextEditor) {
Editor e = ((TextEditor)editor).getEditor();
List<Inlay> existing = e.getInlayModel().getInlineElementsInRange(0, e.getDocument().getTextLength());
for (Inlay inlay : existing) {
if (inlay.getRenderer() instanceof XValueNodeImpl.MyRenderer) {
Disposer.dispose(inlay);
}
}
}
}
});
if (Registry.is("debugger.show.values.inplace")) XDebuggerInlayUtil.clearInlays(tree.getProject());
}
protected XValueContainerNode createNewRootNode(@Nullable XStackFrame stackFrame) {
@@ -17,31 +17,19 @@ package com.intellij.xdebugger.impl.ui.tree.nodes;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorCustomElementRenderer;
import com.intellij.openapi.editor.Inlay;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.FontPreferences;
import com.intellij.openapi.editor.impl.ComplementaryFontsRegistry;
import com.intellij.openapi.editor.impl.FontInfo;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.ColoredTextContainer;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.util.ThreeState;
import com.intellij.util.ui.UIUtil;
import com.intellij.xdebugger.XDebugSession;
import com.intellij.xdebugger.XSourcePosition;
import com.intellij.xdebugger.frame.*;
import com.intellij.xdebugger.frame.presentation.XValuePresentation;
import com.intellij.xdebugger.impl.XDebuggerInlayUtil;
import com.intellij.xdebugger.impl.frame.XDebugView;
import com.intellij.xdebugger.impl.frame.XValueMarkers;
import com.intellij.xdebugger.impl.frame.XValueWithInlinePresentation;
@@ -51,17 +39,14 @@ import com.intellij.xdebugger.impl.ui.XDebuggerUIConstants;
import com.intellij.xdebugger.impl.ui.tree.ValueMarkup;
import com.intellij.xdebugger.impl.ui.tree.XDebuggerTree;
import com.intellij.xdebugger.settings.XDebuggerSettingsManager;
import com.intellij.xdebugger.ui.DebuggerColors;
import gnu.trove.TObjectLongHashMap;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Comparator;
import java.util.List;
/**
* @author nik
@@ -179,23 +164,7 @@ public class XValueNodeImpl extends XValueContainerNode<XValue> implements XValu
if (!(container instanceof XValueWithInlinePresentation)) return false;
String presentation = ((XValueWithInlinePresentation)container).computeInlinePresentation();
if (presentation == null) return false;
UIUtil.invokeLaterIfNeeded(() -> {
FileEditor editor = FileEditorManager.getInstance(myTree.getProject()).getSelectedEditor(file);
if (editor instanceof TextEditor) {
Editor e = ((TextEditor)editor).getEditor();
List<Inlay> existing = e.getInlayModel().getInlineElementsInRange(position.getOffset(), position.getOffset());
for (Inlay inlay : existing) {
if (inlay.getRenderer() instanceof MyRenderer) {
Disposer.dispose(inlay);
}
}
CharSequence text = e.getDocument().getImmutableCharSequence();
int offset = position.getOffset();
while (offset < text.length() && Character.isJavaIdentifierPart(text.charAt(offset))) offset++;
e.getInlayModel().addInlineElement(offset, new MyRenderer(presentation));
}
});
XDebuggerInlayUtil.createInlay(myTree.getProject(), file, position.getOffset(), presentation);
return true;
}
@@ -321,39 +290,4 @@ public class XValueNodeImpl extends XValueContainerNode<XValue> implements XValu
public String toString() {
return getName();
}
public static class MyRenderer implements EditorCustomElementRenderer {
private final String myText;
private MyRenderer(String text) {
myText = "(" + text + ")";
}
private static FontInfo getFontInfo(@NotNull Editor editor) {
EditorColorsScheme colorsScheme = editor.getColorsScheme();
FontPreferences fontPreferences = colorsScheme.getFontPreferences();
TextAttributes attributes = editor.getColorsScheme().getAttributes(DebuggerColors.INLINED_VALUES_EXECUTION_LINE);
int fontStyle = attributes == null ? Font.PLAIN : attributes.getFontType();
return ComplementaryFontsRegistry.getFontAbleToDisplay('a', fontStyle, fontPreferences);
}
@Override
public int calcWidthInPixels(@NotNull Editor editor) {
FontInfo fontInfo = getFontInfo(editor);
return fontInfo.fontMetrics().stringWidth(myText);
}
@Override
public void paint(@NotNull Editor editor, @NotNull Graphics g, @NotNull Rectangle r) {
TextAttributes attributes = editor.getColorsScheme().getAttributes(DebuggerColors.INLINED_VALUES_EXECUTION_LINE);
if (attributes == null) return;
Color fgColor = attributes.getForegroundColor();
if (fgColor == null) return;
g.setColor(fgColor);
FontInfo fontInfo = getFontInfo(editor);
g.setFont(fontInfo.getFont());
FontMetrics metrics = fontInfo.fontMetrics();
g.drawString(myText, r.x, r.y + metrics.getAscent());
}
}
}