Search in sources :

Example 31 with EditorWindow

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

the class LanguageConsoleImpl method printWithHighlighting.

public static String printWithHighlighting(@NotNull LanguageConsoleView console, @NotNull Editor inputEditor, @NotNull TextRange textRange) {
    String text;
    EditorHighlighter highlighter;
    if (inputEditor instanceof EditorWindow) {
        PsiFile file = ((EditorWindow) inputEditor).getInjectedFile();
        highlighter = HighlighterFactory.createHighlighter(file.getVirtualFile(), EditorColorsManager.getInstance().getGlobalScheme(), console.getProject());
        String fullText = InjectedLanguageUtil.getUnescapedText(file, null, null);
        highlighter.setText(fullText);
        text = textRange.substring(fullText);
    } else {
        text = inputEditor.getDocument().getText(textRange);
        highlighter = ((EditorEx) inputEditor).getHighlighter();
    }
    SyntaxHighlighter syntax = highlighter instanceof LexerEditorHighlighter ? ((LexerEditorHighlighter) highlighter).getSyntaxHighlighter() : null;
    ((LanguageConsoleImpl) console).doAddPromptToHistory();
    if (syntax != null) {
        ConsoleViewUtil.printWithHighlighting(console, text, syntax);
    } else {
        console.print(text, ConsoleViewContentType.USER_INPUT);
    }
    console.print("\n", ConsoleViewContentType.NORMAL_OUTPUT);
    return text;
}
Also used : PsiFile(com.intellij.psi.PsiFile) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 32 with EditorWindow

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

the class BackspaceHandler method isOffsetInsideInjected.

private static boolean isOffsetInsideInjected(Editor injectedEditor, int injectedOffset) {
    if (injectedOffset == 0 || injectedOffset >= injectedEditor.getDocument().getTextLength()) {
        return false;
    }
    PsiFile injectedFile = ((EditorWindow) injectedEditor).getInjectedFile();
    InjectedLanguageManager ilm = InjectedLanguageManager.getInstance(injectedFile.getProject());
    TextRange rangeToEdit = new TextRange(injectedOffset - 1, injectedOffset + 1);
    List<TextRange> editables = ilm.intersectWithAllEditableFragments(injectedFile, rangeToEdit);
    return editables.size() == 1 && editables.get(0).equals(rangeToEdit);
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 33 with EditorWindow

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

the class SelectionQuotingTypedHandler method beforeSelectionRemoved.

@Override
public Result beforeSelectionRemoved(char c, Project project, Editor editor, PsiFile file) {
    SelectionModel selectionModel = editor.getSelectionModel();
    if (CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED && selectionModel.hasSelection() && isDelimiter(c)) {
        String selectedText = selectionModel.getSelectedText();
        if (!StringUtil.isEmpty(selectedText)) {
            final int selectionStart = selectionModel.getSelectionStart();
            final int selectionEnd = selectionModel.getSelectionEnd();
            if (selectedText.length() > 1) {
                final char firstChar = selectedText.charAt(0);
                final char lastChar = selectedText.charAt(selectedText.length() - 1);
                if (isSimilarDelimiters(firstChar, c) && lastChar == getMatchingDelimiter(firstChar) && (isQuote(firstChar) || firstChar != c) && !shouldSkipReplacementOfQuotesOrBraces(file, editor, selectedText, c) && selectedText.indexOf(lastChar, 1) == selectedText.length() - 1) {
                    selectedText = selectedText.substring(1, selectedText.length() - 1);
                }
            }
            final int caretOffset = selectionModel.getSelectionStart();
            final char c2 = getMatchingDelimiter(c);
            final String newText = String.valueOf(c) + selectedText + c2;
            boolean ltrSelection = selectionModel.getLeadSelectionOffset() != selectionModel.getSelectionEnd();
            boolean restoreStickySelection = editor instanceof EditorEx && ((EditorEx) editor).isStickySelection();
            selectionModel.removeSelection();
            editor.getDocument().replaceString(selectionStart, selectionEnd, newText);
            TextRange replacedTextRange = Registry.is("editor.smarterSelectionQuoting") ? new TextRange(caretOffset + 1, caretOffset + newText.length() - 1) : new TextRange(caretOffset, caretOffset + newText.length());
            // selection is removed here
            if (replacedTextRange.getEndOffset() <= editor.getDocument().getTextLength()) {
                if (restoreStickySelection) {
                    EditorEx editorEx = (EditorEx) editor;
                    CaretModel caretModel = editorEx.getCaretModel();
                    caretModel.moveToOffset(ltrSelection ? replacedTextRange.getStartOffset() : replacedTextRange.getEndOffset());
                    editorEx.setStickySelection(true);
                    caretModel.moveToOffset(ltrSelection ? replacedTextRange.getEndOffset() : replacedTextRange.getStartOffset());
                } else {
                    if (ltrSelection || editor instanceof EditorWindow) {
                        editor.getSelectionModel().setSelection(replacedTextRange.getStartOffset(), replacedTextRange.getEndOffset());
                    } else {
                        editor.getSelectionModel().setSelection(replacedTextRange.getEndOffset(), replacedTextRange.getStartOffset());
                    }
                    if (Registry.is("editor.smarterSelectionQuoting")) {
                        editor.getCaretModel().moveToOffset(ltrSelection ? replacedTextRange.getEndOffset() : replacedTextRange.getStartOffset());
                    }
                }
            }
            return Result.STOP;
        }
    }
    return super.beforeSelectionRemoved(c, project, editor, file);
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 34 with EditorWindow

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

the class Xslt2RefactoringTest method doExtractFunction.

private void doExtractFunction() throws Throwable {
    myFixture.configureByFile(getTestFileName() + ".xsl");
    final Editor editor = myFixture.getEditor();
    assertTrue("Selection required", editor.getSelectionModel().hasSelection());
    editor.getCaretModel().moveToOffset(editor.getSelectionModel().getSelectionStart());
    final XsltExtractFunctionAction action = new XsltExtractFunctionAction() {

        @Override
        protected RefactoringOptions getSettings(XPathExpression expression, Set<XPathExpression> matchingExpressions) {
            return new RefactoringOptions() {

                @Override
                public boolean isCanceled() {
                    return false;
                }

                @Override
                public String getName() {
                    return "f:foo";
                }
            };
        }
    };
    final PsiFile file = InjectedLanguageUtil.findInjectedPsiNoCommit(myFixture.getFile(), editor.getCaretModel().getOffset());
    final Editor editorWindow = InjectedLanguageUtil.getInjectedEditorForInjectedFile(editor, file);
    assertTrue(editorWindow instanceof EditorWindow);
    action.invoke(myFixture.getProject(), editorWindow, file, null);
    myFixture.checkResultByFile(getTestFileName() + "_after.xsl");
}
Also used : XPathExpression(org.intellij.lang.xpath.psi.XPathExpression) Set(java.util.Set) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) XsltExtractFunctionAction(org.intellij.lang.xpath.xslt.refactoring.XsltExtractFunctionAction) RefactoringOptions(org.intellij.lang.xpath.xslt.refactoring.RefactoringOptions) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 35 with EditorWindow

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

the class VariableInplaceRenamer method restoreSelection.

@Override
protected void restoreSelection() {
    if (mySelectedRange != null) {
        Editor editor = InjectedLanguageUtil.getTopLevelEditor(myEditor);
        TextRange selectedRange;
        if (myEditor instanceof EditorWindow) {
            PsiFile injected = ((EditorWindow) myEditor).getInjectedFile();
            selectedRange = InjectedLanguageManager.getInstance(editor.getProject()).injectedToHost(injected, mySelectedRange);
        } else {
            selectedRange = mySelectedRange;
        }
        TemplateState state = TemplateManagerImpl.getTemplateState(editor);
        if (state != null) {
            for (int i = 0; i < state.getSegmentsCount(); i++) {
                final TextRange segmentRange = state.getSegmentRange(i);
                TextRange intersection = segmentRange.intersection(selectedRange);
                if (intersection != null) {
                    editor.getSelectionModel().setSelection(intersection.getStartOffset(), intersection.getEndOffset());
                    return;
                }
            }
        }
        myEditor.getSelectionModel().setSelection(mySelectedRange.getStartOffset(), mySelectedRange.getEndOffset());
    } else if (!shouldSelectAll()) {
        myEditor.getSelectionModel().removeSelection();
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) EditorWindow(com.intellij.injected.editor.EditorWindow)

Aggregations

EditorWindow (com.intellij.injected.editor.EditorWindow)44 Editor (com.intellij.openapi.editor.Editor)15 TextRange (com.intellij.openapi.util.TextRange)10 Project (com.intellij.openapi.project.Project)9 Document (com.intellij.openapi.editor.Document)8 PsiFile (com.intellij.psi.PsiFile)8 NotNull (org.jetbrains.annotations.NotNull)8 PsiElement (com.intellij.psi.PsiElement)7 com.intellij.openapi.fileEditor (com.intellij.openapi.fileEditor)5 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)4 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)3 EditorEx (com.intellij.openapi.editor.ex.EditorEx)3 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)3 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)3 ArrayList (java.util.ArrayList)3 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)2 DocumentWindow (com.intellij.injected.editor.DocumentWindow)2 InjectedCaret (com.intellij.injected.editor.InjectedCaret)2 SimpleDataContext (com.intellij.openapi.actionSystem.impl.SimpleDataContext)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2