Skip to content

Commit a0ac05c

Browse files
committed
Merge pull request #657 from rubberduck-vba/GrammarIsFun
Rebuilt identifier resolution
2 parents f94cb3d + 783ec08 commit a0ac05c

17 files changed

+2616
-2088
lines changed

RetailCoder.VBE/Inspections/ProcedureNotUsedInspection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private bool IsIgnoredDeclaration(Declarations declarations, Declaration declara
6161
{
6262
var enumerable = classes as IList<Declaration> ?? classes.ToList();
6363
var result = !ProcedureTypes.Contains(declaration.DeclarationType)
64-
|| declaration.References.Any()
64+
|| declaration.References.Any(r => !r.IsAssignment)
6565
|| handlers.Contains(declaration)
6666
|| IsPublicModuleMember(modules, declaration)
6767
|| IsClassLifeCycleHandler(enumerable, declaration)

RetailCoder.VBE/Refactorings/Rename/RenamePresenter.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,23 @@ private void OnOkButtonClicked(object sender, EventArgs e)
148148

149149
// must rename usages first; if target is a module or a project,
150150
// then renaming the declaration first would invalidate the parse results.
151-
RenameUsages(_view.Target);
151+
152+
if (_view.Target.DeclarationType.HasFlag(DeclarationType.Property))
153+
{
154+
// properties can have more than 1 member.
155+
var members = _declarations[_view.Target.IdentifierName]
156+
.Where(item => item.Project == _view.Target.Project
157+
&& item.ComponentName == _view.Target.ComponentName
158+
&& item.DeclarationType.HasFlag(DeclarationType.Property));
159+
foreach (var member in members)
160+
{
161+
RenameUsages(member);
162+
}
163+
}
164+
else
165+
{
166+
RenameUsages(_view.Target);
167+
}
152168

153169
if (ModuleDeclarationTypes.Contains(_view.Target.DeclarationType))
154170
{
@@ -228,10 +244,23 @@ private void RenameDeclaration()
228244
module.ReplaceLine(argList.Start.Line, newContent);
229245
module.DeleteLines(argList.Start.Line + 1, lineNum - 1);
230246
}
231-
else
247+
else if (!_view.Target.DeclarationType.HasFlag(DeclarationType.Property))
232248
{
233249
module.ReplaceLine(_view.Target.Selection.StartLine, newContent);
234250
}
251+
else
252+
{
253+
var members = _declarations[_view.Target.IdentifierName]
254+
.Where(item => item.Project == _view.Target.Project
255+
&& item.ComponentName == _view.Target.ComponentName
256+
&& item.DeclarationType.HasFlag(DeclarationType.Property));
257+
258+
foreach (var member in members)
259+
{
260+
newContent = GetReplacementLine(module, member, _view.NewName);
261+
module.ReplaceLine(member.Selection.StartLine, newContent);
262+
}
263+
}
235264
}
236265

237266
private void RenameControl()
@@ -354,23 +383,23 @@ private string GetReplacementLine(string content, string target, string newName,
354383

355384
private string GetReplacementLine(CodeModule module, Declaration target, string newName)
356385
{
357-
var targetModule = _parseResult.ComponentParseResults.SingleOrDefault(m => m.QualifiedName == _view.Target.QualifiedName.QualifiedModuleName);
386+
var targetModule = _parseResult.ComponentParseResults.SingleOrDefault(m => m.QualifiedName == target.QualifiedName.QualifiedModuleName);
358387
if (targetModule == null)
359388
{
360389
return null;
361390
}
362391

363-
var content = module.Lines[_view.Target.Selection.StartLine, 1];
392+
var content = module.Lines[target.Selection.StartLine, 1];
364393

365394
if (target.DeclarationType == DeclarationType.Parameter)
366395
{
367-
var argContext = (VBAParser.ArgContext)_view.Target.Context;
396+
var argContext = (VBAParser.ArgContext)target.Context;
368397
var rewriter = targetModule.GetRewriter();
369398
rewriter.Replace(argContext.ambiguousIdentifier().Start.TokenIndex, _view.NewName);
370399

371400
// Target.Context is an ArgContext, its parent is an ArgsListContext;
372401
// the ArgsListContext's parent is the procedure context and it includes the body.
373-
var context = (ParserRuleContext) _view.Target.Context.Parent.Parent;
402+
var context = (ParserRuleContext) target.Context.Parent.Parent;
374403
var firstTokenIndex = context.Start.TokenIndex;
375404
var lastTokenIndex = -1; // will blow up if this code runs for any context other than below
376405

RetailCoder.VBE/Settings/DisplayLanguageSetting.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public DisplayLanguageSetting(string code)
4646

4747
public override bool Equals(object obj)
4848
{
49-
var other = (DisplayLanguageSetting) obj;
50-
return Code.Equals(other.Code);
49+
var other = obj as DisplayLanguageSetting;
50+
return other != null && Code.Equals(other.Code);
5151
}
5252

5353
public override int GetHashCode()

RetailCoder.VBE/UI/RefactorMenu.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ public void FindAllReferences()
179179
var selection = IDE.ActiveCodePane.GetSelection();
180180
var progress = new ParsingProgressPresenter();
181181
var result = progress.Parse(_parser, IDE.ActiveVBProject);
182+
if (result == null)
183+
{
184+
return; // bug/todo: something's definitely wrong, exception thrown in resolver code
185+
}
182186

183187
var declarations = result.Declarations.Items
184188
.Where(item => item.DeclarationType != DeclarationType.ModuleOption)

Rubberduck.Parsing/Grammar/VBA.g4

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* - added support for numbered lines (amended lineLabel rule).
5353
* - added support for VBA 7.0 PtrSafe attribute for Declare statements.
5454
* - implemented a fileNumber rule to locate identifier usages in file numbers.
55+
* - added support for anonymous declarations in With blocks (With New Something)
5556
*
5657
*======================================================================================
5758
*
@@ -532,7 +533,7 @@ whileWendStmt :
532533
widthStmt : WIDTH WS valueStmt WS? ',' WS? valueStmt;
533534

534535
withStmt :
535-
WITH WS implicitCallStmt_InStmt NEWLINE+
536+
WITH WS (implicitCallStmt_InStmt | (NEW WS type)) NEWLINE+
536537
(block NEWLINE+)?
537538
END_WITH
538539
;
@@ -558,17 +559,17 @@ eCS_MemberProcedureCall : CALL WS implicitCallStmt_InStmt? '.' ambiguousIdentifi
558559

559560

560561
implicitCallStmt_InBlock :
561-
iCS_B_ProcedureCall
562-
| iCS_B_MemberProcedureCall
562+
iCS_B_MemberProcedureCall
563+
| iCS_B_ProcedureCall
563564
;
564565

566+
iCS_B_MemberProcedureCall : implicitCallStmt_InStmt? '.' ambiguousIdentifier typeHint? (WS argsCall)? dictionaryCallStmt?;
567+
565568
// parantheses are forbidden in case of args
566569
// variables cannot be called in blocks
567570
// certainIdentifier instead of ambiguousIdentifier for preventing ambiguity with statement keywords
568571
iCS_B_ProcedureCall : certainIdentifier (WS argsCall)?;
569572

570-
iCS_B_MemberProcedureCall : implicitCallStmt_InStmt? '.' ambiguousIdentifier typeHint? (WS argsCall)? dictionaryCallStmt?;
571-
572573

573574
// iCS_S_MembersCall first, so that member calls are not resolved as separate iCS_S_VariableOrProcedureCalls
574575
implicitCallStmt_InStmt :

Rubberduck.Parsing/Grammar/VBA.tokens

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
FUNCTION=78
2+
PROPERTY_LET=133
3+
BYREF=23
4+
DEFDBL=39
5+
MOD=111
6+
CONST=32
7+
DO=50
8+
COLORLITERAL=206
9+
NOT=115
10+
EXIT_PROPERTY=72
11+
KILL=90
12+
END_SELECT=59
13+
REDIM=143
14+
TYPE=171
15+
RPAREN=201
16+
LOOP=94
17+
BEGIN=18
18+
CHDIR=27
19+
RETURN=147
20+
IMP=84
21+
GET=79
22+
GEQ=189
23+
GOTO=82
24+
EQ=188
25+
SHARED=157
26+
DEFLNG=43
27+
PRESERVE=129
28+
GOSUB=81
29+
DEFVAR=47
30+
STATIC=160
31+
ELSE=53
32+
DOUBLELITERAL=208
33+
LOAD=91
34+
STOP=162
35+
DEFDEC=40
36+
SAVEPICTURE=150
37+
VARIANT=176
38+
PLUS_EQ=199
39+
DEFOBJ=44
40+
LIB=97
41+
MINUS_EQ=195
42+
OPTION_BASE=122
43+
WS=214
44+
MACRO_END_IF=107
45+
READ=141
46+
TYPEOF=172
47+
DEFDATE=38
48+
ALIAS=12
49+
WIDTH=180
50+
LOCK_READ_WRITE=102
51+
GT=190
52+
RMDIR=148
53+
CALL=25
54+
END=63
55+
END_SUB=60
56+
MACRO_ELSEIF=105
57+
UNLOCK=174
58+
ERASE=66
59+
FILECOPY=75
60+
INPUT=87
61+
STEP=161
62+
SUB=164
63+
VERSION=177
64+
RANDOM=138
65+
LPAREN=192
66+
DATELITERAL=205
67+
AS=17
68+
TIME=168
69+
THEN=167
70+
IMPLEMENTS=85
71+
END_TYPE=61
72+
LINE_CONTINUATION=211
73+
ACCESS=10
74+
LINE_INPUT=99
75+
PLUS=198
76+
EXIT_FUNCTION=71
77+
LIKE=98
78+
INTEGER=89
79+
OPTION_PRIVATE_MODULE=125
80+
DEFBYTE=37
81+
DEFBOOL=36
82+
TO=169
83+
MID=109
84+
AMPERSAND=185
85+
SET=155
86+
MINUS=194
87+
TEXT=166
88+
PRINT=130
89+
RESUME=146
90+
SETATTR=156
91+
DELETESETTING=48
92+
ENUM=64
93+
DATABASE=33
94+
CLOSE=30
95+
END_WITH=62
96+
STRINGLITERAL=204
97+
ADDRESSOF=11
98+
WITHEVENTS=182
99+
DIM=49
100+
END_IF=57
101+
END_PROPERTY=58
102+
DECLARE=35
103+
DEFSNG=45
104+
DIV=187
105+
LONG=93
106+
PUBLIC=136
107+
STRING=163
108+
WEND=178
109+
LT=193
110+
INTEGERLITERAL=207
111+
WHILE=179
112+
SPC=159
113+
RESET=145
114+
CASE=26
115+
NEW=114
116+
BYVAL=22
117+
ME=108
118+
MACRO_IF=104
119+
READ_WRITE=142
120+
BEEP=19
121+
T__8=1
122+
END_ENUM=55
123+
T__7=2
124+
PROPERTY_SET=134
125+
T__6=3
126+
T__5=4
127+
T__4=5
128+
OPTION_EXPLICIT=123
129+
NAME=112
130+
OPTION_COMPARE=124
131+
POW=200
132+
EXIT_SUB=73
133+
LOCK_READ=100
134+
FRIEND=76
135+
LSET=103
136+
DOUBLE=51
137+
EACH=52
138+
COMMENT=213
139+
L_SQUARE_BRACKET=202
140+
SELECT=153
141+
PRIVATE=131
142+
NULL=117
143+
ON=118
144+
MULT=196
145+
ERROR=67
146+
SENDKEYS=154
147+
EXIT_DO=69
148+
UNTIL=175
149+
OR=126
150+
FALSE=74
151+
OUTPUT=127
152+
APPEND=16
153+
CLASS=29
154+
ATTRIBUTE=14
155+
DEFCUR=41
156+
FOR=77
157+
PTRSAFE=135
158+
AND=13
159+
LOCK=92
160+
IF=83
161+
RSET=149
162+
APPACTIVATE=15
163+
BOOLEAN=21
164+
IN=86
165+
PARAMARRAY=128
166+
END_FUNCTION=56
167+
IS=88
168+
IDENTIFIER=210
169+
MACRO_ELSE=106
170+
NOTHING=116
171+
NEXT=113
172+
COLLECTION=31
173+
WITH=181
174+
RAISEEVENT=140
175+
BYTE=24
176+
XOR=184
177+
DEFSTR=46
178+
BYTELITERAL=209
179+
R_SQUARE_BRACKET=203
180+
TAB=165
181+
ON_ERROR=119
182+
PROPERTY_GET=132
183+
REM=144
184+
EVENT=68
185+
TRUE=170
186+
WRITE=183
187+
UNLOAD=173
188+
OPTIONAL=121
189+
ELSEIF=54
190+
SEEK=152
191+
OPEN=120
192+
NEQ=197
193+
NEWLINE=212
194+
MKDIR=110
195+
SINGLE=158
196+
T__1=8
197+
T__0=9
198+
T__3=6
199+
EXIT_FOR=70
200+
T__2=7
201+
DEFINT=42
202+
ASSIGN=186
203+
LEN=95
204+
EQV=65
205+
BINARY=20
206+
LOCK_WRITE=101
207+
GLOBAL=80
208+
CHDRIVE=28
209+
DATE=34
210+
LET=96
211+
SAVESETTING=151
212+
PUT=137
213+
RANDOMIZE=139
214+
LEQ=191
215+
'!'=8
216+
'#'=6
217+
'>='=189
218+
':='=186
219+
'>'=190
220+
';'=7
221+
'='=188
222+
'<>'=197
223+
'@'=5
224+
'+'=198
225+
')'=201
226+
'.'=2
227+
'^'=200
228+
'%'=1
229+
'$'=9
230+
'+='=199
231+
'<='=191
232+
'<'=193
233+
':'=4
234+
'('=192
235+
'['=202
236+
'-'=194
237+
'*'=196
238+
','=3
239+
'&'=185
240+
'-='=195
241+
']'=203

0 commit comments

Comments
 (0)