7171import org .intellij .erlang .sdk .ErlangSdkRelease ;
7272import org .intellij .erlang .sdk .ErlangSdkType ;
7373import org .intellij .erlang .sdk .ErlangSystemUtil ;
74- import org .intellij .erlang .stubs .*;
74+ import org .intellij .erlang .stubs .ErlangFunctionStub ;
75+ import org .intellij .erlang .stubs .ErlangIncludeLibStub ;
76+ import org .intellij .erlang .stubs .ErlangIncludeStub ;
77+ import org .intellij .erlang .stubs .ErlangTypeDefinitionStub ;
7578import org .jetbrains .annotations .NotNull ;
7679import org .jetbrains .annotations .Nullable ;
7780
@@ -719,15 +722,7 @@ private static int calculateFunctionClauseArity(@NotNull ErlangFunctionClause cl
719722
720723 @ NotNull
721724 public static String getName (@ NotNull ErlangFunction o ) {
722- ErlangFunctionStub stub = o .getStub ();
723- if (stub != null ) return StringUtil .notNullize (stub .getName ());
724-
725- ErlangAtom atom = o .getAtomName ().getAtom ();
726- if (atom != null ) {
727- return atom .getName ();
728- }
729- //noinspection ConstantConditions
730- return o .getAtomName ().getMacros ().getText ();
725+ return getNameImpl (o );
731726 }
732727
733728 @ NotNull
@@ -743,10 +738,7 @@ public static int getArity(@NotNull ErlangFunction o) {
743738
744739 @ NotNull
745740 public static String getName (@ NotNull ErlangRecordDefinition o ) {
746- ErlangRecordDefinitionStub stub = o .getStub ();
747- if (stub != null ) return StringUtil .notNullize (stub .getName ());
748- ErlangQAtom atom = o .getQAtom ();
749- return atom != null ? getName (atom ) : "" ;
741+ return getNameImpl (o );
750742 }
751743
752744 @ NotNull
@@ -856,12 +848,7 @@ public static PsiElement setName(@NotNull ErlangTypeDefinition o, @NotNull Strin
856848
857849 @ NotNull
858850 public static String getName (@ NotNull ErlangModule o ) {
859- ErlangModuleStub stub = o .getStub ();
860- if (stub != null ) return StringUtil .notNullize (stub .getName ());
861- ErlangQAtom qAtom = o .getQAtom ();
862- if (qAtom == null ) return "" ;
863- ErlangAtom atom = qAtom .getAtom ();
864- return atom == null ? qAtom .getText () : atom .getName ();
851+ return getNameImpl (o );
865852 }
866853
867854 @ NotNull
@@ -1250,9 +1237,7 @@ public static int getTextOffset(@NotNull ErlangMacrosDefinition o) {
12501237
12511238 @ NotNull
12521239 public static String getName (@ NotNull ErlangMacrosDefinition o ) {
1253- ErlangMacrosDefinitionStub stub = o .getStub ();
1254- if (stub != null ) return StringUtil .notNullize (stub .getName ());
1255- return getNameIdentifier (o ).getText ();
1240+ return getNameImpl (o );
12561241 }
12571242
12581243 @ NotNull
@@ -1303,8 +1288,9 @@ else if (macroName.getVar() != null) {
13031288
13041289 @ NotNull
13051290 public static String getName (@ NotNull ErlangBehaviour o ) {
1306- ErlangBehaviourStub stub = o .getStub ();
1307- if (stub != null ) return StringUtil .notNullize (stub .getName ());
1291+ String fromStub = getNameFromStub (o );
1292+ if (fromStub != null ) return fromStub ;
1293+
13081294 ErlangModuleRef moduleRef = o .getModuleRef ();
13091295 ErlangQAtom atom = moduleRef != null ? moduleRef .getQAtom () : null ;
13101296 return atom == null ? "" : getName (atom );
@@ -1508,9 +1494,7 @@ public static SearchScope getUseScope(@NotNull ErlangQVarImpl o) {
15081494
15091495 @ NotNull
15101496 public static String getName (@ NotNull ErlangTypeDefinition o ) {
1511- ErlangTypeDefinitionStub stub = o .getStub ();
1512- if (stub != null ) return StringUtil .notNullize (stub .getName ());
1513- return o .getNameIdentifier ().getText ();
1497+ return getNameImpl (o );
15141498 }
15151499
15161500 @ Nullable
@@ -1747,6 +1731,22 @@ private static TextRange rangeInParent(@NotNull TextRange parent, @NotNull TextR
17471731 return TextRange .create (start , start + child .getLength ());
17481732 }
17491733
1734+ @ NotNull
1735+ private static String getNameImpl (@ NotNull ErlangNamedElement namedElement ) {
1736+ if (namedElement instanceof StubBasedPsiElement ) {
1737+ String fromStub = getNameFromStub ((StubBasedPsiElement )namedElement );
1738+ if (fromStub != null ) return fromStub ;
1739+ }
1740+ PsiElement nameIdentifier = namedElement .getNameIdentifier ();
1741+ return nameIdentifier != null ? nameIdentifier .getText () : "" ;
1742+ }
1743+
1744+ @ Nullable
1745+ private static String getNameFromStub (StubBasedPsiElement element ) {
1746+ ErlangNamedStubbedPsiElementBase stub = ObjectUtils .tryCast (element .getStub (), ErlangNamedStubbedPsiElementBase .class );
1747+ return stub != null ? StringUtil .notNullize (stub .getName ()) : null ;
1748+ }
1749+
17501750 public static boolean fromTheSameCaseExpression (@ NotNull PsiElement origin , @ NotNull PsiElement element ) {
17511751 if (element instanceof ErlangQVar && Comparing .equal (element .getText (), element .getText ())) {
17521752 ErlangCompositeElement cr2 = PsiTreeUtil .getParentOfType (element , ErlangCrClause .class );
0 commit comments