Search in sources :

Example 76 with FileType

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

the class TypedHandler method getFileType.

private static FileType getFileType(@NotNull PsiFile file, @NotNull Editor editor) {
    FileType fileType = file.getFileType();
    Language language = PsiUtilBase.getLanguageInEditor(editor, file.getProject());
    if (language != null && language != PlainTextLanguage.INSTANCE) {
        LanguageFileType associatedFileType = language.getAssociatedFileType();
        if (associatedFileType != null)
            fileType = associatedFileType;
    }
    return fileType;
}
Also used : LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) PlainTextLanguage(com.intellij.openapi.fileTypes.PlainTextLanguage) FileType(com.intellij.openapi.fileTypes.FileType) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType)

Example 77 with FileType

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

the class FileReferenceQuickFixProvider method registerQuickFix.

@NotNull
public static List<? extends LocalQuickFix> registerQuickFix(@NotNull FileReference reference) {
    final FileReferenceSet fileReferenceSet = reference.getFileReferenceSet();
    int index = reference.getIndex();
    if (index < 0)
        return Collections.emptyList();
    final String newFileName = reference.getFileNameToCreate();
    // check if we could create file
    if (newFileName.isEmpty() || newFileName.indexOf('\\') != -1 || newFileName.indexOf('*') != -1 || newFileName.indexOf('?') != -1 || SystemInfo.isWindows && newFileName.indexOf(':') != -1) {
        return Collections.emptyList();
    }
    PsiFileSystemItem context = null;
    PsiElement element = reference.getElement();
    PsiFile containingFile = element == null ? null : element.getContainingFile();
    if (index > 0) {
        context = fileReferenceSet.getReference(index - 1).resolve();
    } else {
        // index == 0
        final Collection<PsiFileSystemItem> defaultContexts = fileReferenceSet.getDefaultContexts();
        if (defaultContexts.isEmpty()) {
            return Collections.emptyList();
        }
        Module module = containingFile == null ? null : ModuleUtilCore.findModuleForPsiElement(containingFile);
        for (PsiFileSystemItem defaultContext : defaultContexts) {
            if (defaultContext != null) {
                final VirtualFile virtualFile = defaultContext.getVirtualFile();
                if (virtualFile != null && defaultContext.isDirectory() && virtualFile.isInLocalFileSystem()) {
                    if (context == null) {
                        context = defaultContext;
                    }
                    if (module != null && module == getModuleForContext(defaultContext)) {
                        // fixes IDEA-64156
                        // todo: fix it on PsiFileReferenceHelper level in 10.X
                        context = defaultContext;
                        break;
                    }
                }
            }
        }
        if (context == null && ApplicationManager.getApplication().isUnitTestMode()) {
            context = defaultContexts.iterator().next();
        }
    }
    if (context == null)
        return Collections.emptyList();
    final VirtualFile virtualFile = context.getVirtualFile();
    if (virtualFile == null || !virtualFile.isValid())
        return Collections.emptyList();
    final PsiDirectory directory = context.getManager().findDirectory(virtualFile);
    if (directory == null)
        return Collections.emptyList();
    if (fileReferenceSet.isCaseSensitive()) {
        final PsiElement psiElement = containingFile == null ? null : reference.innerSingleResolve(false, containingFile);
        if (psiElement != null) {
            final String existingElementName = ((PsiNamedElement) psiElement).getName();
            final RenameFileReferenceIntentionAction renameRefAction = new RenameFileReferenceIntentionAction(existingElementName, reference);
            final RenameFileFix renameFileFix = new RenameFileFix(newFileName);
            return Arrays.asList(renameRefAction, renameFileFix);
        }
    }
    final boolean isdirectory;
    if (!reference.isLast()) {
        // directory
        try {
            directory.checkCreateSubdirectory(newFileName);
        } catch (IncorrectOperationException ex) {
            return Collections.emptyList();
        }
        isdirectory = true;
    } else {
        FileType ft = FileTypeManager.getInstance().getFileTypeByFileName(newFileName);
        if (ft instanceof UnknownFileType)
            return Collections.emptyList();
        try {
            directory.checkCreateFile(newFileName);
        } catch (IncorrectOperationException ex) {
            return Collections.emptyList();
        }
        isdirectory = false;
    }
    final CreateFileFix action = new MyCreateFileFix(isdirectory, newFileName, directory, reference);
    return Collections.singletonList(action);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) FileType(com.intellij.openapi.fileTypes.FileType) UnknownFileType(com.intellij.openapi.fileTypes.UnknownFileType) UnknownFileType(com.intellij.openapi.fileTypes.UnknownFileType) RenameFileFix(com.intellij.codeInsight.daemon.impl.quickfix.RenameFileFix) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 78 with FileType

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

the class BackspaceHandler method handleBackspace.

protected boolean handleBackspace(Editor editor, Caret caret, DataContext dataContext, boolean toWordStart) {
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return false;
    PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
    if (file == null)
        return false;
    if (editor.getSelectionModel().hasSelection())
        return false;
    int offset = DocumentUtil.getPreviousCodePointOffset(editor.getDocument(), editor.getCaretModel().getOffset());
    if (offset < 0)
        return false;
    CharSequence chars = editor.getDocument().getCharsSequence();
    int c = Character.codePointAt(chars, offset);
    final Editor injectedEditor = TypedHandler.injectedEditorIfCharTypedIsSignificant(c, editor, file);
    final Editor originalEditor = editor;
    if (injectedEditor != editor) {
        int injectedOffset = injectedEditor.getCaretModel().getOffset();
        if (isOffsetInsideInjected(injectedEditor, injectedOffset)) {
            file = PsiDocumentManager.getInstance(project).getPsiFile(injectedEditor.getDocument());
            editor = injectedEditor;
            offset = DocumentUtil.getPreviousCodePointOffset(injectedEditor.getDocument(), injectedOffset);
        }
    }
    final BackspaceHandlerDelegate[] delegates = Extensions.getExtensions(BackspaceHandlerDelegate.EP_NAME);
    if (!toWordStart && Character.isBmpCodePoint(c)) {
        for (BackspaceHandlerDelegate delegate : delegates) {
            delegate.beforeCharDeleted((char) c, file, editor);
        }
    }
    FileType fileType = file.getFileType();
    final QuoteHandler quoteHandler = TypedHandler.getQuoteHandler(file, editor);
    HighlighterIterator hiterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    boolean wasClosingQuote = quoteHandler != null && quoteHandler.isClosingQuote(hiterator, offset);
    myOriginalHandler.execute(originalEditor, caret, dataContext);
    if (!toWordStart && Character.isBmpCodePoint(c)) {
        for (BackspaceHandlerDelegate delegate : delegates) {
            if (delegate.charDeleted((char) c, file, editor)) {
                return true;
            }
        }
    }
    if (offset >= editor.getDocument().getTextLength())
        return true;
    chars = editor.getDocument().getCharsSequence();
    if ((c == '(' || c == '[' || c == '{') && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
        char c1 = chars.charAt(offset);
        if (c1 != getRightChar((char) c))
            return true;
        HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
        BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
        if (!braceMatcher.isLBraceToken(iterator, chars, fileType) && !braceMatcher.isRBraceToken(iterator, chars, fileType)) {
            return true;
        }
        int rparenOffset = BraceMatchingUtil.findRightmostRParen(iterator, iterator.getTokenType(), chars, fileType);
        if (rparenOffset >= 0) {
            iterator = ((EditorEx) editor).getHighlighter().createIterator(rparenOffset);
            boolean matched = BraceMatchingUtil.matchBrace(chars, fileType, iterator, false);
            if (matched)
                return true;
        }
        editor.getDocument().deleteString(offset, offset + 1);
    } else if ((c == '"' || c == '\'' || c == '`') && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE) {
        char c1 = chars.charAt(offset);
        if (c1 != c)
            return true;
        if (wasClosingQuote)
            return true;
        HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
        if (quoteHandler == null || !quoteHandler.isOpeningQuote(iterator, offset))
            return true;
        editor.getDocument().deleteString(offset, offset + 1);
    }
    return true;
}
Also used : BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) EditorEx(com.intellij.openapi.editor.ex.EditorEx) Project(com.intellij.openapi.project.Project) FileType(com.intellij.openapi.fileTypes.FileType) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 79 with FileType

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

the class BraceMatcherBasedSelectioner method select.

@Override
public List<TextRange> select(final PsiElement e, final CharSequence editorText, final int cursorOffset, final Editor editor) {
    final VirtualFile file = e.getContainingFile().getVirtualFile();
    final FileType fileType = file == null ? null : file.getFileType();
    if (fileType == null)
        return super.select(e, editorText, cursorOffset, editor);
    final int textLength = editorText.length();
    final TextRange totalRange = e.getTextRange();
    final HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(totalRange.getStartOffset());
    final BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
    final ArrayList<TextRange> result = new ArrayList<>();
    final LinkedList<Trinity<Integer, Integer, IElementType>> stack = new LinkedList<>();
    while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
        final Trinity<Integer, Integer, IElementType> last;
        if (braceMatcher.isLBraceToken(iterator, editorText, fileType)) {
            stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), iterator.getTokenType()));
        } else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
            stack.removeLast();
            result.addAll(expandToWholeLine(editorText, new TextRange(last.first, iterator.getEnd())));
            int bodyStart = last.second;
            int bodyEnd = iterator.getStart();
            while (bodyStart < textLength && Character.isWhitespace(editorText.charAt(bodyStart))) bodyStart++;
            while (bodyEnd > 0 && bodyStart < bodyEnd && Character.isWhitespace(editorText.charAt(bodyEnd - 1))) bodyEnd--;
            result.addAll(expandToWholeLine(editorText, new TextRange(bodyStart, bodyEnd)));
        }
        iterator.advance();
    }
    result.add(e.getTextRange());
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Trinity(com.intellij.openapi.util.Trinity) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) LinkedList(java.util.LinkedList) IElementType(com.intellij.psi.tree.IElementType) FileType(com.intellij.openapi.fileTypes.FileType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 80 with FileType

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

the class PostfixDescriptionPanel method showUsages.

private static void showUsages(@NotNull JPanel panel, @Nullable TextDescriptor exampleUsage) {
    String text = "";
    FileType fileType = PlainTextFileType.INSTANCE;
    if (exampleUsage != null) {
        try {
            text = exampleUsage.getText();
            String name = exampleUsage.getFileName();
            FileTypeManagerEx fileTypeManager = FileTypeManagerEx.getInstanceEx();
            String extension = fileTypeManager.getExtension(name);
            fileType = fileTypeManager.getFileTypeByExtension(extension);
        } catch (IOException e) {
            LOG.error(e);
        }
    }
    ((ActionUsagePanel) panel.getComponent(0)).reset(text, fileType);
    panel.repaint();
}
Also used : FileTypeManagerEx(com.intellij.openapi.fileTypes.ex.FileTypeManagerEx) PlainTextFileType(com.intellij.openapi.fileTypes.PlainTextFileType) FileType(com.intellij.openapi.fileTypes.FileType) ActionUsagePanel(com.intellij.codeInsight.intention.impl.config.ActionUsagePanel) IOException(java.io.IOException)

Aggregations

FileType (com.intellij.openapi.fileTypes.FileType)198 VirtualFile (com.intellij.openapi.vfs.VirtualFile)60 NotNull (org.jetbrains.annotations.NotNull)38 Language (com.intellij.lang.Language)31 PsiFile (com.intellij.psi.PsiFile)31 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)30 Nullable (org.jetbrains.annotations.Nullable)28 Project (com.intellij.openapi.project.Project)27 Document (com.intellij.openapi.editor.Document)20 IncorrectOperationException (com.intellij.util.IncorrectOperationException)13 ArrayList (java.util.ArrayList)12 IOException (java.io.IOException)11 Editor (com.intellij.openapi.editor.Editor)10 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)10 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)10 PlainTextFileType (com.intellij.openapi.fileTypes.PlainTextFileType)9 UnknownFileType (com.intellij.openapi.fileTypes.UnknownFileType)9 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)8 AbstractFileType (com.intellij.openapi.fileTypes.impl.AbstractFileType)8 PsiElement (com.intellij.psi.PsiElement)8