Search in sources :

Example 1 with PythonFileType

use of com.jetbrains.python.PythonFileType in project intellij-community by JetBrains.

the class PyiFileTypeFactory method createFileTypes.

@Override
public void createFileTypes(@NotNull FileTypeConsumer consumer) {
    final PythonFileType instance = PyiFileType.INSTANCE;
    consumer.consume(instance, instance.getDefaultExtension());
}
Also used : PythonFileType(com.jetbrains.python.PythonFileType)

Example 2 with PythonFileType

use of com.jetbrains.python.PythonFileType in project intellij-community by JetBrains.

the class PyMethodNameTypedHandler method beforeCharTyped.

@Override
public Result beforeCharTyped(char character, Project project, Editor editor, PsiFile file, FileType fileType) {
    // else we'd mess up with other file types!
    if (DumbService.isDumb(project) || !(fileType instanceof PythonFileType))
        return Result.CONTINUE;
    if (character == '(') {
        if (!PyCodeInsightSettings.getInstance().INSERT_SELF_FOR_METHODS) {
            return Result.CONTINUE;
        }
        final Document document = editor.getDocument();
        final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
        final int offset = editor.getCaretModel().getOffset();
        PsiElement token = file.findElementAt(offset - 1);
        // sanity check: beyond EOL
        if (token == null)
            return Result.CONTINUE;
        final ASTNode token_node = token.getNode();
        if (token_node != null && token_node.getElementType() == PyTokenTypes.IDENTIFIER) {
            PsiElement maybe_def = PyPsiUtils.getPrevNonCommentSibling(token.getPrevSibling(), false);
            if (maybe_def != null) {
                ASTNode def_node = maybe_def.getNode();
                if (def_node != null && def_node.getElementType() == PyTokenTypes.DEF_KEYWORD) {
                    PsiElement maybe_func = token.getParent();
                    if (maybe_func instanceof PyFunction) {
                        PyFunction func = (PyFunction) maybe_func;
                        PyUtil.MethodFlags flags = PyUtil.MethodFlags.of(func);
                        if (flags != null) {
                            // we're in a method
                            // TODO: all string constants go to Settings
                            String pname = flags.isClassMethod() || flags.isMetaclassMethod() ? "cls" : "self";
                            final boolean is_new = PyNames.NEW.equals(func.getName());
                            if (flags.isMetaclassMethod() && is_new) {
                                pname = "typ";
                            } else if (flags.isClassMethod() || is_new) {
                                pname = "cls";
                            } else if (flags.isStaticMethod())
                                pname = "";
                            documentManager.commitDocument(document);
                            // TODO: only print the ")" if Settings require it
                            int caretOffset = editor.getCaretModel().getOffset();
                            String textToType = "(" + pname + ")";
                            CharSequence chars = editor.getDocument().getCharsSequence();
                            if (caretOffset == chars.length() || chars.charAt(caretOffset) != ':') {
                                textToType += ':';
                            }
                            // right after param name
                            EditorModificationUtil.insertStringAtCaret(editor, textToType, true, 1 + pname.length());
                            return Result.STOP;
                        }
                    }
                }
            }
        }
    }
    // the default
    return Result.CONTINUE;
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) PythonFileType(com.jetbrains.python.PythonFileType) ASTNode(com.intellij.lang.ASTNode) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PyUtil(com.jetbrains.python.psi.PyUtil) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Aggregations

PythonFileType (com.jetbrains.python.PythonFileType)2 ASTNode (com.intellij.lang.ASTNode)1 Document (com.intellij.openapi.editor.Document)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 PsiElement (com.intellij.psi.PsiElement)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 PyUtil (com.jetbrains.python.psi.PyUtil)1