Skip to content

Commit 8dbf851

Browse files
committed
Fix inspection clipboard copy.
1 parent 932637f commit 8dbf851

File tree

11 files changed

+99
-52
lines changed

11 files changed

+99
-52
lines changed

RetailCoder.VBE/Inspections/Abstract/IInspectionResult.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public interface IInspectionResult : IComparable<IInspectionResult>, IComparable
1111
QualifiedSelection QualifiedSelection { get; }
1212
IInspection Inspection { get; }
1313
object[] ToArray();
14+
string ToClipboardString();
1415
}
1516
}

RetailCoder.VBE/Inspections/Abstract/InspectionResultBase.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.IO;
23
using System.Linq;
34
using Antlr4.Runtime;
45
using Rubberduck.Inspections.Resources;
@@ -87,18 +88,33 @@ public virtual int CompareTo(IInspectionResult other)
8788
return Inspection.CompareTo(other.Inspection);
8889
}
8990

90-
//public override string ToString()
91-
//{
92-
// var module = QualifiedSelection.QualifiedName;
93-
// return string.Format(
94-
// InspectionsUI.QualifiedSelectionInspection,
95-
// Inspection.Severity,
96-
// Description,
97-
// "(" + module.ProjectDisplayName + ")",
98-
// module.ProjectName,
99-
// module.ComponentName,
100-
// QualifiedSelection.Selection.StartLine);
101-
//}
91+
/// <summary>
92+
/// WARNING: This property can have side effects. It can change the ActiveVBProject if the result has a null Declaration,
93+
/// which causes a flicker in the VBE. This should only be called if it is *absolutely* necessary.
94+
/// </summary>
95+
public string ToClipboardString()
96+
{
97+
var module = QualifiedSelection.QualifiedName;
98+
var documentName = _target != null ? _target.ProjectDisplayName : string.Empty;
99+
if (string.IsNullOrEmpty(documentName))
100+
{
101+
var component = module.Component;
102+
documentName = component != null ? component.ParentProject.ProjectDisplayName : string.Empty;
103+
}
104+
if (string.IsNullOrEmpty(documentName))
105+
{
106+
documentName = Path.GetFileName(module.ProjectPath);
107+
}
108+
109+
return string.Format(
110+
InspectionsUI.QualifiedSelectionInspection,
111+
Inspection.Severity,
112+
Description,
113+
"(" + documentName + ")",
114+
module.ProjectName,
115+
module.ComponentName,
116+
QualifiedSelection.Selection.StartLine);
117+
}
102118

103119
public virtual NavigateCodeEventArgs GetNavigationArgs()
104120
{

RetailCoder.VBE/UI/Inspections/InspectionResultsViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ private void ExecuteCopyResultsCommand(object parameter)
430430

431431
var title = string.Format(resource, DateTime.Now.ToString(CultureInfo.InvariantCulture), _results.Count);
432432

433-
var textResults = title + Environment.NewLine + string.Join("", _results.Select(result => result.ToString() + Environment.NewLine).ToArray());
433+
var textResults = title + Environment.NewLine + string.Join("", _results.Select(result => result.ToClipboardString() + Environment.NewLine).ToArray());
434434
var csvResults = ExportFormatter.Csv(aResults, title,ColumnInfos);
435435
var htmlResults = ExportFormatter.HtmlClipboardFragment(aResults, title,ColumnInfos);
436436
var rtfResults = ExportFormatter.RTF(aResults, title);

Rubberduck.Parsing/Symbols/ProjectDeclaration.cs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text.RegularExpressions;
2-
using Rubberduck.Parsing.ComReflection;
1+
using Rubberduck.Parsing.ComReflection;
32
using Rubberduck.VBEditor;
43
using System.Collections.Generic;
54
using System.Linq;
@@ -72,9 +71,6 @@ public void AddProjectReference(string referencedProjectId, int priority)
7271
_projectReferences.Add(new ProjectReference(referencedProjectId, priority));
7372
}
7473

75-
private static readonly Regex CaptionProjectRegex = new Regex(@"^(?:[^-]+)(?:\s-\s)(?<project>.+)(?:\s-\s.*)?$");
76-
private static readonly Regex OpenModuleRegex = new Regex(@"^(?<project>.+)(?<module>\s-\s\[.*\((Code|UserForm)\)\])$");
77-
7874
private string _displayName;
7975
/// <summary>
8076
/// WARNING: This property has side effects. It changes the ActiveVBProject, which causes a flicker in the VBE.
@@ -88,39 +84,8 @@ public override string ProjectDisplayName
8884
{
8985
return _displayName;
9086
}
91-
92-
if (_project == null)
93-
{
94-
_displayName = string.Empty;
95-
return _displayName;
96-
}
97-
98-
var vbe = _project.VBE;
99-
var activeProject = vbe.ActiveVBProject;
100-
var mainWindow = vbe.MainWindow;
101-
{
102-
try
103-
{
104-
if (_project.HelpFile != activeProject.HelpFile)
105-
{
106-
vbe.ActiveVBProject = _project;
107-
}
108-
109-
var caption = mainWindow.Caption;
110-
if (CaptionProjectRegex.IsMatch(caption))
111-
{
112-
caption = CaptionProjectRegex.Matches(caption)[0].Groups["project"].Value;
113-
_displayName = OpenModuleRegex.IsMatch(caption)
114-
? OpenModuleRegex.Matches(caption)[0].Groups["project"].Value
115-
: caption;
116-
}
117-
}
118-
catch
119-
{
120-
_displayName = string.Empty;
121-
}
122-
return _displayName;
123-
}
87+
_displayName = _project != null ? _project.ProjectDisplayName : string.Empty;
88+
return _displayName;
12489
}
12590
}
12691
}

Rubberduck.VBEEditor/QualifiedModuleName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public QualifiedMemberName QualifyMemberName(string member)
101101
public string Name { get { return ToString(); } }
102102

103103
private readonly string _projectName;
104-
public string ProjectName { get { return _projectName; } }
104+
public string ProjectName { get { return _projectName ?? string.Empty; } }
105105

106106
private readonly string _projectPath;
107107
public string ProjectPath { get { return _projectPath; } }

Rubberduck.VBEEditor/SafeComWrappers/Abstract/IVBComponent.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ public interface IVBComponent : ISafeComWrapper, IEquatable<IVBComponent>
2020
void Activate();
2121
void Export(string path);
2222
string ExportAsSourceFile(string folder);
23+
24+
IVBProject ParentProject { get; }
2325
}
2426
}

Rubberduck.VBEEditor/SafeComWrappers/Abstract/IVBProject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public interface IVBProject : ISafeComWrapper, IEquatable<IVBProject>
2828
void SaveAs(string fileName);
2929
void MakeCompiledFile();
3030
void ExportSourceFiles(string folder);
31+
string ProjectDisplayName { get; }
3132

3233
IReadOnlyList<string> ComponentNames();
3334
}

Rubberduck.VBEEditor/SafeComWrappers/VB6/VBComponent.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ public string ExportAsSourceFile(string folder)
132132
return fullPath;
133133
}
134134

135+
public IVBProject ParentProject { get; private set; }
136+
135137
private void ExportUserFormModule(string path)
136138
{
137139
// VBIDE API inserts an extra newline when exporting a UserForm module.

Rubberduck.VBEEditor/SafeComWrappers/VB6/VBProject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,6 @@ public void ExportSourceFiles(string folder)
167167
}
168168
}
169169

170+
public string ProjectDisplayName { get; private set; }
170171
}
171172
}

Rubberduck.VBEEditor/SafeComWrappers/VBA/VBComponent.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ public string ExportAsSourceFile(string folder)
129129
return fullPath;
130130
}
131131

132+
public IVBProject ParentProject
133+
{
134+
get
135+
{
136+
return Collection != null ? Collection.Parent : null;
137+
}
138+
}
139+
132140
private void ExportUserFormModule(string path)
133141
{
134142
// VBIDE API inserts an extra newline when exporting a UserForm module.

0 commit comments

Comments
 (0)