-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add codefix for converting interpolation issues
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
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
37 changes: 37 additions & 0 deletions
37
src/FsAutoComplete/CodeFixes/UseTripleQuotedInterpolation.fs
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,37 @@ | ||
module FsAutoComplete.CodeFix.UseTripleQuotedInterpolation | ||
|
||
open FsToolkit.ErrorHandling | ||
open FsAutoComplete.CodeFix.Types | ||
open Ionide.LanguageServerProtocol.Types | ||
open FsAutoComplete | ||
open FsAutoComplete.LspHelpers | ||
open FsAutoComplete.FCSPatches | ||
|
||
/// a codefix that replaces erroring single-quoted interpolations with triple-quoted interpolations | ||
let fix (getParseResultsForFile: GetParseResultsForFile) (getRangeText: GetRangeText) : CodeFix = | ||
Run.ifDiagnosticByCode (Set.ofList [ "3373" ]) (fun diagnostic codeActionParams -> | ||
asyncResult { | ||
let pos = protocolPosToPos diagnostic.Range.Start | ||
|
||
let filePath = | ||
codeActionParams.TextDocument.GetFilePath() | ||
|> Utils.normalizePath | ||
|
||
let! tyRes, _, sourceText = getParseResultsForFile filePath pos | ||
|
||
match tyRes.GetParseResults.TryRangeOfStringInterpolationContainingPos pos with | ||
| Some range -> | ||
let! interpolationText = sourceText.GetText range | ||
// skip the leading '$' in the existing single-quoted interpolation | ||
let newText = "$\"\"" + interpolationText.[1..] + "\"\"" | ||
|
||
return | ||
[ { File = codeActionParams.TextDocument | ||
SourceDiagnostic = Some diagnostic | ||
Title = "Use triple-quoted string interpolation" | ||
Edits = | ||
[| { Range = fcsRangeToLsp range | ||
NewText = newText } |] | ||
Kind = FixKind.Fix } ] | ||
| None -> return [] | ||
}) |
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
1 change: 1 addition & 0 deletions
1
test/FsAutoComplete.Tests.Lsp/TestCases/TripleQuotedInterpolation/Script.fsx
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 @@ | ||
let a = $":^) {if true then "y" else "n"} d" |