Search in sources :

Example 1 with InjectedCaret

use of com.intellij.injected.editor.InjectedCaret in project intellij-community by JetBrains.

the class CommentByLineCommentHandler method invoke.

@Override
public // first pass - adjacent carets are grouped into blocks
void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull Caret caret, @NotNull PsiFile file) {
    myProject = project;
    file = file.getViewProvider().getPsi(file.getViewProvider().getBaseLanguage());
    PsiElement context = InjectedLanguageManager.getInstance(file.getProject()).getInjectionHost(file);
    if (context != null && (context.textContains('\'') || context.textContains('\"') || context.textContains('/'))) {
        String s = context.getText();
        if (StringUtil.startsWith(s, "\"") || StringUtil.startsWith(s, "\'") || StringUtil.startsWith(s, "/")) {
            file = context.getContainingFile();
            editor = editor instanceof EditorWindow ? ((EditorWindow) editor).getDelegate() : editor;
            caret = caret instanceof InjectedCaret ? ((InjectedCaret) caret).getDelegate() : caret;
        }
    }
    Document document = editor.getDocument();
    boolean hasSelection = caret.hasSelection();
    int startOffset = caret.getSelectionStart();
    int endOffset = caret.getSelectionEnd();
    FoldRegion fold = editor.getFoldingModel().getCollapsedRegionAtOffset(startOffset);
    if (fold != null && fold.shouldNeverExpand() && fold.getStartOffset() == startOffset && fold.getEndOffset() == endOffset) {
        // Foldings that never expand are automatically selected, so the fact it is selected must not interfere with commenter's logic
        hasSelection = false;
    }
    if (document.getTextLength() == 0)
        return;
    while (true) {
        int lastLineEnd = document.getLineEndOffset(document.getLineNumber(endOffset));
        FoldRegion collapsedAt = editor.getFoldingModel().getCollapsedRegionAtOffset(lastLineEnd);
        if (collapsedAt != null) {
            final int regionEndOffset = collapsedAt.getEndOffset();
            if (regionEndOffset <= endOffset) {
                break;
            }
            endOffset = regionEndOffset;
        } else {
            break;
        }
    }
    int startLine = document.getLineNumber(startOffset);
    int endLine = document.getLineNumber(endOffset);
    if (endLine > startLine && document.getLineStartOffset(endLine) == endOffset) {
        endLine--;
    }
    Block lastBlock = myBlocks.isEmpty() ? null : myBlocks.get(myBlocks.size() - 1);
    Block currentBlock;
    if (lastBlock == null || lastBlock.editor != editor || lastBlock.psiFile != file || startLine > (lastBlock.endLine + 1)) {
        currentBlock = new Block();
        currentBlock.editor = editor;
        currentBlock.psiFile = file;
        currentBlock.startLine = startLine;
        myBlocks.add(currentBlock);
    } else {
        currentBlock = lastBlock;
    }
    currentBlock.carets.add(caret);
    currentBlock.endLine = endLine;
    boolean wholeLinesSelected = !hasSelection || startOffset == document.getLineStartOffset(document.getLineNumber(startOffset)) && endOffset == document.getLineEndOffset(document.getLineNumber(endOffset - 1)) + 1;
    boolean startingNewLineComment = !hasSelection && isLineEmpty(document, document.getLineNumber(startOffset)) && !Comparing.equal(IdeActions.ACTION_COMMENT_LINE, ActionManagerEx.getInstanceEx().getPrevPreformedActionId());
    currentBlock.caretUpdate = startingNewLineComment ? CaretUpdate.PUT_AT_COMMENT_START : !hasSelection ? CaretUpdate.SHIFT_DOWN : wholeLinesSelected ? CaretUpdate.RESTORE_SELECTION : null;
}
Also used : InjectedCaret(com.intellij.injected.editor.InjectedCaret) PsiElement(com.intellij.psi.PsiElement) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 2 with InjectedCaret

use of com.intellij.injected.editor.InjectedCaret in project intellij-community by JetBrains.

the class TextEditorPsiDataProvider method getData.

@Override
@Nullable
public Object getData(@NotNull final String dataId, @NotNull final Editor e, @NotNull final Caret caret) {
    if (e.isDisposed() || !(e instanceof EditorEx)) {
        return null;
    }
    VirtualFile file = ((EditorEx) e).getVirtualFile();
    if (file == null || !file.isValid())
        return null;
    Project project = e.getProject();
    if (dataId.equals(injectedId(EDITOR.getName()))) {
        if (project == null || PsiDocumentManager.getInstance(project).isUncommited(e.getDocument())) {
            return e;
        } else {
            return InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(e, caret, getPsiFile(e, file));
        }
    }
    if (HOST_EDITOR.is(dataId)) {
        return e instanceof EditorWindow ? ((EditorWindow) e).getDelegate() : e;
    }
    if (CARET.is(dataId)) {
        return caret;
    }
    if (dataId.equals(injectedId(CARET.getName()))) {
        Editor editor = (Editor) getData(injectedId(EDITOR.getName()), e, caret);
        assert editor != null;
        return getInjectedCaret(editor, caret);
    }
    if (dataId.equals(injectedId(PSI_ELEMENT.getName()))) {
        Editor editor = (Editor) getData(injectedId(EDITOR.getName()), e, caret);
        assert editor != null;
        Caret injectedCaret = getInjectedCaret(editor, caret);
        return getPsiElementIn(editor, injectedCaret, file);
    }
    if (PSI_ELEMENT.is(dataId)) {
        return getPsiElementIn(e, caret, file);
    }
    if (dataId.equals(injectedId(LANGUAGE.getName()))) {
        PsiFile psiFile = (PsiFile) getData(injectedId(PSI_FILE.getName()), e, caret);
        Editor editor = (Editor) getData(injectedId(EDITOR.getName()), e, caret);
        if (psiFile == null || editor == null)
            return null;
        Caret injectedCaret = getInjectedCaret(editor, caret);
        return getLanguageAtCurrentPositionInEditor(injectedCaret, psiFile);
    }
    if (LANGUAGE.is(dataId)) {
        final PsiFile psiFile = getPsiFile(e, file);
        if (psiFile == null)
            return null;
        return getLanguageAtCurrentPositionInEditor(caret, psiFile);
    }
    if (dataId.equals(injectedId(VIRTUAL_FILE.getName()))) {
        PsiFile psiFile = (PsiFile) getData(injectedId(PSI_FILE.getName()), e, caret);
        if (psiFile == null)
            return null;
        return psiFile.getVirtualFile();
    }
    if (dataId.equals(injectedId(PSI_FILE.getName()))) {
        Editor editor = (Editor) getData(injectedId(EDITOR.getName()), e, caret);
        if (editor == null) {
            return null;
        }
        if (project == null) {
            return null;
        }
        return PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    }
    if (PSI_FILE.is(dataId)) {
        return getPsiFile(e, file);
    }
    if (IDE_VIEW.is(dataId)) {
        final PsiFile psiFile = project == null ? null : PsiManager.getInstance(project).findFile(file);
        final PsiDirectory psiDirectory = psiFile != null ? psiFile.getParent() : null;
        if (psiDirectory != null && (psiDirectory.isPhysical() || ApplicationManager.getApplication().isUnitTestMode())) {
            return new IdeView() {

                @Override
                public void selectElement(final PsiElement element) {
                    Editor editor = EditorHelper.openInEditor(element);
                    if (editor != null) {
                        ToolWindowManager.getInstance(element.getProject()).activateEditorComponent();
                    }
                }

                @NotNull
                @Override
                public PsiDirectory[] getDirectories() {
                    return new PsiDirectory[] { psiDirectory };
                }

                @Override
                public PsiDirectory getOrChooseDirectory() {
                    return psiDirectory;
                }
            };
        }
    }
    if (CONTEXT_LANGUAGES.is(dataId)) {
        return computeLanguages(e, caret);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) EditorEx(com.intellij.openapi.editor.ex.EditorEx) Editor(com.intellij.openapi.editor.Editor) IdeView(com.intellij.ide.IdeView) Caret(com.intellij.openapi.editor.Caret) InjectedCaret(com.intellij.injected.editor.InjectedCaret) EditorWindow(com.intellij.injected.editor.EditorWindow) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

EditorWindow (com.intellij.injected.editor.EditorWindow)2 InjectedCaret (com.intellij.injected.editor.InjectedCaret)2 IdeView (com.intellij.ide.IdeView)1 Caret (com.intellij.openapi.editor.Caret)1 Editor (com.intellij.openapi.editor.Editor)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 Nullable (org.jetbrains.annotations.Nullable)1