Skip to content

Commit

Permalink
Fix snippet for abstract method (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Dec 8, 2023
1 parent c25064e commit d8b46c4
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Fixed

- Fix snippets for abstract method ([PR](https://github.com/josefpihrt/snippetica/pull/82))

## [2.0.2] - 2023-11-05

## [2.0.1] - 2023-11-05
Expand Down
1 change: 1 addition & 0 deletions src/Snippetica.CodeGeneration.Common/KnownTags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static class KnownTags
public const string GenerateStaticModifier = GeneratePrefix + "StaticModifier";
public const string GenerateVirtualModifier = GeneratePrefix + "VirtualModifier";
public const string GenerateAbstractModifier = GeneratePrefix + "AbstractModifier";
public const string GenerateAbstractModifierRequired = "AbstractModifierRequired";
public const string GenerateInlineModifier = GeneratePrefix + "InlineModifier";
public const string GenerateConstModifier = GeneratePrefix + "ConstModifier";
public const string GenerateConstExprModifier = GeneratePrefix + "ConstExprModifier";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!--Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0.-->
<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>@type@ method</Title>
<Shortcut>m</Shortcut>
<Description>@type@ method declaration</Description>
<Author>Josef Pihrt</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Keywords>
<Keyword>Meta-AbstractModifierRequired</Keyword>
<Keyword>Meta-GenerateAbstractModifier</Keyword>
<Keyword>Meta-GenerateAccessModifier</Keyword>
<Keyword>Meta-GenerateProtectedModifier</Keyword>
</Keywords>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>_modifiers</ID>
<ToolTip>Modifier(s)</ToolTip>
<Default>public</Default>
</Literal>
<Literal>
<ID>_parameters</ID>
<ToolTip>List of parameters</ToolTip>
<Default>T parameter</Default>
</Literal>
<Literal>
<ID>name</ID>
<ToolTip>Method name</ToolTip>
<Default>MethodName</Default>
</Literal>
<Literal>
<ID>type</ID>
<ToolTip>Return type</ToolTip>
<Default>void</Default>
</Literal>
</Declarations>
<Code Language="CSharp"><![CDATA[$_modifiers$ $type$ $name$($_parameters$);$end$]]></Code>
</Snippet>
</CodeSnippet>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Keywords>
<Keyword>Meta-GenerateAbstractModifier</Keyword>
<Keyword>Meta-GenerateAccessModifier</Keyword>
<Keyword>Meta-GenerateProtectedModifier</Keyword>
<Keyword>Meta-GenerateStaticModifier</Keyword>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!--Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0.-->
<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>@type@ method</Title>
<Shortcut>m</Shortcut>
<Description>@type@ method declaration</Description>
<Author>Josef Pihrt</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Keywords>
<Keyword>Meta-AbstractModifierRequired</Keyword>
<Keyword>Meta-GenerateAbstractModifier</Keyword>
<Keyword>Meta-GenerateAccessModifier</Keyword>
<Keyword>Meta-GenerateProtectedModifier</Keyword>
<Keyword>Meta-GenerateVoidType</Keyword>
<Keyword>Meta-NonUniqueShortcut</Keyword>
</Keywords>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>_as</ID>
<ToolTip>As keyword</ToolTip>
<Default>As</Default>
</Literal>
<Literal>
<ID>_modifiers</ID>
<ToolTip>Modifier(s)</ToolTip>
<Default>Public</Default>
</Literal>
<Literal>
<ID>_parameters</ID>
<ToolTip>List of parameters</ToolTip>
<Default>parameter As T</Default>
</Literal>
<Literal>
<ID>_subOrFunction</ID>
<ToolTip>Sub or Function</ToolTip>
<Default>Function</Default>
</Literal>
<Literal>
<ID>name</ID>
<ToolTip>Method name</ToolTip>
<Default>MethodName</Default>
</Literal>
<Literal>
<ID>type</ID>
<ToolTip>Return type</ToolTip>
<Default>Object</Default>
</Literal>
</Declarations>
<Code Language="VB"><![CDATA[$_modifiers$ $_subOrFunction$ $name$($_parameters$) $_as$ $type$$end$]]></Code>
</Snippet>
</CodeSnippet>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Keywords>
<Keyword>Meta-GenerateAbstractModifier</Keyword>
<Keyword>Meta-GenerateAccessModifier</Keyword>
<Keyword>Meta-GenerateProtectedModifier</Keyword>
<Keyword>Meta-GenerateStaticModifier</Keyword>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ namespace Snippetica.CodeGeneration.Commands;

public class MultiCommandCollection : Collection<MultiCommand>
{
public MultiCommandCollection()
{
}

public MultiCommandCollection(IEnumerable<MultiCommand> commands)
{
AddRange(commands);
}

public void AddRange(IEnumerable<MultiCommand> commands)
{
foreach (MultiCommand command in commands)
Expand Down
10 changes: 9 additions & 1 deletion src/Snippetica.CodeGeneration/LanguageSnippetGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ protected override MultiCommandCollection CreateCommands(Snippet snippet)

commands.AddMultiCommands(GetAccessModifierCommands(snippet));

var abstractModifierRequired = false;

if (snippet.HasTag(KnownTags.GenerateStaticModifier))
commands.AddMultiCommand(CommandUtility.StaticCommand, duplicateWhenEmpty: true);

if (snippet.HasTag(KnownTags.GenerateVirtualModifier))
commands.AddMultiCommand(CommandUtility.VirtualCommand, duplicateWhenEmpty: true);

if (snippet.HasTag(KnownTags.GenerateAbstractModifier))
commands.AddMultiCommand(CommandUtility.AbstractCommand, duplicateWhenEmpty: true);
{
commands.AddMultiCommand(CommandUtility.AbstractCommand, duplicateWhenEmpty: false);
abstractModifierRequired = snippet.HasTag(KnownTags.GenerateAbstractModifierRequired);
}

if (snippet.HasTag(KnownTags.GenerateConstModifier))
commands.AddMultiCommand(CommandUtility.ConstCommand, duplicateWhenEmpty: true);
Expand All @@ -62,6 +67,9 @@ protected override MultiCommandCollection CreateCommands(Snippet snippet)
if (snippet.HasTag(KnownTags.GenerateInitializer))
commands.AddMultiCommand(CommandUtility.InitializerCommand, duplicateWhenEmpty: true);

if (abstractModifierRequired)
commands = new MultiCommandCollection(commands.Where(mc => mc.Commands.Any(c => c.Kind == CommandKind.AbstractModifier)));

return commands;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected override void SaveSnippets(SnippetGeneratorResult result)

MDocument document = Document(
Heading1(result.Language.GetTitle() + " Snippets"),
Inline("Please see ", Link("documentation", "https://josefpihrt.github.io/docs/snippetica/vscode"), " for further information."));
Inline("Please see ", Link("documentation", "https://josefpihrt.github.io/docs/snippetica/snippets/vscode/" + languageId), " for further information."));

IOUtility.WriteAllText(Path.Combine(packageDirectoryPath, "README.md"), document.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# C\# Snippets

Please see [documentation](https://josefpihrt.github.io/docs/snippetica/vscode) for further information\.
Please see [documentation](https://josefpihrt.github.io/docs/snippetica/snippets/vscode/csharp) for further information\.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# C\+\+ Snippets

Please see [documentation](https://josefpihrt.github.io/docs/snippetica/vscode) for further information\.
Please see [documentation](https://josefpihrt.github.io/docs/snippetica/snippets/vscode/cpp) for further information\.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# VB Snippets

Please see [documentation](https://josefpihrt.github.io/docs/snippetica/vscode) for further information\.
Please see [documentation](https://josefpihrt.github.io/docs/snippetica/snippets/vscode/vb) for further information\.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Visual Basic Snippets
# VB Snippets

Please see [documentation](https://josefpihrt.github.io/docs/snippetica/snippets/vscode/vb) for further information\.

0 comments on commit d8b46c4

Please sign in to comment.