Search in sources :

Example 91 with TextAttributes

use of com.intellij.openapi.editor.markup.TextAttributes in project intellij-community by JetBrains.

the class ImportLayoutPanel method resizeColumns.

public static void resizeColumns(final PackageEntryTable packageTable, JBTable result, boolean areStaticImportsEnabled) {
    ColoredTableCellRenderer packageRenderer = new ColoredTableCellRenderer() {

        @Override
        protected void customizeCellRenderer(JTable table, Object value, boolean selected, boolean hasFocus, int row, int column) {
            PackageEntry entry = packageTable.getEntryAt(row);
            if (entry == PackageEntry.BLANK_LINE_ENTRY) {
                append("<blank line>", SimpleTextAttributes.GRAYED_ATTRIBUTES);
            } else {
                TextAttributes attributes = JavaHighlightingColors.KEYWORD.getDefaultAttributes();
                append("import", SimpleTextAttributes.fromTextAttributes(attributes));
                if (entry.isStatic()) {
                    append(" ", SimpleTextAttributes.REGULAR_ATTRIBUTES);
                    append("static", SimpleTextAttributes.fromTextAttributes(attributes));
                }
                append(" ", SimpleTextAttributes.REGULAR_ATTRIBUTES);
                if (entry == PackageEntry.ALL_OTHER_IMPORTS_ENTRY || entry == PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY) {
                    append("all other imports", SimpleTextAttributes.REGULAR_ATTRIBUTES);
                } else {
                    append(entry.getPackageName() + ".*", SimpleTextAttributes.REGULAR_ATTRIBUTES);
                }
            }
        }
    };
    if (areStaticImportsEnabled) {
        fixColumnWidthToHeader(result, 0);
        fixColumnWidthToHeader(result, 2);
        result.getColumnModel().getColumn(1).setCellRenderer(packageRenderer);
        result.getColumnModel().getColumn(0).setCellRenderer(new BooleanTableCellRenderer());
        result.getColumnModel().getColumn(2).setCellRenderer(new BooleanTableCellRenderer());
    } else {
        fixColumnWidthToHeader(result, 1);
        result.getColumnModel().getColumn(0).setCellRenderer(packageRenderer);
        result.getColumnModel().getColumn(1).setCellRenderer(new BooleanTableCellRenderer());
    }
}
Also used : PackageEntry(com.intellij.psi.codeStyle.PackageEntry) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 92 with TextAttributes

use of com.intellij.openapi.editor.markup.TextAttributes in project intellij-community by JetBrains.

the class MoveFieldAssignmentToInitializerAction method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    PsiAssignmentExpression assignment = getAssignmentUnderCaret(editor, file);
    if (assignment == null)
        return;
    PsiField field = getAssignedField(assignment);
    if (field == null)
        return;
    List<PsiAssignmentExpression> assignments = new ArrayList<>();
    if (!isInitializedWithSameExpression(field, assignment, assignments))
        return;
    PsiExpression initializer = assignment.getRExpression();
    field.setInitializer(initializer);
    for (PsiAssignmentExpression assignmentExpression : assignments) {
        PsiElement statement = assignmentExpression.getParent();
        PsiElement parent = statement.getParent();
        if (parent instanceof PsiIfStatement || parent instanceof PsiWhileStatement || parent instanceof PsiForStatement || parent instanceof PsiForeachStatement) {
            PsiStatement emptyStatement = JavaPsiFacade.getInstance(file.getProject()).getElementFactory().createStatementFromText(";", statement);
            statement.replace(emptyStatement);
        } else {
            statement.delete();
        }
    }
    TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    HighlightManager.getInstance(project).addOccurrenceHighlights(editor, new PsiElement[] { field.getInitializer() }, attributes, false, null);
}
Also used : ArrayList(java.util.ArrayList) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 93 with TextAttributes

use of com.intellij.openapi.editor.markup.TextAttributes in project intellij-community by JetBrains.

the class TempWithQueryHandler method invokeOnVariable.

private static void invokeOnVariable(final PsiFile file, final Project project, final PsiLocalVariable local, final Editor editor) {
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file))
        return;
    String localName = local.getName();
    final PsiExpression initializer = local.getInitializer();
    if (initializer == null) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.has.no.initializer", localName));
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
        return;
    }
    final PsiReference[] refs = ReferencesSearch.search(local, GlobalSearchScope.projectScope(project), false).toArray(PsiReference.EMPTY_ARRAY);
    if (refs.length == 0) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.is.never.used", localName));
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
        return;
    }
    final HighlightManager highlightManager = HighlightManager.getInstance(project);
    ArrayList<PsiReference> array = new ArrayList<>();
    EditorColorsManager manager = EditorColorsManager.getInstance();
    final TextAttributes attributes = manager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    for (PsiReference ref : refs) {
        PsiElement refElement = ref.getElement();
        if (PsiUtil.isAccessedForWriting((PsiExpression) refElement)) {
            array.add(ref);
        }
        if (!array.isEmpty()) {
            PsiReference[] refsForWriting = array.toArray(new PsiReference[array.size()]);
            highlightManager.addOccurrenceHighlights(editor, refsForWriting, attributes, true, null);
            String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.is.accessed.for.writing", localName));
            CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
            WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
            return;
        }
    }
    final ExtractMethodProcessor processor = new ExtractMethodProcessor(project, editor, new PsiElement[] { initializer }, local.getType(), REFACTORING_NAME, localName, HelpID.REPLACE_TEMP_WITH_QUERY);
    try {
        if (!processor.prepare())
            return;
    } catch (PrepareFailedException e) {
        CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
        ExtractMethodHandler.highlightPrepareError(e, file, editor, project);
        return;
    }
    final PsiClass targetClass = processor.getTargetClass();
    if (targetClass != null && targetClass.isInterface()) {
        String message = RefactoringBundle.message("cannot.replace.temp.with.query.in.interface");
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
        return;
    }
    if (processor.showDialog()) {
        CommandProcessor.getInstance().executeCommand(project, () -> {
            final Runnable action = () -> {
                try {
                    processor.doRefactoring();
                    local.normalizeDeclaration();
                    PsiExpression initializer1 = local.getInitializer();
                    PsiExpression[] exprs = new PsiExpression[refs.length];
                    for (int idx = 0; idx < refs.length; idx++) {
                        PsiElement ref = refs[idx].getElement();
                        exprs[idx] = (PsiExpression) ref.replace(initializer1);
                    }
                    PsiDeclarationStatement declaration = (PsiDeclarationStatement) local.getParent();
                    declaration.delete();
                    highlightManager.addOccurrenceHighlights(editor, exprs, attributes, true, null);
                } catch (IncorrectOperationException e) {
                    LOG.error(e);
                }
            };
            PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(() -> {
                ApplicationManager.getApplication().runWriteAction(action);
                DuplicatesImpl.processDuplicates(processor, project, editor);
            });
        }, REFACTORING_NAME, null);
    }
    WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
}
Also used : PrepareFailedException(com.intellij.refactoring.extractMethod.PrepareFailedException) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) ExtractMethodProcessor(com.intellij.refactoring.extractMethod.ExtractMethodProcessor) ArrayList(java.util.ArrayList) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 94 with TextAttributes

use of com.intellij.openapi.editor.markup.TextAttributes in project intellij-community by JetBrains.

the class DuplicatesImpl method highlightMatch.

public static void highlightMatch(final Project project, Editor editor, final Match match, final ArrayList<RangeHighlighter> highlighters) {
    EditorColorsManager colorsManager = EditorColorsManager.getInstance();
    TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    HighlightManager.getInstance(project).addRangeHighlight(editor, match.getTextRange().getStartOffset(), match.getTextRange().getEndOffset(), attributes, true, highlighters);
}
Also used : TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager)

Example 95 with TextAttributes

use of com.intellij.openapi.editor.markup.TextAttributes in project intellij-community by JetBrains.

the class PackagingElementNode method addErrorHighlighting.

private static SimpleTextAttributes addErrorHighlighting(boolean error, SimpleTextAttributes attributes) {
    final TextAttributes textAttributes = attributes.toTextAttributes();
    textAttributes.setEffectType(EffectType.WAVE_UNDERSCORE);
    textAttributes.setEffectColor(error ? JBColor.RED : JBColor.GRAY);
    return SimpleTextAttributes.fromTextAttributes(textAttributes);
}
Also used : TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes)

Aggregations

TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)205 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)40 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)31 NotNull (org.jetbrains.annotations.NotNull)29 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)28 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)26 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)26 SimpleTextAttributes (com.intellij.ui.SimpleTextAttributes)23 TextRange (com.intellij.openapi.util.TextRange)21 Nullable (org.jetbrains.annotations.Nullable)21 ArrayList (java.util.ArrayList)19 Editor (com.intellij.openapi.editor.Editor)18 Project (com.intellij.openapi.project.Project)17 PsiElement (com.intellij.psi.PsiElement)12 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 JBColor (com.intellij.ui.JBColor)10 Document (com.intellij.openapi.editor.Document)9 ContainerUtil (com.intellij.util.containers.ContainerUtil)9 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)8