Search in sources :

Example 41 with RangeMarker

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

the class SliceTestUtil method fill.

private static void fill(Map<String, RangeMarker> sliceUsageName2Offset, String name, int offset, final TIntObjectHashMap<IntArrayList> flownOffsets) {
    for (int i = 1; i < 9; i++) {
        String newName = name + i;
        RangeMarker marker = sliceUsageName2Offset.get(newName);
        if (marker == null)
            break;
        IntArrayList offsets = flownOffsets.get(offset);
        if (offsets == null) {
            offsets = new IntArrayList();
            flownOffsets.put(offset, offsets);
        }
        int newStartOffset = marker.getStartOffset();
        offsets.add(newStartOffset);
        fill(sliceUsageName2Offset, newName, newStartOffset, flownOffsets);
    }
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker) IntArrayList(com.intellij.util.containers.IntArrayList)

Example 42 with RangeMarker

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

the class ShowIntentionsPass method addAvailableFixesForGroups.

private static void addAvailableFixesForGroups(@NotNull HighlightInfo info, @NotNull Editor editor, @NotNull PsiFile file, @NotNull List<HighlightInfo.IntentionActionDescriptor> outList, int group, int offset) {
    if (info.quickFixActionMarkers == null)
        return;
    if (group != -1 && group != info.getGroup())
        return;
    boolean fixRangeIsNotEmpty = !info.getFixTextRange().isEmpty();
    Editor injectedEditor = null;
    PsiFile injectedFile = null;
    for (Pair<HighlightInfo.IntentionActionDescriptor, RangeMarker> pair : info.quickFixActionMarkers) {
        HighlightInfo.IntentionActionDescriptor actionInGroup = pair.first;
        RangeMarker range = pair.second;
        if (!range.isValid() || fixRangeIsNotEmpty && isEmpty(range))
            continue;
        if (DumbService.isDumb(file.getProject()) && !DumbService.isDumbAware(actionInGroup.getAction())) {
            continue;
        }
        int start = range.getStartOffset();
        int end = range.getEndOffset();
        final Project project = file.getProject();
        if (start > offset || offset > end) {
            continue;
        }
        Editor editorToUse;
        PsiFile fileToUse;
        if (info.isFromInjection()) {
            if (injectedEditor == null) {
                injectedFile = InjectedLanguageUtil.findInjectedPsiNoCommit(file, offset);
                injectedEditor = InjectedLanguageUtil.getInjectedEditorForInjectedFile(editor, injectedFile);
            }
            editorToUse = injectedEditor;
            fileToUse = injectedFile;
        } else {
            editorToUse = editor;
            fileToUse = file;
        }
        if (actionInGroup.getAction().isAvailable(project, editorToUse, fileToUse)) {
            outList.add(actionInGroup);
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) RangeMarker(com.intellij.openapi.editor.RangeMarker) Editor(com.intellij.openapi.editor.Editor)

Example 43 with RangeMarker

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

the class InstanceofExpressionPostfixTemplate method surroundExpression.

private static void surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull PsiExpression expr) throws IncorrectOperationException {
    assert expr.isValid();
    PsiType[] types = GuessManager.getInstance(project).guessTypeToCast(expr);
    final boolean parenthesesNeeded = expr instanceof PsiPolyadicExpression || expr instanceof PsiConditionalExpression || expr instanceof PsiAssignmentExpression;
    String exprText = parenthesesNeeded ? "(" + expr.getText() + ")" : expr.getText();
    Template template = generateTemplate(project, exprText, types);
    TextRange range;
    if (expr.isPhysical()) {
        range = expr.getTextRange();
    } else {
        RangeMarker rangeMarker = expr.getUserData(ElementToWorkOn.TEXT_RANGE);
        if (rangeMarker == null) {
            PostfixTemplatesUtils.showErrorHint(project, editor);
            return;
        }
        range = new TextRange(rangeMarker.getStartOffset(), rangeMarker.getEndOffset());
    }
    editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    editor.getCaretModel().moveToOffset(range.getStartOffset());
    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    TemplateManager.getInstance(project).startTemplate(editor, template);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 44 with RangeMarker

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

the class StatementMover method surroundWithCodeBlock.

private void surroundWithCodeBlock(@NotNull final MoveInfo info, final boolean down) {
    try {
        final Document document = PsiDocumentManager.getInstance(statementToSurroundWithCodeBlock.getProject()).getDocument(statementToSurroundWithCodeBlock.getContainingFile());
        int startOffset = document.getLineStartOffset(info.toMove.startLine);
        int endOffset = getLineStartSafeOffset(document, info.toMove.endLine);
        if (document.getText().charAt(endOffset - 1) == '\n')
            endOffset--;
        final RangeMarker lineRangeMarker = document.createRangeMarker(startOffset, endOffset);
        final PsiElementFactory factory = JavaPsiFacade.getInstance(statementToSurroundWithCodeBlock.getProject()).getElementFactory();
        PsiCodeBlock codeBlock = factory.createCodeBlock();
        codeBlock.add(statementToSurroundWithCodeBlock);
        final PsiBlockStatement blockStatement = (PsiBlockStatement) factory.createStatementFromText("{}", statementToSurroundWithCodeBlock);
        blockStatement.getCodeBlock().replace(codeBlock);
        PsiBlockStatement newStatement = (PsiBlockStatement) statementToSurroundWithCodeBlock.replace(blockStatement);
        newStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(newStatement);
        info.toMove = new LineRange(document.getLineNumber(lineRangeMarker.getStartOffset()), document.getLineNumber(lineRangeMarker.getEndOffset()) + 1);
        PsiCodeBlock newCodeBlock = newStatement.getCodeBlock();
        if (down) {
            PsiElement blockChild = firstNonWhiteElement(newCodeBlock.getFirstBodyElement(), true);
            if (blockChild == null)
                blockChild = newCodeBlock.getRBrace();
            info.toMove2 = new //document.getLineNumber(newCodeBlock.getParent().getTextRange().getStartOffset()),
            LineRange(//document.getLineNumber(newCodeBlock.getParent().getTextRange().getStartOffset()),
            info.toMove2.startLine, document.getLineNumber(blockChild.getTextRange().getStartOffset()));
        } else {
            int start = document.getLineNumber(newCodeBlock.getRBrace().getTextRange().getStartOffset());
            int end = info.toMove.startLine;
            if (start > end)
                end = start;
            info.toMove2 = new LineRange(start, end);
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document)

Example 45 with RangeMarker

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

the class Bookmark method getLine.

public int getLine() {
    int targetLine = myTarget.getLine();
    if (targetLine == -1)
        return targetLine;
    //What user sees in gutter
    RangeHighlighterEx highlighter = findMyHighlighter();
    if (highlighter != null && highlighter.isValid()) {
        Document document = getDocument();
        if (document != null) {
            return document.getLineNumber(highlighter.getStartOffset());
        }
    }
    RangeMarker marker = myTarget.getRangeMarker();
    if (marker != null && marker.isValid()) {
        Document document = marker.getDocument();
        return document.getLineNumber(marker.getStartOffset());
    }
    return targetLine;
}
Also used : RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document)

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