Skip to content

Commit 458d2be

Browse files
committed
fixes #1216 (bug is actually at import - left export check in place though)
1 parent 86e19e4 commit 458d2be

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

Rubberduck.VBEEditor/Extensions/VBComponentsExtensions.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using Microsoft.Vbe.Interop;
45

56
// todo: untangle this mess
@@ -36,26 +37,42 @@ public static void RemoveSafely(this VBComponents components, VBComponent compon
3637
public static void ImportSourceFile(this VBComponents components, string filePath)
3738
{
3839
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+
}
4245

46+
var codeString = File.ReadAllText(filePath);
47+
var codeLines = codeString.Split(new []{Environment.NewLine}, StringSplitOptions.None);
4348
if (ext == VBComponentExtensions.DocClassExtension)
4449
{
45-
var component = components.Item(fileName);
50+
var component = components.Item(name);
4651
if (component != null)
4752
{
4853
component.CodeModule.Clear();
49-
50-
var text = File.ReadAllText(filePath);
51-
component.CodeModule.AddFromString(text);
54+
component.CodeModule.AddFromString(codeString);
5255
}
53-
5456
}
5557
else if(ext != VBComponentExtensions.FormBinaryExtension)
5658
{
5759
components.Import(filePath);
5860
}
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+
}
5976
}
6077
}
6178
}

0 commit comments

Comments
 (0)