Skip to content

Commit 4040a5e

Browse files
committed
Add default implementations to LanguageService for methods that are not expected to be implemented by all language services
With the introduction of secondary language services, we don’t expect every language service to implement every request anymore. To simplify the addition of language services like `DocumentationLanguageService` add default implementations for methods that satisfy the following criteria: - `SourceKitLSPServer` does not expect side effects to happen when they are called - The method can throw or there is a reasonable default value - It is reasonable to expect that not all language services need to implement it
1 parent 3274349 commit 4040a5e

File tree

7 files changed

+208
-307
lines changed

7 files changed

+208
-307
lines changed

Sources/ClangLanguageService/ClangLanguageService.swift

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ package actor ClangLanguageService: LanguageService, MessageHandler {
229229
// Theoretically they could have changed and we would need to inform SourceKitLSPServer about them.
230230
// But since SourceKitLSPServer more or less ignores them right now anyway, this should be fine for now.
231231
_ = try await self.initialize(initializeRequest)
232-
self.clientInitialized(InitializedNotification())
232+
await self.clientInitialized(InitializedNotification())
233233
if let sourceKitLSPServer {
234234
await sourceKitLSPServer.reopenDocuments(for: self)
235235
} else {
@@ -309,7 +309,7 @@ package actor ClangLanguageService: LanguageService, MessageHandler {
309309
logger.fault(
310310
"Did not receive reply from clangd after \(self.options.semanticServiceRestartTimeoutOrDefault, privacy: .public). Terminating and restarting clangd."
311311
)
312-
self.crash()
312+
await self.crash()
313313
throw error
314314
}
315315
}
@@ -318,7 +318,7 @@ package actor ClangLanguageService: LanguageService, MessageHandler {
318318
return nil
319319
}
320320

321-
package func crash() {
321+
package func crash() async {
322322
clangdProcess?.terminateImmediately()
323323
}
324324
}
@@ -383,7 +383,7 @@ extension ClangLanguageService {
383383
return result
384384
}
385385

386-
package func clientInitialized(_ initialized: InitializedNotification) {
386+
package func clientInitialized(_ initialized: InitializedNotification) async {
387387
clangd.send(initialized)
388388
}
389389

@@ -430,11 +430,11 @@ extension ClangLanguageService {
430430
clangd.send(notification)
431431
}
432432

433-
package func willSaveDocument(_ notification: WillSaveTextDocumentNotification) {
433+
package func willSaveDocument(_ notification: WillSaveTextDocumentNotification) async {
434434

435435
}
436436

437-
package func didSaveDocument(_ notification: DidSaveTextDocumentNotification) {
437+
package func didSaveDocument(_ notification: DidSaveTextDocumentNotification) async {
438438
clangd.send(notification)
439439
}
440440

@@ -458,7 +458,7 @@ extension ClangLanguageService {
458458
}
459459
}
460460

461-
package func documentDependenciesUpdated(_ uris: Set<DocumentURI>) {
461+
package func documentDependenciesUpdated(_ uris: Set<DocumentURI>) async {
462462
for uri in uris {
463463
// In order to tell clangd to reload an AST, we send it an empty `didChangeTextDocument`
464464
// with `forceRebuild` set in case any missing header files have been added.
@@ -510,20 +510,6 @@ extension ClangLanguageService {
510510
return try await forwardRequestToClangd(req)
511511
}
512512

513-
package func symbolGraph(
514-
forOnDiskContentsOf symbolDocumentUri: DocumentURI,
515-
at location: SymbolLocation
516-
) async throws -> String {
517-
throw ResponseError.internalError("Symbol graph is currently not supported for clang files")
518-
}
519-
520-
package func symbolGraph(
521-
for snapshot: SourceKitLSP.DocumentSnapshot,
522-
at position: LanguageServerProtocol.Position
523-
) async throws -> (symbolGraph: String, usr: String, overrideDocComments: [String]) {
524-
throw ResponseError.internalError("Symbol graph is currently not supported for clang files")
525-
}
526-
527513
package func documentSymbolHighlight(_ req: DocumentHighlightRequest) async throws -> [DocumentHighlight]? {
528514
return try await forwardRequestToClangd(req)
529515
}
@@ -630,15 +616,6 @@ extension ClangLanguageService {
630616
return try await forwardRequestToClangd(req)
631617
}
632618

633-
package func openGeneratedInterface(
634-
document: DocumentURI,
635-
moduleName: String,
636-
groupName: String?,
637-
symbolUSR symbol: String?
638-
) async throws -> GeneratedInterfaceDetails? {
639-
throw ResponseError.unknown("unsupported method")
640-
}
641-
642619
package func indexedRename(_ request: IndexedRenameRequest) async throws -> WorkspaceEdit? {
643620
return try await forwardRequestToClangd(request)
644621
}
@@ -649,10 +626,6 @@ extension ClangLanguageService {
649626
return try await forwardRequestToClangd(req)
650627
}
651628

652-
package func getReferenceDocument(_ req: GetReferenceDocumentRequest) async throws -> GetReferenceDocumentResponse {
653-
throw ResponseError.unknown("unsupported method")
654-
}
655-
656629
package func rename(_ renameRequest: RenameRequest) async throws -> (edits: WorkspaceEdit, usr: String?) {
657630
async let edits = forwardRequestToClangd(renameRequest)
658631
let symbolInfoRequest = SymbolInfoRequest(
@@ -663,7 +636,10 @@ extension ClangLanguageService {
663636
return (try await edits ?? WorkspaceEdit(), symbolDetail?.usr)
664637
}
665638

666-
package func syntacticDocumentTests(for uri: DocumentURI, in workspace: Workspace) async -> [AnnotatedTestItem]? {
639+
package func syntacticDocumentTests(
640+
for uri: DocumentURI,
641+
in workspace: Workspace
642+
) async throws -> [AnnotatedTestItem]? {
667643
return nil
668644
}
669645

Sources/DocumentationLanguageService/DoccDocumentationHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ extension DocumentationLanguageService {
108108
let symbolOccurrence = try await index.primaryDefinitionOrDeclarationOccurrence(
109109
ofDocCSymbolLink: symbolLink,
110110
fetchSymbolGraph: { location in
111-
guard let symbolWorkspace = try await workspaceForDocument(uri: location.documentUri) else {
111+
guard let symbolWorkspace = await sourceKitLSPServer.workspaceForDocument(uri: location.documentUri) else {
112112
throw ResponseError.internalError("Unable to find language service for \(location.documentUri)")
113113
}
114114
let languageService = try await sourceKitLSPServer.primaryLanguageService(
@@ -126,7 +126,7 @@ extension DocumentationLanguageService {
126126
throw ResponseError.requestFailed(doccDocumentationError: .symbolNotFound(symbolName))
127127
}
128128
let symbolDocumentUri = symbolOccurrence.location.documentUri
129-
guard let symbolWorkspace = try await workspaceForDocument(uri: symbolDocumentUri) else {
129+
guard let symbolWorkspace = await sourceKitLSPServer.workspaceForDocument(uri: symbolDocumentUri) else {
130130
throw ResponseError.internalError("Unable to find language service for \(symbolDocumentUri)")
131131
}
132132
let languageService = try await sourceKitLSPServer.primaryLanguageService(
@@ -182,7 +182,7 @@ extension DocumentationLanguageService {
182182
catalogURL: catalogURL,
183183
for: symbolUSR,
184184
fetchSymbolGraph: { location in
185-
guard let symbolWorkspace = try await workspaceForDocument(uri: location.documentUri) else {
185+
guard let symbolWorkspace = await sourceKitLSPServer.workspaceForDocument(uri: location.documentUri) else {
186186
throw ResponseError.internalError("Unable to find language service for \(location.documentUri)")
187187
}
188188
let languageService = try await sourceKitLSPServer.primaryLanguageService(

Sources/DocumentationLanguageService/DocumentationLanguageService.swift

Lines changed: 0 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ package actor DocumentationLanguageService: LanguageService, Sendable {
3434
}
3535
}
3636

37-
package static var builtInCommands: [String] { [] }
38-
3937
package static var experimentalCapabilities: [String: LSPAny] {
4038
return [
4139
DoccDocumentationRequest.method: .dictionary(["version": .int(1)])
@@ -53,13 +51,6 @@ package actor DocumentationLanguageService: LanguageService, Sendable {
5351
self.documentationManager = DocCDocumentationManager(buildServerManager: workspace.buildServerManager)
5452
}
5553

56-
func workspaceForDocument(uri: DocumentURI) async throws -> Workspace? {
57-
guard let sourceKitLSPServer else {
58-
throw ResponseError.unknown("Connection to the editor closed")
59-
}
60-
return await sourceKitLSPServer.workspaceForDocument(uri: uri)
61-
}
62-
6354
package nonisolated func canHandle(workspace: Workspace, toolchain: Toolchain) -> Bool {
6455
return true
6556
}
@@ -72,10 +63,6 @@ package actor DocumentationLanguageService: LanguageService, Sendable {
7263
)
7364
}
7465

75-
package func clientInitialized(_ initialized: InitializedNotification) async {
76-
// Nothing to set up
77-
}
78-
7966
package func shutdown() async {
8067
// Nothing to tear down
8168
}
@@ -109,200 +96,4 @@ package actor DocumentationLanguageService: LanguageService, Sendable {
10996
) async {
11097
// The DocumentationLanguageService does not do anything with document events
11198
}
112-
113-
package func willSaveDocument(_ notification: WillSaveTextDocumentNotification) async {
114-
// The DocumentationLanguageService does not do anything with document events
115-
}
116-
117-
package func didSaveDocument(_ notification: DidSaveTextDocumentNotification) async {
118-
// The DocumentationLanguageService does not do anything with document events
119-
}
120-
121-
package func filesDidChange(_ events: [FileEvent]) async {
122-
await documentationManager.filesDidChange(events)
123-
}
124-
125-
package func documentUpdatedBuildSettings(_ uri: DocumentURI) async {
126-
// The DocumentationLanguageService does not do anything with document events
127-
}
128-
129-
package func documentDependenciesUpdated(_ uris: Set<DocumentURI>) async {
130-
// The DocumentationLanguageService does not do anything with document events
131-
}
132-
133-
package func completion(_ req: CompletionRequest) async throws -> CompletionList {
134-
CompletionList(isIncomplete: false, items: [])
135-
}
136-
137-
package func completionItemResolve(_ req: CompletionItemResolveRequest) async throws -> CompletionItem {
138-
return req.item
139-
}
140-
141-
package func hover(_ req: HoverRequest) async throws -> HoverResponse? {
142-
nil
143-
}
144-
145-
package func symbolInfo(_ request: SymbolInfoRequest) async throws -> [SymbolDetails] {
146-
[]
147-
}
148-
149-
package func symbolGraph(
150-
forOnDiskContentsOf symbolDocumentUri: DocumentURI,
151-
at location: SymbolLocation
152-
) async throws -> String {
153-
throw ResponseError.internalError("Not applicable")
154-
}
155-
156-
package func symbolGraph(
157-
for snapshot: SourceKitLSP.DocumentSnapshot,
158-
at position: LanguageServerProtocol.Position
159-
) async throws -> (symbolGraph: String, usr: String, overrideDocComments: [String]) {
160-
throw ResponseError.internalError("Not applicable")
161-
}
162-
163-
package func openGeneratedInterface(
164-
document: DocumentURI,
165-
moduleName: String,
166-
groupName: String?,
167-
symbolUSR symbol: String?
168-
) async throws -> GeneratedInterfaceDetails? {
169-
nil
170-
}
171-
172-
package func definition(_ request: DefinitionRequest) async throws -> LocationsOrLocationLinksResponse? {
173-
nil
174-
}
175-
176-
package func declaration(_ request: DeclarationRequest) async throws -> LocationsOrLocationLinksResponse? {
177-
nil
178-
}
179-
180-
package func documentSymbolHighlight(_ req: DocumentHighlightRequest) async throws -> [DocumentHighlight]? {
181-
nil
182-
}
183-
184-
package func foldingRange(_ req: FoldingRangeRequest) async throws -> [FoldingRange]? {
185-
nil
186-
}
187-
188-
package func documentSymbol(_ req: DocumentSymbolRequest) async throws -> DocumentSymbolResponse? {
189-
nil
190-
}
191-
192-
package func documentColor(_ req: DocumentColorRequest) async throws -> [ColorInformation] {
193-
[]
194-
}
195-
196-
package func documentSemanticTokens(
197-
_ req: DocumentSemanticTokensRequest
198-
) async throws -> DocumentSemanticTokensResponse? {
199-
nil
200-
}
201-
202-
package func documentSemanticTokensDelta(
203-
_ req: DocumentSemanticTokensDeltaRequest
204-
) async throws -> DocumentSemanticTokensDeltaResponse? {
205-
nil
206-
}
207-
208-
package func documentSemanticTokensRange(
209-
_ req: DocumentSemanticTokensRangeRequest
210-
) async throws -> DocumentSemanticTokensResponse? {
211-
nil
212-
}
213-
214-
package func colorPresentation(_ req: ColorPresentationRequest) async throws -> [ColorPresentation] {
215-
[]
216-
}
217-
218-
package func codeAction(_ req: CodeActionRequest) async throws -> CodeActionRequestResponse? {
219-
nil
220-
}
221-
222-
package func inlayHint(_ req: InlayHintRequest) async throws -> [InlayHint] {
223-
[]
224-
}
225-
226-
package func codeLens(_ req: CodeLensRequest) async throws -> [CodeLens] {
227-
[]
228-
}
229-
230-
package func documentDiagnostic(_ req: DocumentDiagnosticsRequest) async throws -> DocumentDiagnosticReport {
231-
.full(RelatedFullDocumentDiagnosticReport(items: []))
232-
}
233-
234-
package func documentFormatting(_ req: DocumentFormattingRequest) async throws -> [TextEdit]? {
235-
nil
236-
}
237-
238-
package func documentRangeFormatting(
239-
_ req: LanguageServerProtocol.DocumentRangeFormattingRequest
240-
) async throws -> [LanguageServerProtocol.TextEdit]? {
241-
return nil
242-
}
243-
244-
package func documentOnTypeFormatting(_ req: DocumentOnTypeFormattingRequest) async throws -> [TextEdit]? {
245-
return nil
246-
}
247-
248-
package func rename(_ request: RenameRequest) async throws -> (edits: WorkspaceEdit, usr: String?) {
249-
(edits: WorkspaceEdit(), usr: nil)
250-
}
251-
252-
package func editsToRename(
253-
locations renameLocations: [RenameLocation],
254-
in snapshot: DocumentSnapshot,
255-
oldName: CrossLanguageName,
256-
newName: CrossLanguageName
257-
) async throws -> [TextEdit] {
258-
[]
259-
}
260-
261-
package func prepareRename(
262-
_ request: PrepareRenameRequest
263-
) async throws -> (prepareRename: PrepareRenameResponse, usr: String?)? {
264-
nil
265-
}
266-
267-
package func indexedRename(_ request: IndexedRenameRequest) async throws -> WorkspaceEdit? {
268-
nil
269-
}
270-
271-
package func editsToRenameParametersInFunctionBody(
272-
snapshot: DocumentSnapshot,
273-
renameLocation: RenameLocation,
274-
newName: CrossLanguageName
275-
) async -> [TextEdit] {
276-
[]
277-
}
278-
279-
package func executeCommand(_ req: ExecuteCommandRequest) async throws -> LSPAny? {
280-
nil
281-
}
282-
283-
package func getReferenceDocument(_ req: GetReferenceDocumentRequest) async throws -> GetReferenceDocumentResponse {
284-
GetReferenceDocumentResponse(content: "")
285-
}
286-
287-
package func syntacticDocumentTests(
288-
for uri: DocumentURI,
289-
in workspace: Workspace
290-
) async throws -> [AnnotatedTestItem]? {
291-
nil
292-
}
293-
294-
package static func syntacticTestItems(in uri: DocumentURI) async -> [AnnotatedTestItem] {
295-
return []
296-
}
297-
298-
package func canonicalDeclarationPosition(
299-
of position: Position,
300-
in uri: DocumentURI
301-
) async -> Position? {
302-
nil
303-
}
304-
305-
package func crash() async {
306-
// There's no way to crash the DocumentationLanguageService
307-
}
30899
}

0 commit comments

Comments
 (0)