|
| 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 | + public interface IParserState |
| 13 | + { |
| 14 | + void Initialize(VBE vbe); |
| 15 | + |
| 16 | + void Parse(); |
| 17 | + void BeginParse(); |
| 18 | + |
| 19 | + Declaration[] AllDeclarations { get; } |
| 20 | + Declaration[] UserDeclarations { get; } |
| 21 | + } |
| 22 | + |
| 23 | + [ComVisible(true)] |
| 24 | + [Guid("3D8EAA28-8983-44D5-83AF-2EEC4C363079")] |
| 25 | + [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] |
| 26 | + public interface IParserStateEvents |
| 27 | + { |
| 28 | + void OnParsed(); |
| 29 | + void OnReady(); |
| 30 | + void OnError(); |
| 31 | + } |
| 32 | + |
| 33 | + [ComVisible(true)] |
| 34 | + [Guid(ClassId)] |
| 35 | + [ProgId(ProgId)] |
| 36 | + [ClassInterface(ClassInterfaceType.AutoDual)] |
| 37 | + [ComDefaultInterface(typeof(IParserState))] |
| 38 | + [ComSourceInterfaces(typeof(IParserStateEvents))] |
| 39 | + [EditorBrowsable(EditorBrowsableState.Always)] |
| 40 | + public class ParserState : IParserState |
| 41 | + { |
| 42 | + private const string ClassId = "28754D11-10CC-45FD-9F6A-525A65412B7A"; |
| 43 | + private const string ProgId = "Rubberduck.ParserState"; |
| 44 | + |
| 45 | + private readonly RubberduckParserState _state; |
| 46 | + private readonly AttributeParser _attributeParser; |
| 47 | + |
| 48 | + private RubberduckParser _parser; |
| 49 | + |
| 50 | + public ParserState() |
| 51 | + { |
| 52 | + _state = new RubberduckParserState(); |
| 53 | + _attributeParser = new AttributeParser(new ModuleExporter()); |
| 54 | + |
| 55 | + _state.StateChanged += _state_StateChanged; |
| 56 | + } |
| 57 | + |
| 58 | + public void Initialize(VBE vbe) |
| 59 | + { |
| 60 | + if (_parser != null) |
| 61 | + { |
| 62 | + throw new InvalidOperationException("ParserState is already initialized."); |
| 63 | + } |
| 64 | + |
| 65 | + _parser = new RubberduckParser(vbe, _state, _attributeParser); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Blocking call, for easier unit-test code |
| 70 | + /// </summary> |
| 71 | + public void Parse() |
| 72 | + { |
| 73 | + // blocking call |
| 74 | + _parser.Parse(); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Begins asynchronous parsing |
| 79 | + /// </summary> |
| 80 | + public void BeginParse() |
| 81 | + { |
| 82 | + // non-blocking call |
| 83 | + _state.OnParseRequested(this); |
| 84 | + } |
| 85 | + |
| 86 | + public event Action OnParsed; |
| 87 | + public event Action OnReady; |
| 88 | + public event Action OnError; |
| 89 | + |
| 90 | + private void _state_StateChanged(object sender, System.EventArgs e) |
| 91 | + { |
| 92 | + _allDeclarations = _state.AllDeclarations |
| 93 | + .Select(item => new Declaration(item)) |
| 94 | + .ToArray(); |
| 95 | + |
| 96 | + _userDeclarations = _state.AllUserDeclarations |
| 97 | + .Select(item => new Declaration(item)) |
| 98 | + .ToArray(); |
| 99 | + |
| 100 | + var errorHandler = OnError; |
| 101 | + if (_state.Status == Parsing.VBA.ParserState.Error && errorHandler != null) |
| 102 | + { |
| 103 | + errorHandler.Invoke(); |
| 104 | + } |
| 105 | + |
| 106 | + var parsedHandler = OnParsed; |
| 107 | + if (_state.Status == Parsing.VBA.ParserState.Parsed && parsedHandler != null) |
| 108 | + { |
| 109 | + parsedHandler.Invoke(); |
| 110 | + } |
| 111 | + |
| 112 | + var readyHandler = OnReady; |
| 113 | + if (_state.Status == Parsing.VBA.ParserState.Ready && readyHandler != null) |
| 114 | + { |
| 115 | + readyHandler.Invoke(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private Declaration[] _allDeclarations; |
| 120 | + |
| 121 | + public Declaration[] AllDeclarations |
| 122 | + { |
| 123 | + [return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)] |
| 124 | + get { return _allDeclarations; } |
| 125 | + } |
| 126 | + |
| 127 | + private Declaration[] _userDeclarations; |
| 128 | + public Declaration[] UserDeclarations |
| 129 | + { |
| 130 | + [return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)] |
| 131 | + get { return _userDeclarations; } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments