@NotNull @Override public PsiKeyword createKeyword(@NotNull @NonNls String keyword, PsiElement context) throws IncorrectOperationException { if (!JavaLexer.isKeyword(keyword, PsiUtil.getLanguageLevel(context))) { throw new IncorrectOperationException("\"" + keyword + "\" is not a keyword."); } return new LightKeyword(myManager, keyword); }
@Override public String fromString(@Nullable @NonNls String s, ConvertContext context) { if (s == null) { return null; } String fieldName = AndroidResourceUtil.getFieldNameByResourceName(s); return StringUtil.isJavaIdentifier(fieldName) && !JavaLexer.isKeyword(fieldName, LanguageLevel.JDK_1_5) ? s : null; }
@NotNull public static Lexer createLexer(@NotNull LanguageLevel level) { return new JavaLexer(level); }
public static boolean isJavaIdentifier(@NotNull String identifier, @NotNull LanguageLevel level) { return StringUtil.isJavaIdentifier(identifier) && !JavaLexer.isKeyword(identifier, level); }
@Override public boolean isIdentifier(@Nullable String text, @NotNull LanguageLevel languageLevel) { return text != null && StringUtil.isJavaIdentifier(text) && !JavaLexer.isKeyword(text, languageLevel); }
@Override public boolean isKeyword(@Nullable String text) { return text != null && JavaLexer.isKeyword(text, getLanguageLevel()); }
@Nullable public String getErrorMessage() { // @null is a valid resource reference, even though it is not otherwise a valid // resource url (it's missing a resource type, "null" is a Java keyword, etc) if ("null".equals(myResourceName) && myResourceType == null && myPrefix == '@') { return null; } if (myResourceName == null || myResourceName.isEmpty()) { if (myResourceType == null && (myPrefix == '@' || myPrefix == '?')) { return "Missing resource type"; } return "Missing resource name"; } ResourceType type; if (myResourceType == null) { if (myPrefix != '?') { if (myPrefix == '@' && myResourceName.indexOf('/') == -1) { return "Missing /"; } return "Missing resource type"; } type = ResourceType.ATTR; } else if (PLUS_ID.equals(myResourceType)) { type = ResourceType.ID; } else { type = ResourceType.getEnum(myResourceType); if (type == null) { return "Unknown resource type " + myResourceType; } } String name = myResourceName; if (FolderTypeRelationship.getRelatedFolders(type).contains(ResourceFolderType.VALUES)) { name = AndroidResourceUtil.getFieldNameByResourceName(name); } if (!AndroidUtils.isIdentifier(name)) { if (JavaLexer.isKeyword(name, LanguageLevel.JDK_1_5)) { return "Resource name cannot be a Java keyword (" + name + ")"; } if (!Character.isJavaIdentifierStart(name.charAt(0))) { return "The resource name must begin with a character"; } for (int i = 1, n = name.length(); i < n; i++) { char c = name.charAt(i); if (!Character.isJavaIdentifierPart(c)) { return String.format("'%1$c' is not a valid resource name character", c); } } return "Resource name '" + name + "' must be a valid Java identifier"; } return null; }
public static boolean isIdentifier(@NotNull String candidate) { return StringUtil.isJavaIdentifier(candidate) && !JavaLexer.isKeyword(candidate, LanguageLevel.JDK_1_5); }
/** * Proper Identifier * * @param candidate * @return */ public static boolean isIdentifier(@NotNull String candidate) { return StringUtil.isJavaIdentifier(candidate) && !JavaLexer.isKeyword(candidate, LanguageLevel.JDK_1_6); }