Search in sources :

Example 61 with RangeMarker

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

the class VariableLookupItem method handleInsert.

@Override
public void handleInsert(InsertionContext context) {
    PsiVariable variable = getObject();
    Document document = context.getDocument();
    document.replaceString(context.getStartOffset(), context.getTailOffset(), variable.getName());
    context.commitDocument();
    if (variable instanceof PsiField) {
        if (willBeImported()) {
            RangeMarker toDelete = JavaCompletionUtil.insertTemporary(context.getTailOffset(), document, " ");
            context.commitDocument();
            final PsiReferenceExpression ref = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), PsiReferenceExpression.class, false);
            if (ref != null) {
                if (ref.isQualified()) {
                    // shouldn't happen, but sometimes we see exceptions because of this
                    return;
                }
                ref.bindToElementViaStaticImport(((PsiField) variable).getContainingClass());
                PostprocessReformattingAspect.getInstance(ref.getProject()).doPostponedFormatting();
            }
            if (toDelete.isValid()) {
                document.deleteString(toDelete.getStartOffset(), toDelete.getEndOffset());
            }
            context.commitDocument();
        } else if (shouldQualify((PsiField) variable, context)) {
            qualifyFieldReference(context, (PsiField) variable);
        }
    }
    PsiReferenceExpression ref = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getTailOffset() - 1, PsiReferenceExpression.class, false);
    if (ref != null) {
        JavaCodeStyleManager.getInstance(context.getProject()).shortenClassReferences(ref);
    }
    ref = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getTailOffset() - 1, PsiReferenceExpression.class, false);
    PsiElement target = ref == null ? null : ref.resolve();
    if (target instanceof PsiLocalVariable || target instanceof PsiParameter) {
        makeFinalIfNeeded(context, (PsiVariable) target);
    }
    final char completionChar = context.getCompletionChar();
    if (completionChar == '=') {
        context.setAddCompletionChar(false);
        TailType.EQ.processTail(context.getEditor(), context.getTailOffset());
    } else if (completionChar == ',' && getAttribute(LookupItem.TAIL_TYPE_ATTR) != TailType.UNKNOWN) {
        context.setAddCompletionChar(false);
        TailType.COMMA.processTail(context.getEditor(), context.getTailOffset());
        AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(context.getEditor(), null);
    } else if (completionChar == ':' && getAttribute(LookupItem.TAIL_TYPE_ATTR) != TailType.UNKNOWN && isTernaryCondition(ref)) {
        context.setAddCompletionChar(false);
        TailType.COND_EXPR_COLON.processTail(context.getEditor(), context.getTailOffset());
    } else if (completionChar == '.') {
        AutoPopupController.getInstance(context.getProject()).autoPopupMemberLookup(context.getEditor(), null);
    } else if (completionChar == '!' && PsiType.BOOLEAN.isAssignableFrom(variable.getType())) {
        context.setAddCompletionChar(false);
        if (ref != null) {
            FeatureUsageTracker.getInstance().triggerFeatureUsed(CodeCompletionFeatures.EXCLAMATION_FINISH);
            document.insertString(ref.getTextRange().getStartOffset(), "!");
        }
    }
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document)

Example 62 with RangeMarker

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

the class FileStatusMap method getFileDirtyScope.

/**
   * @return null for processed file, whole file for untouched or entirely dirty file, range(usually code block) for dirty region (optimization)
   */
@Nullable
public TextRange getFileDirtyScope(@NotNull Document document, int passId) {
    synchronized (myDocumentToStatusMap) {
        PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
        if (!ProblemHighlightFilter.shouldHighlightFile(file))
            return null;
        FileStatus status = myDocumentToStatusMap.get(document);
        if (status == null) {
            return file == null ? null : file.getTextRange();
        }
        if (status.defensivelyMarked) {
            status.markWholeFileDirty(myProject);
            status.defensivelyMarked = false;
        }
        if (!status.dirtyScopes.containsKey(passId))
            throw new IllegalStateException("Unknown pass " + passId);
        RangeMarker marker = status.dirtyScopes.get(passId);
        return marker == null ? null : marker.isValid() ? TextRange.create(marker) : new TextRange(0, document.getTextLength());
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) Nullable(org.jetbrains.annotations.Nullable)

Example 63 with RangeMarker

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

the class UpdateHighlightersUtil method addHighlighterToEditorIncrementally.

static void addHighlighterToEditorIncrementally(@NotNull Project project, @NotNull Document document, @NotNull PsiFile file, int startOffset, int endOffset, @NotNull final HighlightInfo info, // if null global scheme will be used
@Nullable final EditorColorsScheme colorsScheme, final int group, @NotNull Map<TextRange, RangeMarker> ranges2markersCache) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    if (isFileLevelOrGutterAnnotation(info))
        return;
    if (info.getStartOffset() < startOffset || info.getEndOffset() > endOffset)
        return;
    MarkupModel markup = DocumentMarkupModel.forDocument(document, project, true);
    final SeverityRegistrar severityRegistrar = SeverityRegistrar.getSeverityRegistrar(project);
    final boolean myInfoIsError = isSevere(info, severityRegistrar);
    Processor<HighlightInfo> otherHighlightInTheWayProcessor = oldInfo -> {
        if (!myInfoIsError && isCovered(info, severityRegistrar, oldInfo)) {
            return false;
        }
        return oldInfo.getGroup() != group || !oldInfo.equalsByActualOffset(info);
    };
    boolean allIsClear = DaemonCodeAnalyzerEx.processHighlights(document, project, null, info.getActualStartOffset(), info.getActualEndOffset(), otherHighlightInTheWayProcessor);
    if (allIsClear) {
        createOrReuseHighlighterFor(info, colorsScheme, document, group, file, (MarkupModelEx) markup, null, ranges2markersCache, severityRegistrar);
        clearWhiteSpaceOptimizationFlag(document);
        assertMarkupConsistent(markup, project);
    }
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) GutterMark(com.intellij.codeInsight.daemon.GutterMark) HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) RangeMarker(com.intellij.openapi.editor.RangeMarker) RedBlackTree(com.intellij.openapi.editor.impl.RedBlackTree) com.intellij.openapi.editor.markup(com.intellij.openapi.editor.markup) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) RangeMarkerTree(com.intellij.openapi.editor.impl.RangeMarkerTree) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel)

Example 64 with RangeMarker

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

the class DocumentFragmentContent method createRangeMarker.

@NotNull
private static RangeMarker createRangeMarker(@NotNull Document document, @NotNull TextRange range) {
    RangeMarker rangeMarker = document.createRangeMarker(range.getStartOffset(), range.getEndOffset(), true);
    rangeMarker.setGreedyToLeft(true);
    rangeMarker.setGreedyToRight(true);
    return rangeMarker;
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker) NotNull(org.jetbrains.annotations.NotNull)

Example 65 with RangeMarker

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

the class SliceForwardTest method dotest.

private void dotest() throws Exception {
    configureByFile("/codeInsight/slice/forward/" + getTestName(false) + ".java");
    Map<String, RangeMarker> sliceUsageName2Offset = SliceTestUtil.extractSliceOffsetsFromDocument(getEditor().getDocument());
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    PsiElement element = new SliceForwardHandler().getExpressionAtCaret(getEditor(), getFile());
    assertNotNull(element);
    SliceTestUtil.calcRealOffsets(element, sliceUsageName2Offset, myFlownOffsets);
    Collection<HighlightInfo> errors = highlightErrors();
    assertEmpty(errors);
    SliceAnalysisParams params = new SliceAnalysisParams();
    params.scope = new AnalysisScope(getProject());
    params.dataFlowToThis = false;
    SliceUsage usage = LanguageSlicing.getProvider(element).createRootUsage(element, params);
    SliceTestUtil.checkUsages(usage, myFlownOffsets);
}
Also used : AnalysisScope(com.intellij.analysis.AnalysisScope) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) RangeMarker(com.intellij.openapi.editor.RangeMarker) PsiElement(com.intellij.psi.PsiElement)

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