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

Commit92cbb6b

Browse files
refactoring, fix compilation
1 parent1bc77f9 commit92cbb6b

File tree

10 files changed

+128
-108
lines changed

10 files changed

+128
-108
lines changed

‎src/fsharp/NameResolution.fs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,8 +1223,8 @@ type ItemOccurence =
12231223
| RelatedText
12241224

12251225
typeOpenDeclaration=
1226-
| OpenoflongId:Identlist*moduleRefs:ModuleOrNamespaceReflist*scopem:range
1227-
| AutoOpenModuleofidents:stringlist*moduleRef:ModuleOrNamespaceRef*scopem:range
1226+
| OpenoflongId:Identlist*moduleRefs:ModuleOrNamespaceReflist*appliedScope:range
1227+
| AutoOpenModuleofidents:stringlist*moduleRef:ModuleOrNamespaceRef*appliedScope:range
12281228

12291229
/// An abstract type for reporting the results of name resolution and type checking.
12301230
typeITypecheckResultsSink=

‎src/fsharp/NameResolution.fsi‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,11 @@ type internal TcSymbolUses =
308308
memberGetFormatSpecifierLocationsAndArity:unit->(range* int)[]
309309

310310
/// Represents open declaration statement.
311-
#if COMPILER_PUBLIC_API
312-
type OpenDeclaration=
313-
#else
314311
type internal OpenDeclaration=
315-
#endif
316312
/// Ordinary open declaration, i.e. one which opens a namespace or module.
317-
| OpenoflongId:Identlist*moduleRefs:ModuleOrNamespaceReflist*scopem:range
313+
| OpenoflongId:Identlist*moduleRefs:ModuleOrNamespaceReflist*appliedScope:range
318314
/// Syntethic open declaration generated for auto open modules.
319-
| AutoOpenModuleofidents:stringlist*moduleRef:ModuleOrNamespaceRef*scopem:range
315+
| AutoOpenModuleofidents:stringlist*moduleRef:ModuleOrNamespaceRef*appliedScope:range
320316

321317
/// An abstract type for reporting the results of name resolution and type checking
322318
typeITypecheckResultsSink=

‎src/fsharp/symbols/Symbols.fs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,13 @@ type FSharpSymbol with
22592259
|:? FSharpMemberFunctionOrValueas x-> Some x.Accessibility
22602260
|_-> None
22612261

2262+
/// Represents open declaration in F# code.
2263+
typeFSharpOpenDeclaration=
2264+
/// Ordinary open declaration, i.e. one which opens a namespace or module.
2265+
| OpenoflongId:Identlist*modules:FSharpEntitylist*appliedScope:range
2266+
/// Syntethic open declaration generated for auto open modules.
2267+
| AutoOpenModuleofidents:stringlist*modul:FSharpEntity*appliedScope:range
2268+
22622269
[<Sealed>]
22632270
typeFSharpSymbolUse(g:TcGlobals,denv: DisplayEnv,symbol:FSharpSymbol,itemOcc,range: range)=
22642271
member__.Symbol= symbol

‎src/fsharp/symbols/Symbols.fsi‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open Microsoft.FSharp.Compiler
77
openMicrosoft.FSharp.Compiler.AccessibilityLogic
88
openMicrosoft.FSharp.Compiler.CompileOps
99
openMicrosoft.FSharp.Compiler.Range
10+
openMicrosoft.FSharp.Compiler.Ast
1011
openMicrosoft.FSharp.Compiler.Tast
1112
openMicrosoft.FSharp.Compiler.TcGlobals
1213
openMicrosoft.FSharp.Compiler.NameResolution
@@ -1065,7 +1066,16 @@ and [<Class>] internal FSharpAttribute =
10651066
/// Format the attribute using the rules of the given display context
10661067
memberFormat:context:FSharpDisplayContext->string
10671068

1068-
1069+
/// Represents open declaration in F# code.
1070+
#if COMPILER_PUBLIC_API
1071+
type FSharpOpenDeclaration=
1072+
#else
1073+
typeinternalFSharpOpenDeclaration=
1074+
#endif
1075+
/// Ordinary open declaration, i.e. one which opens a namespace or module.
1076+
| OpenoflongId:Identlist*modules:FSharpEntitylist*appliedScope:range
1077+
/// Syntethic open declaration generated for auto open modules.
1078+
| AutoOpenModuleofidents:stringlist*modul:FSharpEntity*appliedScope:range
10691079

10701080
/// Represents the use of an F# symbol from F# source code
10711081
[<Sealed>]

‎src/fsharp/vs/ServiceAnalysis.fs‎

Lines changed: 87 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,20 @@ module UnusedOpens =
1616
Idents:Set<string>
1717
/// Range of open statement itself.
1818
Range:range
19-
///Enclosing module or namespace range (that is, the scopeoninwhich this openstatement isvisible).
20-
ModuleRange:range}
19+
///Scopeon which this opendeclaration isapplied.
20+
AppliedScope:range}
2121

22-
letgetOpenStatements(openDeclarations:OpenDeclaration list):OpenStatement list=
22+
letgetOpenStatements(openDeclarations:FSharpOpenDeclaration list):OpenStatement list=
2323
openDeclarations
2424
|> List.choose(fun openDeclaration->
2525
match openDeclarationwith
26-
| OpenDeclaration.Open(longId, moduleRefs, scopem)whennot(List.isEmpty longId)->
27-
Some{ Idents=
28-
moduleRefs
29-
|> List.choose(fun x-> x.PublicPath|> Option.map(fun(Tast.PublicPath.PubPathpath)-> path|> String.concat"."))
30-
|> Set.ofList
26+
| FSharpOpenDeclaration.Open(longId, modules, appliedScope)whennot(List.isEmpty longId)->
27+
Some{ Idents= modules|> List.choose(fun x-> x.TryFullName)|> Set.ofList
3128
Range=
3229
letfirst= List.head longId
3330
letlast= List.last longId
34-
mkRangescopem.FileName first.idRange.Start last.idRange.End
35-
ModuleRange=scopem}
31+
mkRangeappliedScope.FileName first.idRange.Start last.idRange.End
32+
AppliedScope=appliedScope}
3633
|_-> None// for now
3734
)
3835

@@ -77,99 +74,105 @@ module UnusedOpens =
7774
typeNamespaceUse=
7875
{ Ident:string
7976
Location:range}
80-
81-
letgetUnusedOpens(symbolUses:FSharpSymbolUse[],openDeclarations:OpenDeclaration list,getSourceLineStr:int->string):range list=
82-
letgetPartNamespace(symbolUse:FSharpSymbolUse)(fullName:string)=
83-
// given a symbol range such as `Text.ISegment` and a full name of `MonoDevelop.Core.Text.ISegment`, return `MonoDevelop.Core`
84-
letlength= symbolUse.RangeAlternate.EndColumn- symbolUse.RangeAlternate.StartColumn
85-
letlengthDiff= fullName.Length- length-2
86-
if lengthDiff<=0|| lengthDiff> fullName.Length-1then None
87-
else Some fullName.[0..lengthDiff]
88-
89-
letgetPossibleNamespaces(symbolUse:FSharpSymbolUse):string list=
90-
letisQualified= symbolIsFullyQualified getSourceLineStr symbolUse
91-
92-
(match symbolUsewith
93-
| SymbolUse.Entity(ent, cleanFullNames)whennot(cleanFullNames|> List.exists isQualified)->
94-
Some(cleanFullNames, Some ent)
95-
| SymbolUse.Field fwhennot(isQualified f.FullName)->
96-
Some([f.FullName], Some f.DeclaringEntity)
97-
| SymbolUse.MemberFunctionOrValue mfvwhennot(isQualified mfv.FullName)->
98-
Some([mfv.FullName], mfv.EnclosingEntity)
99-
| SymbolUse.Operator opwhennot(isQualified op.FullName)->
100-
Some([op.FullName], op.EnclosingEntity)
101-
| SymbolUse.ActivePattern apwhennot(isQualified ap.FullName)->
102-
Some([ap.FullName], ap.EnclosingEntity)
103-
| SymbolUse.ActivePatternCase apcwhennot(isQualified apc.FullName)->
104-
Some([apc.FullName], apc.Group.EnclosingEntity)
105-
| SymbolUse.UnionCase ucwhennot(isQualified uc.FullName)->
106-
Some([uc.FullName], Some uc.ReturnType.TypeDefinition)
107-
| SymbolUse.Parameter pwhennot(isQualified p.FullName)&& p.Type.HasTypeDefinition->
108-
Some([p.FullName], Some p.Type.TypeDefinition)
109-
|_-> None)
110-
|> Option.map(fun(fullNames,declaringEntity)->
111-
[for namein fullNamesdo
112-
letpartNamespace= getPartNamespace symbolUse name
113-
yield partNamespace
114-
yield! entityNamespace declaringEntity])
115-
|> Option.toList
116-
|> List.concat
117-
|> List.choose id
118-
119-
letnamespacesInUse:NamespaceUse list=
120-
letimportantSymbolUses=
121-
symbolUses
122-
|> Array.filter(fun(symbolUse: FSharpSymbolUse)->
123-
not symbolUse.IsFromDefinition&&
124-
match symbolUse.Symbolwith
125-
|:? FSharpEntityas e->not e.IsNamespace
126-
|_->true
127-
)
128-
129-
importantSymbolUses
130-
|> Array.toList
131-
|> List.collect(fun su->
132-
letlineStr= getSourceLineStr su.RangeAlternate.StartLine
133-
letpartialName= QuickParse.GetPartialLongNameEx(lineStr, su.RangeAlternate.EndColumn-1)
134-
letqualifier= partialName.QualifyingIdents|> String.concat"."
135-
getPossibleNamespaces su
136-
|> List.distinct
137-
|> List.choose(fun ns->
138-
if qualifier=""then Some ns
139-
elif ns= qualifierthen None
140-
elif ns.EndsWith qualifierthen Some ns.[..(ns.Length- qualifier.Length)-2]
141-
else None)
142-
|> List.map(fun ns->
143-
{ Ident= ns
144-
Location= su.RangeAlternate}))
145-
146-
letfilter list:OpenStatement list=
147-
let recfilterInner acc(list:OpenStatement list)(seenOpenStatements:OpenStatement list)=
77+
78+
letgetPartNamespace(symbolUse:FSharpSymbolUse)(fullName:string)=
79+
// given a symbol range such as `Text.ISegment` and a full name of `MonoDevelop.Core.Text.ISegment`, return `MonoDevelop.Core`
80+
letlength= symbolUse.RangeAlternate.EndColumn- symbolUse.RangeAlternate.StartColumn
81+
letlengthDiff= fullName.Length- length-2
82+
if lengthDiff<=0|| lengthDiff> fullName.Length-1then None
83+
else Some fullName.[0..lengthDiff]
84+
85+
letgetPossibleNamespaces(getSourceLineStr:int->string)(symbolUse:FSharpSymbolUse):string list=
86+
letisQualified= symbolIsFullyQualified getSourceLineStr symbolUse
87+
88+
(match symbolUsewith
89+
| SymbolUse.Entity(ent, cleanFullNames)whennot(cleanFullNames|> List.exists isQualified)->
90+
Some(cleanFullNames, Some ent)
91+
| SymbolUse.Field fwhennot(isQualified f.FullName)->
92+
Some([f.FullName], Some f.DeclaringEntity)
93+
| SymbolUse.MemberFunctionOrValue mfvwhennot(isQualified mfv.FullName)->
94+
Some([mfv.FullName], mfv.EnclosingEntity)
95+
| SymbolUse.Operator opwhennot(isQualified op.FullName)->
96+
Some([op.FullName], op.EnclosingEntity)
97+
| SymbolUse.ActivePattern apwhennot(isQualified ap.FullName)->
98+
Some([ap.FullName], ap.EnclosingEntity)
99+
| SymbolUse.ActivePatternCase apcwhennot(isQualified apc.FullName)->
100+
Some([apc.FullName], apc.Group.EnclosingEntity)
101+
| SymbolUse.UnionCase ucwhennot(isQualified uc.FullName)->
102+
Some([uc.FullName], Some uc.ReturnType.TypeDefinition)
103+
| SymbolUse.Parameter pwhennot(isQualified p.FullName)&& p.Type.HasTypeDefinition->
104+
Some([p.FullName], Some p.Type.TypeDefinition)
105+
|_-> None)
106+
|> Option.map(fun(fullNames,declaringEntity)->
107+
[for namein fullNamesdo
108+
letpartNamespace= getPartNamespace symbolUse name
109+
yield partNamespace
110+
yield! entityNamespace declaringEntity])
111+
|> Option.toList
112+
|> List.concat
113+
|> List.choose id
114+
115+
letgetNamespacesInUse(getSourceLineStr:int->string)(symbolUses:FSharpSymbolUse[]):NamespaceUse list=
116+
letimportantSymbolUses=
117+
symbolUses
118+
|> Array.filter(fun(symbolUse: FSharpSymbolUse)->
119+
not symbolUse.IsFromDefinition&&
120+
match symbolUse.Symbolwith
121+
|:? FSharpEntityas e->not e.IsNamespace
122+
|_->true
123+
)
124+
125+
importantSymbolUses
126+
|> Array.toList
127+
|> List.collect(fun su->
128+
letlineStr= getSourceLineStr su.RangeAlternate.StartLine
129+
letpartialName= QuickParse.GetPartialLongNameEx(lineStr, su.RangeAlternate.EndColumn-1)
130+
letqualifier= partialName.QualifyingIdents|> String.concat"."
131+
getPossibleNamespaces getSourceLineStr su
132+
|> List.distinct
133+
|> List.choose(fun ns->
134+
if qualifier=""then Some ns
135+
elif ns= qualifierthen None
136+
elif ns.EndsWith qualifierthen Some ns.[..(ns.Length- qualifier.Length)-2]
137+
else None)
138+
|> List.map(fun ns->
139+
{ Ident= ns
140+
Location= su.RangeAlternate}))
141+
142+
letgetUnusedOpens(checkFileResults:FSharpCheckFileResults,getSourceLineStr:int->string):Async<rangelist>=
143+
144+
letfilter(openStatements:OpenStatement list)(namespacesInUse:NamespaceUse list):OpenStatement list=
145+
let recfilterInner acc(openStatements:OpenStatement list)(seenOpenStatements:OpenStatement list)=
148146

149147
letnotUsed(os:OpenStatement)=
150148
if os.Idents|> Set.exists(fun x-> x.StartsWith MangledGlobalName)thenfalse
151149
else
152150
letnotUsedAnywhere=
153151
not(namespacesInUse|> List.exists(fun nsu->
154-
rangeContainsRange os.ModuleRange nsu.Location&& os.Idents|> Set.contains nsu.Ident))
152+
rangeContainsRange os.AppliedScope nsu.Location&& os.Idents|> Set.contains nsu.Ident))
155153
if notUsedAnywherethentrue
156154
else
157155
letalreadySeen=
158156
seenOpenStatements
159157
|> List.exists(fun seenNs->
160158
// if such open statement has already been marked as used in this or outer module, we skip it
161159
// (that is, do not mark as used so far)
162-
rangeContainsRange seenNs.ModuleRange os.ModuleRange&&
160+
rangeContainsRange seenNs.AppliedScope os.AppliedScope&&
163161
not(os.Idents|> Set.intersect seenNs.Idents|> Set.isEmpty))
164162
alreadySeen
165163

166-
matchlistwith
164+
matchopenStatementswith
167165
| os:: xswhen notUsed os->
168166
filterInner(os:: acc) xs(os:: seenOpenStatements)
169167
| os:: xs->
170168
filterInner acc xs(os:: seenOpenStatements)
171169
|[]-> List.rev acc
172170

173-
filterInner[] list[]
174-
175-
openDeclarations|> getOpenStatements|> filter|> List.map(fun os-> os.Range)
171+
filterInner[] openStatements[]
172+
173+
async{
174+
let!symbolUses= checkFileResults.GetAllUsesOfAllSymbolsInFile()
175+
letnamespacesInUse= getNamespacesInUse getSourceLineStr symbolUses
176+
letopenStatements= getOpenStatements checkFileResults.OpenDeclarations
177+
return filter openStatements namespacesInUse|> List.map(fun os-> os.Range)
178+
}

‎src/fsharp/vs/ServiceAnalysis.fsi‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ module UnusedOpens =
1111
#else
1212
moduleinternalUnusedOpens=
1313
#endif
14-
valgetUnusedOpens:symbolUses:FSharpSymbolUse[]* openDeclarations: OpenDeclaration list* getSourceLineStr:(int-> string)-> range list
14+
/// Get all unused open declarations in a file
15+
valgetUnusedOpens:checkFileResults:FSharpCheckFileResults* getSourceLineStr:(int-> string)-> Async<range list>

‎src/fsharp/vs/service.fs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2033,7 +2033,15 @@ type FSharpCheckFileResults(filename: string, errors: FSharpErrorInfo[], scopeOp
20332033
[for mimplin scope.ImplementationFiles-> FSharpImplementationFileContents(cenv, mimpl)])
20342034

20352035
memberinfo.OpenDeclarations=
2036-
scopeOptX|> Option.map(fun scope-> scope.OpenDeclarations)
2036+
scopeOptX
2037+
|> Option.map(fun scope->
2038+
letcenv= Impl.cenv(scope.TcGlobals, scope.ThisCcu, scope.TcImports)
2039+
scope.OpenDeclarations|> List.map(function
2040+
| OpenDeclaration.Open(id, mods, appliedScope)->
2041+
FSharpOpenDeclaration.Open(id, mods|> List.map(fun x-> FSharpEntity(cenv, x)), appliedScope)
2042+
| OpenDeclaration.AutoOpenModule(ids, modul, appliedScope)->
2043+
FSharpOpenDeclaration.AutoOpenModule(ids, FSharpEntity(cenv, modul), appliedScope)))
2044+
|> Option.defaultValue[]
20372045

20382046
overrideinfo.ToString()="FSharpCheckFileResults("+ filename+")"
20392047

‎src/fsharp/vs/service.fsi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ type internal FSharpCheckFileResults =
274274
member ImplementationFiles: FSharpImplementationFileContents list option
275275

276276
/// Open declarations in the file, including auto open modules.
277-
member OpenDeclarations:OpenDeclaration list option
277+
member OpenDeclarations:FSharpOpenDeclaration list
278278

279279
/// A handle to the results of CheckFileInProject.
280280
[<Sealed>]

‎vsintegration/src/FSharp.Editor/Diagnostics/UnusedOpensDiagnosticAnalyzer.fs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ type internal UnusedOpensDiagnosticAnalyzer() =
4343
do! Option.guard Settings.CodeFixes.UnusedOpens
4444
let!sourceText= document.GetTextAsync()
4545
let!_,_,checkResults= checker.ParseAndCheckDocument(document, options, sourceText= sourceText, allowStaleResults=true, userOpName= userOpName)
46-
let!openDeclarations= checkResults.OpenDeclarations
46+
letopenDeclarations= checkResults.OpenDeclarations
4747

4848
Logging.Logging.logInfof"*** OpenDeclarations:%+A" openDeclarations
4949

50-
51-
let!symbolUses= checkResults.GetAllUsesOfAllSymbolsInFile()|> liftAsync
52-
return UnusedOpens.getUnusedOpens(symbolUses, openDeclarations,fun lineNumber-> sourceText.Lines.[Line.toZ lineNumber].ToString())
50+
return! UnusedOpens.getUnusedOpens(checkResults,fun lineNumber-> sourceText.Lines.[Line.toZ lineNumber].ToString())|> liftAsync
5351
}
5452

5553
overridethis.AnalyzeSemanticsAsync(document:Document,cancellationToken:CancellationToken)=

‎vsintegration/tests/unittests/UnusedOpensTests.fs‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@ let private checker = FSharpChecker.Create()
2828
let(=>)(source: string)(expectedRanges:((*line*)int*((*start column*)int*(*end column*)int)) list)=
2929
letsourceLines= source.Split([|"\r\n";"\n";"\r"|], StringSplitOptions.None)
3030

31-
letparsedInput,checkFileResults=
32-
letparseResults,checkFileAnswer= checker.ParseAndCheckFileInProject(filePath,0, source, projectOptions)|> Async.RunSynchronously
31+
let_,checkFileAnswer= checker.ParseAndCheckFileInProject(filePath,0, source, projectOptions)|> Async.RunSynchronously
32+
33+
letcheckFileResults=
3334
match checkFileAnswerwith
3435
| FSharpCheckFileAnswer.Aborted-> failwithf"ParseAndCheckFileInProject aborted"
35-
| FSharpCheckFileAnswer.Succeeded(checkFileResults)->
36-
match parseResults.ParseTreewith
37-
| None-> failwith"Parse returns None ParseTree"
38-
| Some parsedInput-> parsedInput, checkFileResults
36+
| FSharpCheckFileAnswer.Succeeded(checkFileResults)-> checkFileResults
3937

40-
letallSymbolUses= checkFileResults.GetAllUsesOfAllSymbolsInFile()|> Async.RunSynchronously
41-
letunusedOpenRanges= UnusedOpens.getUnusedOpens(allSymbolUses, parsedInput,fun lineNum-> sourceLines.[Line.toZ lineNum])
38+
letunusedOpenRanges= UnusedOpens.getUnusedOpens(checkFileResults,fun lineNum-> sourceLines.[Line.toZ lineNum])|> Async.RunSynchronously
4239

4340
unusedOpenRanges
4441
|> List.map(fun x-> x.StartLine,(x.StartColumn, x.EndColumn))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp