-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from aglasencnik/dev
Merge dev into main
- Loading branch information
Showing
18 changed files
with
842 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
namespace BlazoryVS | ||
{ | ||
/// <summary> | ||
/// Represents the default constant values. | ||
/// </summary> | ||
internal class BlazoryVSDefaults | ||
{ | ||
#region Path constants | ||
|
||
/// <summary> | ||
/// Gets the name of the folder that contains the snippets. | ||
/// </summary> | ||
public const string SnippetsFolderName = "Snippets"; | ||
|
||
/// <summary> | ||
/// Gets the name of the folder that contains the C# snippets. | ||
/// </summary> | ||
public const string CSharpSnippetsFolderName = "Blazory CSharp Snippets"; | ||
|
||
/// <summary> | ||
/// Gets the name of the folder that contains the Razor snippets. | ||
/// </summary> | ||
public const string RazorSnippetsFolderName = "Blazory Razor Snippets"; | ||
|
||
#endregion | ||
|
||
#region Snippet constants | ||
|
||
/// <summary> | ||
/// Gets the name of the snippet author. | ||
/// </summary> | ||
public const string SnippetAuthor = "Blazory"; | ||
|
||
/// <summary> | ||
/// Gets the name of the C# snippet language. | ||
/// </summary> | ||
public const string CSharpSnippetLanguage = "CSharp"; | ||
|
||
/// <summary> | ||
/// Gets the name of the Razor snippet language. | ||
/// </summary> | ||
public const string RazorSnippetLanguage = "Razor"; | ||
|
||
/// <summary> | ||
/// Gets the name of the placeholder snippet. | ||
/// </summary> | ||
public const string PlaceholderSnippetName = "placeholder.snippet"; | ||
|
||
#endregion | ||
|
||
#region Blazory repository constants | ||
|
||
/// <summary> | ||
/// Gets the URL of the Blazory C# snippets JSON file. | ||
/// </summary> | ||
public const string CSharpSnippetsJsonUrl = "https://raw.githubusercontent.com/bartvanhoey/Blazory/master/snippets/csharp.json"; | ||
|
||
/// <summary> | ||
/// Gets the URL of the Blazory Razor snippets JSON file. | ||
/// </summary> | ||
public const string RazorSnippetsJsonUrl = "https://raw.githubusercontent.com/bartvanhoey/Blazory/master/snippets/razor.json"; | ||
|
||
#endregion | ||
|
||
#region Settings constants | ||
|
||
/// <summary> | ||
/// Gets the name of the C# snippets setting. | ||
/// </summary> | ||
public const string CSharpSnippetsSettingName = "CSharpSnippets"; | ||
|
||
/// <summary> | ||
/// Gets the name of the Razor snippets setting. | ||
/// </summary> | ||
public const string RazorSnippetsSettingName = "RazorSnippets"; | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace BlazoryVS.Enums | ||
{ | ||
/// <summary> | ||
/// Represents the snippet type. | ||
/// </summary> | ||
internal enum SnippetType | ||
{ | ||
CSharp, | ||
Razor | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using BlazoryVS.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace BlazoryVS.Helpers | ||
{ | ||
/// <summary> | ||
/// Represents a helper class for parsing snippet code. | ||
/// </summary> | ||
internal static class CodeParserHelper | ||
{ | ||
/// <summary> | ||
/// Parses the snippet code. | ||
/// </summary> | ||
/// <param name="code">Snippet code string.</param> | ||
/// <returns>A tuple containing parsed code string and an array of Literal objects.</returns> | ||
public static (string, Literal[]) ParseCode(string code) | ||
{ | ||
try | ||
{ | ||
var regex = new Regex(@"\$\{(\d+)(?:\:([^}|]+))?(\|([^}]+)\|)?\}|(\$0)"); | ||
|
||
var literalsDict = new Dictionary<string, Literal>(); | ||
|
||
var vsFormatted = regex.Replace(code, match => | ||
{ | ||
// Handle the special case of $0 (final cursor position in VS Code) | ||
if (match.Value == "$0") | ||
return "$end$"; | ||
|
||
var id = match.Groups[1].Value; | ||
var defaultValue = match.Groups[2].Value; | ||
|
||
// Handle choices: We'll default to the first choice for VS | ||
// as VS doesn't support choices directly in the snippet | ||
if (string.IsNullOrWhiteSpace(defaultValue)) | ||
{ | ||
var choices = match.Groups[4].Value?.Split(','); | ||
if (choices != null && choices.Length > 0) | ||
{ | ||
defaultValue = choices[0]; | ||
} | ||
} | ||
|
||
if (!literalsDict.ContainsKey(id)) | ||
{ | ||
literalsDict[id] = new Literal { Id = id, Default = defaultValue }; | ||
} | ||
|
||
return $"${id}$"; | ||
}); | ||
|
||
return (vsFormatted, literalsDict.Values.ToArray()); | ||
} | ||
catch | ||
{ | ||
return (string.Empty, Array.Empty<Literal>()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace BlazoryVS.Helpers | ||
{ | ||
/// <summary> | ||
/// Represents a helper class for path operations. | ||
/// </summary> | ||
internal class PathHelper | ||
{ | ||
/// <summary> | ||
/// Sanitizes the file name. | ||
/// </summary> | ||
/// <param name="fileName">Proposed filename.</param> | ||
/// <returns>Sanitized file name string.</returns> | ||
public static string SanitizeFileName(string fileName) | ||
{ | ||
var invalidChars = Path.GetInvalidFileNameChars(); | ||
return new string(fileName.Where(ch => !invalidChars.Contains(ch)).ToArray()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using BlazoryVS.Models; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace BlazoryVS.Helpers | ||
{ | ||
/// <summary> | ||
/// Represents the snippet deserialization helper class. | ||
/// </summary> | ||
internal static class SnippetDeserializationHelper | ||
{ | ||
/// <summary> | ||
/// Deserializes the JSON string to a snippet array. | ||
/// </summary> | ||
/// <param name="json">JSON containing snippet objects.</param> | ||
/// <param name="language">Snippet language.</param> | ||
/// <param name="author">Snippet author.</param> | ||
/// <returns>Array of Snippet objects.</returns> | ||
public static Snippet[] DeserializeJsonToSnippets(string json, string language, string author) | ||
{ | ||
try | ||
{ | ||
if (string.IsNullOrWhiteSpace(json) || string.IsNullOrWhiteSpace(language) || string.IsNullOrWhiteSpace(author)) | ||
return Array.Empty<Snippet>(); | ||
|
||
var jObjects = JsonConvert.DeserializeObject<Dictionary<string, JsonSnippet>>(json); | ||
var snippets = new List<Snippet>(); | ||
|
||
foreach (var kvp in jObjects) | ||
{ | ||
var jsonSnippet = kvp.Value; | ||
|
||
var code = string.Empty; | ||
|
||
if (jsonSnippet.Body.Type == JTokenType.String) | ||
code = jsonSnippet.Body.ToString(); | ||
else if (jsonSnippet.Body.Type == JTokenType.Array) | ||
code = string.Join("\n", jsonSnippet.Body.ToObject<string[]>()); | ||
|
||
var parsedCodeAndLiterals = CodeParserHelper.ParseCode(code); | ||
|
||
snippets.Add(new Snippet | ||
{ | ||
Name = kvp.Key, | ||
Author = author, | ||
Language = language, | ||
Prefix = jsonSnippet.Prefix, | ||
Description = jsonSnippet.Description, | ||
Code = parsedCodeAndLiterals.Item1, | ||
Literals = parsedCodeAndLiterals.Item2 | ||
}); | ||
} | ||
|
||
return snippets.ToArray(); | ||
} | ||
catch | ||
{ | ||
return Array.Empty<Snippet>(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.