Skip to content

Commit

Permalink
Fixed FormatDocument and added better error squiggly colors
Browse files Browse the repository at this point in the history
[release]
  • Loading branch information
madskristensen committed Jan 2, 2022
1 parent d1f1b39 commit 5f85d2d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Full completion provided for variables and registry keys.
![Intellisense](art/intellisense.gif)

## Validation
There's validation for both syntax errors and unknown variables.
There's validation for both syntax errors, unknown variables, and more to help you avoid mistakes.

![Validation](art/validation.png)

Expand Down
35 changes: 33 additions & 2 deletions src/Commands/FormatDocumentCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.ComponentModel.Composition;
using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text;
Expand All @@ -25,8 +27,30 @@ public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionConte
{
if (item is Entry entry)
{
var insertLineBefore = true;

if (!entry.Properties.Any() && NextEntry(entry) is Entry next)
{
var currentKey = entry.RegistryKey.Text.Trim().TrimEnd(']');
var nextKey = next.RegistryKey.Text.Trim().TrimEnd(']');

if (nextKey.IndexOf(currentKey, StringComparison.OrdinalIgnoreCase) > -1)
{
insertLineBefore = false;
}
}

sb.AppendLine();
sb.AppendLine(entry.GetFormattedText());

if (insertLineBefore)
{
sb.AppendLine(entry.GetFormattedText());
}
else
{
sb.Append(entry.GetFormattedText());
}

}
else if (item.Type == ItemType.Comment)
{
Expand All @@ -40,6 +64,13 @@ public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionConte
return true;
}

private 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;
Expand Down
2 changes: 1 addition & 1 deletion src/Language/TokenTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private IEnumerable<ErrorListItem> CreateErrorListItem(ParseItem item)
ProjectName = _document.ProjectName ?? "",
FileName = _document.FileName,
Message = error.Message,
ErrorCategory = PredefinedErrorTypeNames.SyntaxError,
ErrorCategory = error.Category,
Severity = error.Severity,
Line = line.LineNumber,
Column = item.Span.Start - line.Start.Position,
Expand Down
22 changes: 15 additions & 7 deletions src/Parser/DocumentValidator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.Shell.Interop;
using type = Microsoft.VisualStudio.Text.Adornments.PredefinedErrorTypeNames;

namespace PkgdefLanguage
{
Expand All @@ -10,13 +11,14 @@ public partial class Document

private class Errors
{
public static Error PL001 { get; } = new("PL001", "Unknown token at this location", __VSERRORCATEGORY.EC_ERROR);
public static Error PL002 { get; } = new("PL002", "Unclosed registry key entry. Add the missing ] character", __VSERRORCATEGORY.EC_ERROR);
public static Error PL003 { get; } = new("PL003", "Use the backslash character as delimiter instead of forward slash.", __VSERRORCATEGORY.EC_ERROR);
public static Error PL004 { get; } = new("PL004", "To set a registry key's default value, use '@' without quotation marks", __VSERRORCATEGORY.EC_WARNING);
public static Error PL005 { get; } = new("PL005", "Value names must be enclosed in quotation marks.", __VSERRORCATEGORY.EC_ERROR);
public static Error PL006 { get; } = new("PL006", "The variable \"{0}\" doens't exist.", __VSERRORCATEGORY.EC_WARNING);
public static Error PL007 { get; } = new("PL007", "Variables must begin and end with $ character.", __VSERRORCATEGORY.EC_ERROR);
public static Error PL001 { get; } = new("PL001", "Unknown token at this location", type.SyntaxError, __VSERRORCATEGORY.EC_ERROR);
public static Error PL002 { get; } = new("PL002", "Unclosed registry key entry. Add the missing ] character", type.SyntaxError, __VSERRORCATEGORY.EC_ERROR);
public static Error PL003 { get; } = new("PL003", "Use the backslash character as delimiter instead of forward slash.", type.SyntaxError, __VSERRORCATEGORY.EC_ERROR);
public static Error PL004 { get; } = new("PL004", "To set a registry key's default value, use '@' without quotation marks", type.Warning, __VSERRORCATEGORY.EC_WARNING);
public static Error PL005 { get; } = new("PL005", "Value names must be enclosed in quotation marks.", type.SyntaxError, __VSERRORCATEGORY.EC_ERROR);
public static Error PL006 { get; } = new("PL006", "The variable \"{0}\" doens't exist.", type.Warning, __VSERRORCATEGORY.EC_WARNING);
public static Error PL007 { get; } = new("PL007", "Variables must begin and end with $ character.", type.SyntaxError, __VSERRORCATEGORY.EC_ERROR);
public static Error PL008 { get; } = new("PL008", "This registry key \"{0}\" was already defined earlier in the document", type.Suggestion, __VSERRORCATEGORY.EC_MESSAGE);
}

private void AddError(ParseItem item, Error error)
Expand Down Expand Up @@ -50,6 +52,12 @@ private void ValidateDocument()
{
AddError(item, Errors.PL003);
}

var index = Items.IndexOf(item);
if (Items.Take(index).Any(i => i.Type == ItemType.RegistryKey && item.Text == i.Text))
{
AddError(item, Errors.PL008.WithFormat(trimmedText));
}
}

// Properties
Expand Down
6 changes: 4 additions & 2 deletions src/Parser/ParseItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,22 @@ public override bool Equals(object obj)

public class Error
{
public Error(string errorCode, string message, __VSERRORCATEGORY severity)
public Error(string errorCode, string message, string category, __VSERRORCATEGORY severity)
{
ErrorCode = errorCode;
Message = message;
Category = category;
Severity = severity;
}

public string ErrorCode { get; }
public string Message { get; }
public string Category { get; }
public __VSERRORCATEGORY Severity { get; }

public Error WithFormat(params string[] replacements)
{
return new Error(ErrorCode, string.Format(Message, replacements), Severity);
return new Error(ErrorCode, string.Format(Message, replacements), Category, Severity);
}
}
}
2 changes: 1 addition & 1 deletion src/PkgdefLanguage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Community.VisualStudio.Toolkit.16" ExcludeAssets="runtime">
<Version>16.0.365</Version>
<Version>16.0.366</Version>
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools">
Expand Down
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.365" />
<PackageReference Include="Community.VisualStudio.Toolkit.16" Version="16.0.366" />
<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 5f85d2d

Please sign in to comment.