Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected indentation in Polymode #49

Open
flooose opened this issue Jul 12, 2022 · 3 comments
Open

Unexpected indentation in Polymode #49

flooose opened this issue Jul 12, 2022 · 3 comments

Comments

@flooose
Copy link
Contributor

flooose commented Jul 12, 2022

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!

@davazp
Copy link
Owner

davazp commented Jul 12, 2022

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

(defun graphql-indent-line ()
"Indent GraphQL schema language."
(let ((position (point))
(indent-pos))
(save-excursion
(let ((level (car (syntax-ppss (point-at-bol)))))
;; Handle closing pairs
(when (looking-at "\\s-*\\s)")
(setq level (1- level)))
(indent-line-to (* graphql-indent-level level))
(setq indent-pos (point))))
(when (< position indent-pos)
(goto-char indent-pos))))

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.

@flooose
Copy link
Contributor Author

flooose commented Jul 12, 2022

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?

@davazp
Copy link
Owner

davazp commented Jul 12, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants