use of com.intellij.psi.PsiDocumentManager in project Perl5-IDEA by Camelcade.
the class PerlCodeGeneratorImpl method insertCodeAfterElement.
protected void insertCodeAfterElement(PsiElement anchor, String code, Editor editor) {
ApplicationManager.getApplication().runWriteAction(() -> {
FileType fileType = anchor.getContainingFile().getFileType();
final PsiDocumentManager manager = PsiDocumentManager.getInstance(anchor.getProject());
final Document document = manager.getDocument(anchor.getContainingFile());
if (code.length() > 0 && document != null) {
manager.doPostponedOperationsAndUnblockDocument(document);
PsiFile newFile = PerlElementFactory.createFile(anchor.getProject(), "\n" + code, fileType);
PsiElement container = anchor.getParent();
int newOffset = anchor.getTextOffset() + anchor.getTextLength();
if (newFile.getFirstChild() != null && newFile.getLastChild() != null) {
container.addRangeAfter(newFile.getFirstChild(), newFile.getLastChild(), anchor);
}
manager.commitDocument(document);
editor.getCaretModel().moveToOffset(newOffset);
editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
}
});
}
use of com.intellij.psi.PsiDocumentManager in project Perl5-IDEA by Camelcade.
the class StringToLastHeredocIntention method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
PsiElement stringElement = element.getParent();
assert stringElement instanceof PerlString;
char quoteSymbol = '"';
if (stringElement instanceof PsiPerlStringSq) {
quoteSymbol = '\'';
} else if (stringElement instanceof PsiPerlStringXq) {
quoteSymbol = '`';
}
String contentText = ElementManipulators.getValueText(stringElement);
List<PsiElement> heredocElements = PerlElementFactory.createHereDocElements(project, quoteSymbol, HEREDOC_MARKER, "");
PsiFile currentFile = stringElement.getContainingFile();
int newLineIndex = StringUtil.indexOf(currentFile.getNode().getChars(), "\n", stringElement.getTextRange().getEndOffset());
if (newLineIndex > 1) {
PsiElement anchor = currentFile.findElementAt(newLineIndex);
// fixme we should check here if \n in unbreakable entity - regex, qw, string, something else...
if (anchor != null && (anchor.getParent() instanceof PerlString || anchor.getParent() instanceof PsiPerlStringList)) {
return;
}
}
final PsiDocumentManager manager = PsiDocumentManager.getInstance(element.getProject());
final Document document = manager.getDocument(currentFile);
if (document == null) {
return;
}
newLineIndex -= stringElement.getTextLength();
PsiElement heredocOpener = heredocElements.get(0);
newLineIndex += heredocOpener.getTextLength();
// replace string with heredoc opener
stringElement.replace(heredocOpener);
manager.doPostponedOperationsAndUnblockDocument(document);
String heredocString = contentText + "\n" + HEREDOC_MARKER + "\n";
manager.doPostponedOperationsAndUnblockDocument(document);
document.insertString(newLineIndex + 1, heredocString);
manager.commitDocument(document);
}
use of com.intellij.psi.PsiDocumentManager in project Perl5-IDEA by Camelcade.
the class TemplateToolkitStringManipulator method handleContentChange.
@SuppressWarnings("Duplicates")
@Override
public TemplateToolkitString handleContentChange(@NotNull TemplateToolkitString element, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
final PsiDocumentManager manager = PsiDocumentManager.getInstance(element.getProject());
final Document document = manager.getDocument(element.getContainingFile());
if (document != null) {
TextRange elementRange = element.getTextRange();
manager.doPostponedOperationsAndUnblockDocument(document);
document.replaceString(elementRange.getStartOffset() + range.getStartOffset(), elementRange.getStartOffset() + range.getEndOffset(), newContent);
manager.commitDocument(document);
}
return element;
}
use of com.intellij.psi.PsiDocumentManager in project idea-handlebars by dmarcotte.
the class HbFormatterTest method doTextTest.
/**
* This method runs both a full-file reformat on beforeText, and a line-by-line reformat. Though the tests
* would output slightly better errors if these were separate tests, enforcing that they are always both run
* for any test defined is the easiest way to ensure that the line-by-line is not messed up by formatter changes
*
* @param beforeText The text run the formatter on
* @param textAfter The expected result after running the formatter
* @param templateDataLanguageType The templated language of the file
* @throws IncorrectOperationException
*/
void doTextTest(final String beforeText, String textAfter, LanguageFileType templateDataLanguageType) throws IncorrectOperationException {
// define action to run "Reformat Code" on the whole "file" defined by beforeText
FormatRunnableFactory fullFormatRunnableFactory = new FormatRunnableFactory() {
@Override
Runnable createFormatRunnable(final PsiFile file) {
return new Runnable() {
@Override
public void run() {
try {
TextRange rangeToUse = file.getTextRange();
CodeStyleManager styleManager = CodeStyleManager.getInstance(getProject());
styleManager.reformatText(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
} catch (IncorrectOperationException e) {
assertTrue(e.getLocalizedMessage(), false);
}
}
};
}
};
// define action to run "Adjust line indent" on every line in the "file" defined by beforeText
FormatRunnableFactory lineFormatRunnableFactory = new FormatRunnableFactory() {
@Override
Runnable createFormatRunnable(final PsiFile file) {
return new Runnable() {
@Override
public void run() {
try {
final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
final Document document = manager.getDocument(file);
assert document != null;
for (int lineNum = 0; lineNum < document.getLineCount(); lineNum++) {
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
int offset = document.getLineStartOffset(lineNum);
// if this breaks at some point, we should
@SuppressWarnings("deprecation") boolean // instead of doing the indent directly
lineToBeIndented = codeStyleManager.isLineToBeIndented(file, offset);
if (lineToBeIndented) {
codeStyleManager.adjustLineIndent(file, offset);
}
}
} catch (IncorrectOperationException e) {
assertTrue(e.getLocalizedMessage(), false);
}
}
};
}
};
doFormatterActionTest(fullFormatRunnableFactory, beforeText, textAfter, templateDataLanguageType);
doFormatterActionTest(lineFormatRunnableFactory, beforeText, textAfter, templateDataLanguageType);
}
use of com.intellij.psi.PsiDocumentManager in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoRecursiveCallMarkerProvider method collectSlowLineMarkers.
@Override
public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) {
Set<Integer> lines = ContainerUtil.newHashSet();
for (PsiElement element : elements) {
if (element instanceof GoCallExpr) {
PsiElement resolve = GoPsiImplUtil.resolveCall((GoCallExpr) element);
if (resolve instanceof GoFunctionOrMethodDeclaration) {
if (isRecursiveCall(element, (GoFunctionOrMethodDeclaration) resolve)) {
PsiDocumentManager instance = PsiDocumentManager.getInstance(element.getProject());
Document document = instance.getDocument(element.getContainingFile());
int textOffset = element.getTextOffset();
if (document == null)
continue;
int lineNumber = document.getLineNumber(textOffset);
if (!lines.contains(lineNumber)) {
result.add(new RecursiveMethodCallMarkerInfo(element));
}
lines.add(lineNumber);
}
}
}
}
}
Aggregations