Skip to content

Commit 2dcf4c4

Browse files
authored
Merge pull request #3529 from MDoerner/BringUserFormExportEncodingInLineWithTheVBE
Fixes text encoding issues affecting module exports. May cause issue #3461 to reappear.
2 parents c0441e8 + 55839ae commit 2dcf4c4

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

RetailCoder.VBE/Common/ModuleExporter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.IO;
1+
using System.IO;
32
using System.Reflection;
43
using Rubberduck.Parsing.VBA;
54
using Rubberduck.VBEditor.SafeComWrappers.Abstract;

Rubberduck.Parsing/VBA/AttributeParser.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.Text;
78
using System.Threading;
89
using Rubberduck.Parsing.PreProcessing;
910
using Rubberduck.Parsing.Symbols.ParsingExceptions;
@@ -37,7 +38,17 @@ public AttributeParser(IModuleExporter exporter, Func<IVBAPreprocessor> preproce
3738
// a document component without any code wouldn't be exported (file would be empty anyway).
3839
return (null, null, new Dictionary<Tuple<string, DeclarationType>, Attributes>());
3940
}
40-
var code = File.ReadAllText(path);
41+
42+
string code;
43+
if (module.ComponentType == ComponentType.Document)
44+
{
45+
code = File.ReadAllText(path, Encoding.UTF8); //We export the code from Documents as UTF8.
46+
}
47+
else
48+
{
49+
code = File.ReadAllText(path, Encoding.Default); //The VBE exports encoded in the current ANSI codepage from the windows settings.
50+
}
51+
4152
try
4253
{
4354
File.Delete(path);

Rubberduck.VBEEditor/SafeComWrappers/VBA/VBComponent.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4+
using System.Text;
45
using Rubberduck.VBEditor.Extensions;
56
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
67
using Rubberduck.VBEditor.SafeComWrappers.Office.Core.Abstract;
@@ -114,7 +115,8 @@ private void ExportUserFormModule(string path)
114115

115116
var tempFile = ExportToTempFile();
116117
var tempFilePath = Directory.GetParent(tempFile).FullName;
117-
var contents = File.ReadAllLines(tempFile, System.Text.Encoding.UTF7);
118+
var fileEncoding = System.Text.Encoding.Default; //We use the current ANSI codepage because that is what the VBE does.
119+
var contents = File.ReadAllLines(tempFile, fileEncoding);
118120
var nonAttributeLines = contents.TakeWhile(line => !line.StartsWith("Attribute")).Count();
119121
var attributeLines = contents.Skip(nonAttributeLines).TakeWhile(line => line.StartsWith("Attribute")).Count();
120122
var declarationsStartLine = nonAttributeLines + attributeLines + 1;
@@ -130,7 +132,7 @@ private void ExportUserFormModule(string path)
130132
contents.Skip(declarationsStartLine + emptyLineCount - legitEmptyLineCount))
131133
.ToArray();
132134
}
133-
File.WriteAllLines(path, code);
135+
File.WriteAllLines(path, code, fileEncoding);
134136

135137
// LINQ hates this search, therefore, iterate the long way
136138
foreach (string line in contents)
@@ -165,8 +167,10 @@ private void ExportDocumentModule(string path)
165167
var lineCount = CodeModule.CountOfLines;
166168
if (lineCount > 0)
167169
{
170+
//One cannot reimport document modules as such in the VBE; so we simply export and import the contents of the code pane.
171+
//Because of this, it is OK, and actually preferable, to use the default UTF8 encoding.
168172
var text = CodeModule.GetLines(1, lineCount);
169-
File.WriteAllText(path, text);
173+
File.WriteAllText(path, text, Encoding.UTF8);
170174
}
171175
}
172176

Rubberduck.VBEEditor/SafeComWrappers/VBA/VBComponents.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Linq;
66
using System.Runtime.InteropServices;
7+
using System.Text;
78
using Rubberduck.VBEditor.Events;
89
using Rubberduck.VBEditor.Extensions;
910
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
@@ -108,8 +109,6 @@ public void ImportSourceFile(string path)
108109
return;
109110
}
110111

111-
var codeString = File.ReadAllText(path);
112-
var codeLines = codeString.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
113112
if (ext == ComponentTypeExtensions.DocClassExtension)
114113
{
115114
try
@@ -123,6 +122,8 @@ public void ImportSourceFile(string path)
123122

124123
var component = this[name];
125124
component.CodeModule.Clear();
125+
126+
var codeString = File.ReadAllText(path, Encoding.UTF8);
126127
component.CodeModule.AddFromString(codeString);
127128
}
128129
else if (ext == ComponentTypeExtensions.FormExtension)
@@ -138,6 +139,9 @@ public void ImportSourceFile(string path)
138139

139140
var component = this[name];
140141

142+
var codeString = File.ReadAllText(path, Encoding.Default); //The VBE uses the current ANSI codepage from the windows settings to export and import.
143+
var codeLines = codeString.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
144+
141145
var nonAttributeLines = codeLines.TakeWhile(line => !line.StartsWith("Attribute")).Count();
142146
var attributeLines = codeLines.Skip(nonAttributeLines).TakeWhile(line => line.StartsWith("Attribute")).Count();
143147
var declarationsStartLine = nonAttributeLines + attributeLines + 1;

0 commit comments

Comments
 (0)