-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GraphQL indentation inside of JavaScript strings
This builds upon the embedded syntax "magic" we perform in order to identify and highlight GraphQL queries within JavaScript/TypeScript template strings. In order to enable GraphQL syntax-aware indentation within these strings, we replace `'indentexpr'` with our own function which calls `GetGraphQLIndent` inside of a GraphQL syntax region and otherwise defers to the indentation function provided by the filetype plugin. To support loading `indent/graphql.vim` inside of other file types, it no longer sets any buffer-local settings when `b:did_indent` is already set. This means we use the "outer" JavaScript indentation settings for the "inner" GraphQL region. In practice, this is probably only relevant for `'indentkeys'`; I haven't thought of a way to make that setting syntax-aware, too, without also changing it for the JavaScript code. This approach has been tested with all of the supported JavaScript and TypeScript variants, but there still may be some lingering issues that I haven't encountered yet.
- Loading branch information
Showing
7 changed files
with
168 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
" Copyright (c) 2016-2020 Jon Parise <jon@indelible.org> | ||
" | ||
" 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 <jon@indelible.org> | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
" Copyright (c) 2016-2020 Jon Parise <jon@indelible.org> | ||
" | ||
" 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 <jon@indelible.org> | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters