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());
}
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;
}
Aggregations