use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class PsiEventsTest method testEditingInDocComment.
public void testEditingInDocComment() throws Exception {
final Ref<Boolean> gotIt = new Ref<>(false);
getPsiManager().addPsiTreeChangeListener(new PsiTreeChangeAdapter() {
@Override
public void childReplaced(@NotNull PsiTreeChangeEvent event) {
gotIt.set(true);
}
});
GroovyFile file = GroovyPsiElementFactory.getInstance(myProject).createGroovyFile("/** This is doc comment*/class C{}", true, null);
final PsiDocumentManager docManager = PsiDocumentManager.getInstance(myProject);
final Document doc = docManager.getDocument(file);
assertNotNull(doc);
CommandProcessor.getInstance().executeCommand(myProject, () -> ApplicationManager.getApplication().runWriteAction(() -> {
doc.insertString(3, " ");
docManager.commitDocument(doc);
}), "file text set", this);
assertTrue(gotIt.get());
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class XPathAction method isEnabled.
protected boolean isEnabled(AnActionEvent event, boolean checkAvailable) {
final Project project = event.getProject();
if (project == null) {
// no active project
return false;
}
Editor editor = CommonDataKeys.EDITOR.getData(event.getDataContext());
if (editor == null) {
FileEditorManager fem = FileEditorManager.getInstance(project);
editor = fem.getSelectedTextEditor();
}
if (editor == null) {
return false;
}
// do we have an xml file?
final PsiDocumentManager cem = PsiDocumentManager.getInstance(project);
final PsiFile psiFile = cem.getPsiFile(editor.getDocument());
// this is also true for DTD documents...
if (!(psiFile instanceof XmlFile) || psiFile.getLanguage() == StdFileTypes.DTD.getLanguage()) {
return false;
}
return !checkAvailable || isEnabledAt((XmlFile) psiFile, editor.getCaretModel().getOffset());
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class CreateTestAction method invoke.
@Override
public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
final PyFunction srcFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class);
final PyClass srcClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
if (srcClass == null && srcFunction == null)
return;
final PsiDirectory dir = element.getContainingFile().getContainingDirectory();
final CreateTestDialog d = new CreateTestDialog(project);
if (srcClass != null) {
d.setClassName("Test" + StringUtil.capitalize(srcClass.getName()));
d.setFileName("test_" + StringUtil.decapitalize(srcClass.getName()) + ".py");
if (dir != null)
d.setTargetDir(dir.getVirtualFile().getPath());
if (srcFunction != null) {
d.methodsSize(1);
d.addMethod("test_" + srcFunction.getName(), 0);
} else {
final List<PyFunction> methods = Lists.newArrayList();
srcClass.visitMethods(pyFunction -> {
if (pyFunction.getName() != null && !pyFunction.getName().startsWith("__"))
methods.add(pyFunction);
return true;
}, false, null);
d.methodsSize(methods.size());
int i = 0;
for (PyFunction f : methods) {
d.addMethod("test_" + f.getName(), i);
++i;
}
}
} else {
d.setClassName("Test" + StringUtil.capitalize(srcFunction.getName()));
d.setFileName("test_" + StringUtil.decapitalize(srcFunction.getName()) + ".py");
if (dir != null)
d.setTargetDir(dir.getVirtualFile().getPath());
d.methodsSize(1);
d.addMethod("test_" + srcFunction.getName(), 0);
}
if (!d.showAndGet()) {
return;
}
CommandProcessor.getInstance().executeCommand(project, () -> {
PsiFile e = PyTestCreator.generateTestAndNavigate(project, d);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitAllDocuments();
}, CodeInsightBundle.message("intention.create.test"), this);
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class PyConvertTypeCommentToVariableAnnotationIntention method runWithDocumentReleasedAndCommitted.
public static void runWithDocumentReleasedAndCommitted(@NotNull Project project, @NotNull Document document, @NotNull Runnable runnable) {
final PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
manager.doPostponedOperationsAndUnblockDocument(document);
try {
runnable.run();
} finally {
manager.commitDocument(document);
}
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class I18nUtil method createProperty.
public static void createProperty(final Project project, final Collection<PropertiesFile> propertiesFiles, final String key, final String value) throws IncorrectOperationException {
for (PropertiesFile file : propertiesFiles) {
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(documentManager.getDocument(file.getContainingFile()));
IProperty existingProperty = file.findPropertyByKey(key);
if (existingProperty == null) {
file.addProperty(key, value);
}
}
}
Aggregations