Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Issue5109_Consolidate_copy_comma…
Browse files Browse the repository at this point in the history
…nd_logic' into nextIntoIssue5109
  • Loading branch information
IvenBach committed Oct 11, 2019
2 parents d4c3f2b + 9227d59 commit c990acb
Show file tree
Hide file tree
Showing 19 changed files with 295 additions and 116 deletions.
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
Expand Down
Expand Up @@ -11,7 +11,7 @@

namespace Rubberduck.Inspections.Abstract
{
public abstract class InspectionResultBase : IInspectionResult, INavigateSource, IExportable
public abstract class InspectionResultBase : IInspectionResult, INavigateSource
{
protected InspectionResultBase(IInspection inspection,
string description,
Expand Down Expand Up @@ -56,39 +56,6 @@ public int CompareTo(IInspectionResult other)
return Inspection.CompareTo(other.Inspection);
}

/// <summary>
/// WARNING: This property can have side effects. It can change the ActiveVBProject if the result has a null Declaration,
/// which causes a flicker in the VBE. This should only be called if it is *absolutely* necessary.
/// </summary>
public string ToClipboardString()
{
var module = QualifiedSelection.QualifiedName;
var documentName = Target != null
? Target.ProjectDisplayName
: string.Empty;
//todo: Find a sane way to reimplement this.
//if (string.IsNullOrEmpty(documentName))
//{
// var component = module.Component;
// documentName = component != null
// ? component.ParentProject.ProjectDisplayName
// : string.Empty;
//}
if (string.IsNullOrEmpty(documentName))
{
documentName = Path.GetFileName(module.ProjectPath);
}

return string.Format(
InspectionsUI.QualifiedSelectionInspection,
Inspection.Severity,
Description,
$"({documentName})",
module.ProjectName,
module.ComponentName,
QualifiedSelection.Selection.StartLine);
}

private NavigateCodeEventArgs _navigationArgs;
public NavigateCodeEventArgs GetNavigationArgs()
{
Expand All @@ -105,11 +72,5 @@ public int CompareTo(object obj)
{
return CompareTo(obj as IInspectionResult);
}

public object[] ToArray()
{
var module = QualifiedSelection.QualifiedName;
return new object[] { Inspection.Severity.ToString(), module.ProjectName, module.ComponentName, Description, QualifiedSelection.Selection.StartLine, QualifiedSelection.Selection.StartColumn };
}
}
}
61 changes: 60 additions & 1 deletion Rubberduck.Core/Common/ClipboardWriter.cs
@@ -1,6 +1,9 @@
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Collections.Generic;
using System.Linq;
using System;

namespace Rubberduck.Common
{
Expand All @@ -11,6 +14,20 @@ public interface IClipboardWriter
void AppendString(string formatName, string data);
void AppendStream(string formatName, MemoryStream stream);
void Flush();
void AppendInfo<T>(ColumnInfo[] columnInfos,
IEnumerable<T> exportableResults,
string titleFormat,
ClipboardWriterAppendingInformationFormat appendingInformationFormat) where T : IExportable;
}

public enum ClipboardWriterAppendingInformationFormat
{
XmlSpreadsheetFormat = 1 << 0,
RtfFormat = 1 << 1,
HtmlFormat = 1 << 2,
CsvFormat = 1 << 3,
UnicodeFormat = 1 << 4,
All = XmlSpreadsheetFormat | RtfFormat | HtmlFormat | CsvFormat | UnicodeFormat
}

public class ClipboardWriter : IClipboardWriter
Expand All @@ -32,7 +49,6 @@ public void AppendImage(BitmapSource image)
_data.SetImage(image);
}


public void AppendString(string formatName, string data)
{
if (_data == null)
Expand All @@ -59,5 +75,48 @@ public void Flush()
_data = null;
}
}

public void AppendInfo<T>(ColumnInfo[] columnInfos,
IEnumerable<T> results,
string title,
ClipboardWriterAppendingInformationFormat appendingInformationFormat) where T : IExportable
{
object[][] resultsAsArray = results.Select(result => result.ToArray()).ToArray();

var includeXmlSpreadsheetFormat = (appendingInformationFormat & ClipboardWriterAppendingInformationFormat.XmlSpreadsheetFormat) == ClipboardWriterAppendingInformationFormat.XmlSpreadsheetFormat;
if (includeXmlSpreadsheetFormat)
{
const string xmlSpreadsheetDataFormat = "XML Spreadsheet";
using (var stream = ExportFormatter.XmlSpreadsheetNew(resultsAsArray, title, columnInfos))
{
AppendStream(DataFormats.GetDataFormat(xmlSpreadsheetDataFormat).Name, stream);
}
}

var includeRtfFormat = (appendingInformationFormat & ClipboardWriterAppendingInformationFormat.RtfFormat) == ClipboardWriterAppendingInformationFormat.RtfFormat;
if (includeRtfFormat)
{
AppendString(DataFormats.Rtf, ExportFormatter.RTF(resultsAsArray, title));
}

var includeHtmlFormat = (appendingInformationFormat & ClipboardWriterAppendingInformationFormat.HtmlFormat) == ClipboardWriterAppendingInformationFormat.HtmlFormat;
if (includeHtmlFormat)
{
AppendString(DataFormats.Html, ExportFormatter.HtmlClipboardFragment(resultsAsArray, title, columnInfos));
}

var includeCsvFormat = (appendingInformationFormat & ClipboardWriterAppendingInformationFormat.CsvFormat) == ClipboardWriterAppendingInformationFormat.CsvFormat;
if (includeCsvFormat)
{
AppendString(DataFormats.CommaSeparatedValue, ExportFormatter.Csv(resultsAsArray, title, columnInfos));
}

var includeUnicodeFormat = (appendingInformationFormat & ClipboardWriterAppendingInformationFormat.UnicodeFormat) == ClipboardWriterAppendingInformationFormat.UnicodeFormat;
if (includeUnicodeFormat && results is IEnumerable<IExportable> unicodeResults)
{
var unicodeTextFormat = title + Environment.NewLine + string.Join(string.Empty, unicodeResults.Select(result => result.ToClipboardString() + Environment.NewLine).ToArray());
AppendString(DataFormats.UnicodeText, unicodeTextFormat);
}
}
}
}
32 changes: 32 additions & 0 deletions Rubberduck.Core/Formatters/DeclarationFormatter.cs
@@ -0,0 +1,32 @@
using Rubberduck.Common;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Resources;

namespace Rubberduck.Formatters
{
public class DeclarationFormatter : IExportable
{
private readonly Declaration _declaration;

public DeclarationFormatter(Declaration declaration)
{
_declaration = declaration;
}

public object[] ToArray()
{
return _declaration.ToArray();
}

public string ToClipboardString()
{
return string.Format(RubberduckUI.CodeExplorer_IExportable_DeclarationFormat,
_declaration.Project.Name,
_declaration.CustomFolder,
_declaration.ComponentName,
_declaration.DeclarationType,
_declaration.Scope,
_declaration.IdentifierName);
}
}
}
67 changes: 67 additions & 0 deletions Rubberduck.Core/Formatters/InspectionResultFormatter.cs
@@ -0,0 +1,67 @@
using Rubberduck.Common;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Inspections.Abstract;
using System.IO;
using Rubberduck.Resources.Inspections;

namespace Rubberduck.Formatters
{
public class InspectionResultFormatter : IExportable
{
private readonly IInspectionResult _inspectionResult;

public InspectionResultFormatter(IInspectionResult inspectionResult)
{
_inspectionResult = inspectionResult;
}

public object[] ToArray()
{
var module = _inspectionResult.QualifiedSelection.QualifiedName;
return new object[]
{
_inspectionResult.Inspection.Severity.ToString(),
module.ProjectName,
module.ComponentName,
_inspectionResult.Description,
_inspectionResult.QualifiedSelection.Selection.StartLine,
_inspectionResult.QualifiedSelection.Selection.StartColumn
};
}

/// <summary>
/// WARNING: This property can have side effects. It can change the ActiveVBProject if the result has a null Declaration,
/// which causes a flicker in the VBE. This should only be called if it is *absolutely* necessary.
/// </summary>
public string ToClipboardString()
{
var module = _inspectionResult.QualifiedSelection.QualifiedName;
var documentName = _inspectionResult.Target != null
? _inspectionResult.Target.ProjectDisplayName
: string.Empty;

//todo: Find a sane way to reimplement this.
//if (string.IsNullOrEmpty(documentName))
//{
// var component = module.Component;
// documentName = component != null
// ? component.ParentProject.ProjectDisplayName
// : string.Empty;
//}

if (string.IsNullOrEmpty(documentName))
{
documentName = Path.GetFileName(module.ProjectPath);
}

return string.Format(
InspectionsUI.QualifiedSelectionInspection,
_inspectionResult.Inspection.Severity,
_inspectionResult.Description,
$"({documentName})",
module.ProjectName,
module.ComponentName,
_inspectionResult.QualifiedSelection.Selection.StartLine);
}
}
}
25 changes: 25 additions & 0 deletions Rubberduck.Core/Formatters/TestMethodViewModelFormatter.cs
@@ -0,0 +1,25 @@
using Rubberduck.UI.UnitTesting.ViewModels;
using Rubberduck.Common;

namespace Rubberduck.Formatters
{
public class TestMethodViewModelFormatter : IExportable
{
private readonly TestMethodViewModel _testMethodViewModel;

public TestMethodViewModelFormatter(TestMethodViewModel testMethodViewModel)
{
_testMethodViewModel = testMethodViewModel;
}

public object[] ToArray()
{
return _testMethodViewModel.ToArray();
}

public string ToClipboardString()
{
return _testMethodViewModel.ToString();
}
}
}
33 changes: 33 additions & 0 deletions Rubberduck.Core/Formatters/ToDoItemFormatter.cs
@@ -0,0 +1,33 @@
using Rubberduck.ToDoItems;
using Rubberduck.Common;
using Rubberduck.Resources;

namespace Rubberduck.Formatters
{
public class ToDoItemFormatter : IExportable
{
private readonly ToDoItem _toDoItem;

public ToDoItemFormatter(ToDoItem toDoItem)
{
_toDoItem = toDoItem;
}

public object[] ToArray()
{
var module = _toDoItem.Selection.QualifiedName;
return new object[] { _toDoItem.Type, _toDoItem.Description, module.ProjectName, module.ComponentName, _toDoItem.Selection.Selection.StartLine, _toDoItem.Selection.Selection.StartColumn };
}

public string ToClipboardString()
{
var module = _toDoItem.Selection.QualifiedName;
return string.Format(RubberduckUI.ToDoExplorerToDoItemFormat,
_toDoItem.Type,
_toDoItem.Description,
module.ProjectName,
module.ComponentName,
_toDoItem.Selection.Selection.StartLine);
}
}
}
24 changes: 2 additions & 22 deletions Rubberduck.Core/ToDoItems/ToDoItem.cs
@@ -1,7 +1,5 @@
using Rubberduck.Common;
using Rubberduck.Interaction.Navigation;
using Rubberduck.Interaction.Navigation;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Resources;
using Rubberduck.VBEditor;

namespace Rubberduck.ToDoItems
Expand All @@ -10,7 +8,7 @@ namespace Rubberduck.ToDoItems
/// Represents a Todo comment and the necessary information to display and navigate to that comment.
/// This is a binding item. Changing it's properties changes how it is displayed.
/// </summary>
public class ToDoItem : INavigateSource, IExportable
public class ToDoItem : INavigateSource
{
public string Description { get; }

Expand All @@ -29,23 +27,5 @@ public NavigateCodeEventArgs GetNavigationArgs()
{
return new NavigateCodeEventArgs(Selection);
}

public object[] ToArray()
{
var module = Selection.QualifiedName;
return new object[] { Type, Description, module.ProjectName, module.ComponentName, Selection.Selection.StartLine, Selection.Selection.StartColumn };
}

public string ToClipboardString()
{
var module = Selection.QualifiedName;
return string.Format(
RubberduckUI.ToDoExplorerToDoItemFormat,
Type,
Description,
module.ProjectName,
module.ComponentName,
Selection.Selection.StartLine);
}
}
}
2 changes: 1 addition & 1 deletion Rubberduck.Core/UI/CodeExplorer/CodeExplorerWindow.cs
Expand Up @@ -20,7 +20,7 @@ private CodeExplorerWindow()
public CodeExplorerWindow(CodeExplorerViewModel viewModel) : this()
{
ViewModel = viewModel;
codeExplorerControl1.DataContext = viewModel;
codeExplorerControl1.DataContext = ViewModel;
}
public CodeExplorerViewModel ViewModel { get; }
}
Expand Down

0 comments on commit c990acb

Please sign in to comment.