use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class XmlNsPrefixAnnotator method annotate.
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (PsiUtilCore.getElementType(element) != XmlTokenType.XML_NAME)
return;
PsiElement parent = element.getParent();
if (!(parent instanceof XmlTag) && !(parent instanceof XmlAttribute))
return;
TextRange elementRange = element.getTextRange();
List<SchemaPrefixReference> references = ContainerUtil.findAll(parent.getReferences(), SchemaPrefixReference.class);
for (SchemaPrefixReference ref : references) {
TextRange rangeInElement = ref.getRangeInElement();
if (rangeInElement.isEmpty())
continue;
TextRange range = rangeInElement.shiftRight(ref.getElement().getTextRange().getStartOffset());
if (!range.intersects(elementRange))
continue;
holder.createInfoAnnotation(range, null).setTextAttributes(XmlHighlighterColors.XML_NS_PREFIX);
}
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class XmlUnboundNsPrefixInspection method checkUnboundNamespacePrefix.
private static void checkUnboundNamespacePrefix(final XmlElement element, final XmlTag context, String namespacePrefix, final XmlToken token, final ProblemsHolder holder, boolean isOnTheFly) {
if (namespacePrefix.isEmpty() && (!(element instanceof XmlTag) || !(element.getParent() instanceof XmlDocument)) || XML.equals(namespacePrefix)) {
return;
}
final String namespaceByPrefix = context.getNamespaceByPrefix(namespacePrefix);
if (!namespaceByPrefix.isEmpty()) {
return;
}
PsiFile psiFile = context.getContainingFile();
if (!(psiFile instanceof XmlFile))
return;
final XmlFile containingFile = (XmlFile) psiFile;
if (!HighlightingLevelManager.getInstance(containingFile.getProject()).shouldInspect(containingFile))
return;
final XmlExtension extension = XmlExtension.getExtension(containingFile);
if (extension.getPrefixDeclaration(context, namespacePrefix) != null) {
return;
}
final String localizedMessage = isOnTheFly ? XmlErrorMessages.message("unbound.namespace", namespacePrefix) : XmlErrorMessages.message("unbound.namespace.no.param");
if (namespacePrefix.isEmpty()) {
final XmlTag tag = (XmlTag) element;
if (!XmlUtil.JSP_URI.equals(tag.getNamespace())) {
LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(context, namespacePrefix, token) : null;
reportTagProblem(tag, localizedMessage, null, ProblemHighlightType.INFORMATION, fix, holder);
}
return;
}
final int prefixLength = namespacePrefix.length();
final TextRange range = new TextRange(0, prefixLength);
final HighlightInfoType infoType = extension.getHighlightInfoType(containingFile);
final ProblemHighlightType highlightType = infoType == HighlightInfoType.ERROR ? ProblemHighlightType.ERROR : ProblemHighlightType.LIKE_UNKNOWN_SYMBOL;
if (element instanceof XmlTag) {
LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(context, namespacePrefix, token) : null;
reportTagProblem(element, localizedMessage, range, highlightType, fix, holder);
} else if (element instanceof XmlAttribute) {
LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(element, namespacePrefix, token) : null;
XmlAttribute attribute = (XmlAttribute) element;
holder.registerProblem(attribute.getNameElement(), localizedMessage, highlightType, range, fix);
} else {
holder.registerProblem(element, localizedMessage, highlightType, range);
}
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class XmlFormatterTestBase method checkFormattingDoesNotProduceException.
protected void checkFormattingDoesNotProduceException(String name) throws Exception {
String text = loadFile(name + ".xml", null);
final XmlFileImpl file = (XmlFileImpl) createFile(name + ".xml", text);
myTextRange = new TextRange(10000, 10001);
CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> performFormatting(file)), "", "");
myTextRange = new TextRange(1000000, 1000001);
CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> performFormatting(file)), "", "");
myTextRange = new TextRange(0, text.length());
CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> performFormatting(file)), "", "");
}
use of com.intellij.openapi.util.TextRange in project kotlin by JetBrains.
the class KtInvokeFunctionReference method getRanges.
@Override
public List<TextRange> getRanges() {
List<TextRange> list = new ArrayList<TextRange>();
KtValueArgumentList valueArgumentList = getExpression().getValueArgumentList();
if (valueArgumentList != null) {
if (valueArgumentList.getArguments().size() > 0) {
ASTNode valueArgumentListNode = valueArgumentList.getNode();
ASTNode lPar = valueArgumentListNode.findChildByType(KtTokens.LPAR);
if (lPar != null) {
list.add(getRange(lPar));
}
ASTNode rPar = valueArgumentListNode.findChildByType(KtTokens.RPAR);
if (rPar != null) {
list.add(getRange(rPar));
}
} else {
list.add(getRange(valueArgumentList.getNode()));
}
}
List<KtLambdaArgument> functionLiteralArguments = getExpression().getLambdaArguments();
for (KtLambdaArgument functionLiteralArgument : functionLiteralArguments) {
KtLambdaExpression functionLiteralExpression = functionLiteralArgument.getLambdaExpression();
list.add(getRange(functionLiteralExpression.getLeftCurlyBrace()));
ASTNode rightCurlyBrace = functionLiteralExpression.getRightCurlyBrace();
if (rightCurlyBrace != null) {
list.add(getRange(rightCurlyBrace));
}
}
return list;
}
use of com.intellij.openapi.util.TextRange in project kotlin by JetBrains.
the class KotlinNotSurrounder method surroundExpression.
@Nullable
@Override
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull KtExpression expression) {
KtPrefixExpression prefixExpr = (KtPrefixExpression) KtPsiFactoryKt.KtPsiFactory(expression).createExpression("!(a)");
KtParenthesizedExpression parenthesizedExpression = (KtParenthesizedExpression) prefixExpr.getBaseExpression();
assert parenthesizedExpression != null : "JetParenthesizedExpression should exists for " + prefixExpr.getText() + " expression";
KtExpression expressionWithoutParentheses = parenthesizedExpression.getExpression();
assert expressionWithoutParentheses != null : "JetExpression should exists for " + parenthesizedExpression.getText() + " expression";
expressionWithoutParentheses.replace(expression);
expression = (KtExpression) expression.replace(prefixExpr);
CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression);
int offset = expression.getTextRange().getEndOffset();
return new TextRange(offset, offset);
}
Aggregations