Skip to content

Commit

Permalink
Fixed comment/uncomment
Browse files Browse the repository at this point in the history
[release]
  • Loading branch information
madskristensen committed Jan 10, 2022
1 parent b07f230 commit 455dab0
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 87 deletions.
39 changes: 0 additions & 39 deletions src/Commands/CommentCommand.cs

This file was deleted.

62 changes: 62 additions & 0 deletions src/Commands/Commenting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Formatting;

namespace PkgdefLanguage
{
public class Commenting2
{
public static async Task InitializeAsync()
{
// We need to manually intercept the commenting command, because language services swallow these commands.
await VS.Commands.InterceptAsync(VSConstants.VSStd2KCmdID.COMMENT_BLOCK, () => Execute(Comment));
await VS.Commands.InterceptAsync(VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK, () => Execute(Uncomment));
}

private static CommandProgression Execute(Action<DocumentView> action)
{
return ThreadHelper.JoinableTaskFactory.Run(async () =>
{
DocumentView doc = await VS.Documents.GetActiveDocumentViewAsync();

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

return CommandProgression.Continue;
});
}

private static void Comment(DocumentView doc)
{
SnapshotSpan spans = doc.TextView.Selection.SelectedSpans.First();
Collection<ITextViewLine> lines = doc.TextView.TextViewLines.GetTextViewLinesIntersectingSpan(spans);

foreach (ITextViewLine line in lines.Reverse())
{
doc.TextBuffer.Insert(line.Start.Position, Constants.CommentChars[0]);
}
}

private static void Uncomment(DocumentView doc)
{
SnapshotSpan spans = doc.TextView.Selection.SelectedSpans.First();
Collection<ITextViewLine> lines = doc.TextView.TextViewLines.GetTextViewLinesIntersectingSpan(spans);

foreach (ITextViewLine line in lines.Reverse())
{
var span = Span.FromBounds(line.Start, line.End);
var originalText = doc.TextBuffer.CurrentSnapshot.GetText(span).TrimStart('/', ';');
Span commentCharSpan = new(span.Start, span.Length - originalText.Length);

doc.TextBuffer.Delete(commentCharSpan);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace PkgdefLanguage
{
public class FormatDocumentCommand
public class Formatting
{
public static async Task InitializeAsync()
{
Expand Down
41 changes: 0 additions & 41 deletions src/Commands/UncommentCommand.cs

This file was deleted.

7 changes: 6 additions & 1 deletion src/Editor/LanguageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ public LanguageFactory(object site) : base(site)

public override string[] FileExtensions { get; } = new[] { Constants.PkgDefExt, Constants.PkgUndefExt };

public override ViewFilter CreateViewFilter(CodeWindowManager mgr, IVsTextView newView)
{
return base.CreateViewFilter(mgr, newView);
}

public override void SetDefaultPreferences(LanguagePreferences preferences)
{
preferences.EnableCodeSense = false;
preferences.EnableMatchBraces = true;
preferences.EnableMatchBracesAtCaret = true;
preferences.EnableShowMatchingBrace = true;
preferences.EnableCommenting = false;
preferences.EnableCommenting = true;
preferences.HighlightMatchingBraceFlags = _HighlightMatchingBraceFlags.HMB_USERECTANGLEBRACES;
preferences.LineNumbers = true;
preferences.MaxErrorMessages = 100;
Expand Down
5 changes: 2 additions & 3 deletions src/PkgdefLanguage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,10 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\FormatDocumentCommand.cs" />
<Compile Include="Commands\Formatting.cs" />
<Compile Include="Editor\DropdownBars.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Commands\CommentCommand.cs" />
<Compile Include="Commands\UncommentCommand.cs" />
<Compile Include="Commands\Commenting.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Editor\TokenTagger.cs" />
<Compile Include="Parser\PredefinedVariables.cs" />
Expand Down
5 changes: 3 additions & 2 deletions 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 = false, ShowDropDownOptions = true)]
[ProvideLanguageService(typeof(LanguageFactory), Constants.LanguageName, 0, EnableLineNumbers = true, EnableAsyncCompletion = true, ShowCompletion = true, EnableFormatSelection = false, ShowDropDownOptions = true, EnableCommenting = true)]
[ProvideLanguageExtension(typeof(LanguageFactory), Constants.PkgDefExt)]
[ProvideLanguageExtension(typeof(LanguageFactory), Constants.PkgUndefExt)]

Expand All @@ -36,7 +36,8 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
RegisterEditorFactory(language);
((IServiceContainer)this).AddService(typeof(LanguageFactory), language, true);

await FormatDocumentCommand.InitializeAsync();
await Formatting.InitializeAsync();
await Commenting2.InitializeAsync();
}
}
}

0 comments on commit 455dab0

Please sign in to comment.