/** * Returns a {@link Location} for the given node. This attempts to pick a shorter * location range than the entire node; for a class or method for example, it picks * the name node (if found). For statement constructs such as a {@code switch} statement * it will highlight the keyword, etc. * * @param node the AST node to create a location for * @return a location for the given node */ @NonNull public Location getNameLocation(@NonNull Node node) { return parser.getNameLocation(this, node); }
/** * Returns a {@link Location} for the given node. This attempts to pick a shorter * location range than the entire node; for a class or method for example, it picks * the name node (if found). For statement constructs such as a {@code switch} statement * it will highlight the keyword, etc. * * @param element the AST node to create a location for * @return a location for the given node */ @NonNull public Location getNameLocation(@NonNull PsiElement element) { if (element instanceof PsiSwitchStatement) { // Just use keyword return parser.getRangeLocation(this, element, 0, 6); // 6: "switch".length() } return parser.getNameLocation(this, element); }
@Override @NonNull public Location getNameLocation(@NonNull JavaContext context, @NonNull Node node) { // The range on method name identifiers is wrong in the ECJ nodes; just take start of // name + length of name if (node instanceof MethodDeclaration) { MethodDeclaration declaration = (MethodDeclaration) node; Identifier identifier = declaration.astMethodName(); Location location = getLocation(context, identifier); com.android.tools.lint.detector.api.Position start = location.getStart(); com.android.tools.lint.detector.api.Position end = location.getEnd(); int methodNameLength = identifier.astValue().length(); if (start != null && end != null && end.getOffset() - start.getOffset() > methodNameLength) { end = new DefaultPosition(start.getLine(), start.getColumn() + methodNameLength, start.getOffset() + methodNameLength); return Location.create(location.getFile(), start, end); } return location; } return super.getNameLocation(context, node); }