From 63f49a808caa037964f88e000d1f61c25ab417da Mon Sep 17 00:00:00 2001 From: cartermp Date: Fri, 29 Jan 2021 17:37:54 -0800 Subject: [PATCH 1/2] Update 'comparisonToMutableAssignment' code fix to account for properties and qualified names --- src/FsAutoComplete/CodeFixes.fs | 56 ++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/FsAutoComplete/CodeFixes.fs b/src/FsAutoComplete/CodeFixes.fs index 5e32982e7..e731c3b02 100644 --- a/src/FsAutoComplete/CodeFixes.fs +++ b/src/FsAutoComplete/CodeFixes.fs @@ -803,32 +803,38 @@ module Fixes = codeActionParams.TextDocument.GetFilePath() |> Utils.normalizePath let fcsPos = protocolPosToPos diagnostic.Range.Start - let! (tyRes, line, lines) = getParseResultsForFile fileName fcsPos - - let! symbol = - tyRes.TryGetSymbolUse fcsPos line - |> AsyncResult.ofOption (fun _ -> "No symbol found at position") + let! (_, _, lines) = getParseResultsForFile fileName fcsPos - match symbol.Symbol with - // only do anything if the value is mutable - | :? FSharpMemberOrFunctionOrValue as mfv when mfv.IsValue && mfv.IsMutable -> - // try to find the '=' at from the start of the range - let endOfMutableValue = fcsPosToLsp symbol.RangeAlternate.End - - match walkForwardUntilCondition lines endOfMutableValue (fun c -> c = '=') with - | Some equalsPos -> - return - [ { File = codeActionParams.TextDocument - Title = "Use '<-' to mutate value" - SourceDiagnostic = Some diagnostic - Edits = - [| { Range = - { Start = equalsPos - End = (inc lines equalsPos) } - NewText = "<-" } |] - Kind = Refactor } ] - | None -> return [] - | _ -> return [] + match walkForwardUntilCondition lines diagnostic.Range.Start System.Char.IsWhiteSpace with + | None -> return [] + | Some col -> + let fcsPos = protocolPosToPos col + let! (tyRes, line, lines) = getParseResultsForFile fileName fcsPos + + let! symbol = + tyRes.TryGetSymbolUse fcsPos line + |> AsyncResult.ofOption (fun _ -> "No symbol found at position") + + match symbol.Symbol with + // only do anything if the value is mutable + | :? FSharpMemberOrFunctionOrValue as mfv when mfv.IsMutable || mfv.HasSetterMethod -> + // try to find the '=' at from the start of the range + let endOfMutableValue = fcsPosToLsp symbol.RangeAlternate.End + + match walkForwardUntilCondition lines endOfMutableValue (fun c -> c = '=') with + | Some equalsPos -> + return + [ { File = codeActionParams.TextDocument + Title = "Use '<-' to mutate value" + SourceDiagnostic = Some diagnostic + Edits = + [| { Range = + { Start = equalsPos + End = (inc lines equalsPos) } + NewText = "<-" } |] + Kind = Refactor } ] + | None -> return [] + | _ -> return [] } |> AsyncResult.foldResult id (fun _ -> [])) (Set.ofList [ "20" ]) From fb933af76b6a974e799db506c5a0b5fcbcfda8c3 Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Fri, 29 Jan 2021 20:04:53 -0600 Subject: [PATCH 2/2] add getLine helper and use it instead of 2x typechecking --- src/FsAutoComplete/CodeFixes.fs | 9 +++++---- src/FsAutoComplete/LspHelpers.fs | 3 +++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/FsAutoComplete/CodeFixes.fs b/src/FsAutoComplete/CodeFixes.fs index e731c3b02..14084d19c 100644 --- a/src/FsAutoComplete/CodeFixes.fs +++ b/src/FsAutoComplete/CodeFixes.fs @@ -803,13 +803,14 @@ module Fixes = codeActionParams.TextDocument.GetFilePath() |> Utils.normalizePath let fcsPos = protocolPosToPos diagnostic.Range.Start - let! (_, _, lines) = getParseResultsForFile fileName fcsPos + let! (tyRes, line, lines) = getParseResultsForFile fileName fcsPos match walkForwardUntilCondition lines diagnostic.Range.Start System.Char.IsWhiteSpace with | None -> return [] - | Some col -> - let fcsPos = protocolPosToPos col - let! (tyRes, line, lines) = getParseResultsForFile fileName fcsPos + | Some endPos -> + let fcsPos = protocolPosToPos endPos + let line = getLine lines endPos + // let! (tyRes, line, lines) = getParseResultsForFile fileName fcsPos let! symbol = tyRes.TryGetSymbolUse fcsPos line diff --git a/src/FsAutoComplete/LspHelpers.fs b/src/FsAutoComplete/LspHelpers.fs index a6e46a03d..a83bca9e9 100644 --- a/src/FsAutoComplete/LspHelpers.fs +++ b/src/FsAutoComplete/LspHelpers.fs @@ -158,6 +158,9 @@ module Conversions = ) |> Array.map map + let getLine (lines: string[]) (pos: Lsp.Position) = + lines.[pos.Line] + let getText (lines: string []) (r: Lsp.Range) = lines.[r.Start.Line].Substring(r.Start.Character, r.End.Character - r.Start.Character)