use of com.intellij.openapi.util.TextRange 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.openapi.util.TextRange in project kotlin by JetBrains.
the class JavaContext method getUastLocation.
@NonNull
public Location getUastLocation(@Nullable UElement node) {
if (node == null) {
return Location.NONE;
}
if (node instanceof UElementWithLocation) {
UFile file = UastUtils.getContainingFile(node);
if (file == null) {
return Location.NONE;
}
File ioFile = UastUtils.getIoFile(file);
if (ioFile == null) {
return Location.NONE;
}
UElementWithLocation segment = (UElementWithLocation) node;
return Location.create(ioFile, file.getPsi().getText(), segment.getStartOffset(), segment.getEndOffset());
} else {
PsiElement psiElement = node.getPsi();
if (psiElement != null) {
TextRange range = psiElement.getTextRange();
UFile containingFile = getUFile();
if (containingFile == null) {
return Location.NONE;
}
File file = this.file;
if (!containingFile.equals(getUFile())) {
// Reporting an error in a different file.
if (getDriver().getScope().size() == 1) {
// Don't bother with this error if it's in a different file during single-file analysis
return Location.NONE;
}
VirtualFile virtualFile = containingFile.getPsi().getVirtualFile();
if (virtualFile == null) {
return Location.NONE;
}
file = VfsUtilCore.virtualToIoFile(virtualFile);
}
return Location.create(file, getContents(), range.getStartOffset(), range.getEndOffset());
}
}
return Location.NONE;
}
use of com.intellij.openapi.util.TextRange in project kotlin by JetBrains.
the class JavaParser method getLocation.
/**
* Returns a {@link Location} for the given element
*
* @param context information about the file being parsed
* @param element the element to create a location for
* @return a location for the given node
*/
// subclasses may want to override/optimize
@SuppressWarnings("MethodMayBeStatic")
@NonNull
public Location getLocation(@NonNull JavaContext context, @NonNull PsiElement element) {
TextRange range = element.getTextRange();
UFile uFile = (UFile) getUastContext().convertElementWithParent(element.getContainingFile(), UFile.class);
if (uFile == null) {
return Location.NONE;
}
PsiFile containingFile = uFile.getPsi();
File file = context.file;
if (containingFile != context.getUFile().getPsi()) {
// Reporting an error in a different file.
if (context.getDriver().getScope().size() == 1) {
// Don't bother with this error if it's in a different file during single-file analysis
return Location.NONE;
}
VirtualFile virtualFile = containingFile.getVirtualFile();
if (virtualFile == null) {
return Location.NONE;
}
file = VfsUtilCore.virtualToIoFile(virtualFile);
}
return Location.create(file, context.getContents(), range.getStartOffset(), range.getEndOffset());
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class GenericDomValueReference method createTextRange.
protected TextRange createTextRange() {
if (myGenericValue instanceof GenericAttributeValue) {
final GenericAttributeValue genericAttributeValue = (GenericAttributeValue) myGenericValue;
final XmlAttributeValue attributeValue = genericAttributeValue.getXmlAttributeValue();
if (attributeValue == null) {
return TextRange.from(0, genericAttributeValue.getXmlAttribute().getTextLength());
}
final int length = attributeValue.getTextLength();
return length < 2 ? TextRange.from(0, length) : new TextRange(1, length - 1);
}
final XmlTag tag = myGenericValue.getXmlTag();
assert tag != null;
return XmlTagUtil.getTrimmedValueRange(tag);
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class DomElementAnnotationHolderImpl method createAnnotation.
@Override
@NotNull
public Annotation createAnnotation(@NotNull DomElement element, HighlightSeverity severity, @Nullable String message) {
final XmlElement xmlElement = element.getXmlElement();
LOG.assertTrue(xmlElement != null, "No XML element for " + element);
final TextRange range = xmlElement.getTextRange();
final int startOffset = range.getStartOffset();
final int endOffset = message == null ? startOffset : range.getEndOffset();
final Annotation annotation = new Annotation(startOffset, endOffset, severity, message, null);
myAnnotations.add(annotation);
return annotation;
}
Aggregations