Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions after/indent/javascript.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
" Copyright (c) 2016-2020 Jon Parise <[email protected]>
"
" Permission is hereby granted, free of charge, to any person obtaining a copy
" of this software and associated documentation files (the "Software"), to
" deal in the Software without restriction, including without limitation the
" rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
" sell copies of the Software, and to permit persons to whom the Software is
" furnished to do so, subject to the following conditions:
"
" The above copyright notice and this permission notice shall be included in
" all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
" FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
" IN THE SOFTWARE.
"
" Language: GraphQL
" Maintainer: Jon Parise <[email protected]>

runtime! indent/graphql.vim

" Don't redefine our function and also require the standard Javascript indent
" function to exist.
if exists('*GetJavascriptGraphQLIndent') || !exists('*GetJavascriptIndent')
finish
endif

" Set the indentexpr with our own version that will call GetGraphQLIndent when
" we're inside of a GraphQL string and otherwise defer to GetJavascriptIndent.
setlocal indentexpr=GetJavascriptGraphQLIndent()

function GetJavascriptGraphQLIndent()
let l:stack = map(synstack(v:lnum, 1), "synIDattr(v:val,'name')")
if !empty(l:stack) && l:stack[0] ==# 'graphqlTemplateString'
return GetGraphQLIndent()
endif

return GetJavascriptIndent()
endfunction
43 changes: 43 additions & 0 deletions after/indent/typescript.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
" Copyright (c) 2016-2020 Jon Parise <[email protected]>
"
" Permission is hereby granted, free of charge, to any person obtaining a copy
" of this software and associated documentation files (the "Software"), to
" deal in the Software without restriction, including without limitation the
" rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
" sell copies of the Software, and to permit persons to whom the Software is
" furnished to do so, subject to the following conditions:
"
" The above copyright notice and this permission notice shall be included in
" all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
" FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
" IN THE SOFTWARE.
"
" Language: GraphQL
" Maintainer: Jon Parise <[email protected]>

runtime! indent/graphql.vim

" Don't redefine our function and also require the standard Typescript indent
" function to exist.
if exists('*GetTypescriptGraphQLIndent') || !exists('*GetTypescriptIndent')
finish
endif

" Set the indentexpr with our own version that will call GetGraphQLIndent when
" we're inside of a GraphQL string and otherwise defer to GetTypescriptIndent.
setlocal indentexpr=GetTypescriptGraphQLIndent()

function GetTypescriptGraphQLIndent()
let l:stack = map(synstack(v:lnum, 1), "synIDattr(v:val,'name')")
if !empty(l:stack) && l:stack[0] ==# 'graphqlTemplateString'
return GetGraphQLIndent()
endif

return GetTypescriptIndent()
endfunction
23 changes: 12 additions & 11 deletions indent/graphql.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
" Language: GraphQL
" Maintainer: Jon Parise <[email protected]>

if exists('b:did_indent')
finish
endif
let b:did_indent = 1
" Set our local options if indentation hasn't already been set up.
" This generally means we've been detected as the primary filetype.
if !exists('b:did_indent')
setlocal autoindent
setlocal nocindent
setlocal nolisp
setlocal nosmartindent

setlocal autoindent
setlocal nocindent
setlocal nolisp
setlocal nosmartindent
setlocal indentexpr=GetGraphQLIndent()
setlocal indentkeys=0{,0},0),0[,0],0#,!^F,o,O

setlocal indentexpr=GetGraphQLIndent()
setlocal indentkeys=0{,0},0),0[,0],0#,!^F,o,O
let b:did_indent = 1
endif

" If our indentation function already exists, we have nothing more to do.
if exists('*GetGraphQLIndent')
Expand All @@ -44,7 +45,7 @@ set cpoptions&vim

" Check if the character at lnum:col is inside a string.
function s:InString(lnum, col)
return synIDattr(synID(a:lnum, a:col, 1), 'name') is# 'graphqlString'
return synIDattr(synID(a:lnum, a:col, 1), 'name') ==# 'graphqlString'
endfunction

function GetGraphQLIndent()
Expand Down
20 changes: 20 additions & 0 deletions test/javascript/default.vader
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Before:
Save g:graphql_javascript_tags

setlocal shiftwidth=2
source ../after/indent/javascript.vim
source ../after/syntax/javascript/graphql.vim

After:
Expand All @@ -22,6 +24,24 @@ Execute (Syntax assertions):
AssertEqual 'graphqlTemplateExpression', SyntaxOf('${uid}')
AssertEqual 'graphqlName', SyntaxOf('user')

Execute (Indent assertions):
Assert exists('*GetGraphQLIndent')
Assert exists('*GetJavascriptIndent')
Assert exists('*GetJavascriptGraphQLIndent')

Do (re-indent buffer):
gg=G

Expect (propertly indented):
const query = gql`
{
user(id: ${uid}) {
firstName
lastName
}
}
`;

Given javascript (Custom tag):
const query = GraphQL.Tag`{}`;

Expand Down
6 changes: 6 additions & 0 deletions test/javascript/react.vader
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Before:
source ../after/indent/javascript.vim
source ../after/syntax/javascriptreact/graphql.vim

Execute (Expected syntax groups):
Expand All @@ -20,3 +21,8 @@ Execute (Syntax assertions):
AssertEqual 'graphqlTaggedTemplate', SyntaxOf('gql')
AssertEqual 'graphqlTemplateString', SyntaxOf('`')
AssertEqual 'graphqlBraces', SyntaxOf('{}')

Execute (Indent assertions):
Assert exists('*GetGraphQLIndent')
Assert exists('*GetJavascriptIndent')
Assert exists('*GetJavascriptGraphQLIndent')
25 changes: 24 additions & 1 deletion test/javascript/vue.vader
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Before:
filetype plugin indent on
syntax enable

setlocal shiftwidth=2
source ../after/indent/javascript.vim
source ../after/syntax/vue/graphql.vim

After:
Expand All @@ -22,7 +24,8 @@ Given vue (Template):
export default {
apollo: {
hello: gql`query {
hello
firstName
lastName
}`,
},
}
Expand All @@ -32,3 +35,23 @@ Execute (Syntax assertions):
AssertEqual 'graphqlTaggedTemplate', SyntaxOf('gql')
AssertEqual 'graphqlTemplateString', SyntaxOf('`')
AssertEqual 'graphqlStructure', SyntaxOf('query')

Execute (Indent assertions):
Assert exists('*GetGraphQLIndent')
Assert exists('*GetJavascriptIndent')
Assert exists('*GetJavascriptGraphQLIndent')

Do (re-indent buffer):
gg=G

Expect (propertly indented):
<script>
export default {
apollo: {
hello: gql`query {
firstName
lastName
}`,
},
}
</script>
20 changes: 20 additions & 0 deletions test/typescript/default.vader
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Before:
Save g:graphql_javascript_tags

setlocal shiftwidth=2
source ../after/indent/typescript.vim
source ../after/syntax/typescript/graphql.vim

After:
Expand All @@ -25,6 +27,24 @@ Execute (Syntax assertions):
AssertEqual 'typescriptTemplateSB', SyntaxOf('${uid}')
AssertEqual 'graphqlName', SyntaxOf('user')

Execute (Indent assertions):
Assert exists('*GetGraphQLIndent')
Assert exists('*GetTypescriptIndent')
Assert exists('*GetTypescriptGraphQLIndent')

Do (re-indent buffer):
gg=G

Expect (properly indented):
const query = gql`
{
user(id: ${uid}) {
firstName
lastName
}
}
`;

Given typescript (Custom tag):
const query = GraphQL.Tag`{}`;

Expand Down