Skip to content

Commit

Permalink
Propose Uppercase for FS0053
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Dec 27, 2016
1 parent 3041cd8 commit b4a00ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/fsharp/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ undefinedNameRecordLabel,"The record label '%s' is not defined."
undefinedNameRecordLabelDetails,"Maybe you want one of the following:"
undefinedNameTypeParameter,"The type parameter '%s is not defined."
undefinedNamePatternDiscriminator,"The pattern discriminator '%s' is not defined."
replaceWithSuggestion,"Replace with '%s'"
missingElseBranch,"The 'if' expression is missing an 'else' branch. The 'then' branch has type '%s'. Because 'if' is an expression, and not a statement, add an 'else' branch which returns a value of the same type."
elseBranchHasWrongType,"All branches of an 'if' expression must return the same type. This expression was expected to have type '%s' but here has type '%s'."
commaInsteadOfSemicolonInRecord,"A ';' is used to separate field values in records. Consider replacing ',' with ';'."
Expand Down
41 changes: 41 additions & 0 deletions vsintegration/src/FSharp.Editor/CodeFix/ProposeUppercaseLabel.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace rec Microsoft.VisualStudio.FSharp.Editor

open System.Composition
open System.Collections.Immutable
open System.Threading
open System.Threading.Tasks

open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes
open Microsoft.CodeAnalysis.CodeActions

[<ExportCodeFixProvider(FSharpCommonConstants.FSharpLanguageName, Name = "ProposeUpperCaseLabel"); Shared>]
type internal FSharpProposeUpperCaseLabelCodeFixProvider() =
inherit CodeFixProvider()
let fixableDiagnosticIds = ["FS0053"]

let createCodeFix (title: string, context: CodeFixContext, textChange: TextChange) =
CodeAction.Create(
title,
(fun (cancellationToken: CancellationToken) ->
async {
let! sourceText = context.Document.GetTextAsync()
return context.Document.WithText(sourceText.WithChanges(textChange))
} |> CommonRoslynHelpers.StartAsyncAsTask(cancellationToken)),
title)

override __.FixableDiagnosticIds = fixableDiagnosticIds.ToImmutableArray()

override __.RegisterCodeFixesAsync context : Task =
async {
let diagnostics = (context.Diagnostics |> Seq.filter (fun x -> fixableDiagnosticIds |> List.contains x.Id)).ToImmutableArray()
if context.Span.Length > 0 then
let! sourceText = context.Document.GetTextAsync(context.CancellationToken)
let originalText = sourceText.ToString(context.Span)
if originalText.Length > 0 then
let newText = originalText.[0].ToString().ToUpper() + originalText.Substring(1)
context.RegisterCodeFix(createCodeFix(FSComp.SR.replaceWithSuggestion newText, context, TextChange(context.Span, newText)), diagnostics)
} |> CommonRoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)
3 changes: 2 additions & 1 deletion vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -->
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
Expand Down Expand Up @@ -62,6 +62,7 @@
<Compile Include="Commands\FsiCommandService.fs" />
<Compile Include="CodeFix\AddNewKeywordToDisposableConstructorInvocation.fs" />
<Compile Include="CodeFix\AddOpenCodeFixProvider.fs" />
<Compile Include="CodeFix\ProposeUppercaseLabel.fs" />
<Compile Include="CodeFix\PrefixUnusedValueWithUnderscore.fs" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit b4a00ce

Please sign in to comment.