Exemplo n.º 1
0
        public static bool TryGetComponent(this QualifiedModuleName moduleName, IVBE vbe, out IVBComponent component)
        {
            if (TryGetProject(moduleName, vbe, out var project))
            {
                using (project)
                    using (var components = project.VBComponents)
                    {
                        component = components[moduleName.ComponentName];
                        return(true);
                    }
            }

            component = null;
            return(false);
        }
Exemplo n.º 2
0
        public IVBComponent ImportSourceFile(string path)
        {
            if (IsWrappingNullReference)
            {
                return(null);
            }

            var ext = _fileSystem.Path.GetExtension(path);

            if (!_fileSystem.File.Exists(path))
            {
                return(null);
            }

            switch (ext)
            {
            case ComponentTypeExtensions.FormBinaryExtension:
                return(null);

            case ComponentTypeExtensions.DocClassExtension:
            {
                var          name      = _moduleNameFromFileExtractor.ModuleName(path);
                IVBComponent component = null;
                try
                {
                    component = this[name];
                }
                catch
                {
                    throw new IndexOutOfRangeException($"Could not find document component named '{name}'.  Try adding a document component with the same name and try again.");
                }

                var codeString = _fileSystem.File.ReadAllText(path, Encoding.UTF8);
                using (var codeModule = component.CodeModule)
                {
                    codeModule.Clear();
                    codeModule.AddFromString(codeString);
                }

                return(component);
            }

            case ComponentTypeExtensions.FormExtension:
            {
                var          name      = _moduleNameFromFileExtractor.ModuleName(path);
                IVBComponent component = null;
                try
                {
                    component = this[name];
                }
                catch
                {
                    component = Import(path);
                }

                var codeString =
                    _fileSystem.File.ReadAllText(path,
                                                 Encoding
                                                 .Default); //The VBE uses the current ANSI codepage from the windows settings to export and import.
                var codeLines = codeString.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                var nonAttributeLines = codeLines.TakeWhile(line => !line.StartsWith("Attribute")).Count();
                var attributeLines    = codeLines.Skip(nonAttributeLines)
                                        .TakeWhile(line => line.StartsWith("Attribute")).Count();
                var declarationsStartLine = nonAttributeLines + attributeLines + 1;
                var correctCodeString     = string.Join(Environment.NewLine,
                                                        codeLines.Skip(declarationsStartLine - 1).ToArray());

                using (var codeModule = component.CodeModule)
                {
                    codeModule.Clear();
                    codeModule.AddFromString(correctCodeString);
                }

                return(component);
            }

            default:
                return(Import(path));
            }
        }
Exemplo n.º 3
0
        public static Mock <IVBE> BuildFromSingleModule(string content, string name, ComponentType type, out IVBComponent component, Selection selection = default, bool referenceStdLibs = false)
        {
            var vbeBuilder = new MockVbeBuilder();

            var builder = vbeBuilder.ProjectBuilder(TestProjectName, ProjectProtection.Unprotected);

            builder.AddComponent(name, type, content, selection);

            if (referenceStdLibs)
            {
                builder.AddReference("VBA", LibraryPathVBA, 4, 1, true);
            }

            var project = builder.Build();
            var vbe     = vbeBuilder.AddProject(project).Build();

            component = project.Object.VBComponents[0];

            vbe.Object.ActiveVBProject = project.Object;
            vbe.Object.ActiveCodePane  = component.CodeModule.CodePane;

            return(vbe);
        }
Exemplo n.º 4
0
 public ParseErrorEventArgs(SyntaxErrorException exception, IVBComponent component)
 {
     _exception = exception;
     _component = component;
 }
Exemplo n.º 5
0
 public bool Equals(IVBComponent other)
 {
     return(Equals(other as SafeComWrapper <VB.VBComponent>));
 }
 private Mock <IVBE> BuildMockVBE(string inputCode, out IVBComponent component)
 {
     return(MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component));
 }
Exemplo n.º 7
0
 public string Export(IVBComponent component, bool tempFile = true)
 {
     TempFile = tempFile;
     return(component.ExportAsSourceFile(ExportPath, tempFile));
 }
Exemplo n.º 8
0
 public void SetModuleAnnotations(IVBComponent component, IEnumerable <IAnnotation> annotations)
 {
     _moduleStates[new QualifiedModuleName(component)].SetAnnotations(new List <IAnnotation>(annotations));
 }
Exemplo n.º 9
0
 internal IDictionary <Tuple <string, DeclarationType>, Attributes> GetModuleAttributes(IVBComponent vbComponent)
 {
     return(_moduleStates[new QualifiedModuleName(vbComponent)].ModuleAttributes);
 }
Exemplo n.º 10
0
 internal void SetModuleAttributes(IVBComponent component, IDictionary <Tuple <string, DeclarationType>, Attributes> attributes)
 {
     _moduleStates.AddOrUpdate(new QualifiedModuleName(component), new ModuleState(attributes), (c, s) => s.SetModuleAttributes(attributes));
 }
Exemplo n.º 11
0
 public void SetModuleComments(IVBComponent component, IEnumerable <CommentNode> comments)
 {
     _moduleStates[new QualifiedModuleName(component)].SetComments(new List <CommentNode>(comments));
 }
Exemplo n.º 12
0
 public ParserState GetModuleState(IVBComponent component)
 {
     return(_moduleStates.GetOrAdd(new QualifiedModuleName(component), new ModuleState(ParserState.Pending)).State);
 }
Exemplo n.º 13
0
        public bool IsNewOrModified(IVBComponent component)
        {
            var key = new QualifiedModuleName(component);

            return(IsNewOrModified(key));
        }
Exemplo n.º 14
0
 public static int GetContentHash(IVBComponent component)
 {
     return(component?.ContentHash() ?? 0);
 }
Exemplo n.º 15
0
 public void Remove(IVBComponent item)
 {
     _components.Remove(_components.First(m => m == item));
 }
Exemplo n.º 16
0
 public bool ClearStateCache(IVBComponent component, bool notifyStateChanged = false)
 {
     return(component != null && ClearStateCache(new QualifiedModuleName(component), notifyStateChanged));
 }
Exemplo n.º 17
0
 public bool Equals(IVBComponent other)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 18
0
 public void AddTokenStream(IVBComponent component, ITokenStream stream)
 {
     _moduleStates[new QualifiedModuleName(component)].SetTokenStream(stream);
 }
Exemplo n.º 19
0
 public string GetNewTestMethodCodeErrorExpected(IVBComponent component)
 {
     return(string.Join(Environment.NewLine,
                        _indenter.Indent(string.Format(TestMethodExpectedErrorTemplate, GetNextTestMethodName(component)))));
 }
Exemplo n.º 20
0
 public IParseTree GetParseTree(IVBComponent component)
 {
     return(_moduleStates[new QualifiedModuleName(component)].ParseTree);
 }
Exemplo n.º 21
0
 public ComponentEventArgs(string projectId, IVBProject project, IVBComponent component)
 {
     ProjectId = projectId;
     Project   = project;
     Component = component;
 }
Exemplo n.º 22
0
 public TokenStreamRewriter GetRewriter(IVBComponent component)
 {
     return(new TokenStreamRewriter(_moduleStates[new QualifiedModuleName(component)].TokenStream));
 }
Exemplo n.º 23
0
 protected virtual IVBE TestVbe(string code, out IVBComponent component)
 {
     return(MockVbeBuilder.BuildFromSingleStandardModule(code, out component).Object);
 }
Exemplo n.º 24
0
 public ParseProgressEventArgs(IVBComponent component, ParserState state, ParserState oldState)
 {
     _component = component;
     _state     = state;
     _oldState  = oldState;
 }
Exemplo n.º 25
0
 protected override IVBE TestVbe(string code, out IVBComponent component)
 {
     return(TestVbe(code, "Sheet1", out component));
 }
Exemplo n.º 26
0
 public void RemoveSafely(IVBComponent component)
 {
     throw new NotImplementedException();
 }
 public string SourceCode(IVBComponent module)
 {
     return(_tempSourceFileHandler.Read(module) ?? string.Empty);
 }
Exemplo n.º 28
0
 public IVBComponent Add(IVBComponent component)
 {
     _components.Add(component);
     return(component);
 }
Exemplo n.º 29
0
 /// <summary>
 /// Gets a mock <see cref="IVBE"/> instance,
 /// containing a single "TestProject1" <see cref="IVBProject"/>
 /// and a single "TestModule1" <see cref="IVBComponent"/>, with the specified <see cref="content"/>.
 /// </summary>
 /// <param name="content">The VBA code associated to the component.</param>
 /// <param name="component">The created <see cref="IVBComponent"/></param>
 /// <param name="selection">Specifies user selection in the editor.</param>
 /// <param name="referenceStdLibs">Specifies whether standard libraries are referenced.</param>
 /// <returns></returns>
 public static Mock <IVBE> BuildFromSingleStandardModule(string content, out IVBComponent component, Selection selection = default, bool referenceStdLibs = false)
 {
     return(BuildFromSingleModule(content, TestModuleName, ComponentType.StandardModule, out component, selection, referenceStdLibs));
 }
Exemplo n.º 30
0
        public void ImportSourceFile(string path)
        {
            if (IsWrappingNullReference)
            {
                return;
            }

            var ext  = Path.GetExtension(path);
            var name = Path.GetFileNameWithoutExtension(path);

            if (!File.Exists(path))
            {
                return;
            }

            if (ext == ComponentTypeExtensions.DocClassExtension)
            {
                IVBComponent component = null;
                try {
                    try
                    {
                        component = this[name];
                    }
                    catch
                    {
                        throw new IndexOutOfRangeException($"Could not find document component named '{name}'.  Try adding a document component with the same name and try again.");
                    }

                    var codeString = File.ReadAllText(path, Encoding.UTF8);
                    using (var codeModule = component.CodeModule)
                    {
                        codeModule.Clear();
                        codeModule.AddFromString(codeString);
                    }
                }
                finally
                {
                    component?.Dispose();
                }
            }
            else if (ext == ComponentTypeExtensions.FormExtension)
            {
                IVBComponent component = null;
                try
                {
                    try
                    {
                        component = this[name];
                    }
                    catch
                    {
                        component = Import(path);
                    }

                    var codeString =
                        File.ReadAllText(path,
                                         Encoding
                                         .Default); //The VBE uses the current ANSI codepage from the windows settings to export and import.
                    var codeLines = codeString.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                    var nonAttributeLines = codeLines.TakeWhile(line => !line.StartsWith("Attribute")).Count();
                    var attributeLines    = codeLines.Skip(nonAttributeLines)
                                            .TakeWhile(line => line.StartsWith("Attribute")).Count();
                    var declarationsStartLine = nonAttributeLines + attributeLines + 1;
                    var correctCodeString     = string.Join(Environment.NewLine,
                                                            codeLines.Skip(declarationsStartLine - 1).ToArray());

                    using (var codeModule = component.CodeModule)
                    {
                        codeModule.Clear();
                        codeModule.AddFromString(correctCodeString);
                    }
                }
                finally
                {
                    component?.Dispose();
                }
            }
            else if (ext != ComponentTypeExtensions.FormBinaryExtension)
            {
                using (Import(path)){} //Nothing to do here, except properly disposing the wrapper returned from Import.
            }
        }