Search in sources :

Example 6 with RangeMarker

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

the class ExtendedTagInsertHandler method handleInsert.

@Override
public void handleInsert(final InsertionContext context, final LookupElement item) {
    final XmlFile contextFile = (XmlFile) context.getFile();
    final XmlExtension extension = XmlExtension.getExtension(contextFile);
    final XmlFile file = extension.getContainingFile(contextFile);
    final Project project = context.getProject();
    assert file != null;
    final PsiElement psiElement = file.findElementAt(context.getStartOffset());
    assert psiElement != null;
    if (isNamespaceBound(psiElement)) {
        doDefault(context, item);
        return;
    }
    final Editor editor = context.getEditor();
    final Document document = editor.getDocument();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final int caretOffset = editor.getCaretModel().getOffset();
    final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
    caretMarker.setGreedyToRight(true);
    final XmlNamespaceHelper.Runner<String, IncorrectOperationException> runAfter = new XmlNamespaceHelper.Runner<String, IncorrectOperationException>() {

        @Override
        public void run(final String namespacePrefix) {
            PsiDocumentManager.getInstance(project).commitDocument(document);
            final PsiElement element = file.findElementAt(context.getStartOffset());
            if (element != null) {
                qualifyWithPrefix(namespacePrefix, element, document);
                PsiDocumentManager.getInstance(project).commitDocument(document);
            }
            editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
            PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
            doDefault(context, item);
        }
    };
    try {
        final String prefixByNamespace = getPrefixByNamespace(file, myNamespace);
        if (myNamespacePrefix != null || StringUtil.isEmpty(prefixByNamespace)) {
            final String nsPrefix = myNamespacePrefix == null ? suggestPrefix(file, myNamespace) : myNamespacePrefix;
            XmlNamespaceHelper.getHelper(file).insertNamespaceDeclaration(file, editor, Collections.singleton(myNamespace), nsPrefix, runAfter);
            FeatureUsageTracker.getInstance().triggerFeatureUsed(XmlCompletionContributor.TAG_NAME_COMPLETION_FEATURE);
        } else {
            // qualify && complete
            runAfter.run(prefixByNamespace);
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) XmlExtension(com.intellij.xml.XmlExtension) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 7 with RangeMarker

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

the class CreateNSDeclarationIntentionFix method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
    final PsiElement element = myElement.retrieve();
    if (element == null)
        return;
    XmlFile xmlFile = getFile();
    final String[] namespaces = ArrayUtil.toStringArray(getNamespaces(element, xmlFile));
    runActionOverSeveralAttributeValuesAfterLettingUserSelectTheNeededOne(namespaces, project, new StringToAttributeProcessor() {

        @Override
        public void doSomethingWithGivenStringToProduceXmlAttributeNowPlease(@NotNull final String namespace) throws IncorrectOperationException {
            String prefix = myNamespacePrefix;
            if (StringUtil.isEmpty(prefix)) {
                final XmlFile xmlFile = XmlExtension.getExtension(file).getContainingFile(element);
                prefix = ExtendedTagInsertHandler.getPrefixByNamespace(xmlFile, namespace);
                if (StringUtil.isNotEmpty(prefix)) {
                    // namespace already declared
                    ExtendedTagInsertHandler.qualifyWithPrefix(prefix, element);
                    return;
                } else {
                    prefix = ExtendedTagInsertHandler.suggestPrefix(xmlFile, namespace);
                    if (!StringUtil.isEmpty(prefix)) {
                        ExtendedTagInsertHandler.qualifyWithPrefix(prefix, element);
                        PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
                    }
                }
            }
            final int offset = editor.getCaretModel().getOffset();
            final RangeMarker marker = editor.getDocument().createRangeMarker(offset, offset);
            final XmlNamespaceHelper helper = XmlNamespaceHelper.getHelper(file);
            helper.insertNamespaceDeclaration((XmlFile) file, editor, Collections.singleton(namespace), prefix, new XmlNamespaceHelper.Runner<String, IncorrectOperationException>() {

                @Override
                public void run(final String param) throws IncorrectOperationException {
                    if (!namespace.isEmpty()) {
                        editor.getCaretModel().moveToOffset(marker.getStartOffset());
                    }
                }
            });
        }
    }, getTitle(), this, editor);
}
Also used : XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) IncorrectOperationException(com.intellij.util.IncorrectOperationException) RangeMarker(com.intellij.openapi.editor.RangeMarker) PsiElement(com.intellij.psi.PsiElement)

Example 8 with RangeMarker

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

the class ChainCompletionNewVariableLookupElement method handleInsert.

@Override
public void handleInsert(final InsertionContext context) {
    final RangeMarker rangeMarker = context.getDocument().createRangeMarker(context.getStartOffset(), context.getStartOffset());
    getDelegate().handleInsert(context);
    final PsiFile file = context.getFile();
    ((PsiJavaFile) file).importClass(myPsiClass);
    final PsiElement caretElement = file.findElementAt(context.getEditor().getCaretModel().getOffset());
    if (caretElement == null) {
        log.error("element on caret position MUST BE not null");
        return;
    }
    final PsiStatement statement = (PsiStatement) caretElement.getPrevSibling();
    final PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(statement, PsiCodeBlock.class);
    if (codeBlock == null) {
        log.error("code block MUST BE not null");
        return;
    }
    final Project project = context.getProject();
    final Ref<PsiElement> insertedStatementRef = Ref.create();
    final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
    context.commitDocument();
    final PsiStatement statementFromText = elementFactory.createStatementFromText(String.format("%s %s = null;", myPsiClass.getName(), myNewVarName), null);
    insertedStatementRef.set(codeBlock.addBefore(statementFromText, statement));
    final PsiLiteralExpression nullKeyword = findNull(insertedStatementRef.get());
    PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument());
    context.getDocument().insertString(rangeMarker.getStartOffset(), myNewVarName + ".");
    context.commitDocument();
    final int offset = nullKeyword.getTextOffset();
    final int endOffset = offset + nullKeyword.getTextLength();
    context.getEditor().getSelectionModel().setSelection(offset, endOffset);
    context.getEditor().getCaretModel().moveToOffset(offset);
}
Also used : Project(com.intellij.openapi.project.Project) RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 9 with RangeMarker

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

the class OffsetMap method getOffset.

/**
   * @param key key
   * @return offset An offset registered earlier with this key.
   * -1 if offset wasn't registered or became invalidated due to document changes
   */
public int getOffset(OffsetKey key) {
    synchronized (myMap) {
        final RangeMarker marker = myMap.get(key);
        if (marker == null)
            throw new IllegalArgumentException("Offset " + key + " is not registered");
        if (!marker.isValid()) {
            removeOffset(key);
            throw new IllegalStateException("Offset " + key + " is invalid: " + marker);
        }
        final int endOffset = marker.getEndOffset();
        if (marker.getStartOffset() != endOffset) {
            saveOffset(key, endOffset, false);
        }
        return endOffset;
    }
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 10 with RangeMarker

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

the class DaemonCodeAnalyzerImpl method isOffsetInsideHighlightInfo.

private static boolean isOffsetInsideHighlightInfo(int offset, @NotNull HighlightInfo info, boolean includeFixRange) {
    RangeHighlighterEx highlighter = info.getHighlighter();
    if (highlighter == null || !highlighter.isValid())
        return false;
    int startOffset = highlighter.getStartOffset();
    int endOffset = highlighter.getEndOffset();
    if (startOffset <= offset && offset <= endOffset) {
        return true;
    }
    if (!includeFixRange)
        return false;
    RangeMarker fixMarker = info.fixMarker;
    if (fixMarker != null) {
        // null means its range is the same as highlighter
        if (!fixMarker.isValid())
            return false;
        startOffset = fixMarker.getStartOffset();
        endOffset = fixMarker.getEndOffset();
        return startOffset <= offset && offset <= endOffset;
    }
    return false;
}
Also used : RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) RangeMarker(com.intellij.openapi.editor.RangeMarker)

Aggregations

RangeMarker (com.intellij.openapi.editor.RangeMarker)111 Document (com.intellij.openapi.editor.Document)33 TextRange (com.intellij.openapi.util.TextRange)20 Project (com.intellij.openapi.project.Project)19 PsiFile (com.intellij.psi.PsiFile)14 PsiElement (com.intellij.psi.PsiElement)13 IncorrectOperationException (com.intellij.util.IncorrectOperationException)13 Editor (com.intellij.openapi.editor.Editor)11 Nullable (org.jetbrains.annotations.Nullable)11 NotNull (org.jetbrains.annotations.NotNull)10 Template (com.intellij.codeInsight.template.Template)8 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)7 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)6 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)6 RangeHighlighterEx (com.intellij.openapi.editor.ex.RangeHighlighterEx)5 THashMap (gnu.trove.THashMap)5 GutterMark (com.intellij.codeInsight.daemon.GutterMark)4 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)4 ApplicationManager (com.intellij.openapi.application.ApplicationManager)4 RelativePoint (com.intellij.ui.awt.RelativePoint)4