Search in sources :

Example 6 with NamesValidator

use of com.intellij.lang.refactoring.NamesValidator in project intellij-community by JetBrains.

the class GotoDeclarationAction method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    DumbService.getInstance(project).setAlternativeResolveEnabled(true);
    try {
        int offset = editor.getCaretModel().getOffset();
        PsiElement[] elements = underModalProgress(project, "Resolving Reference...", () -> findAllTargetElements(project, editor, offset));
        FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.goto.declaration");
        if (elements.length != 1) {
            if (elements.length == 0 && suggestCandidates(TargetElementUtil.findReference(editor, offset)).isEmpty()) {
                PsiElement element = findElementToShowUsagesOf(editor, editor.getCaretModel().getOffset());
                if (startFindUsages(editor, element)) {
                    return;
                }
                //disable 'no declaration found' notification for keywords
                final PsiElement elementAtCaret = file.findElementAt(offset);
                if (elementAtCaret != null) {
                    final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(elementAtCaret.getLanguage());
                    if (namesValidator != null && namesValidator.isKeyword(elementAtCaret.getText(), project)) {
                        return;
                    }
                }
            }
            chooseAmbiguousTarget(editor, offset, elements, file);
            return;
        }
        PsiElement element = elements[0];
        if (element == findElementToShowUsagesOf(editor, editor.getCaretModel().getOffset()) && startFindUsages(editor, element)) {
            return;
        }
        PsiElement navElement = element.getNavigationElement();
        navElement = TargetElementUtil.getInstance().getGotoDeclarationTarget(element, navElement);
        if (navElement != null) {
            gotoTargetElement(navElement, editor, file);
        }
    } catch (IndexNotReadyException e) {
        DumbService.getInstance(project).showDumbModeNotification("Navigation is not available here during index update");
    } finally {
        DumbService.getInstance(project).setAlternativeResolveEnabled(false);
    }
}
Also used : IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) NamesValidator(com.intellij.lang.refactoring.NamesValidator) RelativePoint(com.intellij.ui.awt.RelativePoint) PsiElement(com.intellij.psi.PsiElement)

Example 7 with NamesValidator

use of com.intellij.lang.refactoring.NamesValidator in project intellij-plugins by JetBrains.

the class AnnotationBackedDescriptorImpl method validateValue.

@Override
@Nullable
public String validateValue(final XmlElement context, String value) {
    final PsiElement parent = context instanceof XmlAttributeValue ? context.getParent() : null;
    if (parent instanceof XmlAttribute && FlexMxmlLanguageAttributeNames.ID.equals(((XmlAttribute) parent).getName())) {
        final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(JavaScriptSupportLoader.JAVASCRIPT.getLanguage());
        return // ok
        namesValidator.isIdentifier(value, context.getProject()) ? // ok
        null : JSBundle.message("invalid.identifier.value");
    }
    // dynamic values
    if (value.indexOf('{') != -1)
        return null;
    if (value.trim().startsWith("@Resource"))
        return null;
    if (myAnnotationName != null && CLEAR_DIRECTIVE.equals(value)) {
        return checkClearDirectiveContext(context);
    }
    if (isAllowsPercentage() && value.endsWith("%")) {
        value = value.substring(0, value.length() - 1);
    }
    boolean uint = false;
    if ("int".equals(type) || (uint = "uint".equals(type))) {
        try {
            boolean startWithSharp;
            if ((startWithSharp = value.startsWith("#")) || value.startsWith("0x")) {
                if (uint) {
                    final long l = Long.parseLong(value.substring(startWithSharp ? 1 : 2), 16);
                    if (l < 0 || l > 0xFFFFFFFFL) {
                        throw new NumberFormatException("value out of range");
                    }
                } else {
                    Integer.parseInt(value.substring(startWithSharp ? 1 : 2), 16);
                }
            } else {
                if ("Color".equals(format) && !StringUtil.isEmptyOrSpaces(value) && !Character.isDigit(value.charAt(0))) {
                    return checkColorAlias(value);
                }
                if (uint) {
                    final long l = Long.parseLong(value);
                    if (l < 0 || l > 0xFFFFFFFFL) {
                        throw new NumberFormatException("value out of range");
                    }
                } else {
                    Integer.parseInt(value);
                }
            }
        } catch (NumberFormatException ex) {
            return FlexBundle.message("flex.invalid.integer.value");
        }
    } else if ("Number".equals(type)) {
        try {
            boolean startWithSharp;
            if (value != null && ((startWithSharp = value.startsWith("#")) || value.startsWith("0x"))) {
                Integer.parseInt(value.substring(startWithSharp ? 1 : 2), 16);
            } else {
                Double.parseDouble(value);
            }
        } catch (NumberFormatException ex) {
            return FlexBundle.message("flex.invalid.number.value");
        }
    }
    return null;
}
Also used : NamesValidator(com.intellij.lang.refactoring.NamesValidator) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with NamesValidator

use of com.intellij.lang.refactoring.NamesValidator in project intellij-plugins by JetBrains.

the class ChoosePackageDialog method doOKAction.

protected void doOKAction() {
    final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(JavascriptLanguage.INSTANCE);
    final String packageName = getPackageName();
    for (final String s : StringUtil.split(packageName, ".")) {
        if (!namesValidator.isIdentifier(s, null)) {
            setErrorText(JSBundle.message("invalid.package", packageName), myPackageCombo);
            return;
        }
    }
    myPackageCombo.updateRecents();
    myTargetDirectory = JSRefactoringUtil.chooseOrCreateDirectoryForClass(myModule.getProject(), myModule, getPackageScope(), packageName, null, myContextFile.getParent(), ThreeState.UNSURE);
    if (myTargetDirectory != null) {
        super.doOKAction();
    }
}
Also used : NamesValidator(com.intellij.lang.refactoring.NamesValidator)

Example 9 with NamesValidator

use of com.intellij.lang.refactoring.NamesValidator in project intellij-community by JetBrains.

the class InvertBooleanDialog method doAction.

protected void doAction() {
    Project project = myElement.getProject();
    final String name = myNameField.getText().trim();
    final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(myElement.getLanguage());
    if (namesValidator != null && !namesValidator.isIdentifier(name, myProject)) {
        CommonRefactoringUtil.showErrorMessage(InvertBooleanHandler.REFACTORING_NAME, RefactoringBundle.message("please.enter.a.valid.name.for.inverted.element", UsageViewUtil.getType(myElement)), InvertBooleanHandler.INVERT_BOOLEAN_HELP_ID, project);
        return;
    }
    invokeRefactoring(new InvertBooleanProcessor(myElement, name));
}
Also used : Project(com.intellij.openapi.project.Project) NamesValidator(com.intellij.lang.refactoring.NamesValidator)

Example 10 with NamesValidator

use of com.intellij.lang.refactoring.NamesValidator in project intellij-community by JetBrains.

the class JavaFxUnresolvedFxIdReferenceInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
    if (!JavaFxFileTypeFactory.isFxml(session.getFile()))
        return PsiElementVisitor.EMPTY_VISITOR;
    return new XmlElementVisitor() {

        @Override
        public void visitXmlAttribute(XmlAttribute attribute) {
            super.visitXmlAttribute(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);
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) NamesValidator(com.intellij.lang.refactoring.NamesValidator) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) JavaFxFieldIdReferenceProvider(org.jetbrains.plugins.javaFX.fxml.refs.JavaFxFieldIdReferenceProvider) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NamesValidator (com.intellij.lang.refactoring.NamesValidator)11 NotNull (org.jetbrains.annotations.NotNull)3 Project (com.intellij.openapi.project.Project)2 PsiElement (com.intellij.psi.PsiElement)2 Module (com.intellij.openapi.module.Module)1 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 RelativePoint (com.intellij.ui.awt.RelativePoint)1 Nullable (org.jetbrains.annotations.Nullable)1 JavaFxFieldIdReferenceProvider (org.jetbrains.plugins.javaFX.fxml.refs.JavaFxFieldIdReferenceProvider)1