graphql-mode
graphql-mode copied to clipboard
Unexpected indentation in Polymode
I work a lot in with ruby, where our tests have a lot of graphql queries written in HEREDOCs. I've written the following polymode definition to allow graphql-mode nested in ruby-mode:
(require 'polymode)
(define-hostmode poly-ruby-hostmode :mode 'ruby-mode)
(define-innermode poly-ruby-gql-metadata-innermode
:mode 'graphql-mode
:head-matcher ".*<<[~-]GRAPHQL\n"
:tail-matcher ".*GRAPHQL\n"
:head-mode 'host
:tail-mode 'host)
(define-polymode poly-ruby-mode
:hostmode 'poly-ruby-hostmode
:innermodes '(poly-ruby-gql-metadata-innermode))
which allows me to write the following in ruby:
def bla
blubb = <<~GRAPHQL
query bla($parent: Int) {
foo
bar {
id
babar {
id
type
}
}
}
GRAPHQL
end
As you can see the indentation works for all but the first and the last lines. For me the expectation would be that these lines are also indented and every indentation after is relative to this first indentation, like in this buffer that only has graphql-mode active (i.e. no polymode).
query bla($parent: Int) {
foo
bar {
id
babar {
id
type
}
}
}
I was able to reproduce the error by defining a polymode for sh-mode, which also supports HEREDOCs
(require 'polymode)
(define-hostmode poly-sh-hostmode :mode 'sh-mode)
(define-innermode poly-sh-gql-metadata-innermode
:mode 'graphql-mode
:head-matcher ".*<<[~-]GRAPHQL\n"
:tail-matcher ".*GRAPHQL\n"
:head-mode 'host
:tail-mode 'host)
(define-polymode poly-sh-mode
:hostmode 'poly-sh-hostmode
:innermodes '(poly-sh-gql-metadata-innermode))
here's the result:
echo 'this is a sh-mode buffer'
bla=<<-GRAPHQL
query($parent: Int) {
foo
bar {
id
babar {
id
type
}
}
}
GRAPHQL
echo 'no longer in the HEREDOC'
If this really is a graphql-mode issue, I'd be happy to take a look at the code, you just have to point me in the right direction.
Thank you in advance!
For me the expectation would be that these lines are also indented and every indentation after is relative to this first indentation, like in this buffer that only has graphql-mode active (i.e. no polymode).
Is that the case? If I type your example and the first line query has some indentation, the rest of the code indents absolutely and not relative to the first line still I think.
If you look at the graphql-indent-line
https://github.com/davazp/graphql-mode/blob/9740e4027bd9313697d5cac5caaa5b15626ab1da/graphql-mode.el#L281-L296
the indentation level is calculated as just the nesting level in the parens.
You could try to find the indentation level of the top-level line and add it as an offset to there.
Is that the case? If I type your example and the first line query has some indentation, the rest of the code indents absolutely and not relative to the first line still I think.
Ah, you're right
If you look at the
graphql-indent-line
I'll have a look and report back. Is this something you'd like as a pull request?
I'll have a look and report back. Is this something you'd like as a pull request?
It'd be great yes!
As long as top-level code is indented at level 0 by default, this seems like a nice extension to the current behavior. I don't see any downside with it 🙂 and looking at other modes, they all seem to behave this way as well.