private static void ExportUserFormModule(VBComponent component, string path) { // VBIDE API inserts an extra newline when exporting a UserForm module. // this issue causes forms to always be treated as "modified" in source control, which causes conflicts. // we need to remove the extra newline before the file gets written to its output location. var visibleCode = component.CodeModule.Lines().Split(new [] { Environment.NewLine }, StringSplitOptions.None); var legitEmptyLineCount = visibleCode.TakeWhile(string.IsNullOrWhiteSpace).Count(); var tempFile = component.ExportToTempFile(); var contents = File.ReadAllLines(tempFile); var nonAttributeLines = contents.TakeWhile(line => !line.StartsWith("Attribute")).Count(); var attributeLines = contents.Skip(nonAttributeLines).TakeWhile(line => line.StartsWith("Attribute")).Count(); var declarationsStartLine = nonAttributeLines + attributeLines + 1; var emptyLineCount = contents.Skip(declarationsStartLine - 1) .TakeWhile(string.IsNullOrWhiteSpace) .Count(); var code = contents; if (emptyLineCount > legitEmptyLineCount) { code = contents.Take(declarationsStartLine).Union( contents.Skip(declarationsStartLine + emptyLineCount - legitEmptyLineCount)) .ToArray(); } File.WriteAllLines(path, code); }
private static void ExportUserFormModule(VBComponent component, string path) { // VBIDE API inserts an extra newline when exporting a UserForm module. // this issue causes forms to always be treated as "modified" in source control, which causes conflicts. // we need to remove the extra newline before the file gets written to its output location. var visibleCode = component.CodeModule.Lines().Split(new []{Environment.NewLine}, StringSplitOptions.None); var legitEmptyLineCount = visibleCode.TakeWhile(string.IsNullOrWhiteSpace).Count(); var tempFile = component.ExportToTempFile(); var contents = File.ReadAllLines(tempFile); var nonAttributeLines = contents.TakeWhile(line => !line.StartsWith("Attribute")).Count(); var attributeLines = contents.Skip(nonAttributeLines).TakeWhile(line => line.StartsWith("Attribute")).Count(); var declarationsStartLine = nonAttributeLines + attributeLines + 1; var emptyLineCount = contents.Skip(declarationsStartLine - 1) .TakeWhile(string.IsNullOrWhiteSpace) .Count(); var code = contents; if (emptyLineCount > legitEmptyLineCount) { code = contents.Take(declarationsStartLine).Union( contents.Skip(declarationsStartLine + emptyLineCount - legitEmptyLineCount)) .ToArray(); } File.WriteAllLines(path, code); }