Skip to content

Commit

Permalink
Merge pull request #3 from aglasencnik/dev
Browse files Browse the repository at this point in the history
Implemented graph export functionality
  • Loading branch information
aglasencnik authored Jan 27, 2024
2 parents 2570a4d + f80dd77 commit afb982f
Show file tree
Hide file tree
Showing 157 changed files with 1,753 additions and 17 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ StyleCopReport.xml
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
Expand Down
50 changes: 50 additions & 0 deletions GraphvizVS/CommandHandlers/TypeCharCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
using System.ComponentModel.Composition;
using System.IO;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;

namespace GraphvizVS.CommandHandlers;

/// <summary>
/// Represents the command handler for char commands.
/// </summary>
[Export(typeof(ICommandHandler))]
[Name(nameof(TypeCharCommandHandler))]
[ContentType(ContentTypes.Text)]
[TextViewRole(PredefinedTextViewRoles.PrimaryDocument)]
internal sealed class TypeCharCommandHandler : ICommandHandler<TypeCharCommandArgs>
{
/// <summary>
/// Gets the display name of the command handler.
/// </summary>
public string DisplayName => Vsix.Name;

/// <summary>
/// Executes command handler.
/// </summary>
/// <param name="args">TypeCharCommandArgs object</param>
/// <param name="executionContext">CommandExecutionContext object</param>
/// <returns>Whether key was overriden successfully.</returns>
public bool ExecuteCommand(TypeCharCommandArgs args, CommandExecutionContext executionContext)
{
var typedChar = args.TypedChar;
var fileExtension = Path.GetExtension(args.SubjectBuffer.GetFileName() ?? "");
if (!string.IsNullOrWhiteSpace(fileExtension) && (fileExtension == ".dot" || fileExtension == ".DOT") && (typedChar == '/' || typedChar == '-'))
{
args.TextView.TextBuffer.Insert(args.TextView.Caret.Position.BufferPosition, typedChar == '/' ? "//" : "->");
return true;
}

return false;
}

/// <summary>
/// Determines whether command handler is enabled.
/// </summary>
/// <param name="args">TypeCharCommandArgs object</param>
/// <returns>CommandState object</returns>
public CommandState GetCommandState(TypeCharCommandArgs args) => CommandState.Available;
}
90 changes: 90 additions & 0 deletions GraphvizVS/Commands/ExportGraphCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.Collections.Generic;
using System.IO;
using CliWrap;
using GraphvizVS.Helpers;
using GraphvizVS.Options;
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.Text;
using Microsoft.Win32;

namespace GraphvizVS.Commands;

/// <summary>
/// Represents the command to export the graph to a file.
/// </summary>
[Command(PackageIds.ExportGraphCommand)]
internal sealed class ExportGraphCommand : BaseCommand<ExportGraphCommand>
{
/// <summary>
/// Executes the command.
/// </summary>
/// <param name="e">OleMenuCmdEventArgs object.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
{
var docView = await VS.Documents.GetActiveDocumentViewAsync();
if (docView?.TextView is null)
return;

try
{
var activeDocumentPath = DocumentHelper.GetActiveDocumentPath();
var activeDocumentExtension = Path.GetExtension(activeDocumentPath);
if (string.IsNullOrWhiteSpace(activeDocumentExtension) || activeDocumentExtension.ToLower() != ".dot")
return;

var saveFileDialog = new SaveFileDialog
{
Title = "Select graph save location",
Filter = OutputFormatHelper.GetFilter()
};

var dialogResult = saveFileDialog.ShowDialog();
if (dialogResult is null || !dialogResult.Value || string.IsNullOrWhiteSpace(saveFileDialog.FileName))
return;

var fileName = saveFileDialog.FileName;
if (string.IsNullOrWhiteSpace(fileName))
return;

var flags = new List<string>();

if (!string.IsNullOrWhiteSpace(GeneralOptions.Instance.AdditionalFlags))
flags.Add(GeneralOptions.Instance.AdditionalFlags);

flags.Add(OutputFormatHelper.GetOutputFormatArgument());
flags.Add(activeDocumentPath);
flags.AddRange(["-o", fileName]);

var result = await Cli.Wrap(CommandLineHelper.GetEnginePath())
.WithArguments(flags)
.ExecuteAsync();

if (result.IsSuccess)
await VS.MessageBox.ShowAsync("Graph export was successful!");
else
await VS.MessageBox.ShowErrorAsync("Graph export has failed!");
}
catch (Exception ex)
{
var infoBar = await docView.TextView.CreateInfoBarAsync(
new InfoBarModel([new InfoBarTextSpan("Something went wrong while exporting graph!")], KnownMonikers.DiagramError)
);

if (infoBar != null)
await infoBar.TryShowInfoBarUIAsync();

await ex.LogAsync();
}
}

/// <summary>
/// Executes after the initialization of the command.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
protected override Task InitializeCompletedAsync()
{
Command.Supported = false;
return base.InitializeCompletedAsync();
}
}
20 changes: 20 additions & 0 deletions GraphvizVS/Commands/OpenGraphPreviewWindowCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using GraphvizVS.ToolWindows;

namespace GraphvizVS.Commands;

/// <summary>
/// Represents the command to open the graph preview window.
/// </summary>
[Command(PackageIds.OpenGraphPreviewWindowCommand)]
internal sealed class OpenGraphPreviewWindowCommand : BaseCommand<OpenGraphPreviewWindowCommand>
{
/// <summary>
/// Executes the command.
/// </summary>
/// <param name="e">OleMenuCmdEventArgs object.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
{
await GraphPreviewWindow.ShowAsync();
}
}
19 changes: 19 additions & 0 deletions GraphvizVS/Enums/LayoutEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace GraphvizVS.Enums;

/// <summary>
/// Represents the layout engine.
/// </summary>
public enum LayoutEngine
{
Dot,
Neato,
Fdp,
Sfdp,
Circo,
Twopi,
Nop,
Nop2,
Osage,
Patchwork,
Custom
}
63 changes: 63 additions & 0 deletions GraphvizVS/Enums/OutputFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
namespace GraphvizVS.Enums;

/// <summary>
/// Represents the output format.
/// </summary>
public enum OutputFormat
{
BMP,
CGIMAGE,
CANON,
DOT,
GV,
XDOT,
XDOT12,
XDOT14,
EPS,
EXR,
FIG,
GD,
GD2,
GIF,
GTK,
ICO,
IMAP,
IMAP_NP,
ISMAP,
CMAP,
CMAPX,
CMAPX_NP,
JPG,
JPEG,
JPE,
JP2,
JSON,
JSON0,
DOT_JSON,
XDOT_JSON,
PDF,
PIC,
PCT,
PICT,
PLAIN,
PLAIN_EXT,
PNG,
POV,
PS,
PS2,
PSD,
SGI,
SVG,
SVGZ,
TGA,
TIF,
TIFF,
TK,
VML,
VMLZ,
VRML,
WBMP,
WEBP,
XLIB,
X11
}
11 changes: 11 additions & 0 deletions GraphvizVS/Enums/RuntimeType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GraphvizVS.Enums;

/// <summary>
/// Represents the type of the runtime.
/// </summary>
public enum RuntimeType
{
Ebedded,
Local,
Custom
}
Binary file added GraphvizVS/Graphviz/ANN.dll
Binary file not shown.
Loading

0 comments on commit afb982f

Please sign in to comment.