forked from Azure/bicep
-
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.
QuickFix support for spelling errors (Azure#630)
* Add quick fix code action handler * Bump @typescript-eslint/parser to eliminate false positive warnings * Refactoring and error fixes * More test coverage for code action handler * Make spell correction action preferred * Fix a test failure due to rebasing * Clean up code * Add unit tests for diagnostics with code fixes
- Loading branch information
Showing
15 changed files
with
519 additions
and
146 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,50 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
using System.Collections.Generic; | ||
|
||
namespace Bicep.Core.CodeAction | ||
{ | ||
public class CodeFix | ||
{ | ||
private readonly CodeReplacement replacement; | ||
private readonly IEnumerable<CodeReplacement>? additionalReplacements; | ||
|
||
public CodeFix(string description, bool isPreferred, CodeReplacement replacement) | ||
{ | ||
this.Description = description; | ||
this.IsPreferred = isPreferred; | ||
this.replacement = replacement; | ||
this.additionalReplacements = null; | ||
} | ||
|
||
public CodeFix(string description, bool isPreferred, CodeReplacement replacement, params CodeReplacement[] additionalReplacement) | ||
: this(description, isPreferred, replacement) | ||
{ | ||
this.additionalReplacements = additionalReplacement; | ||
} | ||
|
||
public string Description { get; } | ||
|
||
/// <summary> | ||
/// Marks this as preferred. Only useful for the language server. | ||
/// In VSCode, preferred actions are used by the "auto fix" command and can be targeted by keybindings. | ||
/// </summary> | ||
public bool IsPreferred { get; } | ||
|
||
public IEnumerable<CodeReplacement> Replacements | ||
{ | ||
get | ||
{ | ||
yield return this.replacement; | ||
|
||
if (this.additionalReplacements != null) | ||
{ | ||
foreach (var replacement in this.additionalReplacements) | ||
{ | ||
yield return replacement; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,15 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
using Bicep.Core.Parser; | ||
|
||
namespace Bicep.Core.CodeAction | ||
{ | ||
public static class CodeManipulator | ||
{ | ||
public static CodeReplacement Replace(TextSpan span, string text) | ||
=> new CodeReplacement(span, text); | ||
|
||
public static CodeReplacement Replace(IPositionable positionable, string text) | ||
=> Replace(positionable.Span, text); | ||
} | ||
} |
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,19 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
using Bicep.Core.Parser; | ||
|
||
namespace Bicep.Core.CodeAction | ||
{ | ||
public class CodeReplacement : IPositionable | ||
{ | ||
public CodeReplacement(TextSpan span, string text) | ||
{ | ||
this.Span = span; | ||
this.Text = text; | ||
} | ||
|
||
public TextSpan Span { get; } | ||
|
||
public string Text { get; } | ||
} | ||
} |
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,12 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
using System.Collections.Generic; | ||
using Bicep.Core.Parser; | ||
|
||
namespace Bicep.Core.CodeAction | ||
{ | ||
public interface IFixable : IPositionable | ||
{ | ||
public IEnumerable<CodeFix> Fixes { get; } | ||
} | ||
} |
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,42 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
using System.Collections.Generic; | ||
using Bicep.Core.CodeAction; | ||
|
||
namespace Bicep.Core.Diagnostics | ||
{ | ||
public class FixableDiagnostic : Diagnostic, IFixable | ||
{ | ||
private readonly CodeFix fix; | ||
private readonly IEnumerable<CodeFix>? additionalFixes; | ||
|
||
public FixableDiagnostic(Parser.TextSpan span, DiagnosticLevel level, string code, string message, CodeFix fix) | ||
: base(span, level, code, message) | ||
{ | ||
this.fix = fix; | ||
this.additionalFixes = null; | ||
} | ||
|
||
public FixableDiagnostic(Parser.TextSpan span, DiagnosticLevel level, string code, string message, CodeFix fix, params CodeFix[] additionalFixes) | ||
: this(span, level, code, message, fix) | ||
{ | ||
this.additionalFixes = additionalFixes; | ||
} | ||
|
||
public IEnumerable<CodeFix> Fixes | ||
{ | ||
get | ||
{ | ||
yield return this.fix; | ||
|
||
if (this.additionalFixes != null) | ||
{ | ||
foreach (var fix in this.additionalFixes) | ||
{ | ||
yield return fix; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,42 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
using System.Collections.Generic; | ||
using Bicep.Core.CodeAction; | ||
|
||
namespace Bicep.Core.Diagnostics | ||
{ | ||
public class FixableErrorDiagnostic : ErrorDiagnostic, IFixable | ||
{ | ||
private readonly CodeFix fix; | ||
private readonly IEnumerable<CodeFix>? additionalFixes; | ||
|
||
public FixableErrorDiagnostic(Parser.TextSpan span, string code, string message, CodeFix fix) | ||
: base(span, code, message) | ||
{ | ||
this.fix = fix; | ||
this.additionalFixes = null; | ||
} | ||
|
||
public FixableErrorDiagnostic(Parser.TextSpan span, string code, string message, CodeFix fix, params CodeFix[] additionalFixes) | ||
: this(span, code, message, fix) | ||
{ | ||
this.additionalFixes = additionalFixes; | ||
} | ||
|
||
public IEnumerable<CodeFix> Fixes | ||
{ | ||
get | ||
{ | ||
yield return this.fix; | ||
|
||
if (this.additionalFixes != null) | ||
{ | ||
foreach (var fix in this.additionalFixes) | ||
{ | ||
yield return fix; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.