1010import org .kdb .inside .brains .psi .impl .QExpressionImpl ;
1111
1212import javax .swing .*;
13- import java .util .List ;
1413
1514public abstract class QLambdaMixin extends QExpressionImpl implements QLambda {
1615 public QLambdaMixin (ASTNode node ) {
@@ -44,27 +43,47 @@ public boolean isImplicitDeclaration(@NotNull QVariable variable) {
4443
4544 @ Override
4645 public @ Nullable QVarDeclaration getLocalDeclaration (@ NotNull QVariable variable ) {
47- // implicit variable or in parameters list - ignore namespace
48- if (isImplicitDeclaration (variable )) {
46+ final String name = variable .getName ();
47+ final QParameters parameters = getParameters ();
48+ if (parameters != null ) {
49+ final QVarDeclaration d = findInParameters (parameters , name );
50+ if (d != null ) {
51+ return d ;
52+ }
53+ } else if (QPsiUtil .isImplicitName (name )) {
4954 return null ;
5055 }
5156
5257 final QExpressions expressions = getExpressions ();
53- if (expressions == null ) {
54- return null ;
55- }
56-
57- final List <QExpression > list = expressions .getExpressionList ();
58- if (list .isEmpty ()) {
59- return null ;
58+ if (expressions != null ) {
59+ return findInExpressions (expressions , variable , name );
6060 }
61+ return null ;
62+ }
6163
62- final String name = variable .getName ();
64+ private QVarDeclaration findInParameters (QParameters parameters , String name ) {
65+ final SearchResult result = new SearchResult ();
66+ parameters .acceptChildren (new DeclarationsVisitor () {
67+ @ Override
68+ public void visitElement (@ NotNull PsiElement element ) {
69+ if (element instanceof @ NotNull QVarDeclaration d ) {
70+ if (d .getName ().equals (name )) {
71+ result .declaration = d ;
72+ stopWalking ();
73+ }
74+ } else {
75+ super .visitElement (element );
76+ }
77+ }
78+ });
79+ return result .declaration ;
80+ }
6381
82+ private QVarDeclaration findInExpressions (QExpressions expressions , QVariable variable , String name ) {
6483 final SearchResult result = new SearchResult ();
65- for (QExpression expression : list ) {
84+ for (QExpression expression : expressions . getExpressionList () ) {
6685 // We iterate over each expression and takes the right as the best because Q is right-to-left language
67- expression .accept (new PsiRecursiveElementWalkingVisitor () {
86+ expression .accept (new DeclarationsVisitor () {
6887 @ Override
6988 public void visitElement (@ NotNull PsiElement element ) {
7089 // we don't search inside others lambda, query, table or dict
@@ -83,26 +102,6 @@ public void visitElement(@NotNull PsiElement element) {
83102 super .visitElement (element );
84103 }
85104 }
86-
87- @ Override
88- public void visitComment (@ NotNull PsiComment ignore ) {
89- }
90-
91- @ Override
92- public void visitPlainText (@ NotNull PsiPlainText ignore ) {
93- }
94-
95- @ Override
96- public void visitWhiteSpace (@ NotNull PsiWhiteSpace ignore ) {
97- }
98-
99- @ Override
100- public void visitErrorElement (@ NotNull PsiErrorElement ignore ) {
101- }
102-
103- @ Override
104- public void visitOuterLanguageElement (@ NotNull OuterLanguageElement ignore ) {
105- }
106105 });
107106
108107 // if the declaration found - grate;
@@ -122,4 +121,26 @@ private static class SearchResult {
122121 boolean containsVariable ;
123122 QVarDeclaration declaration ;
124123 }
124+
125+ private static class DeclarationsVisitor extends PsiRecursiveElementWalkingVisitor {
126+ @ Override
127+ public void visitComment (@ NotNull PsiComment ignore ) {
128+ }
129+
130+ @ Override
131+ public void visitPlainText (@ NotNull PsiPlainText ignore ) {
132+ }
133+
134+ @ Override
135+ public void visitWhiteSpace (@ NotNull PsiWhiteSpace ignore ) {
136+ }
137+
138+ @ Override
139+ public void visitErrorElement (@ NotNull PsiErrorElement ignore ) {
140+ }
141+
142+ @ Override
143+ public void visitOuterLanguageElement (@ NotNull OuterLanguageElement ignore ) {
144+ }
145+ }
125146}
0 commit comments