Skip to content

Commit 156139f

Browse files
authored
Merge pull request #202 from ahoppen/match-spec
Update the struct definitions to match version 3.14 of the LSP spec
2 parents b38e241 + 5f0b2bc commit 156139f

19 files changed

+425
-73
lines changed

Sources/LanguageServerProtocol/ClientCapabilities.swift

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,23 @@ public struct TextDocumentClientCapabilities: Hashable, Codable {
225225

226226
/// Capabilities specific to `SignatureInformation`.
227227
public struct SignatureInformation: Hashable, Codable {
228+
public struct ParameterInformation: Hashable, Codable {
229+
/// The client supports processing label offsets instead of a simple label string.
230+
var labelOffsetSupport: Bool? = nil
231+
232+
public init(labelOffsetSupport: Bool? = nil) {
233+
self.labelOffsetSupport = labelOffsetSupport
234+
}
235+
}
236+
228237
/// Documentation formats supported by the client from most to least preferred.
229-
public var signatureInformation: [MarkupKind]? = nil
238+
public var documentationFormat: [MarkupKind]? = nil
239+
240+
public var parameterInformation: ParameterInformation? = nil
230241

231-
public init(signatureInformation: [MarkupKind]? = nil) {
232-
self.signatureInformation = signatureInformation
242+
public init(signatureInformation: [MarkupKind]? = nil, parameterInformation: ParameterInformation? = nil) {
243+
self.documentationFormat = signatureInformation
244+
self.parameterInformation = parameterInformation
233245
}
234246
}
235247

@@ -276,6 +288,19 @@ public struct TextDocumentClientCapabilities: Hashable, Codable {
276288
}
277289
}
278290

291+
public struct DynamicRegistrationLinkSupportCapability: Hashable, Codable {
292+
/// Whether the client supports dynamic registaration of this request.
293+
public var dynamicRegistration: Bool? = nil
294+
295+
/// The client supports additional metadata in the form of declaration links.
296+
public var linkSupport: Bool? = nil
297+
298+
public init(dynamicRegistration: Bool? = nil, linkSupport: Bool?) {
299+
self.dynamicRegistration = dynamicRegistration
300+
self.linkSupport = linkSupport
301+
}
302+
}
303+
279304
/// Capabilities specific to the `textDocument/codeAction` request.
280305
public struct CodeAction: Hashable, Codable {
281306

@@ -294,24 +319,38 @@ public struct TextDocumentClientCapabilities: Hashable, Codable {
294319
}
295320
}
296321

297-
/// Whether the client supports dynamic registaration of this request.
298-
public var dynamicRegistration: Bool? = nil
299-
300322
public var codeActionKind: CodeActionKind
301323

302-
public init(dynamicRegistration: Bool? = nil, codeActionKind: CodeActionKind) {
303-
self.dynamicRegistration = dynamicRegistration
324+
public init(codeActionKind: CodeActionKind) {
304325
self.codeActionKind = codeActionKind
305326
}
306327
}
307328

329+
/// Whether the client supports dynamic registaration of this request.
330+
public var dynamicRegistration: Bool?
331+
308332
public var codeActionLiteralSupport: CodeActionLiteralSupport? = nil
309333

310-
public init(codeActionLiteralSupport: CodeActionLiteralSupport? = nil) {
334+
public init(dynamicRegistration: Bool? = nil, codeActionLiteralSupport: CodeActionLiteralSupport? = nil) {
311335
self.codeActionLiteralSupport = codeActionLiteralSupport
312336
}
313337
}
314338

339+
/// Capabilities specific to `textDocument/rename`.
340+
public struct Rename: Hashable, Codable {
341+
342+
/// Whether the client supports dynamic registaration of this request.
343+
public var dynamicRegistration: Bool?
344+
345+
/// The client supports testing for validity of rename operations before execution.
346+
public var prepareSupport: Bool?
347+
348+
public init(dynamicRegistration: Bool? = nil, prepareSupport: Bool? = nil) {
349+
self.dynamicRegistration = dynamicRegistration
350+
self.prepareSupport = prepareSupport
351+
}
352+
}
353+
315354
/// Capabilities specific to `textDocument/publishDiagnostics`.
316355
public struct PublishDiagnostics: Hashable, Codable {
317356
/// Whether the client accepts diagnostics with related information.
@@ -364,11 +403,13 @@ public struct TextDocumentClientCapabilities: Hashable, Codable {
364403

365404
public var onTypeFormatting: DynamicRegistrationCapability? = nil
366405

367-
public var definition: DynamicRegistrationCapability? = nil
406+
public var declaration: DynamicRegistrationLinkSupportCapability? = nil
407+
408+
public var definition: DynamicRegistrationLinkSupportCapability? = nil
368409

369-
public var typeDefinition: DynamicRegistrationCapability? = nil
410+
public var typeDefinition: DynamicRegistrationLinkSupportCapability? = nil
370411

371-
public var implementation: DynamicRegistrationCapability? = nil
412+
public var implementation: DynamicRegistrationLinkSupportCapability? = nil
372413

373414
public var codeAction: CodeAction? = nil
374415

@@ -384,13 +425,27 @@ public struct TextDocumentClientCapabilities: Hashable, Codable {
384425

385426
public var foldingRange: FoldingRange? = nil
386427

387-
public init(synchronization: Synchronization? = nil, completion: Completion? = nil, hover: Hover? = nil,
388-
signatureHelp: SignatureHelp? = nil, references: DynamicRegistrationCapability? = nil, documentHighlight: DynamicRegistrationCapability? = nil,
389-
documentSymbol: DocumentSymbol? = nil, formatting: DynamicRegistrationCapability? = nil, rangeFormatting: DynamicRegistrationCapability? = nil,
390-
onTypeFormatting: DynamicRegistrationCapability? = nil, definition: DynamicRegistrationCapability? = nil, typeDefinition: DynamicRegistrationCapability? = nil,
391-
implementation: DynamicRegistrationCapability? = nil, codeAction: CodeAction? = nil, codeLens: DynamicRegistrationCapability? = nil,
392-
documentLink: DynamicRegistrationCapability? = nil, colorProvider: DynamicRegistrationCapability? = nil, rename: DynamicRegistrationCapability? = nil,
393-
publishDiagnostics: PublishDiagnostics? = nil, foldingRange: FoldingRange? = nil) {
428+
public init(synchronization: Synchronization? = nil,
429+
completion: Completion? = nil,
430+
hover: Hover? = nil,
431+
signatureHelp: SignatureHelp? = nil,
432+
references: DynamicRegistrationCapability? = nil,
433+
documentHighlight: DynamicRegistrationCapability? = nil,
434+
documentSymbol: DocumentSymbol? = nil,
435+
formatting: DynamicRegistrationCapability? = nil,
436+
rangeFormatting: DynamicRegistrationCapability? = nil,
437+
onTypeFormatting: DynamicRegistrationCapability? = nil,
438+
declaration: DynamicRegistrationLinkSupportCapability? = nil,
439+
definition: DynamicRegistrationLinkSupportCapability? = nil,
440+
typeDefinition: DynamicRegistrationLinkSupportCapability? = nil,
441+
implementation: DynamicRegistrationLinkSupportCapability? = nil,
442+
codeAction: CodeAction? = nil,
443+
codeLens: DynamicRegistrationCapability? = nil,
444+
documentLink: DynamicRegistrationCapability? = nil,
445+
colorProvider: DynamicRegistrationCapability? = nil,
446+
rename: DynamicRegistrationCapability? = nil,
447+
publishDiagnostics: PublishDiagnostics? = nil,
448+
foldingRange: FoldingRange? = nil) {
394449
self.synchronization = synchronization
395450
self.completion = completion
396451
self.hover = hover
@@ -401,6 +456,7 @@ public struct TextDocumentClientCapabilities: Hashable, Codable {
401456
self.formatting = formatting
402457
self.rangeFormatting = rangeFormatting
403458
self.onTypeFormatting = onTypeFormatting
459+
self.declaration = declaration
404460
self.definition = definition
405461
self.typeDefinition = typeDefinition
406462
self.implementation = implementation

Sources/LanguageServerProtocol/Completion.swift

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,45 @@ public struct CompletionRequest: TextDocumentRequest, Hashable {
3232

3333
public var position: Position
3434

35-
// public var context: CompletionContext?
35+
public var context: CompletionContext?
3636

3737
public init(textDocument: TextDocumentIdentifier, position: Position) {
3838
self.textDocument = textDocument
3939
self.position = position
4040
}
4141
}
4242

43+
/// How a completion was triggered
44+
public struct CompletionTriggerKind: RawRepresentable, Codable, Hashable {
45+
/// Completion was triggered by typing an identifier (24x7 code complete), manual invocation (e.g Ctrl+Space) or via API.
46+
public static let invoked = CompletionTriggerKind(rawValue: 1)
47+
48+
/// Completion was triggered by a trigger character specified by the `triggerCharacters` properties of the `CompletionRegistrationOptions`.
49+
public static let triggerCharacter = CompletionTriggerKind(rawValue: 2)
50+
51+
/// Completion was re-triggered as the current completion list is incomplete.
52+
public static let triggerFromIncompleteCompletions = CompletionTriggerKind(rawValue: 3)
53+
54+
public let rawValue: Int
55+
public init(rawValue: Int) {
56+
self.rawValue = rawValue
57+
}
58+
}
59+
60+
/// Contains additional information about the context in which a completion request is triggered.
61+
public struct CompletionContext: Codable, Hashable {
62+
/// How the completion was triggered.
63+
public var triggerKind: CompletionTriggerKind
64+
65+
/// The trigger character (a single character) that has trigger code complete. Is undefined if `triggerKind !== CompletionTriggerKind.TriggerCharacter`
66+
public var triggerCharacter: String?
67+
68+
public init(triggerKind: CompletionTriggerKind, triggerCharacter: String? = nil) {
69+
self.triggerKind = triggerKind
70+
self.triggerCharacter = triggerCharacter
71+
}
72+
}
73+
4374
/// List of completion items. If this list has been filtered already, the `isIncomplete` flag
4475
/// indicates that the client should re-query code-completions if the filter text changes.
4576
public struct CompletionList: ResponseType, Hashable {
@@ -56,4 +87,24 @@ public struct CompletionList: ResponseType, Hashable {
5687
self.isIncomplete = isIncomplete
5788
self.items = items
5889
}
90+
91+
public init(from decoder: Decoder) throws {
92+
// Try decoding as CompletionList
93+
do {
94+
let container = try decoder.container(keyedBy: CodingKeys.self)
95+
self.isIncomplete = try container.decode(Bool.self, forKey: .isIncomplete)
96+
self.items = try container.decode([CompletionItem].self, forKey: .items)
97+
return
98+
} catch {}
99+
100+
// Try decoding as [CompletionItem]
101+
do {
102+
self.items = try [CompletionItem](from: decoder)
103+
self.isIncomplete = false
104+
return
105+
} catch {}
106+
107+
let context = DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Expected CompletionList or [CompletionItem]")
108+
throw DecodingError.dataCorrupted(context)
109+
}
59110
}

Sources/LanguageServerProtocol/CompletionItem.swift

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ public struct CompletionItem: Codable, Hashable {
1616
/// The display name of the completion.
1717
public var label: String
1818

19+
/// The kind of completion item - e.g. method, property.
20+
public var kind: CompletionItemKind
21+
1922
/// An extended human-readable name (longer than `label`, but simpler than `documentation`).
2023
public var detail: String?
2124

25+
/// A human-readable string that represents a doc-comment.
26+
public var documentation: CompletionItemDocumentation?
27+
2228
/// The name to use for sorting the result. If `nil`, use `label.
2329
public var sortText: String?
2430

@@ -40,23 +46,19 @@ public struct CompletionItem: Codable, Hashable {
4046
/// The format of the `textEdit.nextText` or `insertText` value.
4147
public var insertTextFormat: InsertTextFormat?
4248

43-
/// The kind of completion item - e.g. method, property.
44-
public var kind: CompletionItemKind
45-
4649
/// Whether the completion is for a deprecated symbol.
4750
public var deprecated: Bool?
4851

49-
// TODO: remaining members
50-
5152
public init(
5253
label: String,
54+
kind: CompletionItemKind,
5355
detail: String? = nil,
56+
documentation: CompletionItemDocumentation? = nil,
5457
sortText: String? = nil,
5558
filterText: String? = nil,
5659
textEdit: TextEdit? = nil,
5760
insertText: String? = nil,
5861
insertTextFormat: InsertTextFormat? = nil,
59-
kind: CompletionItemKind,
6062
deprecated: Bool? = nil)
6163
{
6264
self.label = label
@@ -80,3 +82,30 @@ public enum InsertTextFormat: Int, Codable, Hashable {
8082
/// The text to insert is a "snippet", which may contain placeholders.
8183
case snippet = 2
8284
}
85+
86+
public enum CompletionItemDocumentation: Hashable {
87+
case string(String)
88+
case markupContent(MarkupContent)
89+
}
90+
91+
extension CompletionItemDocumentation: Codable {
92+
public init(from decoder: Decoder) throws {
93+
if let string = try? String(from: decoder) {
94+
self = .string(string)
95+
} else if let markupContent = try? MarkupContent(from: decoder) {
96+
self = .markupContent(markupContent)
97+
} else {
98+
let context = DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Expected String or MarkupContent")
99+
throw DecodingError.dataCorrupted(context)
100+
}
101+
}
102+
103+
public func encode(to encoder: Encoder) throws {
104+
switch self {
105+
case .string(let string):
106+
try string.encode(to: encoder)
107+
case .markupContent(let markupContent):
108+
try markupContent.encode(to: encoder)
109+
}
110+
}
111+
}

Sources/LanguageServerProtocol/DefinitionRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/// - Returns: The location of the definition(s).
2525
public struct DefinitionRequest: TextDocumentRequest, Hashable {
2626
public static let method: String = "textDocument/definition"
27-
public typealias Response = [Location]
27+
public typealias Response = LocationsOrLocationLinksResponse?
2828

2929
/// The document in which to lookup the symbol location.
3030
public var textDocument: TextDocumentIdentifier

Sources/LanguageServerProtocol/DocumentSymbol.swift

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/// - Returns: An array of document symbols, if any.
2525
public struct DocumentSymbolRequest: TextDocumentRequest, Hashable {
2626
public static let method: String = "textDocument/documentSymbol"
27-
public typealias Response = [DocumentSymbol]?
27+
public typealias Response = DocumentSymbolResponse?
2828

2929
/// The document in which to lookup the symbol location.
3030
public var textDocument: TextDocumentIdentifier
@@ -34,6 +34,31 @@ public struct DocumentSymbolRequest: TextDocumentRequest, Hashable {
3434
}
3535
}
3636

37+
public enum DocumentSymbolResponse: ResponseType, Hashable {
38+
case documentSymbols([DocumentSymbol])
39+
case symbolInformation([SymbolInformation])
40+
41+
public init(from decoder: Decoder) throws {
42+
if let documentSymbols = try? [DocumentSymbol](from: decoder) {
43+
self = .documentSymbols(documentSymbols)
44+
} else if let symbolInformation = try? [SymbolInformation](from: decoder) {
45+
self = .symbolInformation(symbolInformation)
46+
} else {
47+
let context = DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Expected [DocumentSymbol] or [SymbolInformation]")
48+
throw DecodingError.dataCorrupted(context)
49+
}
50+
}
51+
52+
public func encode(to encoder: Encoder) throws {
53+
switch self {
54+
case .documentSymbols(let documentSymbols):
55+
try documentSymbols.encode(to: encoder)
56+
case .symbolInformation(let symbolInformation):
57+
try symbolInformation.encode(to: encoder)
58+
}
59+
}
60+
}
61+
3762
/// Represents programming constructs like variables, classes, interfaces etc. that appear
3863
/// in a document. Document symbols can be hierarchical and they have two ranges: one that encloses
3964
/// its definition and one that points to its most interesting range, e.g. the range of an identifier.
@@ -70,12 +95,12 @@ public struct DocumentSymbol: Hashable, Codable, ResponseType {
7095

7196
public init(
7297
name: String,
73-
detail: String?,
98+
detail: String? = nil,
7499
kind: SymbolKind,
75-
deprecated: Bool?,
100+
deprecated: Bool? = nil,
76101
range: Range<Position>,
77102
selectionRange: Range<Position>,
78-
children: [DocumentSymbol]?)
103+
children: [DocumentSymbol]? = nil)
79104
{
80105
self.name = name
81106
self.detail = detail

Sources/LanguageServerProtocol/Hover.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extension MarkedString: Codable {
7878
} else if let codeBlock = try? MarkdownCodeBlock(from: decoder) {
7979
self = .codeBlock(language: codeBlock.language, value: codeBlock.value)
8080
} else {
81-
let context = DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "MarkedString is neither pure string nor code block")
81+
let context = DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Expected String or MarkdownCodeBlock")
8282
throw DecodingError.dataCorrupted(context)
8383
}
8484
}
@@ -104,7 +104,7 @@ extension HoverResponseContents: Codable {
104104
} else if let value = try? MarkupContent(from: decoder) {
105105
self = .markupContent(value)
106106
} else {
107-
let context = DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "HoverResponseContents is neither MarkedString, nor [MarkedString], nor MarkupContent")
107+
let context = DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Expected MarkedString, [MarkedString], or MarkupContent")
108108
throw DecodingError.dataCorrupted(context)
109109
}
110110
}

Sources/LanguageServerProtocol/ImplementationRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/// protocol conforming types, subclasses, or overrides.
2626
public struct ImplementationRequest: TextDocumentRequest, Hashable {
2727
public static let method: String = "textDocument/implementation"
28-
public typealias Response = [Location]
28+
public typealias Response = LocationsOrLocationLinksResponse?
2929

3030
/// The document in which the given symbol is located.
3131
public var textDocument: TextDocumentIdentifier

0 commit comments

Comments
 (0)