Skip to content

Commit

Permalink
Intercepted the formatting command
Browse files Browse the repository at this point in the history
[release]
  • Loading branch information
madskristensen committed Jan 10, 2022
1 parent 7eec395 commit b07f230
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 68 deletions.
49 changes: 26 additions & 23 deletions src/Commands/FormatDocumentCommand.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
using Microsoft.VisualStudio.Utilities;

namespace PkgdefLanguage
{
[Export(typeof(ICommandHandler))]
[Name(nameof(FormatDocumentCommand))]
[ContentType(Constants.LanguageName)]
[TextViewRole(PredefinedTextViewRoles.PrimaryDocument)]
public class FormatDocumentCommand : ICommandHandler<FormatDocumentCommandArgs>
public class FormatDocumentCommand
{
public string DisplayName => nameof(CommentCommand);
public static async Task InitializeAsync()
{
// We need to manually intercept the FormatDocument command, because language services swallow formatting commands.
await VS.Commands.InterceptAsync(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.FORMATDOCUMENT, () =>
{
return ThreadHelper.JoinableTaskFactory.Run(async () =>
{
DocumentView doc = await VS.Documents.GetActiveDocumentViewAsync();

if (doc?.TextBuffer != null && doc.TextBuffer.ContentType.IsOfType(Constants.LanguageName))
{
Format(doc.TextBuffer);
return CommandProgression.Stop;
}

public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionContext executionContext)
return CommandProgression.Continue;
});
});
}

private static void Format(ITextBuffer buffer)
{
Document doc = args.SubjectBuffer.GetDocument();
Document doc = buffer.GetDocument();
var sb = new StringBuilder();

foreach (ParseItem item in doc.Items)
Expand Down Expand Up @@ -58,22 +68,15 @@ public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionConte
}
}

var wholeDocSpan = new Span(0, args.SubjectBuffer.CurrentSnapshot.Length);
args.SubjectBuffer.Replace(wholeDocSpan, sb.ToString().Trim());

return true;
var wholeDocSpan = new Span(0, buffer.CurrentSnapshot.Length);
buffer.Replace(wholeDocSpan, sb.ToString().Trim());
}

private Entry NextEntry(Entry current)
private static Entry NextEntry(Entry current)
{
return current.Document.Items
.OfType<Entry>()
.FirstOrDefault(e => e.Span.Start >= current.Span.End);
}

public CommandState GetCommandState(FormatDocumentCommandArgs args)
{
return CommandState.Available;
}
}
}
39 changes: 0 additions & 39 deletions src/Commands/FormatSelectionCommand.cs

This file was deleted.

2 changes: 0 additions & 2 deletions src/Editor/EditorFeatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion;
using Microsoft.VisualStudio.Language.StandardClassification;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text.BraceCompletion;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Utilities;

Expand Down
3 changes: 1 addition & 2 deletions src/PkgdefLanguage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Community.VisualStudio.Toolkit.16" version="16.0.378" ExcludeAssets="runtime">
<PackageReference Include="Community.VisualStudio.Toolkit.16" version="16.0.382" ExcludeAssets="runtime">
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools">
Expand All @@ -80,7 +80,6 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\FormatSelectionCommand.cs" />
<Compile Include="Commands\FormatDocumentCommand.cs" />
<Compile Include="Editor\DropdownBars.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
5 changes: 4 additions & 1 deletion src/PkgdefPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace PkgdefLanguage
[Guid(PackageGuids.PkgdefLanguageString)]
[ProvideMenuResource("Menus.ctmenu", 1)]

[ProvideLanguageService(typeof(LanguageFactory), Constants.LanguageName, 0, EnableLineNumbers = true, EnableAsyncCompletion = true, ShowCompletion = true, EnableFormatSelection = true, ShowDropDownOptions = true)]
[ProvideLanguageService(typeof(LanguageFactory), Constants.LanguageName, 0, EnableLineNumbers = true, EnableAsyncCompletion = true, ShowCompletion = true, EnableFormatSelection = false, ShowDropDownOptions = true)]
[ProvideLanguageExtension(typeof(LanguageFactory), Constants.PkgDefExt)]
[ProvideLanguageExtension(typeof(LanguageFactory), Constants.PkgUndefExt)]

Expand All @@ -31,9 +31,12 @@ public sealed class PkgdefPackage : ToolkitPackage
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await JoinableTaskFactory.SwitchToMainThreadAsync();

var language = new LanguageFactory(this);
RegisterEditorFactory(language);
((IServiceContainer)this).AddService(typeof(LanguageFactory), language, true);

await FormatDocumentCommand.InitializeAsync();
}
}
}
2 changes: 1 addition & 1 deletion test/PkgdefLanguage.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Compile Remove="C:\Users\madsk\.nuget\packages\community.visualstudio.toolkit.16\16.0.357\build\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Community.VisualStudio.Toolkit.16" Version="16.0.378" />
<PackageReference Include="Community.VisualStudio.Toolkit.16" Version="16.0.382" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
Expand Down

0 comments on commit b07f230

Please sign in to comment.