use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class CodeStyleAbstractPanel method collectChangesBeforeCurrentSettingsAppliance.
/**
* Reformats {@link #myTextToReformat target text} with the {@link #mySettings current code style settings} and returns
* list of changes applied to the target text during that.
*
* @param project project to use
* @return list of changes applied to the {@link #myTextToReformat target text} during reformatting. It is sorted
* by change start offset in ascending order
*/
@Nullable
private Document collectChangesBeforeCurrentSettingsAppliance(Project project) {
PsiFile psiFile = createFileFromText(project, myTextToReformat);
prepareForReformat(psiFile);
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
if (documentManager != null) {
Document document = documentManager.getDocument(psiFile);
if (document != null) {
CodeStyleSettings clone = mySettings.clone();
clone.setRightMargin(getDefaultLanguage(), getAdjustedRightMargin());
CodeStyleSettingsManager.getInstance(project).setTemporarySettings(clone);
try {
CodeStyleManager.getInstance(project).reformat(psiFile);
} finally {
CodeStyleSettingsManager.getInstance(project).dropTemporarySettings();
}
return document;
}
}
return null;
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class FileContentUtil method setFileText.
/**
* @deprecated to be removed after IDEA 15. Use {@link VfsUtil#saveText(VirtualFile, String)} instead.
*/
public static void setFileText(@Nullable Project project, final VirtualFile virtualFile, final String text) throws IOException {
if (project == null) {
project = ProjectUtil.guessProjectForFile(virtualFile);
}
if (project != null) {
final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
final Document document = psiFile == null ? null : psiDocumentManager.getDocument(psiFile);
if (document != null) {
document.setText(text != null ? text : "");
psiDocumentManager.commitDocument(document);
FileDocumentManager.getInstance().saveDocument(document);
return;
}
}
VfsUtil.saveText(virtualFile, text != null ? text : "");
virtualFile.refresh(false, false);
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class BaseMoveHandler method executeWriteAction.
@Override
public void executeWriteAction(Editor editor, Caret caret, DataContext dataContext) {
final Project project = editor.getProject();
assert project != null;
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document document = editor.getDocument();
documentManager.commitDocument(document);
PsiFile file = getRoot(documentManager.getPsiFile(document), editor);
if (file != null) {
final MoverWrapper mover = getSuitableMover(editor, file);
if (mover != null && mover.getInfo().toMove2 != null) {
LineRange range = mover.getInfo().toMove;
if ((range.startLine > 0 || isDown) && (range.endLine < document.getLineCount() || !isDown)) {
mover.move(editor, file);
}
}
}
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class ImageOrColorPreviewManager method getPsiElementsAt.
@NotNull
private static Collection<PsiElement> getPsiElementsAt(Point point, Editor editor) {
if (editor.isDisposed()) {
return Collections.emptySet();
}
Project project = editor.getProject();
if (project == null || project.isDisposed()) {
return Collections.emptySet();
}
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document document = editor.getDocument();
PsiFile psiFile = documentManager.getPsiFile(document);
if (psiFile == null || psiFile instanceof PsiCompiledElement || !psiFile.isValid()) {
return Collections.emptySet();
}
final Set<PsiElement> elements = Collections.newSetFromMap(new WeakHashMap<PsiElement, Boolean>());
final int offset = editor.logicalPositionToOffset(editor.xyToLogicalPosition(point));
if (documentManager.isCommitted(document)) {
ContainerUtil.addIfNotNull(elements, InjectedLanguageUtil.findElementAtNoCommit(psiFile, offset));
}
for (PsiFile file : psiFile.getViewProvider().getAllFiles()) {
ContainerUtil.addIfNotNull(elements, file.findElementAt(offset));
}
return elements;
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class FormatterImpl method validateModel.
private static void validateModel(FormattingModel model) throws FormattingModelInconsistencyException {
FormattingDocumentModel documentModel = model.getDocumentModel();
Document document = documentModel.getDocument();
Block rootBlock = model.getRootBlock();
if (rootBlock instanceof ASTBlock) {
PsiElement rootElement = ((ASTBlock) rootBlock).getNode().getPsi();
if (!rootElement.isValid()) {
throw new FormattingModelInconsistencyException("Invalid root block PSI element");
}
PsiFile file = rootElement.getContainingFile();
Project project = file.getProject();
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
if (documentManager.isUncommited(document)) {
throw new FormattingModelInconsistencyException("Uncommitted document");
}
if (document.getTextLength() != file.getTextLength()) {
throw new FormattingModelInconsistencyException("Document length " + document.getTextLength() + " doesn't match PSI file length " + file.getTextLength() + ", language: " + file.getLanguage());
}
}
}
Aggregations