Skip to content

Commit

Permalink
Add support for b:graphql_javascript_tags (#100)
Browse files Browse the repository at this point in the history
This allows buffer-level JavaScript tag values, avoiding the need
to modify the global namespace for more granular configurations.

This also introduces graphql#var() as a convenience function to perform
this kind of tiered variable look-up.
  • Loading branch information
jparise authored Jul 8, 2024
1 parent abb022a commit ce16abf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const query = gql`
```

The list of recognized tag names is defined by the `g:graphql_javascript_tags`
variable, which defaults to `["gql", "graphql", "Relay.QL"]`.
variable, which defaults to `["gql", "graphql", "Relay.QL"]`. This can also
be set on a per-buffer basis using `b:graphql_javascript_tags`.

You can also add a `# gql` or `# graphql` comment at the start of a template
string to indicate that its contents should be considered GraphQL syntax.
Expand Down
8 changes: 7 additions & 1 deletion autoload/graphql.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
" Language: GraphQL
" Maintainer: Jon Parise <[email protected]>

" Look up the named variable in buffer scope and then in global scope.
" Returns default if the named variable can't be found in either.
function! graphql#var(name, default) abort
return get(b:, a:name, get(g:, a:name, a:default))
endfunction

function! graphql#has_syntax_group(group) abort
try
silent execute 'silent highlight ' . a:group
Expand All @@ -31,5 +37,5 @@ function! graphql#has_syntax_group(group) abort
endfunction

function! graphql#javascript_tags() abort
return get(g:, 'graphql_javascript_tags', ['gql', 'graphql', 'Relay.QL'])
return graphql#var('graphql_javascript_tags', ['gql', 'graphql', 'Relay.QL'])
endfunction
1 change: 1 addition & 0 deletions doc/graphql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ supported.
*graphql-javascript-options*

*g:graphql_javascript_tags*
*b:graphql_javascript_tags*
|g:graphql_javascript_tags| list of strings

Default: `["gql", "graphql", "Relay.QL"]`
Expand Down
17 changes: 17 additions & 0 deletions test/var.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Before:
let g:graphql_some_variable = 'abc'

After:
unlet! g:graphql_some_variable
unlet! b:undefined_variable_name

Execute(graphql#var should return global variables):
AssertEqual 'abc', graphql#var('graphql_some_variable', '')

Execute(graphql#var should return buffer overrides):
let b:graphql_some_variable = 'def'

AssertEqual 'def', graphql#var('graphql_some_variable', '')

Execute(graphql#var should return default value for undefined variables):
AssertEqual 'default', graphql#var('undefined_variable_name', 'default')

0 comments on commit ce16abf

Please sign in to comment.