use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class I18nizeAction method getHandler.
@Nullable
public static I18nQuickFixHandler getHandler(final AnActionEvent e) {
final Editor editor = getEditor(e);
if (editor == null)
return null;
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
if (psiFile == null)
return null;
TextRange range = JavaI18nUtil.getSelectedRange(editor, psiFile);
if (range == null)
return null;
final PsiLiteralExpression literalExpression = getEnclosingStringLiteral(psiFile, editor);
PsiElement element = psiFile.findElementAt(editor.getCaretModel().getOffset());
if (element == null)
return null;
if (I18nizeConcatenationQuickFix.getEnclosingLiteralConcatenation(element) != null) {
return new I18nizeConcatenationQuickFix();
} else if (literalExpression != null && literalExpression.getTextRange().contains(range)) {
return new I18nizeQuickFix();
}
for (I18nizeHandlerProvider handlerProvider : I18nizeHandlerProvider.EP_NAME.getExtensions()) {
I18nQuickFixHandler handler = handlerProvider.getHandler(psiFile, editor, range);
if (handler != null) {
return handler;
}
}
return null;
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class PyParameterInfoHandler method updateParameterInfo.
/**
<b>Note: instead of parameter index, we directly store parameter's offset for later use.</b><br/>
We cannot store an index since we cannot determine what is an argument until we actually map arguments to parameters.
This is because a tuple in arguments may be a whole argument or map to a tuple parameter.
*/
@Override
public void updateParameterInfo(@NotNull PyArgumentList argumentList, @NotNull UpdateParameterInfoContext context) {
if (context.getParameterOwner() != argumentList) {
context.removeHint();
return;
}
// align offset to nearest expression; context may point to a space, etc.
final List<PyExpression> flattenedArguments = PyUtil.flattenedParensAndLists(argumentList.getArguments());
// this is already shifted backwards to skip spaces
final int allegedCursorOffset = context.getOffset();
if (!argumentList.getTextRange().contains(allegedCursorOffset) && argumentList.getText().endsWith(")")) {
context.removeHint();
return;
}
final PsiFile file = context.getFile();
final CharSequence chars = file.getViewProvider().getContents();
int offset = -1;
for (PyExpression argument : flattenedArguments) {
final TextRange range = argument.getTextRange();
// widen the range to include all whitespace around the argument
final int left = CharArrayUtil.shiftBackward(chars, range.getStartOffset() - 1, " \t\r\n");
int right = CharArrayUtil.shiftForwardCarefully(chars, range.getEndOffset(), " \t\r\n");
if (argument.getParent() instanceof PyListLiteralExpression || argument.getParent() instanceof PyTupleExpression) {
right = CharArrayUtil.shiftForward(chars, range.getEndOffset(), " \t\r\n])");
}
if (left <= allegedCursorOffset && right >= allegedCursorOffset) {
offset = range.getStartOffset();
break;
}
}
context.setCurrentParameter(offset);
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class PyReplaceExpressionUtil method getPositionInRanges.
private static int getPositionInRanges(@NotNull List<TextRange> ranges, @NotNull TextRange range) {
final int end = range.getEndOffset();
final int size = ranges.size();
for (int i = 0; i < size; i++) {
final TextRange r = ranges.get(i);
if (end < r.getStartOffset()) {
return i;
}
}
return size;
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class DomUtil method getOriginalElement.
@NotNull
public static <T extends DomElement> T getOriginalElement(@NotNull final T domElement) {
final XmlElement psiElement = domElement.getXmlElement();
if (psiElement == null)
return domElement;
final PsiFile psiFile = psiElement.getContainingFile().getOriginalFile();
final TextRange range = psiElement.getTextRange();
final PsiElement element = psiFile.findElementAt(range.getStartOffset());
final int maxLength = range.getLength();
final boolean isAttribute = psiElement instanceof XmlAttribute;
final Class<? extends XmlElement> clazz = isAttribute ? XmlAttribute.class : XmlTag.class;
final DomManager domManager = domElement.getManager();
DomElement current = null;
for (XmlElement next = PsiTreeUtil.getParentOfType(element, clazz, false); next != null && next.getTextLength() <= maxLength; next = PsiTreeUtil.getParentOfType(next, clazz, true)) {
current = isAttribute ? domManager.getDomElement((XmlAttribute) next) : domManager.getDomElement((XmlTag) next);
if (current != null && domElement.getClass() != current.getClass())
current = null;
}
return (T) current;
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class DomTarget method getTarget.
@Nullable
public static DomTarget getTarget(DomElement element, GenericDomValue nameElement) {
if (nameElement instanceof GenericAttributeValue) {
final GenericAttributeValue genericAttributeValue = (GenericAttributeValue) nameElement;
final XmlAttributeValue attributeValue = genericAttributeValue.getXmlAttributeValue();
if (attributeValue == null) {
return null;
}
final int length = attributeValue.getTextLength();
if (length >= 2) {
return new DomTarget(element, attributeValue, new TextRange(1, length - 1), nameElement);
}
}
final XmlTag tag = nameElement.getXmlTag();
if (tag == null) {
return null;
}
XmlTagValue tagValue = tag.getValue();
if (StringUtil.isEmpty(tagValue.getTrimmedText())) {
return null;
}
return new DomTarget(element, tag, XmlTagUtil.getTrimmedValueRange(tag), nameElement);
}
Aggregations