Search in sources :

Example 16 with Project

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

the class GroovyStatementMover method checkAvailable.

@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
    final Project project = file.getProject();
    if (!HandlerUtils.canBeInvoked(editor, project) || !(file instanceof GroovyFileBase))
        return false;
    LineRange range = getLineRangeFromSelection(editor);
    final Document document = editor.getDocument();
    final int offset = document.getLineStartOffset(range.startLine);
    final GrLiteral literal = PsiTreeUtil.findElementOfClassAtOffset(file, offset, GrLiteral.class, false);
    //multiline string
    if (literal != null && literal.textContains('\n'))
        return false;
    final GroovyPsiElement pivot = getElementToMove((GroovyFileBase) file, offset);
    if (pivot == null)
        return false;
    final LineRange pivotRange = getLineRange(pivot);
    range = new LineRange(Math.min(range.startLine, pivotRange.startLine), Math.max(range.endLine, pivotRange.endLine));
    final GroovyPsiElement scope = PsiTreeUtil.getParentOfType(pivot, GrMethod.class, GrTypeDefinitionBody.class, GroovyFileBase.class);
    final boolean stmtLevel = isStatement(pivot);
    boolean topLevel = pivot instanceof GrTypeDefinition && pivot.getParent() instanceof GroovyFileBase;
    final List<LineRange> allRanges = allRanges(scope, stmtLevel, topLevel);
    LineRange prev = null;
    LineRange next = null;
    for (LineRange each : allRanges) {
        if (each.endLine <= range.startLine) {
            prev = each;
        }
        if (each.containsLine(range.startLine)) {
            range = new LineRange(each.startLine, range.endLine);
        }
        if (each.startLine < range.endLine && each.endLine > range.endLine) {
            range = new LineRange(range.startLine, each.endLine);
        }
        if (each.startLine >= range.endLine && next == null) {
            next = each;
        }
    }
    info.toMove = range;
    info.toMove2 = down ? next : prev;
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) Document(com.intellij.openapi.editor.Document) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 17 with Project

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

the class GrExecuteCommandAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = e.getProject();
    final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
    final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
    if (project == null || editor == null || virtualFile == null)
        return;
    FileDocumentManager.getInstance().saveAllDocuments();
    final Document document = editor.getDocument();
    final TextRange selectedRange = EditorUtil.getSelectionInAnyMode(editor);
    final String command = (selectedRange.isEmpty() ? document.getText() : document.getText(selectedRange));
    final GroovyConsole existingConsole = virtualFile.getUserData(GroovyConsole.GROOVY_CONSOLE);
    if (existingConsole == null) {
        GroovyConsole.getOrCreateConsole(project, virtualFile, console -> console.execute(command));
    } else {
        existingConsole.execute(command);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GroovyConsole(org.jetbrains.plugins.groovy.console.GroovyConsole) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document)

Example 18 with Project

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

the class GrNewConsoleAction method getModule.

@Nullable
protected Module getModule(AnActionEvent e) {
    final Project project = e.getProject();
    if (project == null)
        return null;
    final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
    if (file != null) {
        final Module moduleForFile = ModuleUtilCore.findModuleForFile(file, project);
        if (moduleForFile != null)
            return moduleForFile;
    }
    final List<Module> modules = ModuleChooserUtil.filterGroovyCompatibleModules(Arrays.asList(ModuleManager.getInstance(project).getModules()), APPLICABLE_MODULE);
    return modules.isEmpty() ? null : modules.get(0);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with Project

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

the class GrNewConsoleAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = e.getProject();
    final Module module = getModule(e);
    if (project == null || module == null)
        return;
    final VirtualFile contentFile = ConsoleHistoryController.getContentFile(GroovyConsoleRootType.getInstance(), GroovyConsoleRootType.CONTENT_ID, ScratchFileService.Option.create_new_always);
    assert contentFile != null;
    GroovyConsole.createConsole(project, contentFile, module);
    FileEditorManager.getInstance(project).openFile(contentFile, true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module)

Example 20 with Project

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

the class GrSelectModuleAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = e.getProject();
    if (project == null)
        return;
    GroovyConsoleUtil.selectModuleAndRun(project, module -> {
        final Module existingModule = myProjectConsole.getSelectedModule(myFile);
        if (module.equals(existingModule))
            return;
        final GroovyConsole existingConsole = myFile.getUserData(GroovyConsole.GROOVY_CONSOLE);
        if (existingConsole != null)
            existingConsole.stop();
        myProjectConsole.setFileModule(myFile, module);
        myFile.putUserData(GroovyConsole.GROOVY_CONSOLE, null);
        ProjectView.getInstance(project).refresh();
    }, e.getDataContext());
}
Also used : Project(com.intellij.openapi.project.Project) GroovyConsole(org.jetbrains.plugins.groovy.console.GroovyConsole) Module(com.intellij.openapi.module.Module)

Aggregations

Project (com.intellij.openapi.project.Project)3623 VirtualFile (com.intellij.openapi.vfs.VirtualFile)874 NotNull (org.jetbrains.annotations.NotNull)580 Nullable (org.jetbrains.annotations.Nullable)478 Module (com.intellij.openapi.module.Module)368 PsiFile (com.intellij.psi.PsiFile)334 Editor (com.intellij.openapi.editor.Editor)301 PsiElement (com.intellij.psi.PsiElement)292 ArrayList (java.util.ArrayList)214 File (java.io.File)212 Document (com.intellij.openapi.editor.Document)180 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)172 List (java.util.List)158 IOException (java.io.IOException)107 TextRange (com.intellij.openapi.util.TextRange)99 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)96 IncorrectOperationException (com.intellij.util.IncorrectOperationException)95 Presentation (com.intellij.openapi.actionSystem.Presentation)94 DataContext (com.intellij.openapi.actionSystem.DataContext)92 PsiDirectory (com.intellij.psi.PsiDirectory)90