Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4b4d90b

Browse files
vasily-kirichenkoKevinRansom
authored andcommitted
ReplaceUnusedValueWithUnderscore code fix replaces "this" var with double underscore (#2960)
* ReplaceUnusedValueWithUnderscore code fix replaces "this" var with double underscore instead of single one* do not show code fix for anything but funcOrMemberOrValuedo not suggest to rename members with underscoreshow multiple suggestions for all symbols on same line* Show symbol name in RenameUnusedValue code fix action name
1 parenta5ef022 commit4b4d90b

File tree

5 files changed

+81
-47
lines changed

5 files changed

+81
-47
lines changed

‎vsintegration/src/FSharp.Editor/CodeFix/PrefixUnusedValueWithUnderscore.fs‎

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// 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.
2+
3+
namespacerecMicrosoft.VisualStudio.FSharp.Editor
4+
5+
openSystem
6+
openSystem.Composition
7+
openSystem.Threading
8+
openSystem.Threading.Tasks
9+
10+
openMicrosoft.CodeAnalysis
11+
openMicrosoft.CodeAnalysis.Text
12+
openMicrosoft.CodeAnalysis.CodeFixes
13+
openMicrosoft.CodeAnalysis.CodeActions
14+
15+
openMicrosoft.FSharp.Compiler
16+
openMicrosoft.FSharp.Compiler.SourceCodeServices
17+
18+
[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name="RenameUnusedValue"); Shared>]
19+
typeinternalFSharpRenameUnusedValueCodeFixProvider
20+
[<ImportingConstructor>]
21+
(
22+
checkerProvider: FSharpCheckerProvider,
23+
projectInfoManager: ProjectInfoManager
24+
)=
25+
26+
inherit CodeFixProvider()
27+
letfixableDiagnosticIds=["FS1182"]
28+
29+
letcreateCodeFix(context:CodeFixContext,symbolName:string,titleFormat:string,textChange:TextChange)=
30+
lettitle= String.Format(titleFormat, symbolName)
31+
letcodeAction=
32+
CodeAction.Create(
33+
title,
34+
(fun(cancellationToken: CancellationToken)->
35+
async{
36+
let!sourceText= context.Document.GetTextAsync()
37+
return context.Document.WithText(sourceText.WithChanges(textChange))
38+
}|> RoslynHelpers.StartAsyncAsTask(cancellationToken)),
39+
title)
40+
letdiagnostics= context.Diagnostics|> Seq.filter(fun x-> fixableDiagnosticIds|> List.contains x.Id)|> Seq.toImmutableArray
41+
context.RegisterCodeFix(codeAction, diagnostics)
42+
43+
override__.FixableDiagnosticIds= Seq.toImmutableArray fixableDiagnosticIds
44+
45+
override__.RegisterCodeFixesAsync context:Task=
46+
asyncMaybe{
47+
letdocument= context.Document
48+
let!sourceText= document.GetTextAsync()
49+
letident= sourceText.ToString(context.Span)
50+
// Prefixing operators and backticked identifiers does not make sense.
51+
// We have to use the additional check for backtickes because `IsOperatorOrBacktickedName` operates on display names
52+
// where backtickes are replaced with parens.
53+
ifnot(PrettyNaming.IsOperatorOrBacktickedName ident)&&not(ident.StartsWith"``")then
54+
let!options= projectInfoManager.TryGetOptionsForEditingDocumentOrProject document
55+
let!_,_,checkResults= checkerProvider.Checker.ParseAndCheckDocument(document, options, allowStaleResults=true, sourceText= sourceText)
56+
letm= RoslynHelpers.TextSpanToFSharpRange(document.FilePath, context.Span, sourceText)
57+
letdefines= CompilerEnvironment.GetCompilationDefinesForEditing(document.FilePath, options.OtherOptions|> Seq.toList)
58+
let!lexerSymbol= Tokenizer.getSymbolAtPosition(document.Id, sourceText, context.Span.Start, document.FilePath, defines, SymbolLookupKind.Greedy,false)
59+
letlineText=(sourceText.Lines.GetLineFromPosition context.Span.Start).ToString()
60+
let!symbolUse= checkResults.GetSymbolUseAtLocation(m.StartLine, m.EndColumn, lineText, lexerSymbol.FullIsland)
61+
letsymbolName= symbolUse.Symbol.DisplayName
62+
63+
match symbolUse.Symbolwith
64+
|:? FSharpMemberOrFunctionOrValue as func->
65+
createCodeFix(context, symbolName, SR.PrefixValueNameWithUnderscore.Value, TextChange(TextSpan(context.Span.Start,0),"_"))
66+
67+
if func.IsMemberThisValuethen
68+
createCodeFix(context, symbolName, SR.RenameValueToDoubleUnderscore.Value, TextChange(context.Span,"__"))
69+
elifnot func.IsMemberthen
70+
createCodeFix(context, symbolName, SR.RenameValueToUnderscore.Value, TextChange(context.Span,"_"))
71+
|_->()
72+
}
73+
|> Async.Ignore
74+
|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)

‎vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
<CompileInclude="CodeFix\AddOpenCodeFixProvider.fs" />
8787
<CompileInclude="CodeFix\ProposeUppercaseLabel.fs" />
8888
<CompileInclude="CodeFix\ReplaceWithSuggestion.fs" />
89-
<CompileInclude="CodeFix\PrefixUnusedValueWithUnderscore.fs" />
89+
<CompileInclude="CodeFix\RenameUnusedValue.fs" />
9090
<CompileInclude="CodeFix\ImplementInterfaceCodeFixProvider.fs" />
9191
<CompileInclude="CodeFix\SimplifyName.fs" />
9292
<CompileInclude="CodeFix\RemoveUnusedOpens.fs" />

‎vsintegration/src/FSharp.Editor/FSharp.Editor.resx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@
127127
<value>Implement interface without type annotation</value>
128128
</data>
129129
<dataname="PrefixValueNameWithUnderscore"xml:space="preserve">
130-
<value>Prefixvalue name with underscore</value>
130+
<value>Prefix'{0}' with underscore</value>
131131
</data>
132132
<dataname="RenameValueToUnderscore"xml:space="preserve">
133-
<value>Renamevalue to '_'</value>
133+
<value>Rename'{0}' to '_'</value>
134134
</data>
135135
<dataname="SimplifyName"xml:space="preserve">
136136
<value>Simplify name</value>
@@ -195,4 +195,7 @@
195195
<dataname="ExceptionsHeader"xml:space="preserve">
196196
<value>Exceptions:</value>
197197
</data>
198+
<dataname="RenameValueToDoubleUnderscore"xml:space="preserve">
199+
<value>Rename '{0}' to '__'</value>
200+
</data>
198201
</root>

‎vsintegration/src/FSharp.Editor/srFSharp.Editor.fs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module SR =
2020
letAddNewKeyword=lazy( GetString"AddNewKeyword")// "Add 'new' keyword"
2121
letPrefixValueNameWithUnderscore=lazy( GetString"PrefixValueNameWithUnderscore")// "Prefix value name with underscore"
2222
letRenameValueToUnderscore=lazy( GetString"RenameValueToUnderscore")// "Rename value to '_'"
23+
letRenameValueToDoubleUnderscore=lazy( GetString"RenameValueToDoubleUnderscore")// "Rename value to '__'"
2324
letImplementInterface=lazy( GetString"ImplementInterface")
2425
letImplementInterfaceWithoutTypeAnnotation=lazy( GetString"ImplementInterfaceWithoutTypeAnnotation")
2526
letSimplifyName=lazy( GetString"SimplifyName")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp