protected void addSeenWord(HashSet<String> seenNames, String word, Language language) { final String lowerWord = word.toLowerCase(); if (globalSeenNames.contains(lowerWord)) { return; } final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(language); if (namesValidator != null && namesValidator.isKeyword(word, myProject)) { return; } globalSeenNames.add(lowerWord); if (mySpellCheckerManager.hasProblem(lowerWord)){ seenNames.add(lowerWord); } }
public static boolean isValidName(final Project project, final PsiElement psiElement, final String newName) { if (newName == null || newName.length() == 0) { return false; } final Condition<String> inputValidator = RenameInputValidatorRegistry.getInputValidator(psiElement); if (inputValidator != null) { return inputValidator.value(newName); } if (psiElement instanceof PsiFile || psiElement instanceof PsiDirectory) { return newName.indexOf('\\') < 0 && newName.indexOf('/') < 0; } if (psiElement instanceof PomTargetPsiElement) { return !StringUtil.isEmptyOrSpaces(newName); } final PsiFile file = psiElement.getContainingFile(); final Language elementLanguage = psiElement.getLanguage(); final Language fileLanguage = file == null ? null : file.getLanguage(); Language language = fileLanguage == null ? elementLanguage : fileLanguage.isKindOf(elementLanguage) ? fileLanguage : elementLanguage; return LanguageNamesValidation.INSTANCE.forLanguage(language).isIdentifier(newName.trim(), project); }
public IntroduceParameterDialog(XPathExpression expression, int numberOfExpressions, boolean forceDefault) { super(expression.getProject(), LanguageNamesValidation.INSTANCE.forLanguage(expression.getLanguage())); myForceDefault = forceDefault; init(expression, numberOfExpressions, TITLE); getOKAction().addPropertyChangeListener(new PropertyChangeListener() { @SuppressWarnings({"AutoUnboxing"}) public void propertyChange(PropertyChangeEvent evt) { if (evt.getPropertyName().equals("enabled")) { myPreviewAction.setEnabled(((Boolean)evt.getNewValue()) && !myCreateWithDefault.isSelected()); } } }); if (forceDefault) { myCreateWithDefault.setSelected(true); myCreateWithDefault.setVisible(false); } else { myCreateWithDefault.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { myPreviewAction.setEnabled(getOKAction().isEnabled() && !myCreateWithDefault.isSelected()); } }); } myPreviewAction.setEnabled(false); }
@RequiredReadAction PsiElement handleElementRenameInternal(String newElementName) throws IncorrectOperationException { final int i = newElementName.lastIndexOf('.'); if(i != -1) { newElementName = newElementName.substring(0, i); } if(!LanguageNamesValidation.INSTANCE.forLanguage(JavaScriptLanguage.INSTANCE).isIdentifier(newElementName, null)) { throw new IncorrectOperationException("Invalid javascript element name:" + newElementName); } final PsiElement parent = getParent(); if(parent instanceof JSClass || parent instanceof JSFunction) { final PsiElement node = ((JSNamedElement) parent).getNameIdentifier(); if(node != null && node == this) { return this; // JSNamedElement.setName will care of things } } JSChangeUtil.doIdentifierReplacement(this, getNameElement(), newElementName); return getParent(); }
@Override public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (!startElement.isValid()) { return false; } final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(file.getLanguage()); return namesValidator != null && namesValidator.isIdentifier(myNewName, project); }
/** * Assign a suitable new and unique id to the given component. The set of * existing id's is provided in the given list. */ @NotNull public String assignId(RadViewComponent component, Collection<String> idList) { String idValue = StringUtil.decapitalize(component.getMetaModel().getTag()); XmlTag tag = component.getTag(); Module module = AndroidPsiUtils.getModuleSafely(tag); if (module != null) { idValue = ResourceHelper.prependResourcePrefix(module, idValue); } String nextIdValue = idValue; int index = 0; // Ensure that we don't create something like "switch" as an id, which won't compile when used // in the R class NamesValidator validator = LanguageNamesValidation.INSTANCE.forLanguage(JavaLanguage.INSTANCE); Project project = tag.getProject(); while (idList.contains(nextIdValue) || validator != null && validator.isKeyword(nextIdValue, project)) { ++index; if (index == 1 && (validator == null || !validator.isKeyword(nextIdValue, project))) { nextIdValue = idValue; } else { nextIdValue = idValue + Integer.toString(index); } } String newId = NEW_ID_PREFIX + idValue + (index == 0 ? "" : Integer.toString(index)); tag.setAttribute(ATTR_ID, ANDROID_URI, newId); return newId; }
@NotNull @Override public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, final boolean isOnTheFly, @NotNull LocalInspectionToolSession session) { return new XmlElementVisitor() { @Override public void visitXmlFile(XmlFile file) { if (!JavaFxFileTypeFactory.isFxml(file)) return; super.visitXmlFile(file); } @Override public void visitXmlAttribute(XmlAttribute attribute) { if (FxmlConstants.FX_ID.equals(attribute.getName())) { final XmlAttributeValue valueElement = attribute.getValueElement(); if (valueElement != null && valueElement.getTextLength() > 0) { final PsiClass controllerClass = JavaFxPsiUtil.getControllerClass(attribute.getContainingFile()); if (controllerClass != null) { final PsiReference reference = valueElement.getReference(); if (reference instanceof JavaFxFieldIdReferenceProvider.JavaFxControllerFieldRef && ((JavaFxFieldIdReferenceProvider.JavaFxControllerFieldRef)reference).isUnresolved()) { final PsiClass fieldClass = checkContext(((JavaFxFieldIdReferenceProvider.JavaFxControllerFieldRef)reference).getXmlAttributeValue()); if (fieldClass != null) { final String text = reference.getCanonicalText(); final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(fieldClass.getLanguage()); boolean validName = namesValidator != null && namesValidator.isIdentifier(text, fieldClass.getProject()); holder.registerProblem(reference.getElement(), reference.getRangeInElement(), "Unresolved fx:id reference", isOnTheFly && validName ? new LocalQuickFix[]{new CreateFieldFromUsageQuickFix(text)} : LocalQuickFix.EMPTY_ARRAY); } } } } } } }; }
@Override public boolean isAvailable(@NotNull Project project, @Nullable Editor editor, PsiFile file) { if (!myElement.isValid()) { return false; } final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(file.getLanguage()); return namesValidator != null && namesValidator.isIdentifier(myNewName, project); }
public String createId(RadViewComponent component) { String idValue = StringUtil.decapitalize(component.getMetaModel().getTag()); XmlTag tag = component.getTag(); Module module = CordovaPsiUtils.getModuleSafely(tag); String nextIdValue = idValue; int index = 0; // Ensure that we don't create something like "switch" as an id, which won't compile when used // in the R class NamesValidator validator = LanguageNamesValidation.INSTANCE.forLanguage(JavaLanguage.INSTANCE); Project project = tag.getProject(); while (myIdList.contains(nextIdValue) || validator != null && validator.isKeyword(nextIdValue, project)) { ++index; if (index == 1 && (validator == null || !validator.isKeyword(nextIdValue, project))) { nextIdValue = idValue; } else { nextIdValue = idValue + Integer.toString(index); } } myIdList.add(nextIdValue); String newId = idValue + (index == 0 ? "" : Integer.toString(index)); tag.setAttribute(ATTR_ID, CORDOVASTUDIO_URI, newId); return newId; }
@Override public boolean isAvailable(@Nonnull Project project, @Nonnull PsiFile file, @Nonnull PsiElement startElement, @Nonnull PsiElement endElement) { if (!startElement.isValid()) { return false; } final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(file.getLanguage()); return namesValidator != null && namesValidator.isIdentifier(myNewName, project); }
private static boolean isValidIdentifier(String typeString, PsiElement context) { if(typeString == null) { return false; } Language language = context.getContainingFile().getLanguage(); return LanguageNamesValidation.INSTANCE.forLanguage(language).isIdentifier(typeString, context.getProject()); }
protected boolean isIdentifier(final String newName, final Language language) { final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(language); return namesValidator == null || namesValidator.isIdentifier(newName, myProject); }
public boolean isNameValid(final String name, final Project project) { final NamesValidator validator = LanguageNamesValidation.INSTANCE.forLanguage(PythonLanguage.getInstance()); return (name != null) && (validator.isIdentifier(name, project)) && !(validator.isKeyword(name, project)); }
public NamesValidator getNamesValidator() { if (myNamesValidator == null) { myNamesValidator = LanguageNamesValidation.INSTANCE.forLanguage(XPathFileType.XPATH.getLanguage()); } return myNamesValidator; }
public boolean isInputValid(String newName, PsiElement element, ProcessingContext context) { return LanguageNamesValidation.INSTANCE.forLanguage(XPathFileType.XPATH.getLanguage()) .isIdentifier(newName, element.getProject()); }
public IntroduceVariableDialog(XPathExpression expression, int numberOfExpressions) { super(expression.getProject(), LanguageNamesValidation.INSTANCE.forLanguage(expression.getLanguage())); init(expression, numberOfExpressions, TITLE); }
@NotNull @Override public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, final boolean isOnTheFly, @NotNull LocalInspectionToolSession session) { return new XmlElementVisitor() { @Override public void visitXmlFile(XmlFile file) { if(!JavaFxFileTypeFactory.isFxml(file)) { return; } super.visitXmlFile(file); } @Override public void visitXmlAttribute(XmlAttribute attribute) { if(FxmlConstants.FX_ID.equals(attribute.getName())) { final XmlAttributeValue valueElement = attribute.getValueElement(); if(valueElement != null && valueElement.getTextLength() > 0) { final PsiClass controllerClass = JavaFxPsiUtil.getControllerClass(attribute.getContainingFile()); if(controllerClass != null) { final PsiReference reference = valueElement.getReference(); if(reference instanceof JavaFxFieldIdReferenceProvider.JavaFxControllerFieldRef && ((JavaFxFieldIdReferenceProvider.JavaFxControllerFieldRef) reference).isUnresolved()) { final PsiClass fieldClass = checkContext(((JavaFxFieldIdReferenceProvider.JavaFxControllerFieldRef) reference).getXmlAttributeValue()); if(fieldClass != null) { final String text = reference.getCanonicalText(); final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(fieldClass.getLanguage()); boolean validName = namesValidator != null && namesValidator.isIdentifier(text, fieldClass.getProject()); holder.registerProblem(reference.getElement(), reference.getRangeInElement(), "Unresolved fx:id reference", isOnTheFly && validName ? new LocalQuickFix[]{new CreateFieldFromUsageQuickFix(text)} : LocalQuickFix.EMPTY_ARRAY); } } } } } } }; }
public NameColumnInfo(Language lang, Project project) { super("Name"); myNameValidator = (paramName) -> LanguageNamesValidation.INSTANCE.forLanguage(lang).isIdentifier(paramName, project); }
private boolean isValidName(final String name) { final PsiFile containingFile = myMainOccurence.getContainingFile(); return LanguageNamesValidation.INSTANCE.forLanguage(containingFile.getLanguage()).isIdentifier(name, myProject); }