Search in sources :

Example 1 with NamesValidator

use of com.intellij.lang.refactoring.NamesValidator in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoRefactoringUtil method getSuggestedNames.

@NotNull
private static LinkedHashSet<String> getSuggestedNames(GoExpression expression, PsiElement context) {
    // todo rewrite with names resolve; check occurrences contexts
    if (expression.isEquivalentTo(context)) {
        context = PsiTreeUtil.getParentOfType(context, GoBlock.class);
    }
    LinkedHashSet<String> usedNames = getNamesInContext(context);
    LinkedHashSet<String> names = ContainerUtil.newLinkedHashSet();
    NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(GoLanguage.INSTANCE);
    if (expression instanceof GoCallExpr) {
        GoReferenceExpression callReference = PsiTreeUtil.getChildOfType(expression, GoReferenceExpression.class);
        if (callReference != null) {
            String name = StringUtil.decapitalize(callReference.getIdentifier().getText());
            for (String candidate : NameUtil.getSuggestionsByName(name, "", "", false, false, false)) {
                if (usedNames.contains(candidate))
                    continue;
                if (!isValidName(namesValidator, candidate))
                    continue;
                names.add(candidate);
            }
        }
    }
    GoType type = expression.getGoType(null);
    String typeText = GoPsiImplUtil.getText(type);
    if (StringUtil.isNotEmpty(typeText)) {
        boolean array = GoTypeUtil.isIterable(type) && !GoTypeUtil.isString(type);
        for (String candidate : NameUtil.getSuggestionsByName(typeText, "", "", false, false, array)) {
            if (usedNames.contains(candidate) || typeText.equals(candidate))
                continue;
            if (!isValidName(namesValidator, candidate))
                continue;
            names.add(candidate);
        }
    }
    if (names.isEmpty()) {
        names.add(UniqueNameGenerator.generateUniqueName("i", usedNames));
    }
    return names;
}
Also used : NamesValidator(com.intellij.lang.refactoring.NamesValidator) NotNull(org.jetbrains.annotations.NotNull)

Example 2 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 3 with NamesValidator

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

the class FlexRenameTest method testNamesValidator.

public void testNamesValidator() {
    final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(JSSupportLoader.getJsFileType().getLanguage());
    assertTrue(namesValidator.isIdentifier("zzz", myProject));
    assertTrue(!namesValidator.isIdentifier("\"zzz\"", myProject));
    assertTrue(namesValidator.isKeyword("function", myProject));
    assertTrue(!namesValidator.isIdentifier("zzz<=", myProject));
    final NamesValidator ecmaL4NamesValidator = LanguageNamesValidation.INSTANCE.forLanguage(JavaScriptSupportLoader.ECMA_SCRIPT_L4);
    assertTrue(ecmaL4NamesValidator.isKeyword("private", myProject));
    assertTrue(ecmaL4NamesValidator.isKeyword("namespace", myProject));
    assertTrue(ecmaL4NamesValidator.isIdentifier("namespace", myProject));
    final NamesValidator jsonNamesValidator = LanguageNamesValidation.INSTANCE.forLanguage(JsonLanguage.INSTANCE);
    assertTrue(jsonNamesValidator.isIdentifier("aaa.bbb", myProject));
}
Also used : NamesValidator(com.intellij.lang.refactoring.NamesValidator)

Example 4 with NamesValidator

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

the class FlexMoveClassDialog method canRun.

@Override
protected void canRun() throws ConfigurationException {
    final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(JavaScriptSupportLoader.JAVASCRIPT.getLanguage());
    if (myFileLocal) {
        final String className = myClassNameField.getText();
        if (StringUtil.isEmpty(className)) {
            throw new ConfigurationException(FlexBundle.message("element.name.empty", JSBundle.message(JSNamedElementKind.kind(myElements.iterator().next()).humanReadableKey())));
        }
        if (!namesValidator.isIdentifier(className, myProject)) {
            throw new ConfigurationException(FlexBundle.message("invalid.element.name", StringUtil.decapitalize(JSBundle.message(JSNamedElementKind.kind(myElements.iterator().next()).humanReadableKey())), className));
        }
    }
    final String packageName = myTargetPackageField.getText();
    for (final String s : StringUtil.split(packageName, ".")) {
        if (!namesValidator.isIdentifier(s, myProject)) {
            throw new ConfigurationException(JSBundle.message("invalid.package", packageName));
        }
    }
}
Also used : ConfigurationException(com.intellij.openapi.options.ConfigurationException) NamesValidator(com.intellij.lang.refactoring.NamesValidator)

Example 5 with NamesValidator

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

the class CreateFlexSkinDialog method doOKAction.

protected void doOKAction() {
    final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(JavaScriptSupportLoader.JAVASCRIPT.getLanguage());
    final String packageName = getPackageName();
    for (final String s : StringUtil.split(packageName, ".")) {
        if (!namesValidator.isIdentifier(s, null)) {
            setErrorText(JSBundle.message("invalid.package", packageName), myPackageCombo);
            return;
        }
    }
    myHostComponentCombo.updateRecents();
    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)

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