Skip to content

Commit

Permalink
#2761 fixed name suggestion for non-identifier indexes
Browse files Browse the repository at this point in the history
#CAMELCADE-4604 Fixed
  • Loading branch information
hurricup committed Oct 1, 2023
1 parent 9282bc7 commit 62d16e8
Show file tree
Hide file tree
Showing 57 changed files with 306 additions and 213 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2020 Alexandr Evstigneev
* Copyright 2015-2023 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,6 +54,7 @@
import java.io.File;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.perl5.lang.perl.lexer.PerlElementTypesGenerated.*;
import static com.perl5.lang.perl.lexer.PerlTokenSets.*;
Expand Down Expand Up @@ -362,36 +363,37 @@ private static String suggestAndAddNameForString(@NotNull PerlString expression,
result.add(COMMAND_OUTPUT);
return COMMAND_OUTPUT;
}
if (expression.getChildren().length == 0) {
String nameFromManipulator = getNameFromManipulator(expression);
if (nameFromManipulator != null) {
result.add(nameFromManipulator);
return nameFromManipulator;
}
String valueText = ElementManipulators.getValueText(expression);

if (PerlString.looksLikePackage(valueText)) {
result.addAll(PACKAGE_BASE_NAMES);
List<String> namespaceVariants = getVariantsFromNamespaceName(valueText);
result.addAll(namespaceVariants);
if (!namespaceVariants.isEmpty()) {
return namespaceVariants.get(0);
}
}
String recommendedString = STRING;
if (expression.getChildren().length != 0) {
return recommendedString;
}
String nameFromManipulator = getNameFromManipulator(expression);
if (nameFromManipulator != null) {
result.add(nameFromManipulator);
recommendedString = nameFromManipulator;
}

// this probably should be the last
String valueText = ElementManipulators.getValueText(expression);
if (PerlString.looksLikePackage(valueText)) {
result.addAll(PACKAGE_BASE_NAMES);
List<String> namespaceVariants = getVariantsFromNamespaceName(valueText);
result.addAll(namespaceVariants);
if (!namespaceVariants.isEmpty() && STRING.equals(recommendedString)) {
recommendedString = namespaceVariants.get(0);
}
}
else{
String independentPath = FileUtil.toSystemIndependentName(valueText);
if (PerlString.looksLikePath(independentPath)) {
result.addAll(BASE_PATH_NAMES);
List<String> pathVariants = getVariantsFromPath(independentPath);
result.addAll(pathVariants);
if (!pathVariants.isEmpty()) {
return pathVariants.get(0);
if (!pathVariants.isEmpty() && STRING.equals(recommendedString)) {
recommendedString = pathVariants.get(0);
}
}
}

return STRING;
return recommendedString;
}

@Contract("null->null")
Expand Down Expand Up @@ -484,7 +486,8 @@ else if (CAST_EXPRESSIONS.contains(elementType)) {
String nameFromKey = getBaseName(indexExpr);
if (indexExpr instanceof PerlString) {
if (StringUtil.isEmpty(nameFromKey)) {
LOG.error("Unable to extract name from: " + indexExpr + "; '" + indexExpr.getText() + "'");
LOG.debug("Unable to extract name from: ", indexExpr, "; '", indexExpr.getText(), "'");
recommendation = "element";
}
else {
recommendation = nameFromKey;
Expand Down Expand Up @@ -587,16 +590,15 @@ public static void suggestNames(@NotNull PerlVariableDeclarationElement variable
}

private static @Nullable String join(@NotNull List<String> source) {
for (String element : source) {
if (StringUtil.isEmptyOrSpaces(element)) {
return null;
}
}
return validateName(StringUtil.join(ContainerUtil.map(source, it -> it.toLowerCase(Locale.getDefault())), "_"));
var result = source.stream()
.filter(chunk -> !StringUtil.isEmptyOrSpaces(chunk))
.collect(Collectors.joining("_"))
.toLowerCase(Locale.getDefault());
return validateName(result);
}

private static @Nullable String spacedToSnakeCase(@NotNull String source) {
return join(source.toLowerCase(Locale.getDefault()).split("\\s+"));
return join(source.toLowerCase(Locale.getDefault()).split("\\W+"));
}

@Contract("null->null")
Expand Down
14 changes: 13 additions & 1 deletion plugin/src/test/java/editor/PerlNamesSuggesterTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2019 Alexandr Evstigneev
* Copyright 2015-2023 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,18 @@ protected String getBaseDataPath() {
return "nameSuggester/perl";
}

@Test
public void testStrangeIndex() { doTest(); }

@Test
public void testSeparatorCharIndex() { doTest(); }

@Test
public void testSpacesIndex() { doTest(); }

@Test
public void testSpecialCharIndex() { doTest(); }

@Test
public void testListToScalarDeclaration() {doTest();}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
my $<selection>string<caret></selection> = qq<bl"alba>;
my $<selection>bl_alba<caret></selection> = qq<bl"alba>;
say <<"EOM";
test $string text
test $bl_alba text
EOM

say <<EOM;
test $string text
test $bl_alba text
EOM

say <<'EOM';
test bl"alba text
EOM

say <<`EOM`;
test $string text
test $bl_alba text
EOM


Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
my $<selection>string<caret></selection> = qq[bl"al>ba];
my $<selection>bl_al_ba<caret></selection> = qq[bl"al>ba];
say <<"EOM";
test $string text
test $bl_al_ba text
EOM

say <<EOM;
test $string text
test $bl_al_ba text
EOM

say <<'EOM';
test bl"al>ba text
EOM

say <<`EOM`;
test $string text
test $bl_al_ba text
EOM


Expand Down
22 changes: 11 additions & 11 deletions plugin/src/test/resources/introduce/full/packageForms.pl.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package Foo::Bar;

my $<selection>bar<caret></selection> = 'Foo::Bar';
$bar->method;
$bar->method;
$bar->method;
$bar->method;
$bar->method;
$bar->method;
my $<selection>foo_bar<caret></selection> = 'Foo::Bar';
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
`Foo::Bar::`->method;
`Foo::Bar`->method;

$bar->method;
$bar->method;
$bar->method;
$bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
qx`Foo::Bar::`->method;
qx`Foo::Bar`->method;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package Foo::Bar;

my $<selection>bar<caret></selection> = 'Foo::Bar';
$bar->method;
$bar->method;
my $<selection>foo_bar<caret></selection> = 'Foo::Bar';
$foo_bar->method;
$foo_bar->method;
================ AFTER RENAME =================
package Foo::Bar;

Expand Down
26 changes: 13 additions & 13 deletions plugin/src/test/resources/introduce/full/packageFormsString.pl.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package Foo::Bar;

my $<selection>string<caret></selection> = 'Foo::Bar::';
$string->method;
$string->method;
$string->method;
$string->method;
$string->method;
$string->method;
`$string`->method;
my $<selection>foo_bar<caret></selection> = 'Foo::Bar::';
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
`$foo_bar`->method;
`Foo::Bar`->method;

$string->method;
$string->method;
$string->method;
$string->method;
qx`$string`->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
qx`$foo_bar`->method;
qx`Foo::Bar`->method;

================ AFTER RENAME =================
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package Foo::Bar;

my $<selection>string<caret></selection> = qq"Foo::Bar::";
$string->method;
$string->method;
$string->method;
$string->method;
$string->method;
$string->method;
`$string`->method;
my $<selection>foo_bar<caret></selection> = qq"Foo::Bar::";
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
`$foo_bar`->method;
`Foo::Bar`->method;

$string->method;
$string->method;
$string->method;
$string->method;
qx`$string`->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
$foo_bar->method;
qx`$foo_bar`->method;
qx`Foo::Bar`->method;

================ AFTER RENAME =================
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
my $<selection>ond<caret></selection> = q<sec'ond>;
('first', $ond, 'third');
my $<selection>sec_ond<caret></selection> = q<sec'ond>;
('first', $sec_ond, 'third');

================ AFTER RENAME =================
my $test_name<caret> = q<sec'ond>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
my $<selection>string<caret></selection> = 're b${variable}la';
'something he' . $string;
my $<selection>re_b_variable_la<caret></selection> = 're b${variable}la';
'something he' . $re_b_variable_la;
"something here b${variable}la";
`something here b${variable}la`;
'something he' . $string;
'something he' . $re_b_variable_la;
qq {something here b${variable}la};
qx fsomething here b${variable}laf;
qw/something here b${variable}la/;
something => "here b${variable}la";

$string;
$re_b_variable_la;
"re b${variable}la";
`re b${variable}la`;
$string;
$re_b_variable_la;
qq {re b${variable}la};
qx fre b${variable}laf;
qw/re b${variable}la/;
something => "";

'something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' ';
'something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' ';
"something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la ";
`something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la `;
'something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' ';
'something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' ';
qq {something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la };
qx fsomething here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la f;
qw/something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la /;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
my $<selection>string<caret></selection> = 're b${variable}la';
'something he' . $string;
my $<selection>re_b_variable_la<caret></selection> = 're b${variable}la';
'something he' . $re_b_variable_la;
"something here b${variable}la";
`something here b${variable}la`;
'something he' . $string;
'something he' . $re_b_variable_la;
qq {something here b${variable}la};
qx fsomething here b${variable}laf;
qw/something here b${variable}la/;
something => "here b${variable}la";

$string;
$re_b_variable_la;
"re b${variable}la";
`re b${variable}la`;
$string;
$re_b_variable_la;
qq {re b${variable}la};
qx fre b${variable}laf;
qw/re b${variable}la/;
something => "";

'something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string;
'something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la;
"something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la ";
`something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la `;
'something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' something he' . $string . ' ';
'something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' something he' . $re_b_variable_la . ' ';
qq {something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la };
qx fsomething here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la f;
qw/something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la something here b${variable}la /;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
my $<selection>string<caret></selection> = 'ing he${variable}re b';
'someth' . $string . 'la';
my $<selection>ing_he_variable_re_b<caret></selection> = 'ing he${variable}re b';
'someth' . $ing_he_variable_re_b . 'la';
"something he${variable}re bla";
`something he${variable}re bla`;
'someth' . $string . 'la';
'someth' . $ing_he_variable_re_b . 'la';
qq {something he${variable}re bla};
qx fsomething he${variable}re blaf;
qw/something he${variable}re bla/;
something => "here bla";

$string;
$ing_he_variable_re_b;
"ing he${variable}re b";
`ing he${variable}re b`;
$string;
$ing_he_variable_re_b;
qq {ing he${variable}re b};
qx fing he${variable}re bf;
qw/ing he${variable}re b/;
something => "";

'someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la ';
'someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la ';
"something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla ";
`something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla `;
'someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la someth' . $string . 'la ';
'someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la someth' . $ing_he_variable_re_b . 'la ';
qq {something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla };
qx fsomething he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla f;
qw/something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla something he${variable}re bla /;
Expand Down
Loading

0 comments on commit 62d16e8

Please sign in to comment.