use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class DefaultXmlTagNameProvider method getRootTagsVariants.
private static List<LookupElement> getRootTagsVariants(final XmlTag tag, final List<LookupElement> elements) {
elements.add(LookupElementBuilder.create("?xml version=\"1.0\" encoding=\"\" ?>").withPresentableText("<?xml version=\"1.0\" encoding=\"\" ?>").withInsertHandler(new InsertHandler<LookupElement>() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
int offset = context.getEditor().getCaretModel().getOffset();
context.getEditor().getCaretModel().moveToOffset(offset - 4);
AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(context.getEditor());
}
}));
final FileBasedIndex fbi = FileBasedIndex.getInstance();
Collection<String> result = new ArrayList<>();
Processor<String> processor = Processors.cancelableCollectProcessor(result);
fbi.processAllKeys(XmlNamespaceIndex.NAME, processor, tag.getProject());
final GlobalSearchScope scope = new EverythingGlobalScope();
for (final String ns : result) {
if (ns.startsWith("file://"))
continue;
fbi.processValues(XmlNamespaceIndex.NAME, ns, null, new FileBasedIndex.ValueProcessor<XsdNamespaceBuilder>() {
@Override
public boolean process(final VirtualFile file, XsdNamespaceBuilder value) {
List<String> tags = value.getRootTags();
for (String s : tags) {
elements.add(LookupElementBuilder.create(s).withTypeText(ns).withInsertHandler(new XmlTagInsertHandler() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
final Editor editor = context.getEditor();
final Document document = context.getDocument();
final int caretOffset = editor.getCaretModel().getOffset();
final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
caretMarker.setGreedyToRight(true);
XmlFile psiFile = (XmlFile) context.getFile();
boolean incomplete = XmlUtil.getTokenOfType(tag, XmlTokenType.XML_TAG_END) == null && XmlUtil.getTokenOfType(tag, XmlTokenType.XML_EMPTY_ELEMENT_END) == null;
XmlNamespaceHelper.getHelper(psiFile).insertNamespaceDeclaration(psiFile, editor, Collections.singleton(ns), null, null);
editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
XmlTag rootTag = psiFile.getRootTag();
if (incomplete) {
XmlToken token = XmlUtil.getTokenOfType(rootTag, XmlTokenType.XML_EMPTY_ELEMENT_END);
// remove tag end added by smart attribute adder :(
if (token != null)
token.delete();
PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(document);
super.handleInsert(context, item);
}
}
}));
}
return true;
}
}, scope);
}
return elements;
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class XmlTagTest method testXHTMLRangeMarkers2.
public void testXHTMLRangeMarkers2() throws Exception {
XmlTag tag = createTag("file.xhtml", "<a>xyz</a>");
PsiFile psiFile = tag.getContainingFile();
Document document = PsiDocumentManager.getInstance(psiFile.getProject()).getDocument(psiFile);
RangeMarker rangeMarker = document.createRangeMarker(5, 5);
final XmlText text = (XmlText) tag.getValue().getChildren()[0];
ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(getProject(), () -> {
try {
text.removeText(2, 3);
} catch (IncorrectOperationException ioe) {
}
}, "", null, UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION));
assertEquals(5, rangeMarker.getStartOffset());
assertEquals(5, rangeMarker.getEndOffset());
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class PyWithTryExceptSurrounder method surroundStatement.
@Override
@Nullable
protected TextRange surroundStatement(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) throws IncorrectOperationException {
PyTryExceptStatement tryStatement = PyElementGenerator.getInstance(project).createFromText(LanguageLevel.getDefault(), PyTryExceptStatement.class, getTemplate());
final PsiElement parent = elements[0].getParent();
final PyStatementList statementList = tryStatement.getTryPart().getStatementList();
statementList.addRange(elements[0], elements[elements.length - 1]);
statementList.getFirstChild().delete();
tryStatement = (PyTryExceptStatement) parent.addBefore(tryStatement, elements[0]);
parent.deleteChildRange(elements[0], elements[elements.length - 1]);
final PsiFile psiFile = parent.getContainingFile();
final Document document = psiFile.getViewProvider().getDocument();
final TextRange range = tryStatement.getTextRange();
assert document != null;
final RangeMarker rangeMarker = document.createRangeMarker(range);
final PsiElement element = psiFile.findElementAt(rangeMarker.getStartOffset());
tryStatement = PsiTreeUtil.getParentOfType(element, PyTryExceptStatement.class);
if (tryStatement != null) {
return getResultRange(tryStatement);
}
return null;
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class GroovyMethodInliner method inlineUsage.
@Override
public void inlineUsage(@NotNull UsageInfo usage, @NotNull PsiElement referenced) {
PsiElement element = usage.getElement();
if (!(element instanceof GrExpression && element.getParent() instanceof GrCallExpression))
return;
final Editor editor = getCurrentEditorIfApplicable(element);
GrCallExpression call = (GrCallExpression) element.getParent();
RangeMarker marker = inlineReferenceImpl(call, myMethod, isOnExpressionOrReturnPlace(call), GroovyInlineMethodUtil.isTailMethodCall(call), editor);
// highlight replaced result
if (marker != null) {
Project project = referenced.getProject();
TextRange range = TextRange.create(marker);
GroovyRefactoringUtil.highlightOccurrencesByRanges(project, editor, new TextRange[] { range });
WindowManager.getInstance().getStatusBar(project).setInfo(GroovyRefactoringBundle.message("press.escape.to.remove.the.highlighting"));
if (editor != null) {
editor.getCaretModel().moveToOffset(marker.getEndOffset());
}
}
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class EduAnswerPlaceholderPainter method createGuardedBlocks.
public static void createGuardedBlocks(@NotNull final Editor editor, AnswerPlaceholder placeholder) {
Document document = editor.getDocument();
if (document instanceof DocumentImpl) {
DocumentImpl documentImpl = (DocumentImpl) document;
List<RangeMarker> blocks = documentImpl.getGuardedBlocks();
Pair<Integer, Integer> offsets = StudyUtils.getPlaceholderOffsets(placeholder, editor.getDocument());
Integer start = offsets.first;
Integer end = offsets.second;
if (start != 0) {
createGuardedBlock(editor, blocks, start - 1, start);
}
if (end != document.getTextLength()) {
createGuardedBlock(editor, blocks, end, end + 1);
}
}
}
Aggregations