|
1 | 1 | using System;
|
2 | 2 | using System.IO;
|
| 3 | +using System.Linq; |
3 | 4 | using Microsoft.Vbe.Interop;
|
4 | 5 |
|
5 | 6 | // todo: untangle this mess
|
@@ -36,26 +37,42 @@ public static void RemoveSafely(this VBComponents components, VBComponent compon
|
36 | 37 | public static void ImportSourceFile(this VBComponents components, string filePath)
|
37 | 38 | {
|
38 | 39 | var ext = Path.GetExtension(filePath);
|
39 |
| - var fileName = Path.GetFileNameWithoutExtension(filePath); |
40 |
| - |
41 |
| - if (!File.Exists(filePath)) { return; } |
| 40 | + var name = Path.GetFileNameWithoutExtension(filePath); |
| 41 | + if (!File.Exists(filePath)) |
| 42 | + { |
| 43 | + return; |
| 44 | + } |
42 | 45 |
|
| 46 | + var codeString = File.ReadAllText(filePath); |
| 47 | + var codeLines = codeString.Split(new []{Environment.NewLine}, StringSplitOptions.None); |
43 | 48 | if (ext == VBComponentExtensions.DocClassExtension)
|
44 | 49 | {
|
45 |
| - var component = components.Item(fileName); |
| 50 | + var component = components.Item(name); |
46 | 51 | if (component != null)
|
47 | 52 | {
|
48 | 53 | component.CodeModule.Clear();
|
49 |
| - |
50 |
| - var text = File.ReadAllText(filePath); |
51 |
| - component.CodeModule.AddFromString(text); |
| 54 | + component.CodeModule.AddFromString(codeString); |
52 | 55 | }
|
53 |
| - |
54 | 56 | }
|
55 | 57 | else if(ext != VBComponentExtensions.FormBinaryExtension)
|
56 | 58 | {
|
57 | 59 | components.Import(filePath);
|
58 | 60 | }
|
| 61 | + |
| 62 | + if (ext == VBComponentExtensions.FormExtension) |
| 63 | + { |
| 64 | + var component = components.Item(name); |
| 65 | + // note: vbeCode contains an extraneous line here: |
| 66 | + //var vbeCode = component.CodeModule.Lines().Split(new []{Environment.NewLine}, StringSplitOptions.None); |
| 67 | + |
| 68 | + var nonAttributeLines = codeLines.TakeWhile(line => !line.StartsWith("Attribute")).Count(); |
| 69 | + var attributeLines = codeLines.Skip(nonAttributeLines).TakeWhile(line => line.StartsWith("Attribute")).Count(); |
| 70 | + var declarationsStartLine = nonAttributeLines + attributeLines + 1; |
| 71 | + var correctCodeString = string.Join(Environment.NewLine, codeLines.Skip(declarationsStartLine - 1).ToArray()); |
| 72 | + |
| 73 | + component.CodeModule.Clear(); |
| 74 | + component.CodeModule.AddFromString(correctCodeString); |
| 75 | + } |
59 | 76 | }
|
60 | 77 | }
|
61 | 78 | }
|
0 commit comments