Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit 77cf3d3

Browse files
vasily-kirichenkoKevinRansom
authored andcommitted
PrefixUnusedValueWithUnderscore Code Fix does not trigger on operators and backticked identifiers (dotnet#2424)
1 parent 6c96574 commit 77cf3d3

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

CodeFix/PrefixUnusedValueWithUnderscore.fs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ open Microsoft.CodeAnalysis.Text
1212
open Microsoft.CodeAnalysis.CodeFixes
1313
open Microsoft.CodeAnalysis.CodeActions
1414

15+
open Microsoft.FSharp.Compiler
16+
1517
[<ExportCodeFixProvider(FSharpCommonConstants.FSharpLanguageName, Name = "PrefixUnusedValueWithUnderscore"); Shared>]
1618
type internal FSharpPrefixUnusedValueWithUnderscoreCodeFixProvider() =
1719
inherit CodeFixProvider()
@@ -30,8 +32,14 @@ type internal FSharpPrefixUnusedValueWithUnderscoreCodeFixProvider() =
3032
override __.FixableDiagnosticIds = fixableDiagnosticIds.ToImmutableArray()
3133

3234
override __.RegisterCodeFixesAsync context : Task =
33-
async {
34-
let diagnostics = (context.Diagnostics |> Seq.filter (fun x -> fixableDiagnosticIds |> List.contains x.Id)).ToImmutableArray()
35-
context.RegisterCodeFix(createCodeFix(SR.PrefixValueNameWithUnderscore.Value, context, TextChange(TextSpan(context.Span.Start, 0), "_")), diagnostics)
36-
context.RegisterCodeFix(createCodeFix(SR.RenameValueToUnderscore.Value, context, TextChange(context.Span, "_")), diagnostics)
35+
async {
36+
let! sourceText = context.Document.GetTextAsync()
37+
let ident = sourceText.ToString(context.Span)
38+
// Prefixing operators and backticked identifiers does not make sense.
39+
// We have to use the additional check for backtickes because `IsOperatorOrBacktickedName` operates on display names
40+
// where backtickes are replaced with parens.
41+
if not (PrettyNaming.IsOperatorOrBacktickedName ident) && not (ident.StartsWith "``") then
42+
let diagnostics = (context.Diagnostics |> Seq.filter (fun x -> fixableDiagnosticIds |> List.contains x.Id)).ToImmutableArray()
43+
context.RegisterCodeFix(createCodeFix(SR.PrefixValueNameWithUnderscore.Value, context, TextChange(TextSpan(context.Span.Start, 0), "_")), diagnostics)
44+
context.RegisterCodeFix(createCodeFix(SR.RenameValueToUnderscore.Value, context, TextChange(context.Span, "_")), diagnostics)
3745
} |> CommonRoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)

DocumentHighlights/DocumentHighlightsService.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ open Microsoft.CodeAnalysis.Host.Mef
1515
open Microsoft.CodeAnalysis.Text
1616

1717
open Microsoft.FSharp.Compiler.SourceCodeServices
18+
open Microsoft.FSharp.Compiler.Range
1819

1920
type internal FSharpHighlightSpan =
2021
{ IsDefinition: bool
@@ -56,7 +57,7 @@ type internal FSharpDocumentHighlightsService [<ImportingConstructor>] (checkerP
5657
asyncMaybe {
5758
let textLine = sourceText.Lines.GetLineFromPosition(position)
5859
let textLinePos = sourceText.Lines.GetLinePosition(position)
59-
let fcsTextLineNumber = textLinePos.Line + 1
60+
let fcsTextLineNumber = Line.fromZ textLinePos.Line
6061
let! symbol = CommonHelpers.getSymbolAtPosition(documentKey, sourceText, position, filePath, defines, SymbolLookupKind.Greedy)
6162
let! _, _, checkFileResults = checker.ParseAndCheckDocument(filePath, textVersionHash, sourceText.ToString(), options, allowStaleResults = true)
6263
let! symbolUse = checkFileResults.GetSymbolUseAtLocation(fcsTextLineNumber, symbol.Ident.idRange.EndColumn, textLine.ToString(), symbol.FullIsland)

0 commit comments

Comments
 (0)