replace obsolete myFileType/lexer-highlighting with ConsoleViewUtil.printAsFileType

This commit is contained in:
Gregory.Shrago
2013-11-18 18:29:26 +04:00
parent 68807585ea
commit 3ab82b6fa6
5 changed files with 68 additions and 95 deletions
@@ -54,7 +54,7 @@ public class TextConsoleBuilderImpl extends TextConsoleBuilder {
}
protected ConsoleView createConsole() {
return new ConsoleViewImpl(myProject, myScope, myViewer, null, myUsePredefinedMessageFilter);
return new ConsoleViewImpl(myProject, myScope, myViewer, myUsePredefinedMessageFilter);
}
@Override
@@ -43,21 +43,16 @@ import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.event.*;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.ex.MarkupModelEx;
import com.intellij.openapi.editor.ex.RangeHighlighterEx;
import com.intellij.openapi.editor.ex.util.EditorUtil;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.editor.highlighter.HighlighterClient;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.editor.impl.softwrap.SoftWrapAppliancePlaces;
import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.keymap.Keymap;
import com.intellij.openapi.keymap.KeymapManager;
@@ -67,12 +62,13 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.*;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.tree.IElementType;
import com.intellij.ui.EditorNotificationPanel;
import com.intellij.util.*;
import com.intellij.util.Alarm;
import com.intellij.util.Consumer;
import com.intellij.util.EditorPopupHandler;
import com.intellij.util.SystemProperties;
import com.intellij.util.text.CharArrayUtil;
import gnu.trove.TIntObjectHashMap;
import org.jetbrains.annotations.NonNls;
@@ -249,41 +245,15 @@ public class ConsoleViewImpl extends JPanel implements ConsoleView, ObservableCo
private final Alarm myFoldingAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD, this);
private final List<FoldRegion> myPendingFoldRegions = new ArrayList<FoldRegion>();
private FileType myFileType;
/**
* Use it for custom highlighting for user text.
* This will be highlighted as appropriate file to this file type.
*
* @param fileType according to which use highlighting
*/
public void setFileType(FileType fileType) {
myFileType = fileType;
}
public ConsoleViewImpl(final Project project, boolean viewer) {
this(project, viewer, null);
}
public ConsoleViewImpl(@NotNull final Project project, boolean viewer, @Nullable FileType fileType) {
this(project, GlobalSearchScope.allScope(project), viewer, fileType);
this(project, GlobalSearchScope.allScope(project), viewer, true);
}
public ConsoleViewImpl(@NotNull final Project project,
@NotNull GlobalSearchScope searchScope,
boolean viewer,
@Nullable FileType fileType)
{
this(project, searchScope, viewer, fileType, true);
}
public ConsoleViewImpl(@NotNull final Project project,
@NotNull GlobalSearchScope searchScope,
boolean viewer,
@Nullable FileType fileType,
boolean usePredefinedMessageFilter)
{
this(project, searchScope, viewer, fileType,
boolean usePredefinedMessageFilter) {
this(project, searchScope, viewer,
new ConsoleState.NotStartedStated() {
@Override
public ConsoleState attachTo(ConsoleViewImpl console, ProcessHandler processHandler) {
@@ -296,7 +266,6 @@ public class ConsoleViewImpl extends JPanel implements ConsoleView, ObservableCo
protected ConsoleViewImpl(@NotNull final Project project,
@NotNull GlobalSearchScope searchScope,
boolean viewer,
@Nullable FileType fileType,
@NotNull final ConsoleState initialState,
boolean usePredefinedMessageFilter)
{
@@ -305,7 +274,6 @@ public class ConsoleViewImpl extends JPanel implements ConsoleView, ObservableCo
myState = initialState;
myPsiDisposedCheck = new DisposedPsiManagerCheck(project);
myProject = project;
myFileType = fileType;
myCustomFilter = new CompositeFilter(project);
myPredefinedMessageFilter = new CompositeFilter(project);
@@ -597,13 +565,6 @@ public class ConsoleViewImpl extends JPanel implements ConsoleView, ObservableCo
}
}
/**
* todo python plugin compatibility. Remove on the next update.
*/
@Deprecated
protected void beforeExternalAddContentToDocument(int length, ConsoleViewContentType contentType) {
}
private void addToken(int length, @Nullable HyperlinkInfo info, ConsoleViewContentType contentType) {
ConsoleUtil.addToken(length, info, contentType, myTokens);
}
@@ -928,9 +889,6 @@ public class ConsoleViewImpl extends JPanel implements ConsoleView, ObservableCo
LOG.warn("unhandled external change: " + event);
}
}
if (myFileType != null) {
highlightUserTokens();
}
}
protected EditorEx createRealEditor() {
@@ -941,40 +899,6 @@ public class ConsoleViewImpl extends JPanel implements ConsoleView, ObservableCo
return new MyHighlighter();
}
private void highlightUserTokens() {
if (myTokens.isEmpty()) return;
final TokenInfo token = myTokens.get(myTokens.size() - 1);
if (token.contentType == ConsoleViewContentType.USER_INPUT) {
String text = myEditor.getDocument().getText().substring(token.startOffset, token.endOffset);
PsiFile file = PsiFileFactory.getInstance(myProject).
createFileFromText("dummy", myFileType, text, LocalTimeCounter.currentTime(), true);
Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
assert document != null;
Editor editor = EditorFactory.getInstance().createEditor(document, myProject, myFileType, false);
try {
MarkupModelEx markup = (MarkupModelEx)myEditor.getMarkupModel();
List<RangeHighlighterEx> list = new ArrayList<RangeHighlighterEx>();
markup.processRangeHighlightersOutside(0, token.startOffset-1, new CommonProcessors.CollectProcessor<RangeHighlighterEx>(list));
for (RangeHighlighter highlighter : list) {
if (highlighter.getStartOffset() >= token.startOffset) {
highlighter.dispose();
}
}
HighlighterIterator iterator = ((EditorEx)editor).getHighlighter().createIterator(0);
while (!iterator.atEnd()) {
markup
.addRangeHighlighter(iterator.getStart() + token.startOffset, iterator.getEnd() + token.startOffset, HighlighterLayer.SYNTAX,
iterator.getTextAttributes(),
HighlighterTargetArea.EXACT_RANGE);
iterator.advance();
}
}
finally {
EditorFactory.getInstance().releaseEditor(editor);
}
}
}
private static void registerConsoleEditorActions(Editor editor) {
new EnterHandler().registerCustomShortcutSet(CommonShortcuts.ENTER, editor.getContentComponent());
registerActionHandler(editor, IdeActions.ACTION_EDITOR_PASTE, new PasteHandler());
@@ -1212,9 +1136,6 @@ public class ConsoleViewImpl extends JPanel implements ConsoleView, ObservableCo
@Override
public TextAttributes getTextAttributes() {
if (myFileType != null && getTokenInfo().contentType == ConsoleViewContentType.USER_INPUT) {
return ConsoleViewContentType.NORMAL_OUTPUT.getAttributes();
}
return getTokenInfo() == null ? null : getTokenInfo().attributes;
}
@@ -15,25 +15,35 @@
*/
package com.intellij.execution.impl;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.ide.ui.UISettings;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.EditorFontType;
import com.intellij.openapi.editor.colors.FontPreferences;
import com.intellij.openapi.editor.HighlighterColors;
import com.intellij.openapi.editor.colors.*;
import com.intellij.openapi.editor.colors.impl.DelegateColorScheme;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.impl.EditorFactoryImpl;
import com.intellij.openapi.editor.impl.softwrap.SoftWrapAppliancePlaces;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.containers.FactoryMap;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.util.*;
import java.util.List;
import static com.intellij.execution.ui.ConsoleViewContentType.registerNewConsoleViewType;
/**
* @author peter
@@ -122,4 +132,48 @@ public class ConsoleViewUtil {
public static boolean isConsoleViewEditor(Editor editor) {
return editor.getUserData(EDITOR_IS_CONSOLE_VIEW) == Boolean.TRUE;
}
// @noinspection MismatchedQueryAndUpdateOfCollection
private static final Map<List<TextAttributesKey>, Key> ourContentTypes = Collections.synchronizedMap(new FactoryMap<List<TextAttributesKey>, Key>() {
protected Key create(List<TextAttributesKey> keys) {
EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
TextAttributes result = scheme.getAttributes(HighlighterColors.TEXT);
StringBuilder keyName = new StringBuilder("Generated_");
for (TextAttributesKey key : keys) {
TextAttributes attributes = scheme.getAttributes(key);
if (attributes != null) {
keyName.append("_").append(key.getExternalName());
result = TextAttributes.merge(result, attributes);
}
}
Key newKey = new Key(keyName.toString());
ConsoleViewContentType contentType = new ConsoleViewContentType(keyName.toString(), result);
registerNewConsoleViewType(newKey, contentType);
return newKey;
}
});
public static void printWithHighlighting(@NotNull ConsoleView console, @NotNull String text, @NotNull SyntaxHighlighter highlighter) {
Lexer lexer = highlighter.getHighlightingLexer();
lexer.start(text, 0, text.length(), 0);
IElementType tokenType;
while ((tokenType = lexer.getTokenType()) != null) {
TextAttributesKey[] keys = highlighter.getTokenHighlights(tokenType);
ConsoleViewContentType type = keys.length == 0 ? ConsoleViewContentType.NORMAL_OUTPUT :
ConsoleViewContentType.getConsoleViewType(ourContentTypes.get(Arrays.asList(keys)));
console.print(lexer.getTokenText(), type);
lexer.advance();
}
}
public static void printAsFileType(@NotNull ConsoleView console, @NotNull String text, @NotNull FileType fileType) {
SyntaxHighlighter highlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(fileType, null, null);
if (highlighter != null) {
printWithHighlighting(console, text, highlighter);
}
else {
console.print(text, ConsoleViewContentType.NORMAL_OUTPUT);
}
}
}
@@ -35,6 +35,6 @@ public class TestsConsoleBuilderImpl extends TextConsoleBuilderImpl {
@Override
protected ConsoleView createConsole() {
return new TestsConsoleViewImpl(getProject(), getScope(), isViewer(), null, isUsePredefinedMessageFilter());
return new TestsConsoleViewImpl(getProject(), getScope(), isViewer(), isUsePredefinedMessageFilter());
}
}
@@ -19,7 +19,6 @@ import com.intellij.execution.impl.ConsoleState;
import com.intellij.execution.impl.ConsoleViewImpl;
import com.intellij.execution.impl.ConsoleViewRunningState;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.psi.search.GlobalSearchScope;
@@ -31,9 +30,8 @@ public class TestsConsoleViewImpl extends ConsoleViewImpl {
public TestsConsoleViewImpl(final Project project,
final GlobalSearchScope searchScope,
final boolean viewer,
final FileType fileType,
boolean usePredefinedMessageFilter) {
super(project, searchScope, viewer, fileType,
super(project, searchScope, viewer,
new ConsoleState.NotStartedStated() {
@Override
public ConsoleState attachTo(ConsoleViewImpl console, ProcessHandler processHandler) {