use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class EventLog method formatForLog.
public static LogEntry formatForLog(@NotNull final Notification notification, final String indent) {
DocumentImpl logDoc = new DocumentImpl("", true);
AtomicBoolean showMore = new AtomicBoolean(false);
Map<RangeMarker, HyperlinkInfo> links = new LinkedHashMap<>();
List<RangeMarker> lineSeparators = new ArrayList<>();
String title = notification.getTitle();
String subtitle = notification.getSubtitle();
if (StringUtil.isNotEmpty(title) && StringUtil.isNotEmpty(subtitle)) {
title += " (" + subtitle + ")";
}
title = truncateLongString(showMore, title);
String content = truncateLongString(showMore, notification.getContent());
RangeMarker afterTitle = null;
boolean hasHtml = parseHtmlContent(addIndents(title, indent), notification, logDoc, showMore, links, lineSeparators);
if (StringUtil.isNotEmpty(title)) {
if (StringUtil.isNotEmpty(content)) {
appendText(logDoc, ": ");
afterTitle = logDoc.createRangeMarker(logDoc.getTextLength() - 2, logDoc.getTextLength());
}
}
int titleLength = logDoc.getTextLength();
hasHtml |= parseHtmlContent(addIndents(content, indent), notification, logDoc, showMore, links, lineSeparators);
List<AnAction> actions = notification.getActions();
if (!actions.isEmpty()) {
String text = "<p>" + StringUtil.join(actions, new Function<AnAction, String>() {
private int index;
@Override
public String fun(AnAction action) {
return "<a href=\"" + index++ + "\">" + action.getTemplatePresentation().getText() + "</a>";
}
}, isLongLine(actions) ? "<br>" : " ") + "</p>";
Notification n = new Notification("", "", ".", NotificationType.INFORMATION, new NotificationListener() {
@Override
public void hyperlinkUpdate(@NotNull Notification n, @NotNull HyperlinkEvent event) {
Notification.fire(notification, notification.getActions().get(Integer.parseInt(event.getDescription())));
}
});
if (title.length() > 0 || content.length() > 0) {
lineSeparators.add(logDoc.createRangeMarker(TextRange.from(logDoc.getTextLength(), 0)));
}
hasHtml |= parseHtmlContent(text, n, logDoc, showMore, links, lineSeparators);
}
String status = getStatusText(logDoc, showMore, lineSeparators, indent, hasHtml);
indentNewLines(logDoc, lineSeparators, afterTitle, hasHtml, indent);
ArrayList<Pair<TextRange, HyperlinkInfo>> list = new ArrayList<>();
for (RangeMarker marker : links.keySet()) {
if (!marker.isValid()) {
showMore.set(true);
continue;
}
list.add(Pair.create(new TextRange(marker.getStartOffset(), marker.getEndOffset()), links.get(marker)));
}
if (showMore.get()) {
String sb = "show balloon";
if (!logDoc.getText().endsWith(" ")) {
appendText(logDoc, " ");
}
appendText(logDoc, "(" + sb + ")");
list.add(new Pair<>(TextRange.from(logDoc.getTextLength() - 1 - sb.length(), sb.length()), new ShowBalloon(notification)));
}
return new LogEntry(logDoc.getText(), status, list, titleLength);
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class SmartEnterProcessorWithFixers method doEnter.
protected void doEnter(@NotNull PsiElement atCaret, @NotNull PsiFile psiFile, @NotNull Editor editor, boolean afterCompletion) throws IncorrectOperationException {
if (myFirstErrorOffset != Integer.MAX_VALUE) {
editor.getCaretModel().moveToOffset(myFirstErrorOffset);
reformat(atCaret);
return;
}
final RangeMarker rangeMarker = createRangeMarker(atCaret);
if (reformatBeforeEnter(atCaret)) {
reformat(atCaret);
}
commit(editor);
PsiElement actualAtCaret = restoreElementAtCaret(psiFile, atCaret, rangeMarker);
int endOffset = rangeMarker.getEndOffset();
rangeMarker.dispose();
if (actualAtCaret != null) {
for (FixEnterProcessor enterProcessor : myEnterProcessors) {
if (enterProcessor.doEnter(actualAtCaret, psiFile, editor, isModified(editor))) {
return;
}
}
}
if (!isModified(editor) && !afterCompletion) {
if (actualAtCaret != null) {
plainEnter(editor);
}
} else {
editor.getCaretModel().moveToOffset(myFirstErrorOffset == Integer.MAX_VALUE ? (actualAtCaret != null ? actualAtCaret.getTextRange().getEndOffset() : endOffset) : myFirstErrorOffset);
}
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class TodoPanel method updatePreviewPanel.
private void updatePreviewPanel() {
if (myProject == null || myProject.isDisposed())
return;
List<UsageInfo> infos = new ArrayList<>();
final TreePath path = myTree.getSelectionPath();
if (path != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
Object userObject = node.getUserObject();
if (userObject instanceof NodeDescriptor) {
Object element = ((NodeDescriptor) userObject).getElement();
TodoItemNode pointer = myTodoTreeBuilder.getFirstPointerForElement(element);
if (pointer != null) {
final SmartTodoItemPointer value = pointer.getValue();
final Document document = value.getDocument();
final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
final RangeMarker rangeMarker = value.getRangeMarker();
if (psiFile != null) {
infos.add(new UsageInfo(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset()));
}
}
}
}
myUsagePreviewPanel.updateLayout(infos.isEmpty() ? null : infos);
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class DocumentBasedReplaceHandler method prepare.
@Override
public void prepare(ReplacementInfo info) {
assert info instanceof ReplacementInfoImpl;
MatchResult result = ((ReplacementInfoImpl) info).getMatchResult();
PsiElement element = result.getMatch();
PsiFile file = element instanceof PsiFile ? (PsiFile) element : element.getContainingFile();
Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
TextRange textRange = result.getMatch().getTextRange();
assert textRange != null;
RangeMarker rangeMarker = document.createRangeMarker(textRange);
rangeMarker.setGreedyToLeft(true);
rangeMarker.setGreedyToRight(true);
myRangeMarkers.put(info, rangeMarker);
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class GroovyCompletionUtil method addImportForClass.
public static int addImportForClass(PsiFile file, int startOffset, int endOffset, PsiClass aClass) throws IncorrectOperationException {
// LOG.assertTrue(CommandProcessor.getInstance().getCurrentCommand() != null);
// LOG.assertTrue(
// ApplicationManager.getApplication().isUnitTestMode() || ApplicationManager.getApplication().getCurrentWriteAction(null) != null);
final PsiManager manager = file.getManager();
final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
int newStartOffset = startOffset;
final PsiReference reference = file.findReferenceAt(endOffset - 1);
if (reference != null) {
final PsiElement resolved = reference.resolve();
if (resolved instanceof PsiClass) {
if (((PsiClass) resolved).getQualifiedName() == null || manager.areElementsEquivalent(aClass, resolved)) {
return newStartOffset;
}
}
}
String name = aClass.getName();
document.replaceString(startOffset, endOffset, name);
final RangeMarker toDelete = JavaCompletionUtil.insertTemporary(endOffset, document, " ");
PsiDocumentManager.getInstance(manager.getProject()).commitAllDocuments();
final PsiReference ref = file.findReferenceAt(startOffset);
if (ref instanceof GrReferenceElement && aClass.isValid()) {
PsiElement newElement = ref.bindToElement(aClass);
RangeMarker marker = document.createRangeMarker(newElement.getTextRange());
CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(newElement);
newStartOffset = marker.getStartOffset();
}
if (toDelete.isValid()) {
document.deleteString(toDelete.getStartOffset(), toDelete.getEndOffset());
}
return newStartOffset;
}
Aggregations