use of com.intellij.openapi.editor.markup.MarkupModel in project intellij-community by JetBrains.
the class TextWithMarkupProcessor method collectTransferableData.
@NotNull
@Override
public List<RawTextWithMarkup> collectTransferableData(PsiFile file, Editor editor, int[] startOffsets, int[] endOffsets) {
if (!RichCopySettings.getInstance().isEnabled()) {
return Collections.emptyList();
}
try {
RichCopySettings settings = RichCopySettings.getInstance();
List<Caret> carets = editor.getCaretModel().getAllCarets();
Caret firstCaret = carets.get(0);
final int indentSymbolsToStrip;
final int firstLineStartOffset;
if (Registry.is("editor.richcopy.strip.indents") && carets.size() == 1) {
Pair<Integer, Integer> p = calcIndentSymbolsToStrip(editor.getDocument(), firstCaret.getSelectionStart(), firstCaret.getSelectionEnd());
firstLineStartOffset = p.first;
indentSymbolsToStrip = p.second;
} else {
firstLineStartOffset = firstCaret.getSelectionStart();
indentSymbolsToStrip = 0;
}
logInitial(editor, startOffsets, endOffsets, indentSymbolsToStrip, firstLineStartOffset);
CharSequence text = editor.getDocument().getCharsSequence();
EditorColorsScheme schemeToUse = settings.getColorsScheme(editor.getColorsScheme());
EditorHighlighter highlighter = HighlighterFactory.createHighlighter(file.getViewProvider().getVirtualFile(), schemeToUse, file.getProject());
highlighter.setText(text);
MarkupModel markupModel = DocumentMarkupModel.forDocument(editor.getDocument(), file.getProject(), false);
Context context = new Context(text, schemeToUse, indentSymbolsToStrip);
int endOffset = 0;
Caret prevCaret = null;
for (Caret caret : carets) {
int caretSelectionStart = caret.getSelectionStart();
int caretSelectionEnd = caret.getSelectionEnd();
int startOffsetToUse;
int additionalShift = 0;
if (caret == firstCaret) {
startOffsetToUse = firstLineStartOffset;
} else {
startOffsetToUse = caretSelectionStart;
assert prevCaret != null;
String prevCaretSelectedText = prevCaret.getSelectedText();
// Block selection fills short lines by white spaces
int fillStringLength = prevCaretSelectedText == null ? 0 : prevCaretSelectedText.length() - (prevCaret.getSelectionEnd() - prevCaret.getSelectionStart());
context.addCharacter(endOffset + fillStringLength);
additionalShift = fillStringLength + 1;
}
context.reset(endOffset - caretSelectionStart + additionalShift);
endOffset = caretSelectionEnd;
prevCaret = caret;
if (endOffset <= startOffsetToUse) {
continue;
}
MyMarkupIterator markupIterator = new MyMarkupIterator(text, new CompositeRangeIterator(schemeToUse, new HighlighterRangeIterator(highlighter, startOffsetToUse, endOffset), new MarkupModelRangeIterator(markupModel, schemeToUse, startOffsetToUse, endOffset)), schemeToUse);
try {
context.iterate(markupIterator, endOffset);
} finally {
markupIterator.dispose();
}
}
SyntaxInfo syntaxInfo = context.finish();
logSyntaxInfo(syntaxInfo);
createResult(syntaxInfo, editor);
return ObjectUtils.notNull(myResult, Collections.<RawTextWithMarkup>emptyList());
} catch (Exception e) {
// catching the exception so that the rest of copy/paste functionality can still work fine
LOG.error(e);
}
return Collections.emptyList();
}
use of com.intellij.openapi.editor.markup.MarkupModel in project intellij-community by JetBrains.
the class UnifiedEditorRangeHighlighter method erase.
public static void erase(@Nullable Project project, @NotNull Document document) {
MarkupModel model = DocumentMarkupModel.forDocument(document, project, true);
model.removeAllHighlighters();
}
use of com.intellij.openapi.editor.markup.MarkupModel in project intellij-community by JetBrains.
the class DefaultHighlightInfoProcessor method highlightsInsideVisiblePartAreProduced.
@Override
public void highlightsInsideVisiblePartAreProduced(@NotNull final HighlightingSession session, @NotNull final List<HighlightInfo> infos, @NotNull TextRange priorityRange, @NotNull TextRange restrictRange, final int groupId) {
final PsiFile psiFile = session.getPsiFile();
final Project project = psiFile.getProject();
final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
if (document == null)
return;
final long modificationStamp = document.getModificationStamp();
final TextRange priorityIntersection = priorityRange.intersection(restrictRange);
final Editor editor = session.getEditor();
((HighlightingSessionImpl) session).applyInEDT(() -> {
if (modificationStamp != document.getModificationStamp())
return;
if (priorityIntersection != null) {
MarkupModel markupModel = DocumentMarkupModel.forDocument(document, project, true);
EditorColorsScheme scheme = session.getColorsScheme();
UpdateHighlightersUtil.setHighlightersInRange(project, document, priorityIntersection, scheme, infos, (MarkupModelEx) markupModel, groupId);
}
if (editor != null && !editor.isDisposed()) {
// usability: show auto import popup as soon as possible
if (!DumbService.isDumb(project)) {
new ShowAutoImportPass(project, psiFile, editor).doApplyInformationToEditor();
}
DaemonListeners.repaintErrorStripeRenderer(editor, project);
}
});
}
use of com.intellij.openapi.editor.markup.MarkupModel in project intellij-community by JetBrains.
the class NavigationUtil method patchAttributesColor.
/**
* Patches attributes to be visible under debugger active line
*/
@SuppressWarnings("UseJBColor")
public static TextAttributes patchAttributesColor(TextAttributes attributes, @NotNull TextRange range, @NotNull Editor editor) {
if (attributes.getForegroundColor() == null && attributes.getEffectColor() == null)
return attributes;
MarkupModel model = DocumentMarkupModel.forDocument(editor.getDocument(), editor.getProject(), false);
if (model != null) {
if (!((MarkupModelEx) model).processRangeHighlightersOverlappingWith(range.getStartOffset(), range.getEndOffset(), highlighter -> {
if (highlighter.isValid() && highlighter.getTargetArea() == HighlighterTargetArea.LINES_IN_RANGE) {
TextAttributes textAttributes = highlighter.getTextAttributes();
if (textAttributes != null) {
Color color = textAttributes.getBackgroundColor();
return !(color != null && color.getBlue() > 128 && color.getRed() < 128 && color.getGreen() < 128);
}
}
return true;
})) {
TextAttributes clone = attributes.clone();
clone.setForegroundColor(Color.orange);
clone.setEffectColor(Color.orange);
return clone;
}
}
return attributes;
}
use of com.intellij.openapi.editor.markup.MarkupModel in project intellij-community by JetBrains.
the class ParameterNameHintsConfigurable method highlightErrorLines.
private static void highlightErrorLines(@NotNull List<Integer> lines, @NotNull EditorTextField editorTextField) {
Editor editor = editorTextField.getEditor();
if (editor == null)
return;
final TextAttributes attributes = editor.getColorsScheme().getAttributes(ERRORS_ATTRIBUTES);
final Document document = editor.getDocument();
final int totalLines = document.getLineCount();
MarkupModel model = editor.getMarkupModel();
model.removeAllHighlighters();
lines.stream().filter((current) -> current < totalLines).forEach((line) -> model.addLineHighlighter(line, HighlighterLayer.ERROR, attributes));
}
Aggregations