Skip to content

Commit 73efc8a

Browse files
committed
Merge pull request #1224 from retailcoder/next
SC fixes
2 parents 841646f + 458d2be commit 73efc8a

File tree

4 files changed

+108
-26
lines changed

4 files changed

+108
-26
lines changed

RetailCoder.VBE/UI/SourceControl/SourceControlViewViewModel.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private void InitRepo()
247247
var repo = _provider.InitVBAProject(folderPicker.SelectedPath);
248248
Provider = _providerFactory.CreateProvider(_vbe.ActiveVBProject, repo, _wrapperFactory);
249249

250-
AddRepoToConfig((Repository)repo);
250+
AddOrUpdateLocalPathConfig((Repository)repo);
251251
Status = RubberduckUI.Online;
252252
}
253253
}
@@ -267,10 +267,27 @@ private void SetChildPresenterSourceControlProviders(ISourceControlProvider prov
267267
}
268268
}
269269

270-
private void AddRepoToConfig(Repository repo)
270+
private void AddOrUpdateLocalPathConfig(Repository repo)
271271
{
272-
_config.Repositories.Add(repo);
273-
_configService.SaveConfiguration(_config);
272+
if (_config.Repositories.All(repository => repository.LocalLocation != repo.LocalLocation))
273+
{
274+
_config.Repositories.Add(repo);
275+
_configService.SaveConfiguration(_config);
276+
}
277+
else
278+
{
279+
var existing = _config.Repositories.Single(repository => repository.LocalLocation == repo.LocalLocation);
280+
if (string.IsNullOrEmpty(repo.RemoteLocation) && !string.IsNullOrEmpty(existing.RemoteLocation))
281+
{
282+
// config already has remote location and correct repository name - nothing to update
283+
return;
284+
}
285+
286+
existing.Name = repo.Name;
287+
existing.RemoteLocation = repo.RemoteLocation;
288+
289+
_configService.SaveConfiguration(_config);
290+
}
274291
}
275292

276293
private void OpenRepo()
@@ -295,7 +312,7 @@ private void OpenRepo()
295312
return;
296313
}
297314

298-
AddRepoToConfig(repo);
315+
AddOrUpdateLocalPathConfig(repo);
299316

300317
Status = RubberduckUI.Online;
301318
}
@@ -308,7 +325,7 @@ private void CloneRepo()
308325
_provider = _providerFactory.CreateProvider(_vbe.ActiveVBProject);
309326
var repo = _provider.Clone(RemotePath, LocalDirectory);
310327
Provider = _providerFactory.CreateProvider(_vbe.ActiveVBProject, repo, _wrapperFactory);
311-
AddRepoToConfig(new Repository
328+
AddOrUpdateLocalPathConfig(new Repository
312329
{
313330
Name = _vbe.ActiveVBProject.Name,
314331
LocalLocation = repo.LocalLocation,

Rubberduck.SourceControl/GitProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public override void Stage(IEnumerable<string> filePaths)
288288

289289
public override void Merge(string sourceBranch, string destinationBranch)
290290
{
291-
_repo.Checkout(_repo.Branches[destinationBranch]);
291+
Checkout(destinationBranch);
292292

293293
var oldHeadCommit = _repo.Head.Tip;
294294
var signature = GetSignature();

Rubberduck.VBEEditor/Extensions/VBComponentExtensions.cs

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
14
using System.IO;
25
using Microsoft.Vbe.Interop;
36

@@ -18,22 +21,67 @@ public static class VBComponentExtensions
1821
/// <param name="directoryPath">Destination Path for the resulting source file.</param>
1922
public static string ExportAsSourceFile(this VBComponent component, string directoryPath)
2023
{
21-
var filePath = Path.Combine(directoryPath, component.Name + component.Type.FileExtension());
22-
if (component.Type == vbext_ComponentType.vbext_ct_Document)
24+
var path = Path.Combine(directoryPath, component.Name + component.Type.FileExtension());
25+
switch (component.Type)
2326
{
24-
var lineCount = component.CodeModule.CountOfLines;
25-
if (lineCount > 0)
26-
{
27-
var text = component.CodeModule.get_Lines(1, lineCount);
28-
File.WriteAllText(filePath, text);
29-
}
27+
case vbext_ComponentType.vbext_ct_MSForm:
28+
ExportUserFormModule(component, path);
29+
break;
30+
case vbext_ComponentType.vbext_ct_Document:
31+
ExportDocumentModule(component, path);
32+
break;
33+
default:
34+
component.Export(path);
35+
break;
36+
}
37+
38+
return path;
39+
}
40+
41+
private static void ExportUserFormModule(VBComponent component, string path)
42+
{
43+
// VBIDE API inserts an extra newline when exporting a UserForm module.
44+
// this issue causes forms to always be treated as "modified" in source control, which causes conflicts.
45+
// we need to remove the extra newline before the file gets written to its output location.
46+
47+
var visibleCode = component.CodeModule.Lines().Split(new []{Environment.NewLine}, StringSplitOptions.None);
48+
var legitEmptyLineCount = visibleCode.TakeWhile(string.IsNullOrWhiteSpace).Count();
49+
50+
var tempFile = component.ExportToTempFile();
51+
var contents = File.ReadAllLines(tempFile);
52+
var nonAttributeLines = contents.TakeWhile(line => !line.StartsWith("Attribute")).Count();
53+
var attributeLines = contents.Skip(nonAttributeLines).TakeWhile(line => line.StartsWith("Attribute")).Count();
54+
var declarationsStartLine = nonAttributeLines + attributeLines + 1;
55+
56+
var emptyLineCount = contents.Skip(declarationsStartLine - 1)
57+
.TakeWhile(string.IsNullOrWhiteSpace)
58+
.Count();
59+
60+
var code = contents;
61+
if (emptyLineCount > legitEmptyLineCount)
62+
{
63+
code = contents.Take(declarationsStartLine).Union(
64+
contents.Skip(declarationsStartLine + emptyLineCount - legitEmptyLineCount))
65+
.ToArray();
3066
}
31-
else
67+
File.WriteAllLines(path, code);
68+
}
69+
70+
private static void ExportDocumentModule(VBComponent component, string path)
71+
{
72+
var lineCount = component.CodeModule.CountOfLines;
73+
if (lineCount > 0)
3274
{
33-
component.Export(filePath);
75+
var text = component.CodeModule.Lines[1, lineCount];
76+
File.WriteAllText(path, text);
3477
}
78+
}
3579

36-
return filePath;
80+
private static string ExportToTempFile(this VBComponent component)
81+
{
82+
var path = Path.Combine(Path.GetTempPath(), component.Name + component.Type.FileExtension());
83+
component.Export(path);
84+
return path;
3785
}
3886

3987
/// <summary>

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)