@@ -23,6 +23,9 @@ public final class CapabilityRegistry {
2323 /// Dynamically registered completion options.
2424 private var completion : [ CapabilityRegistration : CompletionRegistrationOptions ] = [ : ]
2525
26+ /// Dynamically registered folding range options.
27+ private var foldingRange : [ CapabilityRegistration : FoldingRangeRegistrationOptions ] = [ : ]
28+
2629 /// Dynamically registered semantic tokens options.
2730 private var semanticTokens : [ CapabilityRegistration : SemanticTokensRegistrationOptions ] = [ : ]
2831
@@ -36,6 +39,10 @@ public final class CapabilityRegistry {
3639 clientCapabilities. textDocument? . completion? . dynamicRegistration == true
3740 }
3841
42+ public var clientHasDynamicFoldingRangeRegistration : Bool {
43+ clientCapabilities. textDocument? . foldingRange? . dynamicRegistration == true
44+ }
45+
3946 public var clientHasDynamicSemanticTokensRegistration : Bool {
4047 clientCapabilities. textDocument? . semanticTokens? . dynamicRegistration == true
4148 }
@@ -68,6 +75,34 @@ public final class CapabilityRegistry {
6875 registerOnClient ( registration)
6976 }
7077
78+ /// Dynamically register folding range capabilities if the client supports it and
79+ /// we haven't yet registered any folding range capabilities for the given
80+ /// languages.
81+ public func registerFoldingRangeIfNeeded(
82+ options: FoldingRangeOptions ,
83+ for languages: [ Language ] ,
84+ registerOnClient: ClientRegistrationHandler
85+ ) {
86+ guard clientHasDynamicFoldingRangeRegistration else { return }
87+ if let registration = registration ( for: languages, in: foldingRange) {
88+ if options != registration. foldingRangeOptions {
89+ log ( " Unable to register new folding range options \( options) for " +
90+ " \( languages) due to pre-existing options \( registration. foldingRangeOptions) " , level: . warning)
91+ }
92+ return
93+ }
94+ let registrationOptions = FoldingRangeRegistrationOptions (
95+ documentSelector: self . documentSelector ( for: languages) ,
96+ foldingRangeOptions: options)
97+ let registration = CapabilityRegistration (
98+ method: FoldingRangeRequest . method,
99+ registerOptions: self . encode ( registrationOptions) )
100+
101+ self . foldingRange [ registration] = registrationOptions
102+
103+ registerOnClient ( registration)
104+ }
105+
71106 /// Dynamically register semantic tokens capabilities if the client supports
72107 /// it and we haven't yet registered any semantic tokens capabilities for the
73108 /// given languages.
0 commit comments