Skip to content

Commit

Permalink
[release] TAEIR for headers
Browse files Browse the repository at this point in the history
Enabled Trial and Error removal for header files, wrote a little guide
  • Loading branch information
Agrael1 authored Aug 27, 2022
2 parents 4c5dcb0 + 6215e29 commit 75a6d40
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 210 deletions.
3 changes: 0 additions & 3 deletions IncludeToolbox2019/VSCommandTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ internal sealed partial class PackageGuids

public const string GOnlyVCString = "1175290a-3e8f-4718-868c-c08b5d2b09a7";
public static Guid GOnlyVC = new Guid(GOnlyVCString);

public const string GOnlyCppString = "bf397895-9307-431f-a823-1c85d430243a";
public static Guid GOnlyCpp = new Guid(GOnlyCppString);
}
/// <summary>
/// Helper class that encapsulates all CommandIDs uses across VS Package.
Expand Down
3 changes: 1 addition & 2 deletions IncludeToolbox2019/VSCommandTable.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<VisibilityConstraints>
<VisibilityItem guid="IncludeToolbox" id="GenMap" context="GHeaderOnly"/>
<VisibilityItem guid="IncludeToolbox" id="IncludeWhatYouUseId" context="GOnlyVC"/>
<VisibilityItem guid="IncludeToolbox" id="TrialAndError" context="GOnlyCpp"/>
<VisibilityItem guid="IncludeToolbox" id="TrialAndError" context="GOnlyVC"/>
</VisibilityConstraints>
<KeyBindings>
<KeyBinding guid="IncludeToolbox" id="IncludeWhatYouUseId" key1="K" key2="I" mod1="CONTROL" editor="guidVSStd97"/>
Expand Down Expand Up @@ -156,6 +156,5 @@
<!-- Visibility Constraints -->
<GuidSymbol name="GHeaderOnly" value="{a34e853e-8679-4a07-918e-982a1b3b0a6b}"/>
<GuidSymbol name="GOnlyVC" value="{1175290a-3e8f-4718-868c-c08b5d2b09a7}"/>
<GuidSymbol name="GOnlyCpp" value="{bf397895-9307-431f-a823-1c85d430243a}"/>
</Symbols>
</CommandTable>
3 changes: 0 additions & 3 deletions IncludeToolbox2022/VSCommandTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ internal sealed partial class PackageGuids

public const string GOnlyVCString = "1175290a-3e8f-4718-868c-c08b5d2b09a7";
public static Guid GOnlyVC = new Guid(GOnlyVCString);

public const string GOnlyCppString = "bf397895-9307-431f-a823-1c85d430243a";
public static Guid GOnlyCpp = new Guid(GOnlyCppString);
}
/// <summary>
/// Helper class that encapsulates all CommandIDs uses across VS Package.
Expand Down
3 changes: 1 addition & 2 deletions IncludeToolbox2022/VSCommandTable.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<VisibilityConstraints>
<VisibilityItem guid="IncludeToolbox" id="GenMap" context="GHeaderOnly"/>
<VisibilityItem guid="IncludeToolbox" id="IncludeWhatYouUseId" context="GOnlyVC"/>
<VisibilityItem guid="IncludeToolbox" id="TrialAndError" context="GOnlyCpp"/>
<VisibilityItem guid="IncludeToolbox" id="TrialAndError" context="GOnlyVC"/>
</VisibilityConstraints>
<KeyBindings>
<KeyBinding guid="IncludeToolbox" id="IncludeWhatYouUseId" key1="K" key2="I" mod1="CONTROL" editor="guidVSStd97"/>
Expand Down Expand Up @@ -156,6 +156,5 @@
<!-- Visibility Constraints -->
<GuidSymbol name="GHeaderOnly" value="{a34e853e-8679-4a07-918e-982a1b3b0a6b}"/>
<GuidSymbol name="GOnlyVC" value="{1175290a-3e8f-4718-868c-c08b5d2b09a7}"/>
<GuidSymbol name="GOnlyCpp" value="{bf397895-9307-431f-a823-1c85d430243a}"/>
</Symbols>
</CommandTable>
74 changes: 70 additions & 4 deletions IncludeToolboxShared/Commands/TrialAndErrorRemoval_CodeWindow.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,95 @@
using Community.VisualStudio.Toolkit;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.VCProjectEngine;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Task = System.Threading.Tasks.Task;

namespace IncludeToolbox.Commands
{
internal sealed class TempGuard : IDisposable
{
readonly VCFile file;
private bool disposedValue;

private void Dispose()
{
if (!disposedValue)
{
file.Remove();
disposedValue = true;
}
}
public TempGuard(VCFile file)
{
this.file = file;
}

~TempGuard()
{
Dispose();
}

void IDisposable.Dispose()
{
Dispose();
GC.SuppressFinalize(this);
}
}


[Command(PackageIds.TrialAndError)]
internal sealed class TrialAndErrorRemoval_CodeWindow : BaseCommand<TrialAndErrorRemoval_CodeWindow>
{
TrialAndErrorRemoval impl = new();
static string support_cpp_path = null;

protected override Task InitializeCompletedAsync()
{
Command.Supported = false;
support_cpp_path = Path.ChangeExtension(Path.GetTempFileName(), ".cpp");
return Task.CompletedTask;
}

async Task<string> TAERHeaderAsync(VCFile file)
{
_ = Output.WriteLineAsync($"Starting Trial And Error Include removal on header file {file.FullPath}");
File.WriteAllText(support_cpp_path, "#include \"" + file.FullPath + "\"");

var proj = await VS.Solutions.GetActiveProjectAsync();
var vc = await proj.ToVCProjectAsync();
var xfile = (VCFile)vc.AddFile(support_cpp_path);
using TempGuard tg = new(xfile);
return await impl.StartHeaderAsync(file, xfile, await TrialAndErrorRemovalOptions.GetLiveInstanceAsync());
}
async Task<string> TAERCodeAsync(VCFile file)
{
_ = Output.WriteLineAsync($"Starting Trial And Error Include removal on {file.FullPath}");
return await impl.StartAsync(file, await TrialAndErrorRemovalOptions.GetLiveInstanceAsync());
}

protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
{
var x = await VS.Solutions.GetActiveItemAsync();
var file = (await x.ToVCProjectItemAsync()) as VCFile;

if (file == null || file.FileType != eFileType.eFileTypeCppCode) return;
_ = Output.WriteLineAsync($"Starting Trial And Error Include removal on {x.FullPath}");
string err = await impl.StartAsync(file, await TrialAndErrorRemovalOptions.GetLiveInstanceAsync());
if ((await x.ToVCProjectItemAsync()) is not VCFile file)
return;
string err = "";

switch (file.FileType)
{
case eFileType.eFileTypeCppCode:
err = await TAERCodeAsync(file);
break;
case eFileType.eFileTypeCppHeader:
err = await TAERHeaderAsync(file);
break;
default:
break;
}

if (string.IsNullOrEmpty(err)) return;
_ = Output.WriteLineAsync(err);
}
Expand Down
2 changes: 1 addition & 1 deletion IncludeToolboxShared/IWYU/IWYU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ bool StartImpl(string file, string cmd)
File.WriteAllText(support_path, cmd);

var ext = Path.GetExtension(file);
if (ext == ".h" || ext == ".hpp")
if (ext == ".h" || ext == ".hpp" || ext == ".hxx")
{
File.WriteAllText(support_cpp_path, "#include \"" + file + "\"");
file = " -Xiwyu --check_also=" + file;
Expand Down
Loading

0 comments on commit 75a6d40

Please sign in to comment.