use of com.intellij.openapi.fileEditor.TextEditor in project intellij-community by JetBrains.
the class UnresolvedRefCreateFunctionQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PyCallExpression callExpr = myCallExpr.getElement();
final PyReferenceExpression referenceExpr = myReferenceExpr.getElement();
if (callExpr == null || !callExpr.isValid() || referenceExpr == null || !referenceExpr.isValid()) {
return;
}
final PyFunctionBuilder functionBuilder = new PyFunctionBuilder(referenceExpr.getText(), callExpr);
// if function is actually an argument of a call, don't use other arguments of the call to create parameter list of new function
final PyArgumentList argumentList = callExpr.getArgumentList();
if (argumentList != null && !PsiTreeUtil.isAncestor(argumentList, referenceExpr, false)) {
for (PyExpression param : argumentList.getArguments()) {
if (param instanceof PyKeywordArgument) {
functionBuilder.parameter(((PyKeywordArgument) param).getKeyword());
} else if (param instanceof PyReferenceExpression) {
PyReferenceExpression refex = (PyReferenceExpression) param;
functionBuilder.parameter(refex.getReferencedName());
} else {
functionBuilder.parameter("param");
}
}
} else {
functionBuilder.parameter("args");
}
PyFunction function = functionBuilder.buildFunction(project, LanguageLevel.getDefault());
final InjectedLanguageManager instance = InjectedLanguageManager.getInstance(project);
final PsiLanguageInjectionHost host = instance.getInjectionHost(callExpr);
final PsiElement insertAnchor = host != null ? host : callExpr;
final PyFunction parentFunction = PsiTreeUtil.getTopmostParentOfType(insertAnchor, PyFunction.class);
if (parentFunction != null) {
final PyClass parentClass = PsiTreeUtil.getTopmostParentOfType(parentFunction, PyClass.class);
if (parentClass != null) {
final PsiElement parent = parentClass.getParent();
function = (PyFunction) parent.addBefore(function, parentClass);
} else {
final PsiElement parent = parentFunction.getParent();
function = (PyFunction) parent.addBefore(function, parentFunction);
}
} else {
final PyStatement statement = PsiTreeUtil.getTopmostParentOfType(insertAnchor, PyStatement.class);
if (statement != null) {
final PsiElement parent = statement.getParent();
if (parent != null) {
function = (PyFunction) parent.addBefore(function, statement);
}
}
}
function = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(function);
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(function);
ParamHelper.walkDownParamArray(function.getParameterList().getParameters(), new ParamHelper.ParamVisitor() {
public void visitNamedParameter(PyNamedParameter param, boolean first, boolean last) {
builder.replaceElement(param, param.getName());
}
});
builder.replaceElement(function.getStatementList(), PyNames.PASS);
final FileEditor editor = FileEditorManager.getInstance(project).getSelectedEditor(insertAnchor.getContainingFile().getVirtualFile());
if (!(editor instanceof TextEditor)) {
return;
}
builder.run(((TextEditor) editor).getEditor(), false);
}
use of com.intellij.openapi.fileEditor.TextEditor in project intellij-community by JetBrains.
the class DomStructureViewBuilder method createStructureView.
@Override
@NotNull
public StructureView createStructureView(final FileEditor fileEditor, @NotNull final Project project) {
return new StructureViewComponent(fileEditor, createStructureViewModel(fileEditor instanceof TextEditor ? ((TextEditor) fileEditor).getEditor() : null), project, true) {
@Override
public AsyncResult<AbstractTreeNode> expandPathToElement(final Object element) {
if (element instanceof XmlElement && ((XmlElement) element).isValid()) {
final XmlElement xmlElement = (XmlElement) element;
XmlTag tag = PsiTreeUtil.getParentOfType(xmlElement, XmlTag.class, false);
while (tag != null) {
final DomElement domElement = DomManager.getDomManager(xmlElement.getProject()).getDomElement(tag);
if (domElement != null) {
for (DomElement curElement = domElement; curElement != null; curElement = curElement.getParent()) {
if (myDescriptor.fun(curElement) == DomService.StructureViewMode.SHOW) {
return super.expandPathToElement(curElement.getXmlElement());
}
}
}
tag = PsiTreeUtil.getParentOfType(tag, XmlTag.class, true);
}
}
return super.expandPathToElement(element);
}
};
}
use of com.intellij.openapi.fileEditor.TextEditor in project intellij-plugins by JetBrains.
the class ActionScriptStubsTest method doTest.
private void doTest(@Nullable final ThrowableRunnable<Exception> runnable, String... files) throws Exception {
Runnable r = runnable != null ? () -> {
try {
runnable.run();
} catch (Exception e) {
throw new RuntimeException(e);
}
} : null;
doTestFor(true, r, files);
// the first one was parsed during highlighting
assertNotParsed(myPsiFiles.subList(1, myPsiFiles.size()));
// we need to go though files open in editors
assertNotParsed(ContainerUtil.mapNotNull(FileEditorManager.getInstance(myProject).getOpenFiles(), virtualFile -> {
if (Comparing.equal(virtualFile, myFile.getVirtualFile())) {
return null;
}
Document document = ((TextEditor) FileEditorManager.getInstance(myProject).getSelectedEditor(virtualFile)).getEditor().getDocument();
return PsiDocumentManager.getInstance(myProject).getPsiFile(document);
}));
}
use of com.intellij.openapi.fileEditor.TextEditor in project intellij-plugins by JetBrains.
the class ActionScriptHighlightingTest method setActiveEditor.
private void setActiveEditor(String relativePath) {
VirtualFile file = myFile.getVirtualFile().findFileByRelativePath(relativePath);
FileEditor[] editors = FileEditorManager.getInstance(myProject).getEditors(file);
assertEquals(1, editors.length);
FileEditor fileEditor = editors[0];
assertTrue(fileEditor instanceof TextEditor);
setActiveEditor(((TextEditor) fileEditor).getEditor());
}
use of com.intellij.openapi.fileEditor.TextEditor in project intellij-plugins by JetBrains.
the class MarkdownActionUtil method findMarkdownTextEditor.
@Nullable
public static Editor findMarkdownTextEditor(AnActionEvent e) {
final SplitFileEditor splitEditor = findSplitEditor(e);
if (splitEditor == null) {
// This fallback is used primarily for testing
final PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
if (psiFile != null && psiFile.getLanguage() == MarkdownLanguage.INSTANCE && ApplicationManager.getApplication().isUnitTestMode()) {
return e.getData(CommonDataKeys.EDITOR);
} else {
return null;
}
}
if (!(splitEditor.getMainEditor() instanceof TextEditor)) {
return null;
}
final TextEditor mainEditor = (TextEditor) splitEditor.getMainEditor();
if (!mainEditor.getComponent().isVisible()) {
return null;
}
return mainEditor.getEditor();
}
Aggregations