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); }
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)); } }
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); }
public ParseErrorEventArgs(SyntaxErrorException exception, IVBComponent component) { _exception = exception; _component = component; }
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)); }
public string Export(IVBComponent component, bool tempFile = true) { TempFile = tempFile; return(component.ExportAsSourceFile(ExportPath, tempFile)); }
public void SetModuleAnnotations(IVBComponent component, IEnumerable <IAnnotation> annotations) { _moduleStates[new QualifiedModuleName(component)].SetAnnotations(new List <IAnnotation>(annotations)); }
internal IDictionary <Tuple <string, DeclarationType>, Attributes> GetModuleAttributes(IVBComponent vbComponent) { return(_moduleStates[new QualifiedModuleName(vbComponent)].ModuleAttributes); }
internal void SetModuleAttributes(IVBComponent component, IDictionary <Tuple <string, DeclarationType>, Attributes> attributes) { _moduleStates.AddOrUpdate(new QualifiedModuleName(component), new ModuleState(attributes), (c, s) => s.SetModuleAttributes(attributes)); }
public void SetModuleComments(IVBComponent component, IEnumerable <CommentNode> comments) { _moduleStates[new QualifiedModuleName(component)].SetComments(new List <CommentNode>(comments)); }
public ParserState GetModuleState(IVBComponent component) { return(_moduleStates.GetOrAdd(new QualifiedModuleName(component), new ModuleState(ParserState.Pending)).State); }
public bool IsNewOrModified(IVBComponent component) { var key = new QualifiedModuleName(component); return(IsNewOrModified(key)); }
public static int GetContentHash(IVBComponent component) { return(component?.ContentHash() ?? 0); }
public void Remove(IVBComponent item) { _components.Remove(_components.First(m => m == item)); }
public bool ClearStateCache(IVBComponent component, bool notifyStateChanged = false) { return(component != null && ClearStateCache(new QualifiedModuleName(component), notifyStateChanged)); }
public bool Equals(IVBComponent other) { throw new NotImplementedException(); }
public void AddTokenStream(IVBComponent component, ITokenStream stream) { _moduleStates[new QualifiedModuleName(component)].SetTokenStream(stream); }
public string GetNewTestMethodCodeErrorExpected(IVBComponent component) { return(string.Join(Environment.NewLine, _indenter.Indent(string.Format(TestMethodExpectedErrorTemplate, GetNextTestMethodName(component))))); }
public IParseTree GetParseTree(IVBComponent component) { return(_moduleStates[new QualifiedModuleName(component)].ParseTree); }
public ComponentEventArgs(string projectId, IVBProject project, IVBComponent component) { ProjectId = projectId; Project = project; Component = component; }
public TokenStreamRewriter GetRewriter(IVBComponent component) { return(new TokenStreamRewriter(_moduleStates[new QualifiedModuleName(component)].TokenStream)); }
protected virtual IVBE TestVbe(string code, out IVBComponent component) { return(MockVbeBuilder.BuildFromSingleStandardModule(code, out component).Object); }
public ParseProgressEventArgs(IVBComponent component, ParserState state, ParserState oldState) { _component = component; _state = state; _oldState = oldState; }
protected override IVBE TestVbe(string code, out IVBComponent component) { return(TestVbe(code, "Sheet1", out component)); }
public void RemoveSafely(IVBComponent component) { throw new NotImplementedException(); }
public string SourceCode(IVBComponent module) { return(_tempSourceFileHandler.Read(module) ?? string.Empty); }
public IVBComponent Add(IVBComponent component) { _components.Add(component); return(component); }
/// <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)); }
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. } }