use of com.intellij.psi.PsiDocumentManager in project buck by facebook.
the class DependenciesOptimizer method optimzeDeps.
public static void optimzeDeps(@NotNull PsiFile file) {
final PropertyVisitor visitor = new PropertyVisitor();
file.accept(new BuckVisitor() {
@Override
public void visitElement(PsiElement node) {
node.acceptChildren(this);
node.accept(visitor);
}
});
// Commit modifications.
final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject());
manager.doPostponedOperationsAndUnblockDocument(manager.getDocument(file));
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class XsltBreakpointHandler method getActualLineNumber.
public static int getActualLineNumber(Project project, @Nullable XSourcePosition position) {
if (position == null) {
return -1;
}
final PsiElement element = findContextElement(project, position);
if (element == null) {
return -1;
}
if (element instanceof XmlToken) {
final IElementType tokenType = ((XmlToken) element).getTokenType();
if (tokenType == XmlTokenType.XML_START_TAG_START || tokenType == XmlTokenType.XML_NAME) {
final PsiManager psiManager = PsiManager.getInstance(project);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final PsiFile psiFile = psiManager.findFile(position.getFile());
if (psiFile == null) {
return -1;
}
final Document document = documentManager.getDocument(psiFile);
if (document == null) {
return -1;
}
if (document.getLineNumber(element.getTextRange().getStartOffset()) == position.getLine()) {
final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);
if (tag != null) {
final ASTNode node = tag.getNode();
assert node != null;
// TODO: re-check if/when Xalan is supported
final ASTNode end = XmlChildRole.START_TAG_END_FINDER.findChild(node);
if (end != null) {
return document.getLineNumber(end.getTextRange().getEndOffset()) + 1;
} else {
final ASTNode end2 = XmlChildRole.EMPTY_TAG_END_FINDER.findChild(node);
if (end2 != null) {
return document.getLineNumber(end2.getTextRange().getEndOffset()) + 1;
}
}
}
}
}
}
return -1;
}
use of com.intellij.psi.PsiDocumentManager in project kotlin by JetBrains.
the class AbstractFormatterTest method doTextTest.
public void doTextTest(final Action action, final String text, File fileAfter, String extension) throws IncorrectOperationException {
final PsiFile file = createFile("A" + extension, text);
if (myLineRange != null) {
DocumentImpl document = new DocumentImpl(text);
myTextRange = new TextRange(document.getLineStartOffset(myLineRange.getStartOffset()), document.getLineEndOffset(myLineRange.getEndOffset()));
}
final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
final Document document = manager.getDocument(file);
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
document.replaceString(0, document.getTextLength(), text);
manager.commitDocument(document);
try {
TextRange rangeToUse = myTextRange;
if (rangeToUse == null) {
rangeToUse = file.getTextRange();
}
ACTIONS.get(action).run(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
} catch (IncorrectOperationException e) {
assertTrue(e.getLocalizedMessage(), false);
}
}
});
}
}, "", "");
if (document == null) {
fail("Don't expect the document to be null");
return;
}
KotlinTestUtils.assertEqualsToFile(fileAfter, document.getText());
manager.commitDocument(document);
KotlinTestUtils.assertEqualsToFile(fileAfter, file.getText());
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class DomUIFactoryImpl method createDomHighlighter.
@Override
public BackgroundEditorHighlighter createDomHighlighter(final Project project, final PerspectiveFileEditor editor, final DomElement element) {
return new BackgroundEditorHighlighter() {
@Override
@NotNull
public HighlightingPass[] createPassesForEditor() {
if (!element.isValid())
return HighlightingPass.EMPTY_ARRAY;
final XmlFile psiFile = DomUtil.getFile(element);
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
final Document document = psiDocumentManager.getDocument(psiFile);
if (document == null)
return HighlightingPass.EMPTY_ARRAY;
editor.commit();
GeneralHighlightingPass ghp = new GeneralHighlightingPass(project, psiFile, document, 0, document.getTextLength(), true, new ProperTextRange(0, document.getTextLength()), null, new DefaultHighlightInfoProcessor());
LocalInspectionsPass lip = new LocalInspectionsPass(psiFile, document, 0, document.getTextLength(), LocalInspectionsPass.EMPTY_PRIORITY_RANGE, true, new DefaultHighlightInfoProcessor());
return new HighlightingPass[] { ghp, lip };
}
@Override
@NotNull
public HighlightingPass[] createPassesForVisibleArea() {
return createPassesForEditor();
}
};
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class DefaultGenerateElementProvider method generate.
@SuppressWarnings({ "unchecked" })
@Nullable
public T generate(@Nullable final DomElement parent, final Editor editor) {
if (parent == null) {
return null;
}
final List<? extends DomCollectionChildDescription> list = parent.getGenericInfo().getCollectionChildrenDescriptions();
for (DomCollectionChildDescription childDescription : list) {
if (ReflectionUtil.getRawType(childDescription.getType()).isAssignableFrom(myChildElementClass)) {
XmlTag parentTag = parent.getXmlTag();
if (editor != null && parentTag != null) {
int offset = editor.getCaretModel().getOffset();
PsiFile file = parentTag.getContainingFile();
PsiElement psiElement = file.findElementAt(offset);
if (psiElement instanceof PsiWhiteSpace && PsiTreeUtil.isAncestor(parentTag, psiElement, false)) {
Document document = editor.getDocument();
XmlTag childTag = parentTag.createChildTag(childDescription.getXmlElementName(), null, null, true);
String text = childTag.getText();
document.insertString(offset, text);
Project project = editor.getProject();
assert project != null;
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(document);
PsiElement element = file.findElementAt(offset + 1);
T domElement = DomUtil.findDomElement(element, myChildElementClass);
if (domElement != null)
return domElement;
document.deleteString(offset, offset + text.length());
documentManager.commitDocument(document);
}
}
int index = getCollectionIndex(parent, childDescription, editor);
return index < 0 ? (T) childDescription.addValue(parent, myChildElementClass) : (T) childDescription.addValue(parent, myChildElementClass, index);
}
}
return null;
}
Aggregations