use of com.intellij.openapi.editor.richcopy.model.SyntaxInfo 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.richcopy.model.SyntaxInfo in project intellij-community by JetBrains.
the class SyntaxInfoConstructionTest method getSyntaxInfo.
private static String getSyntaxInfo(Editor editor, PsiFile psiFile) {
final StringBuilder builder = new StringBuilder();
SelectionModel selectionModel = editor.getSelectionModel();
String selectedText = selectionModel.getSelectedText(true);
assertNotNull(selectedText);
final String text = StringUtil.convertLineSeparators(selectedText);
TextWithMarkupProcessor processor = new TextWithMarkupProcessor() {
@Override
void createResult(SyntaxInfo syntaxInfo, Editor editor) {
final ColorRegistry colorRegistry = syntaxInfo.getColorRegistry();
assertEquals(JBColor.BLACK, colorRegistry.dataById(syntaxInfo.getDefaultForeground()));
assertEquals(JBColor.WHITE, colorRegistry.dataById(syntaxInfo.getDefaultBackground()));
assertEquals((float) editor.getColorsScheme().getEditorFontSize(), syntaxInfo.getFontSize(), 0.01f);
syntaxInfo.processOutputInfo(new MarkupHandler() {
@Override
public void handleText(int startOffset, int endOffset) throws Exception {
builder.append("text=").append(text.substring(startOffset, endOffset)).append('\n');
}
@Override
public void handleForeground(int foregroundId) throws Exception {
builder.append("foreground=").append(colorRegistry.dataById(foregroundId)).append(',');
}
@Override
public void handleBackground(int backgroundId) throws Exception {
builder.append("background=").append(colorRegistry.dataById(backgroundId)).append(',');
}
@Override
public void handleFont(int fontNameId) throws Exception {
assertEquals(1, fontNameId);
}
@Override
public void handleStyle(int style) throws Exception {
builder.append("fontStyle=").append(style).append(',');
}
@Override
public boolean canHandleMore() {
return true;
}
});
}
};
processor.collectTransferableData(psiFile, editor, selectionModel.getBlockSelectionStarts(), selectionModel.getBlockSelectionEnds());
return builder.toString();
}
Aggregations