use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.
the class CodeFormatterFacade method wrapLongLinesIfNecessary.
/**
* Inspects all lines of the given document and wraps all of them that exceed {@link CodeStyleSettings#getRightMargin(Language)}
* right margin}.
* <p/>
* I.e. the algorithm is to do the following for every line:
* <p/>
* <pre>
* <ol>
* <li>
* Check if the line exceeds {@link CodeStyleSettings#getRightMargin(Language)} right margin}. Go to the next line in the case of
* negative answer;
* </li>
* <li>Determine line wrap position; </li>
* <li>
* Perform 'smart wrap', i.e. not only wrap the line but insert additional characters over than line feed if necessary.
* For example consider that we wrap a single-line comment - we need to insert comment symbols on a start of the wrapped
* part as well. Generally, we get the same behavior as during pressing 'Enter' at wrap position during editing document;
* </li>
* </ol>
</pre>
*
* @param file file that holds parsed document tree
* @param document target document
* @param startOffset start offset of the first line to check for wrapping (inclusive)
* @param endOffset end offset of the first line to check for wrapping (exclusive)
*/
private void wrapLongLinesIfNecessary(@NotNull final PsiFile file, @Nullable final Document document, final int startOffset, final int endOffset) {
if (!mySettings.getCommonSettings(file.getLanguage()).WRAP_LONG_LINES || PostprocessReformattingAspect.getInstance(file.getProject()).isViewProviderLocked(file.getViewProvider()) || document == null) {
return;
}
FormatterTagHandler formatterTagHandler = new FormatterTagHandler(CodeStyleSettingsManager.getSettings(file.getProject()));
List<TextRange> enabledRanges = formatterTagHandler.getEnabledRanges(file.getNode(), new TextRange(startOffset, endOffset));
final VirtualFile vFile = FileDocumentManager.getInstance().getFile(document);
if ((vFile == null || vFile instanceof LightVirtualFile) && !ApplicationManager.getApplication().isUnitTestMode()) {
// a formatter affect only PSI and it is out of sync with a document text
return;
}
Editor editor = PsiUtilBase.findEditor(file);
EditorFactory editorFactory = null;
if (editor == null) {
if (!ApplicationManager.getApplication().isDispatchThread()) {
return;
}
editorFactory = EditorFactory.getInstance();
editor = editorFactory.createEditor(document, file.getProject(), file.getVirtualFile(), false);
}
try {
final Editor editorToUse = editor;
ApplicationManager.getApplication().runWriteAction(() -> {
final CaretModel caretModel = editorToUse.getCaretModel();
final int caretOffset = caretModel.getOffset();
final RangeMarker caretMarker = editorToUse.getDocument().createRangeMarker(caretOffset, caretOffset);
doWrapLongLinesIfNecessary(editorToUse, file.getProject(), editorToUse.getDocument(), startOffset, endOffset, enabledRanges);
if (caretMarker.isValid() && caretModel.getOffset() != caretMarker.getStartOffset()) {
caretModel.moveToOffset(caretMarker.getStartOffset());
}
});
} finally {
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(file.getProject());
if (documentManager.isUncommited(document))
documentManager.commitDocument(document);
if (editorFactory != null) {
editorFactory.releaseEditor(editor);
}
}
}
use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.
the class PsiDocumentManagerImplTest method testDocumentFromAlienProjectDoesNotEndUpInMyUncommittedList.
public void testDocumentFromAlienProjectDoesNotEndUpInMyUncommittedList() throws Exception {
PsiFile file = findFile(createFile());
final Document document = getDocument(file);
File temp = createTempDirectory();
final Project alienProject = createProject(temp + "/alien.ipr", DebugUtil.currentStackTrace());
boolean succ2 = ProjectManagerEx.getInstanceEx().openProject(alienProject);
assertTrue(succ2);
// startup activities
UIUtil.dispatchAllInvocationEvents();
try {
PsiManager alienManager = PsiManager.getInstance(alienProject);
final String alienText = "alien";
LightVirtualFile alienVirt = new LightVirtualFile("foo.txt", alienText);
final PsiFile alienFile = alienManager.findFile(alienVirt);
final PsiDocumentManagerImpl alienDocManager = (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(alienProject);
final Document alienDocument = alienDocManager.getDocument(alienFile);
assertEquals(0, alienDocManager.getUncommittedDocuments().length);
assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
WriteCommandAction.runWriteCommandAction(null, () -> {
changeDocument(alienDocument, getPsiDocumentManager());
assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
assertEquals(0, alienDocManager.getUncommittedDocuments().length);
changeDocument(alienDocument, alienDocManager);
assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
assertEquals(1, alienDocManager.getUncommittedDocuments().length);
changeDocument(document, getPsiDocumentManager());
assertEquals(1, getPsiDocumentManager().getUncommittedDocuments().length);
assertEquals(1, alienDocManager.getUncommittedDocuments().length);
changeDocument(document, alienDocManager);
assertEquals(1, getPsiDocumentManager().getUncommittedDocuments().length);
assertEquals(1, alienDocManager.getUncommittedDocuments().length);
});
} finally {
ProjectUtil.closeAndDispose(alienProject);
}
}
use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.
the class PsiDocumentManagerImplTest method testDocumentFromAlienProjectGetsCommittedInBackground.
public void testDocumentFromAlienProjectGetsCommittedInBackground() throws Exception {
LightVirtualFile virtualFile = createFile();
PsiFile file = findFile(virtualFile);
final Document document = getDocument(file);
File temp = createTempDirectory();
final Project alienProject = createProject(temp + "/alien.ipr", DebugUtil.currentStackTrace());
boolean succ2 = ProjectManagerEx.getInstanceEx().openProject(alienProject);
assertTrue(succ2);
// startup activities
UIUtil.dispatchAllInvocationEvents();
try {
PsiManager alienManager = PsiManager.getInstance(alienProject);
final PsiFile alienFile = alienManager.findFile(virtualFile);
assertNotNull(alienFile);
final PsiDocumentManagerImpl alienDocManager = (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(alienProject);
final Document alienDocument = alienDocManager.getDocument(alienFile);
assertSame(document, alienDocument);
assertEmpty(alienDocManager.getUncommittedDocuments());
assertEmpty(getPsiDocumentManager().getUncommittedDocuments());
WriteCommandAction.runWriteCommandAction(null, () -> {
document.setText("xxx");
assertOrderedEquals(getPsiDocumentManager().getUncommittedDocuments(), document);
assertOrderedEquals(alienDocManager.getUncommittedDocuments(), alienDocument);
});
assertEquals("xxx", document.getText());
assertEquals("xxx", alienDocument.getText());
waitForCommit(document, TIMEOUT);
assertTrue("Still not committed: " + document, getPsiDocumentManager().isCommitted(document));
long t2 = System.currentTimeMillis() + TIMEOUT;
while (!alienDocManager.isCommitted(alienDocument) && System.currentTimeMillis() < t2) {
UIUtil.dispatchAllInvocationEvents();
}
assertTrue("Still not committed: " + alienDocument, alienDocManager.isCommitted(alienDocument));
} finally {
ProjectUtil.closeAndDispose(alienProject);
}
}
use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.
the class GrAnnotationNameValuePairImpl method getDetachedValue.
@Override
@Nullable
public PsiAnnotationMemberValue getDetachedValue() {
GrNameValuePairStub stub = getStub();
if (stub != null) {
String text = stub.getValue();
PsiAnnotationMemberValue result = SoftReference.dereference(myDetachedValue);
if (result == null) {
GrAnnotation annotation = GroovyPsiElementFactory.getInstance(getProject()).createAnnotationFromText("@F(" + text + ")", this);
((LightVirtualFile) annotation.getContainingFile().getViewProvider().getVirtualFile()).setWritable(false);
PsiAnnotationMemberValue value = annotation.findAttributeValue(null);
myDetachedValue = new SoftReference<>(result = value);
}
return result;
}
return getValue();
}
use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.
the class CellOperationTest method testAddCell.
public void testAddCell() throws IOException {
final String fileName = "testData/emptyFile.ipynb";
final String fileText = IpnbTestCase.getFileText(fileName);
final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
ipnbFile.addCell(IpnbCodeCell.createEmptyCodeCell(), ipnbFile.getCells().size());
final IpnbCodeCell cell = (IpnbCodeCell) ipnbFile.getCells().get(ipnbFile.getCells().size() - 1);
assertTrue(cell.getCellOutputs().isEmpty());
assertNull(cell.getPromptNumber());
assertTrue(cell.getMetadata().isEmpty());
}
Aggregations