Search in sources :

Example 76 with LogicalPosition

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

the class LineMover method checkAvailable.

@Override
public boolean checkAvailable(@NotNull final Editor editor, @NotNull final PsiFile file, @NotNull final MoveInfo info, final boolean down) {
    LineRange range = StatementUpDownMover.getLineRangeFromSelection(editor);
    LogicalPosition maxLinePos = editor.offsetToLogicalPosition(editor.getDocument().getTextLength());
    int maxLine = maxLinePos.column == 0 ? maxLinePos.line : maxLinePos.line + 1;
    if (range.startLine == 0 && !down)
        return false;
    if (range.endLine >= maxLine && down)
        return false;
    int nearLine = down ? range.endLine : range.startLine - 1;
    info.toMove = range;
    info.toMove2 = new LineRange(nearLine, nearLine + 1);
    return true;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition)

Example 77 with LogicalPosition

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

the class BaseIndentEnterHandler method preprocessEnter.

@Override
public Result preprocessEnter(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Ref<Integer> caretOffset, @NotNull final Ref<Integer> caretAdvance, @NotNull final DataContext dataContext, final EditorActionHandler originalHandler) {
    Result res = shouldSkipWithResult(file, editor, dataContext);
    if (res != null) {
        return res;
    }
    final Document document = editor.getDocument();
    int caret = editor.getCaretModel().getOffset();
    final int lineNumber = document.getLineNumber(caret);
    final int lineStartOffset = document.getLineStartOffset(lineNumber);
    final int previousLineStartOffset = lineNumber > 0 ? document.getLineStartOffset(lineNumber - 1) : lineStartOffset;
    final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    final HighlighterIterator iterator = highlighter.createIterator(caret - 1);
    final IElementType type = getNonWhitespaceElementType(iterator, lineStartOffset, previousLineStartOffset);
    final CharSequence editorCharSequence = document.getCharsSequence();
    final CharSequence lineIndent = editorCharSequence.subSequence(lineStartOffset, EditorActionUtil.findFirstNonSpaceOffsetOnTheLine(document, lineNumber));
    // Enter in line comment
    if (type == myLineCommentType) {
        final String restString = editorCharSequence.subSequence(caret, document.getLineEndOffset(lineNumber)).toString();
        if (!StringUtil.isEmptyOrSpaces(restString)) {
            final String linePrefix = lineIndent + myLineCommentPrefix;
            EditorModificationUtil.insertStringAtCaret(editor, "\n" + linePrefix);
            editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(lineNumber + 1, linePrefix.length()));
            return Result.Stop;
        } else if (iterator.getStart() < lineStartOffset) {
            EditorModificationUtil.insertStringAtCaret(editor, "\n" + lineIndent);
            return Result.Stop;
        }
    }
    if (!myWorksWithFormatter && LanguageFormatting.INSTANCE.forLanguage(myLanguage) != null) {
        return Result.Continue;
    } else {
        if (myIndentTokens.contains(type)) {
            final String newIndent = getNewIndent(file, document, lineIndent);
            EditorModificationUtil.insertStringAtCaret(editor, "\n" + newIndent);
            return Result.Stop;
        }
        EditorModificationUtil.insertStringAtCaret(editor, "\n" + lineIndent);
        editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(lineNumber + 1, calcLogicalLength(editor, lineIndent)));
        return Result.Stop;
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) EditorEx(com.intellij.openapi.editor.ex.EditorEx) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 78 with LogicalPosition

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

the class ExternalAnnotationsManagerImpl method chooseAnnotationsPlace.

@Override
@NotNull
public AnnotationPlace chooseAnnotationsPlace(@NotNull final PsiElement element) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    //element just created
    if (!element.isPhysical())
        return AnnotationPlace.IN_CODE;
    if (!element.getManager().isInProject(element))
        return AnnotationPlace.EXTERNAL;
    final Project project = myPsiManager.getProject();
    //otherwise external annotations should be read-only
    if (CodeStyleSettingsManager.getSettings(project).USE_EXTERNAL_ANNOTATIONS) {
        final PsiFile containingFile = element.getContainingFile();
        final VirtualFile virtualFile = containingFile.getVirtualFile();
        LOG.assertTrue(virtualFile != null);
        final List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
        if (!entries.isEmpty()) {
            for (OrderEntry entry : entries) {
                if (!(entry instanceof ModuleOrderEntry)) {
                    if (AnnotationOrderRootType.getUrls(entry).length > 0) {
                        return AnnotationPlace.EXTERNAL;
                    }
                    break;
                }
            }
        }
        final MyExternalPromptDialog dialog = ApplicationManager.getApplication().isUnitTestMode() || ApplicationManager.getApplication().isHeadlessEnvironment() ? null : new MyExternalPromptDialog(project);
        if (dialog != null && dialog.isToBeShown()) {
            final PsiElement highlightElement = element instanceof PsiNameIdentifierOwner ? ((PsiNameIdentifierOwner) element).getNameIdentifier() : element.getNavigationElement();
            LOG.assertTrue(highlightElement != null);
            final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
            final List<RangeHighlighter> highlighters = new ArrayList<>();
            final boolean highlight = editor != null && editor.getDocument() == PsiDocumentManager.getInstance(project).getDocument(containingFile);
            try {
                if (highlight) {
                    //do not highlight for batch inspections
                    final EditorColorsManager colorsManager = EditorColorsManager.getInstance();
                    final TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
                    final TextRange textRange = highlightElement.getTextRange();
                    HighlightManager.getInstance(project).addRangeHighlight(editor, textRange.getStartOffset(), textRange.getEndOffset(), attributes, true, highlighters);
                    final LogicalPosition logicalPosition = editor.offsetToLogicalPosition(textRange.getStartOffset());
                    editor.getScrollingModel().scrollTo(logicalPosition, ScrollType.CENTER);
                }
                dialog.show();
                if (dialog.getExitCode() == 2) {
                    return AnnotationPlace.EXTERNAL;
                } else if (dialog.getExitCode() == 1) {
                    return AnnotationPlace.NOWHERE;
                }
            } finally {
                if (highlight) {
                    HighlightManager.getInstance(project).removeSegmentHighlighter(editor, highlighters.get(0));
                }
            }
        } else if (dialog != null) {
            dialog.close(DialogWrapper.OK_EXIT_CODE);
        }
    }
    return AnnotationPlace.IN_CODE;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) Project(com.intellij.openapi.project.Project) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull)

Example 79 with LogicalPosition

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

the class HighlighterUtil method formatTooltip.

private static Object formatTooltip(Editor e, PsiElement element) {
    if (!(element instanceof XmlTag)) {
        final String text = element.getText();
        if ((text == null || text.length() == 0) && MyPsiUtil.isNameElement(element)) {
            final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, true);
            if (tag != null) {
                return tag.getName();
            }
        }
        return text;
    }
    // have to use html/preformatted or else the tooltip gets formatted totally weird.
    final CodeStyleSettingsManager instance = CodeStyleSettingsManager.getInstance(element.getProject());
    final int tabSize = instance.getCurrentSettings().getTabSize(FileTypeManager.getInstance().getFileTypeByExtension("xml"));
    final char[] spaces = new char[tabSize];
    for (int i = 0; i < spaces.length; i++) {
        spaces[i] = ' ';
    }
    final int textOffset = element.getTextOffset();
    final int lineStartOffset = e.logicalPositionToOffset(new LogicalPosition(e.offsetToLogicalPosition(textOffset).line, 0));
    final CharSequence chars = e.getDocument().getCharsSequence();
    int indent = 0;
    for (int i = lineStartOffset; i < textOffset; i++) {
        if (chars.charAt(i) == ' ') {
            indent++;
        } else if (chars.charAt(i) == '\t') {
            indent += ((indent + tabSize) / tabSize) * tabSize - indent;
        } else {
            break;
        }
    }
    final String text = element.getText().replaceAll("\\t", new String(spaces)).replaceAll("&", "&amp;").replaceAll("<", "&lt;");
    final Pattern indentPattern = Pattern.compile("^(\\s*).+");
    final StringBuilder sb = new StringBuilder("<html><pre>");
    final String[] lines = text.split("\\n");
    for (String line : lines) {
        final Matcher matcher = indentPattern.matcher(line);
        if (matcher.matches()) {
            // strip off the amount of spaces the top-level element is indented with
            line = line.substring(Math.min(matcher.group(1).length(), indent));
        }
        sb.append(line).append("\n");
    }
    return sb.append("</pre></html>").toString();
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) Pattern(java.util.regex.Pattern) CodeStyleSettingsManager(com.intellij.psi.codeStyle.CodeStyleSettingsManager) Matcher(java.util.regex.Matcher) XmlTag(com.intellij.psi.xml.XmlTag)

Example 80 with LogicalPosition

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

the class EditorGotoLineNumberDialog method doOKAction.

protected void doOKAction() {
    Coordinates coordinates = getCoordinates();
    if (coordinates == null)
        return;
    LogicalPosition position = new LogicalPosition(coordinates.row, coordinates.column);
    myEditor.getCaretModel().removeSecondaryCarets();
    myEditor.getCaretModel().moveToLogicalPosition(position);
    myEditor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
    myEditor.getSelectionModel().removeSelection();
    IdeFocusManager.getGlobalInstance().requestFocus(myEditor.getContentComponent(), true);
    super.doOKAction();
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition)

Aggregations

LogicalPosition (com.intellij.openapi.editor.LogicalPosition)97 Document (com.intellij.openapi.editor.Document)12 PsiElement (com.intellij.psi.PsiElement)12 Editor (com.intellij.openapi.editor.Editor)11 TextRange (com.intellij.openapi.util.TextRange)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 EditorEx (com.intellij.openapi.editor.ex.EditorEx)7 NotNull (org.jetbrains.annotations.NotNull)6 CaretModel (com.intellij.openapi.editor.CaretModel)5 RelativePoint (com.intellij.ui.awt.RelativePoint)5 JSFile (com.intellij.lang.javascript.psi.JSFile)4 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)4 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)4 Project (com.intellij.openapi.project.Project)4 PsiFile (com.intellij.psi.PsiFile)4 PsiReference (com.intellij.psi.PsiReference)4 PsiMultiReference (com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)4 Mark (com.maddyhome.idea.vim.common.Mark)4 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)3 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)3