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;
}
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);
}
}
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));
}
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));
}
}
}
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();
}
}
Aggregations