Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Code Action for Replacing Deprecated Unit Type(#73) (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
bettinaheim authored Aug 17, 2019
1 parent 1795656 commit 482dc08
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/QsCompiler/CompilationManager/DiagnosticTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ internal static Func<Diagnostic, bool> ErrorType(params ErrorCode[] types)
return m => m.Severity == DiagnosticSeverity.Error && codes.Contains(m.Code);
}

/// <summary>
/// Returns true if the WarningType of the given Diagnostic is one of the given types.
/// </summary>
internal static Func<Diagnostic, bool> WarningType(params WarningCode[] types)
{
var codes = types.Select(err => err.Code());
return m => m.Severity == DiagnosticSeverity.Warning && codes.Contains(m.Code);
}

/// <summary>
/// Returns true if the given diagnostics is an error.
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions src/QsCompiler/CompilationManager/EditorSupport/CodeActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,29 @@ private static IEnumerable<NonNullable<string>> NamespaceSuggestionsForTypeAtPos
.Select(SuggestedOpenDirective);
return suggestionsForIds.Concat(suggestionsForTypes);
}

/// <summary>
/// Returns a sequence of suggestions on how deprecated syntax can be updated based on the generated diagnostics,
/// and given the file for which those diagnostics were generated.
/// Returns an empty enumerable if any of the given arguments is null.
/// </summary>
internal static IEnumerable<(string, WorkspaceEdit)> SuggestionsForDeprecatedSyntax
(this FileContentManager file, IEnumerable<Diagnostic> diagnostics)
{
if (file == null || diagnostics == null) return Enumerable.Empty<(string, WorkspaceEdit)>();
var deprecatedUnitTypes = diagnostics.Where(DiagnosticTools.WarningType(WarningCode.DeprecatedUnitType));
if (!deprecatedUnitTypes.Any()) return Enumerable.Empty<(string, WorkspaceEdit)>();

// Suggestion for replacing deprecated unit types
(string, WorkspaceEdit) EditAtPosition(string text, Position start, Position end)
{
var edit = new TextEdit { Range = new Range { Start = start, End = end }, NewText = text };
return ("Replace with \"" + $"{text}" + "\"", file.GetWorkspaceEdit(edit));
}

var suggestionsForUnitType = deprecatedUnitTypes
.Select(d => EditAtPosition(Keywords.qsUnit.id, d.Range.Start, d.Range.End));
return suggestionsForUnitType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public static ImmutableDictionary<string, WorkspaceEdit> CodeActions(this FileCo
if (range?.Start == null || range.End == null || file == null || !Utils.IsValidRange(range, file)) return null;
var suggestionsForUnknownIds = file.SuggestionsForUnknownIdentifiers(compilation, range.Start.Line, context?.Diagnostics);
var suggestionsForAmbiguousIds = file.SuggestionsForAmbiguousIdentifiers(compilation, context?.Diagnostics);
return suggestionsForUnknownIds.Concat(suggestionsForAmbiguousIds)
var suggestionsForDeprecatedSyntax = file.SuggestionsForDeprecatedSyntax(context?.Diagnostics);
return suggestionsForUnknownIds.Concat(suggestionsForAmbiguousIds).Concat(suggestionsForDeprecatedSyntax)
.ToImmutableDictionary(s => s.Item1, s => s.Item2);
}

Expand Down

0 comments on commit 482dc08

Please sign in to comment.