Search in sources :

Example 81 with Editor

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

the class AddEncodingQuickFix method applyFix.

public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PsiFile file = element.getContainingFile();
    if (file == null)
        return;
    PsiElement firstLine = file.getFirstChild();
    if (firstLine instanceof PsiComment && firstLine.getText().startsWith("#!")) {
        firstLine = firstLine.getNextSibling();
    }
    final LanguageLevel languageLevel = LanguageLevel.forElement(file);
    final String commentText = String.format(PyEncodingUtil.ENCODING_FORMAT_PATTERN[myEncodingFormatIndex], myDefaultEncoding);
    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    PsiComment encodingComment = elementGenerator.createFromText(languageLevel, PsiComment.class, commentText);
    encodingComment = (PsiComment) file.addBefore(encodingComment, firstLine);
    final FileEditor fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(element.getContainingFile().getVirtualFile());
    if (fileEditor instanceof TextEditor) {
        if (encodingComment.getNextSibling() == null || !encodingComment.getNextSibling().textContains('\n')) {
            file.addAfter(elementGenerator.createFromText(languageLevel, PsiWhiteSpace.class, "\n"), encodingComment);
        }
        final Editor editor = ((TextEditor) fileEditor).getEditor();
        final Document document = editor.getDocument();
        final int insertedLineNumber = document.getLineNumber(encodingComment.getTextOffset());
        editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(insertedLineNumber + 1, 0));
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Document(com.intellij.openapi.editor.Document) PsiComment(com.intellij.psi.PsiComment) TextEditor(com.intellij.openapi.fileEditor.TextEditor) LanguageLevel(com.jetbrains.python.psi.LanguageLevel) PsiFile(com.intellij.psi.PsiFile) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 82 with Editor

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

the class AddFieldQuickFix method showTemplateBuilder.

private void showTemplateBuilder(PsiElement initStatement, @NotNull final PsiFile file) {
    initStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(initStatement);
    if (initStatement instanceof PyAssignmentStatement) {
        final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(initStatement);
        final PyExpression assignedValue = ((PyAssignmentStatement) initStatement).getAssignedValue();
        final PyExpression leftExpression = ((PyAssignmentStatement) initStatement).getLeftHandSideExpression();
        if (assignedValue != null && leftExpression != null) {
            if (replaceInitializer)
                builder.replaceElement(assignedValue, myInitializer);
            else
                builder.replaceElement(leftExpression.getLastChild(), myIdentifier);
            final VirtualFile virtualFile = file.getVirtualFile();
            if (virtualFile == null)
                return;
            final Editor editor = FileEditorManager.getInstance(file.getProject()).openTextEditor(new OpenFileDescriptor(file.getProject(), virtualFile), true);
            if (editor == null)
                return;
            builder.run(editor, false);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor)

Example 83 with Editor

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

the class PyRenameArgumentQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    if (!(element instanceof PsiNamedElement))
        return;
    final VirtualFile virtualFile = element.getContainingFile().getVirtualFile();
    if (virtualFile != null) {
        final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
        final TemplateBuilderImpl builder = new TemplateBuilderImpl(element);
        final String name = ((PsiNamedElement) element).getName();
        assert name != null;
        assert editor != null;
        builder.replaceElement(element, TextRange.create(0, name.length()), name);
        builder.run(editor, false);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) PsiNamedElement(com.intellij.psi.PsiNamedElement) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 84 with Editor

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

the class ProcessWithConsoleRunner method getHighlightedStringsInConsole.

/**
   * Gets highlighted information from test console. Some parts of output (like file links) may be highlighted, and you need to check them.
   *
   * @return pair of [[ranges], [texts]] where range is [from,to] in doc. for each region, and "text" is text extracted from this region.
   * For example assume that in document "spam eggs ham" words "ham" and "spam" are highlighted.
   * You should have 2 ranges (0, 4) and (10, 13) and 2 strings (spam and ham)
   */
@NotNull
public Pair<List<Pair<Integer, Integer>>, List<String>> getHighlightedStringsInConsole() {
    final List<String> resultStrings = new ArrayList<>();
    final List<Pair<Integer, Integer>> resultRanges = new ArrayList<>();
    ApplicationManager.getApplication().invokeAndWait(() -> {
        myConsole.flushDeferredText();
        final Editor editor = myConsole.getEditor();
        for (final RangeHighlighter highlighter : editor.getMarkupModel().getAllHighlighters()) {
            if (highlighter instanceof RangeHighlighterEx) {
                final int start = ((RangeHighlighterEx) highlighter).getAffectedAreaStartOffset();
                final int end = ((RangeHighlighterEx) highlighter).getAffectedAreaEndOffset();
                resultRanges.add(Pair.create(start, end));
                resultStrings.add(editor.getDocument().getText().substring(start, end));
            }
        }
    }, ModalityState.NON_MODAL);
    return Pair.create(resultRanges, resultStrings);
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) ArrayList(java.util.ArrayList) Editor(com.intellij.openapi.editor.Editor) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 85 with Editor

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

the class MvcProjectViewPane method selectElement.

@Override
public void selectElement(PsiElement element) {
    PsiFileSystemItem psiFile;
    if (element instanceof PsiFileSystemItem) {
        psiFile = (PsiFileSystemItem) element;
    } else {
        psiFile = element.getContainingFile();
        if (psiFile == null) {
            return;
        }
    }
    VirtualFile virtualFile = psiFile.getVirtualFile();
    if (virtualFile == null) {
        return;
    }
    selectFile(virtualFile, false);
    boolean requestFocus = true;
    if (psiFile instanceof PsiFile) {
        Editor editor = EditorHelper.openInEditor(element);
        if (editor != null) {
            ToolWindowManager.getInstance(myProject).activateEditorComponent();
            requestFocus = false;
        }
    }
    if (requestFocus) {
        selectFile(virtualFile, true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor)

Aggregations

Editor (com.intellij.openapi.editor.Editor)748 Project (com.intellij.openapi.project.Project)281 PsiFile (com.intellij.psi.PsiFile)171 VirtualFile (com.intellij.openapi.vfs.VirtualFile)122 NotNull (org.jetbrains.annotations.NotNull)110 Document (com.intellij.openapi.editor.Document)108 PsiElement (com.intellij.psi.PsiElement)107 Nullable (org.jetbrains.annotations.Nullable)103 TextRange (com.intellij.openapi.util.TextRange)77 FileEditor (com.intellij.openapi.fileEditor.FileEditor)67 TextEditor (com.intellij.openapi.fileEditor.TextEditor)48 ArrayList (java.util.ArrayList)39 IncorrectOperationException (com.intellij.util.IncorrectOperationException)36 List (java.util.List)36 EditorEx (com.intellij.openapi.editor.ex.EditorEx)35 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)29 DataContext (com.intellij.openapi.actionSystem.DataContext)27 ApplicationManager (com.intellij.openapi.application.ApplicationManager)25 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)25 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)22