Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 31 additions & 24 deletions src/FsAutoComplete/CodeFixes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -805,30 +805,37 @@ module Fixes =
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")

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 endPos ->
let fcsPos = protocolPosToPos endPos
let line = getLine lines endPos
// 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" ])
Expand Down
3 changes: 3 additions & 0 deletions src/FsAutoComplete/LspHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down