Search in sources :

Example 41 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project intellij-plugins by StepicOrg.

the class ModuleBuilderWithSrc method setupRootModel.

@Override
public void setupRootModel(ModifiableRootModel rootModel) throws ConfigurationException {
    super.setupRootModel(rootModel);
    ContentEntry contentEntry = this.doAddContentEntry(rootModel);
    String moduleLibraryPath;
    if (contentEntry != null) {
        moduleLibraryPath = this.getContentEntryPath() + File.separator + "src";
        //noinspection ResultOfMethodCallIgnored
        (new File(moduleLibraryPath)).mkdirs();
        LocalFileSystem localFS = LocalFileSystem.getInstance();
        String name = FileUtil.toSystemIndependentName(moduleLibraryPath);
        VirtualFile sourceLibraryPath = localFS.refreshAndFindFileByPath(name);
        if (sourceLibraryPath != null) {
            contentEntry.addSourceFolder(sourceLibraryPath, false, "");
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContentEntry(com.intellij.openapi.roots.ContentEntry) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 42 with VirtualFile

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

the class GroovyEnterHandler method handleInString.

private static boolean handleInString(Editor editor, int caretOffset, DataContext dataContext, EditorActionHandler originalHandler) {
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return false;
    final VirtualFile vfile = FileDocumentManager.getInstance().getFile(editor.getDocument());
    assert vfile != null;
    PsiFile file = PsiManager.getInstance(project).findFile(vfile);
    Document document = editor.getDocument();
    String fileText = document.getText();
    if (fileText.length() == caretOffset)
        return false;
    if (!checkStringApplicable(editor, caretOffset))
        return false;
    if (file == null)
        return false;
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final PsiElement stringElement = inferStringPair(file, caretOffset);
    if (stringElement == null)
        return false;
    ASTNode node = stringElement.getNode();
    final IElementType nodeElementType = node.getElementType();
    boolean isInsertIndent = isInsertIndent(caretOffset, stringElement.getTextRange().getStartOffset(), fileText);
    // For simple String literals like 'abc'
    CaretModel caretModel = editor.getCaretModel();
    if (nodeElementType == GroovyTokenTypes.mSTRING_LITERAL) {
        if (isSingleQuoteString(stringElement)) {
            //the case of print '\<caret>'
            if (isSlashBeforeCaret(caretOffset, fileText)) {
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else if (stringElement.getParent() instanceof GrReferenceExpression) {
                TextRange range = stringElement.getTextRange();
                convertEndToMultiline(range.getEndOffset(), document, fileText, '\'');
                document.insertString(range.getStartOffset(), "''");
                caretModel.moveToOffset(caretOffset + 2);
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else {
                EditorModificationUtil.insertStringAtCaret(editor, "'+");
                originalHandler.execute(editor, dataContext);
                EditorModificationUtil.insertStringAtCaret(editor, "'");
                PsiDocumentManager.getInstance(project).commitDocument(document);
                CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
            }
        } else {
            insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        }
        return true;
    }
    if (GSTRING_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && GSTRING_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.GSTRING) {
        PsiElement parent = stringElement.getParent();
        if (nodeElementType == GroovyTokenTypes.mGSTRING_LITERAL) {
            parent = stringElement;
        } else {
            while (parent != null && !(parent instanceof GrLiteral)) {
                parent = parent.getParent();
            }
        }
        if (parent == null)
            return false;
        if (isDoubleQuotedString(parent)) {
            PsiElement exprSibling = stringElement.getNextSibling();
            boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
            if (rightFromDollar)
                caretOffset--;
            TextRange parentRange = parent.getTextRange();
            if (rightFromDollar || parent.getParent() instanceof GrReferenceExpression) {
                convertEndToMultiline(parent.getTextRange().getEndOffset(), document, fileText, '"');
                document.insertString(parentRange.getStartOffset(), "\"\"");
                caretModel.moveToOffset(caretOffset + 2);
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
                if (rightFromDollar) {
                    caretModel.moveCaretRelatively(1, 0, false, false, true);
                }
            } else if (isSlashBeforeCaret(caretOffset, fileText)) {
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else {
                EditorModificationUtil.insertStringAtCaret(editor, "\"+");
                originalHandler.execute(editor, dataContext);
                EditorModificationUtil.insertStringAtCaret(editor, "\"");
                PsiDocumentManager.getInstance(project).commitDocument(document);
                CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
            }
        } else {
            insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        }
        return true;
    }
    if (REGEX_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && REGEX_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.REGEX) {
        PsiElement parent = stringElement.getParent();
        if (nodeElementType == GroovyTokenTypes.mREGEX_LITERAL || nodeElementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
            parent = stringElement;
        } else {
            while (parent != null && !(parent instanceof GrLiteral)) {
                parent = parent.getParent();
            }
        }
        if (parent == null || parent.getLastChild() instanceof PsiErrorElement)
            return false;
        PsiElement exprSibling = stringElement.getNextSibling();
        boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
        if (rightFromDollar) {
            caretModel.moveToOffset(caretOffset - 1);
        }
        insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        if (rightFromDollar) {
            caretModel.moveCaretRelatively(1, 0, false, false, true);
        }
        return true;
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CaretModel(com.intellij.openapi.editor.CaretModel) TextRange(com.intellij.openapi.util.TextRange) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Document(com.intellij.openapi.editor.Document) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) IElementType(com.intellij.psi.tree.IElementType) Project(com.intellij.openapi.project.Project) ASTNode(com.intellij.lang.ASTNode) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 43 with VirtualFile

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

the class GroovyAntCustomCompilerProvider method hasCustomCompile.

/**
   * {@inheritDoc}
   */
@Override
public boolean hasCustomCompile(ModuleChunk chunk) {
    for (Module m : chunk.getModules()) {
        if (LibrariesUtil.hasGroovySdk(m)) {
            final Set<String> scriptExtensions = GroovyFileTypeLoader.getCustomGroovyScriptExtensions();
            final ContentIterator groovyFileSearcher = new ContentIterator() {

                @Override
                public boolean processFile(VirtualFile fileOrDir) {
                    ProgressManager.checkCanceled();
                    if (isCompilableGroovyFile(fileOrDir, scriptExtensions)) {
                        return false;
                    }
                    return true;
                }
            };
            final ModuleRootManager rootManager = ModuleRootManager.getInstance(m);
            final ModuleFileIndex fileIndex = rootManager.getFileIndex();
            for (VirtualFile file : rootManager.getSourceRoots(JavaModuleSourceRootTypes.SOURCES)) {
                if (!fileIndex.iterateContentUnderDirectory(file, groovyFileSearcher)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContentIterator(com.intellij.openapi.roots.ContentIterator) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) ModuleFileIndex(com.intellij.openapi.roots.ModuleFileIndex) Module(com.intellij.openapi.module.Module)

Example 44 with VirtualFile

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

the class GroovyConsoleStateService method loadState.

@Override
public void loadState(MyState state) {
    synchronized (myFileModuleMap) {
        myFileModuleMap.clear();
        for (Entry entry : state.list) {
            final VirtualFile file = myFileManager.findFileByUrl(entry.url);
            final Module module = myModuleManager.findModuleByName(entry.moduleName);
            if (file != null) {
                myFileModuleMap.put(file, Pair.create(module, entry.title));
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Module(com.intellij.openapi.module.Module)

Example 45 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile 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)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)5465 File (java.io.File)762 Project (com.intellij.openapi.project.Project)720 Nullable (org.jetbrains.annotations.Nullable)720 NotNull (org.jetbrains.annotations.NotNull)703 PsiFile (com.intellij.psi.PsiFile)571 Module (com.intellij.openapi.module.Module)501 IOException (java.io.IOException)327 ArrayList (java.util.ArrayList)260 Document (com.intellij.openapi.editor.Document)244 PsiElement (com.intellij.psi.PsiElement)209 Test (org.junit.Test)196 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)124 PsiDirectory (com.intellij.psi.PsiDirectory)124 XmlFile (com.intellij.psi.xml.XmlFile)124 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)116 Editor (com.intellij.openapi.editor.Editor)115 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)101 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)91 List (java.util.List)90