Skip to content

Commit

Permalink
consolidate to use new TryGetSymbolUses
Browse files Browse the repository at this point in the history
  • Loading branch information
dawedawe authored and baronfel committed Jul 1, 2023
1 parent caca858 commit 7fc82db
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 157 deletions.
89 changes: 1 addition & 88 deletions src/FsAutoComplete.Core/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -943,38 +943,6 @@ module Commands =
/// * When exact ranges are required
/// -> for "Rename"
let symbolUseWorkspace
(getDeclarationLocation: FSharpSymbolUse * NamedText -> Async<SymbolDeclarationLocation option>)
(findReferencesForSymbolInFile: (string<LocalPath> * FSharpProjectOptions * FSharpSymbol) -> Async<Range seq>)
(tryGetFileSource: string<LocalPath> -> Async<ResultOrString<NamedText>>)
(tryGetProjectOptionsForFsproj: string<LocalPath> -> Async<FSharpProjectOptions option>)
(getAllProjectOptions: unit -> Async<FSharpProjectOptions seq>)
(includeDeclarations: bool)
(includeBackticks: bool)
(errorOnFailureToFixRange: bool)
pos
lineStr
(text: NamedText)
(tyRes: ParseAndCheckResults)
: Async<Result<(FSharpSymbol * IDictionary<string<LocalPath>, Range[]>), string>> =
asyncResult {
let! symbolUse = tyRes.TryGetSymbolUse pos lineStr |> Result.ofOption (fun _ -> "No symbol")

return!
symbolUseWorkspaceAux
getDeclarationLocation
findReferencesForSymbolInFile
tryGetFileSource
tryGetProjectOptionsForFsproj
getAllProjectOptions
includeDeclarations
includeBackticks
errorOnFailureToFixRange
text
tyRes
symbolUse
}

let symbolUseWorkspace2
(getDeclarationLocation: FSharpSymbolUse * NamedText -> Async<SymbolDeclarationLocation option>)
(findReferencesForSymbolInFile: (string<LocalPath> * FSharpProjectOptions * FSharpSymbol) -> Async<Range seq>)
(tryGetFileSource: string<LocalPath> -> Async<ResultOrString<NamedText>>)
Expand Down Expand Up @@ -2291,61 +2259,6 @@ type Commands(checker: FSharpCompilerServiceChecker, state: State, hasAnalyzers:
tyRes
}

member x.SymbolUseWorkspace2
(
pos,
lineStr,
text: NamedText,
tyRes: ParseAndCheckResults,
includeDeclarations: bool,
includeBackticks: bool,
errorOnFailureToFixRange: bool
) =
asyncResult {
let findReferencesForSymbolInFile (file: string<LocalPath>, project, symbol) =
if File.Exists(UMX.untag file) then
// `FSharpChecker.FindBackgroundReferencesInFile` only works with existing files
checker.FindReferencesForSymbolInFile(UMX.untag file, project, symbol)
else
// untitled script files
async {
match state.TryGetFileCheckerOptionsWithLines(file) with
| Error _ -> return [||]
| Ok(opts, source) ->
match checker.TryGetRecentCheckResultsForFile(file, opts, source) with
| None -> return [||]
| Some tyRes ->
let! ct = Async.CancellationToken
let usages = tyRes.GetCheckResults.GetUsesOfSymbolInFile(symbol, ct)
return usages |> Seq.map (fun u -> u.Range)
}

let tryGetFileSource symbolFile =
state.TryGetFileSource symbolFile |> Async.singleton

let tryGetProjectOptionsForFsproj (fsprojPath: string<LocalPath>) =
state.ProjectController.GetProjectOptionsForFsproj(UMX.untag fsprojPath)
|> Async.singleton

let getAllProjectOptions () =
state.ProjectController.ProjectOptions |> Seq.map snd |> Async.singleton

return!
Commands.symbolUseWorkspace2
x.GetDeclarationLocation
findReferencesForSymbolInFile
tryGetFileSource
tryGetProjectOptionsForFsproj
getAllProjectOptions
includeDeclarations
includeBackticks
errorOnFailureToFixRange
pos
lineStr
text
tyRes
}

member x.RenameSymbolRange(pos: Position, tyRes: ParseAndCheckResults, lineStr: LineStr, text: NamedText) =
Commands.renameSymbolRange x.GetDeclarationLocation false pos lineStr text tyRes

Expand All @@ -2355,7 +2268,7 @@ type Commands(checker: FSharpCompilerServiceChecker, state: State, hasAnalyzers:
// safety check: rename valid?
let! _ = x.RenameSymbolRange(pos, tyRes, lineStr, text)

let! (_, usages) = x.SymbolUseWorkspace(pos, lineStr, text, tyRes, true, true, true)
let! usages = x.SymbolUseWorkspace(pos, lineStr, text, tyRes, true, true, true)
return usages
}

Expand Down
51 changes: 4 additions & 47 deletions src/FsAutoComplete/LspServers/AdaptiveFSharpLspServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1542,49 +1542,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
text
tyRes

let symbolUseWorkspace2
(includeDeclarations: bool)
(includeBackticks: bool)
(errorOnFailureToFixRange: bool)
pos
lineStr
text
tyRes
=
let findReferencesForSymbolInFile (file: string<LocalPath>, project, symbol) =
async {
let checker = checker |> AVal.force

if File.Exists(UMX.untag file) then
// `FSharpChecker.FindBackgroundReferencesInFile` only works with existing files
return! checker.FindReferencesForSymbolInFile(UMX.untag file, project, symbol)
else
// untitled script files
match! forceGetTypeCheckResultsStale file with
| Error _ -> return [||]
| Ok tyRes ->
let! ct = Async.CancellationToken
let usages = tyRes.GetCheckResults.GetUsesOfSymbolInFile(symbol, ct)
return usages |> Seq.map (fun u -> u.Range)
}

let tryGetProjectOptionsForFsproj (file: string<LocalPath>) =
forceGetProjectOptions file |> Async.map Option.ofResult

Commands.symbolUseWorkspace2
getDeclarationLocation
findReferencesForSymbolInFile
forceFindSourceText
tryGetProjectOptionsForFsproj
(getAllProjectOptions >> Async.map Array.toSeq)
includeDeclarations
includeBackticks
errorOnFailureToFixRange
pos
lineStr
text
tyRes

let codefixes =
let getFileLines = forceFindSourceText

Expand Down Expand Up @@ -1709,7 +1666,7 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
GenerateXmlDocumentation.fix tryGetParseResultsForFile
Run.ifEnabled
(fun _ -> config.AddPrivateAccessModifier)
(AddPrivateAccessModifier.fix tryGetParseResultsForFile symbolUseWorkspace2)
(AddPrivateAccessModifier.fix tryGetParseResultsForFile symbolUseWorkspace)
UseTripleQuotedInterpolation.fix tryGetParseResultsForFile getRangeText
RenameParamToMatchSignature.fix tryGetParseResultsForFile |])

Expand Down Expand Up @@ -2753,7 +2710,7 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
Commands.renameSymbolRange getDeclarationLocation false pos lineStr namedText.Lines tyRes
|> AsyncResult.mapError (fun msg -> JsonRpc.Error.Create(JsonRpc.ErrorCodes.invalidParams, msg))

let! (_, ranges) =
let! ranges =
symbolUseWorkspace true true true pos lineStr namedText.Lines tyRes
|> AsyncResult.mapError (fun msg -> JsonRpc.Error.Create(JsonRpc.ErrorCodes.invalidParams, msg))

Expand Down Expand Up @@ -2874,7 +2831,7 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
let! lineStr = tryGetLineStr pos namedText.Lines |> Result.ofStringErr
and! tyRes = forceGetTypeCheckResults filePath |> AsyncResult.ofStringErr

let! (_, usages) =
let! usages =
symbolUseWorkspace true true false pos lineStr namedText.Lines tyRes
|> AsyncResult.mapError (JsonRpc.Error.InternalErrorMessage)

Expand Down Expand Up @@ -3408,7 +3365,7 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
Arguments = None } }
)

| Ok(_, uses) ->
| Ok uses ->
let allUses = uses.Values |> Array.concat

let cmd =
Expand Down
25 changes: 3 additions & 22 deletions src/FsAutoComplete/LspServers/FsAutoComplete.Lsp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,25 +1164,6 @@ type FSharpLspServer(state: State, lspClient: FSharpLspClient) =
errorOnFailureToFixRange
)

let symbolUseWorkspace2
(includeDeclarations: bool)
(includeBackticks: bool)
(errorOnFailureToFixRange: bool)
pos
lineStr
text
tyRes
=
commands.SymbolUseWorkspace2(
pos,
lineStr,
text,
tyRes,
includeDeclarations,
includeBackticks,
errorOnFailureToFixRange
)

codefixes <-
[| Run.ifEnabled (fun _ -> config.UnusedOpensAnalyzer) (RemoveUnusedOpens.fix getFileLines)
Run.ifEnabled
Expand Down Expand Up @@ -1243,7 +1224,7 @@ type FSharpLspServer(state: State, lspClient: FSharpLspClient) =
GenerateXmlDocumentation.fix tryGetParseResultsForFile
Run.ifEnabled
(fun _ -> config.AddPrivateAccessModifier)
(AddPrivateAccessModifier.fix tryGetParseResultsForFile symbolUseWorkspace2)
(AddPrivateAccessModifier.fix tryGetParseResultsForFile symbolUseWorkspace)
UseTripleQuotedInterpolation.fix tryGetParseResultsForFile getRangeText
RenameParamToMatchSignature.fix tryGetParseResultsForFile |]

Expand Down Expand Up @@ -1758,7 +1739,7 @@ type FSharpLspServer(state: State, lspClient: FSharpLspClient) =
p
|> x.positionHandler (fun p pos tyRes lineStr lines ->
asyncResult {
let! (_, usages) =
let! usages =
commands.SymbolUseWorkspace(pos, lineStr, lines, tyRes, true, true, false)
|> AsyncResult.mapError (JsonRpc.Error.InternalErrorMessage)

Expand Down Expand Up @@ -2130,7 +2111,7 @@ type FSharpLspServer(state: State, lspClient: FSharpLspClient) =
Arguments = None } }
)

| Ok(_, uses) ->
| Ok uses ->
let allUses = uses.Values |> Array.concat

let cmd =
Expand Down

0 comments on commit 7fc82db

Please sign in to comment.