1
+ using System . Collections ;
2
+ using System . Collections . Generic ;
1
3
using System . Linq ;
2
4
using Microsoft . CodeAnalysis ;
5
+ using Microsoft . CodeAnalysis . CSharp ;
3
6
using Microsoft . CodeAnalysis . CSharp . Syntax ;
4
7
5
8
namespace OmniSharp . Extensions . JsonRpc . Generators . Contexts
6
9
{
7
10
record RegistrationOptionAttributes (
8
11
SyntaxAttributeData ? GenerateRegistrationOptions ,
9
12
string ? Key ,
10
- ExpressionSyntax ? KeyExpression ,
13
+ ExpressionSyntax [ ] ? KeyExpression ,
11
14
bool SupportsWorkDoneProgress ,
12
15
bool SupportsDocumentSelector ,
13
16
bool SupportsStaticRegistrationOptions ,
@@ -19,13 +22,16 @@ bool ImplementsStaticRegistrationOptions
19
22
{
20
23
public static RegistrationOptionAttributes ? Parse ( GeneratorExecutionContext context , TypeDeclarationSyntax syntax , INamedTypeSymbol symbol )
21
24
{
22
- var registrationOptionsAttributeSymbol = context . Compilation . GetTypeByMetadataName ( $ "OmniSharp.Extensions.LanguageServer.Protocol.Generation.GenerateRegistrationOptionsAttribute") ;
23
- var registrationOptionsConverterAttributeSymbol = context . Compilation . GetTypeByMetadataName ( $ "OmniSharp.Extensions.LanguageServer.Protocol.RegistrationOptionsConverterAttribute") ;
25
+ var registrationOptionsAttributeSymbol =
26
+ context . Compilation . GetTypeByMetadataName ( $ "OmniSharp.Extensions.LanguageServer.Protocol.Generation.GenerateRegistrationOptionsAttribute") ;
27
+ var registrationOptionsConverterAttributeSymbol =
28
+ context . Compilation . GetTypeByMetadataName ( $ "OmniSharp.Extensions.LanguageServer.Protocol.RegistrationOptionsConverterAttribute") ;
24
29
// var registrationOptionsInterfaceSymbol = context.Compilation.GetTypeByMetadataName("OmniSharp.Extensions.LanguageServer.Protocol.IRegistrationOptions");
25
30
var textDocumentRegistrationOptionsInterfaceSymbol =
26
31
context . Compilation . GetTypeByMetadataName ( "OmniSharp.Extensions.LanguageServer.Protocol.Models.ITextDocumentRegistrationOptions" ) ;
27
32
var workDoneProgressOptionsInterfaceSymbol = context . Compilation . GetTypeByMetadataName ( "OmniSharp.Extensions.LanguageServer.Protocol.Models.IWorkDoneProgressOptions" ) ;
28
- var staticRegistrationOptionsInterfaceSymbol = context . Compilation . GetTypeByMetadataName ( "OmniSharp.Extensions.LanguageServer.Protocol.Models.IStaticRegistrationOptions" ) ;
33
+ var staticRegistrationOptionsInterfaceSymbol =
34
+ context . Compilation . GetTypeByMetadataName ( "OmniSharp.Extensions.LanguageServer.Protocol.Models.IStaticRegistrationOptions" ) ;
29
35
30
36
if ( ! ( symbol . GetAttribute ( registrationOptionsAttributeSymbol ) is { } data ) ) return null ;
31
37
if ( ! ( data . ApplicationSyntaxReference ? . GetSyntax ( ) is AttributeSyntax attributeSyntax ) ) return null ;
@@ -34,12 +40,12 @@ bool ImplementsStaticRegistrationOptions
34
40
ITypeSymbol ? converter = null ;
35
41
36
42
var supportsDocumentSelector = data . NamedArguments . Any ( z => z is { Key : nameof ( SupportsDocumentSelector ) , Value : { Value : true } } )
37
- || symbol . AllInterfaces . Length > 0 && symbol . AllInterfaces . Any (
38
- z => SymbolEqualityComparer . Default . Equals ( z , textDocumentRegistrationOptionsInterfaceSymbol )
39
- )
40
- || textDocumentRegistrationOptionsInterfaceSymbol is { } && syntax . BaseList ? . Types . Any (
41
- type => type . Type . GetSyntaxName ( ) ? . Contains ( textDocumentRegistrationOptionsInterfaceSymbol . Name ) == true
42
- ) == true ;
43
+ || symbol . AllInterfaces . Length > 0 && symbol . AllInterfaces . Any (
44
+ z => SymbolEqualityComparer . Default . Equals ( z , textDocumentRegistrationOptionsInterfaceSymbol )
45
+ )
46
+ || textDocumentRegistrationOptionsInterfaceSymbol is { } && syntax . BaseList ? . Types . Any (
47
+ type => type . Type . GetSyntaxName ( ) ? . Contains ( textDocumentRegistrationOptionsInterfaceSymbol . Name ) == true
48
+ ) == true ;
43
49
var supportsWorkDoneProgress = data . NamedArguments . Any ( z => z is { Key : nameof ( SupportsWorkDoneProgress ) , Value : { Value : true } } )
44
50
|| symbol . AllInterfaces . Length > 0 && symbol . AllInterfaces . Any (
45
51
z => SymbolEqualityComparer . Default . Equals ( z , workDoneProgressOptionsInterfaceSymbol )
@@ -82,17 +88,47 @@ bool ImplementsStaticRegistrationOptions
82
88
}
83
89
84
90
string ? value = null ;
85
- ExpressionSyntax ? valueSyntax = null ;
91
+ ExpressionSyntax [ ] ? valueExpressionSyntaxes = null ;
86
92
if ( data is { ConstructorArguments : { Length : > 0 } arguments } && arguments [ 0 ] . Kind is TypedConstantKind . Primitive && arguments [ 0 ] . Value is string )
87
93
{
88
- value = arguments [ 0 ] . Value as string ;
89
- valueSyntax = attributeSyntax . ArgumentList ! . Arguments [ 0 ] . Expression ;
94
+ static IEnumerable < string > getStringValue ( TypedConstant constant )
95
+ {
96
+ if ( constant . Kind is TypedConstantKind . Primitive && constant . Value is string s )
97
+ {
98
+ yield return s ;
99
+ }
100
+
101
+ if ( constant . Kind is TypedConstantKind . Array )
102
+ {
103
+ foreach ( var i in constant . Values . SelectMany ( getStringValue ) )
104
+ {
105
+ yield return i ;
106
+ }
107
+ }
108
+ }
109
+
110
+ static IEnumerable < ExpressionSyntax > getStringExpressionSyntaxes ( AttributeArgumentSyntax syntax )
111
+ {
112
+ switch ( syntax . Expression )
113
+ {
114
+ case LiteralExpressionSyntax literalExpressionSyntax when literalExpressionSyntax . Token . IsKind ( SyntaxKind . StringLiteralToken ) :
115
+ yield return literalExpressionSyntax ;
116
+ break ;
117
+ case InvocationExpressionSyntax
118
+ { Expression : IdentifierNameSyntax { Identifier : { Text : "nameof" } } } :
119
+ yield return syntax . Expression ;
120
+ break ;
121
+ }
122
+ }
123
+
124
+ value = string . Join ( "." , arguments . SelectMany ( getStringValue ) ) ;
125
+ valueExpressionSyntaxes = attributeSyntax . ArgumentList ! . Arguments . SelectMany ( getStringExpressionSyntaxes ) . ToArray ( ) ;
90
126
}
91
127
92
128
return new RegistrationOptionAttributes (
93
129
new SyntaxAttributeData ( attributeSyntax , data ) ,
94
130
value ,
95
- valueSyntax ,
131
+ valueExpressionSyntaxes ,
96
132
supportsWorkDoneProgress ,
97
133
supportsDocumentSelector ,
98
134
supportsStaticRegistrationOptions ,
0 commit comments