-
Notifications
You must be signed in to change notification settings - Fork 34
/
Rename.fs
182 lines (155 loc) · 7.84 KB
/
Rename.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
namespace CSharpLanguageServer.Handlers
open System
open System.Threading
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.CSharp.Syntax
open Microsoft.CodeAnalysis.FindSymbols
open Microsoft.CodeAnalysis.Rename
open FSharpPlus
open Ionide.LanguageServerProtocol.Server
open Ionide.LanguageServerProtocol.Types
open Ionide.LanguageServerProtocol.Types.LspResult
open CSharpLanguageServer.State
open CSharpLanguageServer.Logging
open CSharpLanguageServer.Conversions
open CSharpLanguageServer.Types
[<RequireQualifiedAccess>]
module Rename =
let private logger = LogProvider.getLoggerByName "Rename"
let private lspDocChangesFromSolutionDiff
(ct: CancellationToken)
(originalSolution: Solution)
(updatedSolution: Solution)
(tryGetDocVersionByUri: string -> int option)
: Async<TextDocumentEdit[]> =
let getEdits
(originalSolution: Solution)
(updatedSolution: Solution)
(docId: DocumentId)
: Async<TextDocumentEdit> = async {
let originalDoc = originalSolution.GetDocument(docId)
let! originalDocText = originalDoc.GetTextAsync(ct) |> Async.AwaitTask
let updatedDoc = updatedSolution.GetDocument(docId)
let! docChanges = updatedDoc.GetTextChangesAsync(originalDoc, ct) |> Async.AwaitTask
let diffEdits: U2<TextEdit, AnnotatedTextEdit> array =
docChanges
|> Seq.sortBy (fun c -> c.Span.Start)
|> Seq.map (TextEdit.fromTextChange originalDocText.Lines)
|> Seq.map U2.C1
|> Array.ofSeq
let uri = originalDoc.FilePath |> Path.toUri
let textEditDocument =
{ Uri = uri
Version = tryGetDocVersionByUri uri }
return
{ TextDocument = textEditDocument
Edits = diffEdits }
}
updatedSolution.GetChanges(originalSolution).GetProjectChanges()
|> Seq.collect (fun projectChange -> projectChange.GetChangedDocuments())
|> Seq.map (getEdits originalSolution updatedSolution)
|> Async.Parallel
|> map (Seq.distinct >> Array.ofSeq)
let private dynamicRegistration (clientCapabilities: ClientCapabilities) =
clientCapabilities.TextDocument
|> Option.bind (fun x -> x.Rename)
|> Option.bind (fun x -> x.DynamicRegistration)
|> Option.defaultValue false
let private prepareSupport (clientCapabilities: ClientCapabilities) =
clientCapabilities.TextDocument
|> Option.bind (fun x -> x.Rename)
|> Option.bind (fun x -> x.PrepareSupport)
|> Option.defaultValue false
let provider (clientCapabilities: ClientCapabilities): U2<bool, RenameOptions> option =
match dynamicRegistration clientCapabilities, prepareSupport clientCapabilities with
| true, _ -> None
| false, true -> Some (U2.C2 { PrepareProvider = Some true; WorkDoneProgress = None })
| false, false -> Some (U2.C1 true)
let registration (clientCapabilities: ClientCapabilities) : Registration option =
match dynamicRegistration clientCapabilities with
| false -> None
| true ->
let registerOptions: RenameRegistrationOptions = {
PrepareProvider = Some (prepareSupport clientCapabilities)
DocumentSelector = Some defaultDocumentSelector
WorkDoneProgress = None
}
Some
{ Id = Guid.NewGuid().ToString()
Method = "textDocument/rename"
RegisterOptions = registerOptions |> serialize |> Some }
let prepare (context: ServerRequestContext)
(p: PrepareRenameParams)
: AsyncLspResult<PrepareRenameResult option> = async {
match context.GetUserDocument p.TextDocument.Uri with
| None -> return None |> success
| Some doc ->
let! ct = Async.CancellationToken
let! docSyntaxTree = doc.GetSyntaxTreeAsync(ct) |> Async.AwaitTask
let! docText = doc.GetTextAsync(ct) |> Async.AwaitTask
let position = Position.toRoslynPosition docText.Lines p.Position
let! symbolMaybe = SymbolFinder.FindSymbolAtPositionAsync(doc, position, ct) |> Async.AwaitTask
let symbolIsFromMetadata =
symbolMaybe
|> Option.ofObj
|> Option.map (fun s -> s.MetadataToken <> 0)
|> Option.defaultValue false
let linePositionSpan =
Range.toLinePositionSpan docText.Lines { Start = p.Position; End = p.Position }
let textSpan = docText.Lines.GetTextSpan(linePositionSpan)
let! rootNode = docSyntaxTree.GetRootAsync(ct) |> Async.AwaitTask
let nodeOnPos =
rootNode.FindNode(textSpan, findInsideTrivia = false, getInnermostNodeForTie = true)
let spanMaybe =
match nodeOnPos with
| :? PropertyDeclarationSyntax as propDec -> propDec.Identifier.Span |> Some
| :? MethodDeclarationSyntax as methodDec -> methodDec.Identifier.Span |> Some
| :? BaseTypeDeclarationSyntax as typeDec -> typeDec.Identifier.Span |> Some
| :? VariableDeclaratorSyntax as varDec -> varDec.Identifier.Span |> Some
| :? EnumMemberDeclarationSyntax as enumMemDec -> enumMemDec.Identifier.Span |> Some
| :? ParameterSyntax as paramSyn -> paramSyn.Identifier.Span |> Some
| :? NameSyntax as nameSyn -> nameSyn.Span |> Some
| :? SingleVariableDesignationSyntax as designationSyn -> designationSyn.Identifier.Span |> Some
| :? ForEachStatementSyntax as forEachSyn -> forEachSyn.Identifier.Span |> Some
| :? LocalFunctionStatementSyntax as localFunStSyn -> localFunStSyn.Identifier.Span |> Some
| node ->
logger.debug (
Log.setMessage "textDocument/prepareRename: unhandled Type={type}"
>> Log.addContext "type" (node.GetType().Name)
)
None
let rangeWithPlaceholderMaybe: PrepareRenameResult option =
match spanMaybe, symbolIsFromMetadata with
| Some span, false ->
let range = Range.fromTextSpan docText.Lines span
let text = docText.ToString(span)
{ Range = range; Placeholder = text } |> U3.C2 |> Some
| _, _ -> None
return rangeWithPlaceholderMaybe |> success
}
let handle
(context: ServerRequestContext)
(p: RenameParams)
: AsyncLspResult<WorkspaceEdit option> = async {
match! context.FindSymbol' p.TextDocument.Uri p.Position with
| None -> return None |> success
| Some (symbol, doc) ->
let! ct = Async.CancellationToken
let originalSolution = doc.Project.Solution
let! updatedSolution =
Renamer.RenameSymbolAsync(
doc.Project.Solution,
symbol,
SymbolRenameOptions(RenameOverloads = true, RenameFile = true),
p.NewName,
ct
)
|> Async.AwaitTask
let! docTextEdit =
lspDocChangesFromSolutionDiff
ct
originalSolution
updatedSolution
(fun uri -> context.OpenDocs.TryFind uri |> Option.map _.Version)
return WorkspaceEdit.Create(docTextEdit, context.ClientCapabilities) |> Some |> success
}