Skip to content

Commit 000d6be

Browse files
committed
Merge pull request #1238 from retailcoder/API
A quick and under-featured COM API for ParserState
2 parents 48a72ef + 60d2a21 commit 000d6be

File tree

12 files changed

+400
-18
lines changed

12 files changed

+400
-18
lines changed

RetailCoder.VBE/API/Accessibility.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace Rubberduck.API
4+
{
5+
[ComVisible(true)]
6+
public enum Accessibility
7+
{
8+
Implicit,
9+
Private,
10+
Public,
11+
Global,
12+
Friend,
13+
Static,
14+
}
15+
}

RetailCoder.VBE/API/Declaration.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.ComponentModel;
2+
using System.Linq;
3+
using System.Runtime.InteropServices;
4+
using RubberduckDeclaration = Rubberduck.Parsing.Symbols.Declaration;
5+
6+
namespace Rubberduck.API
7+
{
8+
[ComVisible(true)]
9+
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
10+
public interface IDeclaration
11+
{
12+
string Name { get; }
13+
Accessibility Accessibility { get; }
14+
DeclarationType DeclarationType { get; }
15+
string TypeName { get; }
16+
bool IsArray { get; }
17+
18+
Declaration ParentDeclaration { get; }
19+
20+
IdentifierReference[] References { get; }
21+
}
22+
23+
[ComVisible(true)]
24+
[Guid(ClassId)]
25+
[ProgId(ProgId)]
26+
[ComDefaultInterface(typeof(IDeclaration))]
27+
[EditorBrowsable(EditorBrowsableState.Always)]
28+
public class Declaration : IDeclaration
29+
{
30+
private const string ClassId = "67940D0B-081A-45BE-B0B9-CAEAFE034BC0";
31+
private const string ProgId = "Rubberduck.Declaration";
32+
33+
private readonly RubberduckDeclaration _declaration;
34+
35+
internal Declaration(RubberduckDeclaration declaration)
36+
{
37+
_declaration = declaration;
38+
}
39+
40+
protected RubberduckDeclaration Instance { get { return _declaration; } }
41+
42+
public string Name { get { return _declaration.IdentifierName; } }
43+
public Accessibility Accessibility { get { return (Accessibility)_declaration.Accessibility; } }
44+
public DeclarationType DeclarationType {get { return (DeclarationType)_declaration.DeclarationType; }}
45+
public string TypeName { get { return _declaration.AsTypeName; } }
46+
public bool IsArray { get { return _declaration.IsArray(); } }
47+
48+
private Declaration _parentDeclaration;
49+
public Declaration ParentDeclaration
50+
{
51+
get
52+
{
53+
return _parentDeclaration ?? (_parentDeclaration = new Declaration(Instance));
54+
}
55+
}
56+
57+
private IdentifierReference[] _references;
58+
public IdentifierReference[] References
59+
{
60+
get
61+
{
62+
return _references ?? (_references = _declaration.References.Select(item => new IdentifierReference(item)).ToArray());
63+
}
64+
}
65+
}
66+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace Rubberduck.API
5+
{
6+
[ComVisible(true)]
7+
[Flags]
8+
public enum DeclarationType
9+
{
10+
Project = 1 << 0,
11+
Module = 1 << 1,
12+
Class = 1 << 2,
13+
Control = 1 << 3,
14+
UserForm = 1 << 4,
15+
Document = 1 << 5,
16+
ModuleOption = 1 << 6,
17+
Member = 1 << 7,
18+
Procedure = 1 << 8 | Member,
19+
Function = 1 << 9 | Member,
20+
Property = 1 << 10 | Member,
21+
PropertyGet = 1 << 11 | Property | Function,
22+
PropertyLet = 1 << 12 | Property | Procedure,
23+
PropertySet = 1 << 13 | Property | Procedure,
24+
Parameter = 1 << 14,
25+
Variable = 1 << 15,
26+
Constant = 1 << 16,
27+
Enumeration = 1 << 17,
28+
EnumerationMember = 1 << 18 | Constant,
29+
Event = 1 << 19,
30+
UserDefinedType = 1 << 20,
31+
UserDefinedTypeMember = 1 << 21 | Variable,
32+
LibraryFunction = 1 << 22 | Function,
33+
LibraryProcedure = 1 << 23 | Procedure,
34+
LineLabel = 1 << 24
35+
}
36+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+

2+
using System.ComponentModel;
3+
using System.Runtime.InteropServices;
4+
5+
namespace Rubberduck.API
6+
{
7+
[ComVisible(true)]
8+
public interface IIdentifierReference
9+
{
10+
Declaration Declaration { get; }
11+
Declaration ParentScope { get; }
12+
Declaration ParentNonScoping { get; }
13+
int StartLine { get; }
14+
int StartColumn { get; }
15+
int EndLine { get; }
16+
int EndColumn { get; }
17+
}
18+
19+
[ComVisible(true)]
20+
[Guid(ClassId)]
21+
[ProgId(ProgId)]
22+
[ComDefaultInterface(typeof(IIdentifierReference))]
23+
[EditorBrowsable(EditorBrowsableState.Always)]
24+
public class IdentifierReference : IIdentifierReference
25+
{
26+
private const string ClassId = "57F78E64-8ADF-4D81-A467-A0139B877D14";
27+
private const string ProgId = "Rubberduck.IdentifierReference";
28+
29+
private readonly Parsing.Symbols.IdentifierReference _reference;
30+
31+
public IdentifierReference(Parsing.Symbols.IdentifierReference reference)
32+
{
33+
_reference = reference;
34+
}
35+
36+
private Declaration _declaration;
37+
public Declaration Declaration
38+
{
39+
get { return _declaration ?? (_declaration = new Declaration(_reference.Declaration)); }
40+
}
41+
42+
private Declaration _parentScoping;
43+
public Declaration ParentScope
44+
{
45+
get { return _parentScoping ?? (_parentScoping = new Declaration(_reference.ParentScoping)); }
46+
}
47+
48+
private Declaration _parentNonScoping;
49+
public Declaration ParentNonScoping
50+
{
51+
get { return _parentNonScoping ?? (_parentNonScoping = new Declaration(_reference.ParentNonScoping)); }
52+
}
53+
54+
public int StartLine { get { return _reference.Selection.StartLine; } }
55+
public int EndLine { get { return _reference.Selection.EndLine; } }
56+
public int StartColumn { get { return _reference.Selection.StartColumn; } }
57+
public int EndColumn { get { return _reference.Selection.EndColumn; } }
58+
}
59+
}

RetailCoder.VBE/API/ParserState.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
using Microsoft.Vbe.Interop;
6+
using Rubberduck.Common;
7+
using Rubberduck.Parsing.VBA;
8+
9+
namespace Rubberduck.API
10+
{
11+
[ComVisible(true)]
12+
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
13+
public interface IParserState
14+
{
15+
void Initialize(VBE vbe);
16+
17+
void Parse();
18+
void BeginParse();
19+
20+
Declaration[] AllDeclarations
21+
{
22+
get;
23+
}
24+
25+
Declaration[] UserDeclarations
26+
{
27+
get;
28+
}
29+
}
30+
31+
[ComVisible(true)]
32+
[Guid(ClassId)]
33+
[ProgId(ProgId)]
34+
[ComDefaultInterface(typeof(IParserState))]
35+
[EditorBrowsable(EditorBrowsableState.Always)]
36+
public class ParserState : IParserState
37+
{
38+
private const string ClassId = "28754D11-10CC-45FD-9F6A-525A65412B7A";
39+
private const string ProgId = "Rubberduck.ParserState";
40+
41+
private readonly RubberduckParserState _state;
42+
private readonly AttributeParser _attributeParser;
43+
44+
private RubberduckParser _parser;
45+
46+
public ParserState()
47+
{
48+
_state = new RubberduckParserState();
49+
_attributeParser = new AttributeParser(new ModuleExporter());
50+
51+
_state.StateChanged += _state_StateChanged;
52+
}
53+
54+
public void Initialize(VBE vbe)
55+
{
56+
if (_parser != null)
57+
{
58+
throw new InvalidOperationException("ParserState is already initialized.");
59+
}
60+
61+
_parser = new RubberduckParser(vbe, _state, _attributeParser);
62+
}
63+
64+
public void Parse()
65+
{
66+
// blocking call
67+
_parser.Parse();
68+
}
69+
70+
public void BeginParse()
71+
{
72+
// non-blocking call
73+
_state.OnParseRequested(this);
74+
}
75+
76+
private void _state_StateChanged(object sender, System.EventArgs e)
77+
{
78+
_allDeclarations = _state.AllDeclarations
79+
.Select(item => new Declaration(item))
80+
.ToArray();
81+
82+
_userDeclarations = _state.AllUserDeclarations
83+
.Select(item => new Declaration(item))
84+
.ToArray();
85+
}
86+
87+
private Declaration[] _allDeclarations;
88+
89+
public Declaration[] AllDeclarations
90+
{
91+
[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)]
92+
get { return _allDeclarations; }
93+
}
94+
95+
private Declaration[] _userDeclarations;
96+
public Declaration[] UserDeclarations
97+
{
98+
[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)]
99+
get { return _userDeclarations; }
100+
}
101+
}
102+
}

RetailCoder.VBE/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[assembly: AssemblyConfiguration("")]
1010
[assembly: AssemblyCompany("Rubberduck")]
1111
[assembly: AssemblyProduct("Rubberduck")]
12-
[assembly: AssemblyCopyright("Copyright © 2014")]
12+
[assembly: AssemblyCopyright("Copyright © 2014-2016")]
1313
[assembly: AssemblyTrademark("")]
1414
[assembly: AssemblyCulture("")]
1515

@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.4.3.*")]
35-
[assembly: AssemblyFileVersion("1.4.3.0")]
34+
[assembly: AssemblyVersion("2.0.0.*")]
35+
[assembly: AssemblyFileVersion("2.0.0.0")]

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@
275275
</Reference>
276276
</ItemGroup>
277277
<ItemGroup>
278+
<Compile Include="API\Accessibility.cs" />
279+
<Compile Include="API\Declaration.cs" />
280+
<Compile Include="API\DeclarationType.cs" />
281+
<Compile Include="API\IdentifierReference.cs" />
282+
<Compile Include="API\ParserState.cs" />
278283
<Compile Include="AppMenu.cs" />
279284
<Compile Include="AutoSave\AutoSave.cs" />
280285
<Compile Include="Common\ExportFormatter.cs" />

Rubberduck.Parsing/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[assembly: AssemblyConfiguration("")]
1010
[assembly: AssemblyCompany("Rubberduck")]
1111
[assembly: AssemblyProduct("Rubberduck.Parsing")]
12-
[assembly: AssemblyCopyright("Copyright © 2015")]
12+
[assembly: AssemblyCopyright("Copyright © 2015-2016")]
1313
[assembly: AssemblyTrademark("")]
1414
[assembly: AssemblyCulture("")]
1515

@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyFileVersion("1.0.0.0")]
34+
[assembly: AssemblyVersion("2.0.*")]
35+
[assembly: AssemblyFileVersion("2.0.0.0")]

Rubberduck.Parsing/Symbols/Declaration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public Declaration(QualifiedMemberName qualifiedName, Declaration parentDeclarat
2727
accessibility, declarationType, context, selection, isBuiltIn, annotations, attributes)
2828
{
2929
_parentScopeDeclaration = parentScope;
30-
}
30+
}
3131

3232
public Declaration(QualifiedMemberName qualifiedName, Declaration parentDeclaration, string parentScope,
3333
string asTypeName, bool isSelfAssigned, bool isWithEvents,
@@ -44,7 +44,7 @@ public Declaration(QualifiedMemberName qualifiedName, Declaration parentDeclarat
4444
_parentScope = parentScope ?? string.Empty;
4545
_identifierName = qualifiedName.MemberName;
4646
_asTypeName = asTypeName;
47-
_isSelfAssigned = isSelfAssigned;
47+
_isSelfAssigned = isSelfAssigned || (declarationType == DeclarationType.Variable && parentDeclaration != null && parentDeclaration.IdentifierName == ComponentName);
4848
_isWithEvents = isWithEvents;
4949
_accessibility = accessibility;
5050
_declarationType = declarationType;

0 commit comments

Comments
 (0)