@@ -26,11 +26,14 @@ import { AllTypeInfo, IPosition } from 'graphql-language-service-types';
26
26
import { Hover } from 'vscode-languageserver-types' ;
27
27
import { getTokenAtPosition , getTypeInfo } from './getAutocompleteSuggestions' ;
28
28
29
+ export type HoverConfig = { useMarkdown ?: boolean } ;
30
+
29
31
export function getHoverInformation (
30
32
schema : GraphQLSchema ,
31
33
queryText : string ,
32
34
cursor : IPosition ,
33
35
contextToken ?: ContextToken ,
36
+ config ?: HoverConfig ,
34
37
) : Hover [ 'contents' ] {
35
38
const token = contextToken || getTokenAtPosition ( queryText , cursor ) ;
36
39
@@ -42,7 +45,7 @@ export function getHoverInformation(
42
45
const kind = state . kind ;
43
46
const step = state . step ;
44
47
const typeInfo = getTypeInfo ( schema , token . state ) ;
45
- const options = { schema } ;
48
+ const options = { ... config , schema } ;
46
49
47
50
// Given a Schema and a Token, produce the contents of an info tooltip.
48
51
// To do this, create a div element that we will render "into" and then pass
@@ -52,17 +55,23 @@ export function getHoverInformation(
52
55
( kind === 'AliasedField' && step === 2 && typeInfo . fieldDef )
53
56
) {
54
57
const into : string [ ] = [ ] ;
58
+ renderMdCodeStart ( into , options ) ;
55
59
renderField ( into , typeInfo , options ) ;
60
+ renderMdCodeEnd ( into , options ) ;
56
61
renderDescription ( into , options , typeInfo . fieldDef ) ;
57
62
return into . join ( '' ) . trim ( ) ;
58
63
} else if ( kind === 'Directive' && step === 1 && typeInfo . directiveDef ) {
59
64
const into : string [ ] = [ ] ;
65
+ renderMdCodeStart ( into , options ) ;
60
66
renderDirective ( into , typeInfo , options ) ;
67
+ renderMdCodeEnd ( into , options ) ;
61
68
renderDescription ( into , options , typeInfo . directiveDef ) ;
62
69
return into . join ( '' ) . trim ( ) ;
63
70
} else if ( kind === 'Argument' && step === 0 && typeInfo . argDef ) {
64
71
const into : string [ ] = [ ] ;
72
+ renderMdCodeStart ( into , options ) ;
65
73
renderArg ( into , typeInfo , options ) ;
74
+ renderMdCodeEnd ( into , options ) ;
66
75
renderDescription ( into , options , typeInfo . argDef ) ;
67
76
return into . join ( '' ) . trim ( ) ;
68
77
} else if (
@@ -71,7 +80,9 @@ export function getHoverInformation(
71
80
'description' in typeInfo . enumValue
72
81
) {
73
82
const into : string [ ] = [ ] ;
83
+ renderMdCodeStart ( into , options ) ;
74
84
renderEnumValue ( into , typeInfo , options ) ;
85
+ renderMdCodeEnd ( into , options ) ;
75
86
renderDescription ( into , options , typeInfo . enumValue ) ;
76
87
return into . join ( '' ) . trim ( ) ;
77
88
} else if (
@@ -80,13 +91,26 @@ export function getHoverInformation(
80
91
'description' in typeInfo . type
81
92
) {
82
93
const into : string [ ] = [ ] ;
94
+ renderMdCodeStart ( into , options ) ;
83
95
renderType ( into , typeInfo , options , typeInfo . type ) ;
96
+ renderMdCodeEnd ( into , options ) ;
84
97
renderDescription ( into , options , typeInfo . type ) ;
85
98
return into . join ( '' ) . trim ( ) ;
86
99
}
87
100
return '' ;
88
101
}
89
102
103
+ function renderMdCodeStart ( into : string [ ] , options : any ) {
104
+ if ( options . useMarkdown ) {
105
+ text ( into , '```graphql\n' ) ;
106
+ }
107
+ }
108
+ function renderMdCodeEnd ( into : string [ ] , options : any ) {
109
+ if ( options . useMarkdown ) {
110
+ text ( into , '\n```' ) ;
111
+ }
112
+ }
113
+
90
114
function renderField ( into : string [ ] , typeInfo : AllTypeInfo , options : any ) {
91
115
renderQualifiedField ( into , typeInfo , options ) ;
92
116
renderTypeAnnotation ( into , typeInfo , options , typeInfo . type as GraphQLType ) ;
@@ -168,6 +192,7 @@ function renderType(
168
192
if ( ! t ) {
169
193
return ;
170
194
}
195
+
171
196
if ( t instanceof GraphQLNonNull ) {
172
197
renderType ( into , typeInfo , options , t . ofType ) ;
173
198
text ( into , '!' ) ;
0 commit comments