jparise / vim-graphql

A Vim plugin that provides GraphQL file detection, syntax highlighting, and indentation.
MIT License
490 stars 25 forks source link

Indentation issue when graphql schema is embedded in a gql string in typescript #84

Closed zenazn closed 2 years ago

zenazn commented 2 years ago

In a typescript file that looks like this:

import gql from 'graphql-tag';

export default gql`
  type Foo {
    bar: String!
  }
`;

If my cursor is after the ! in String!, and I type <CR>baz: Int!, I get:

import gql from 'graphql-tag';

export default gql`
  type Foo {
    bar: String!
      baz: Int!
  }
`;

I think the correct way to indent this is:

import gql from 'graphql-tag';

export default gql`
  type Foo {
    bar: String!
    baz: Int!
  }
`;

The ! is important. If I omit the ! I get the expected result.

The fact that it's embedded in a typescript file is important. If I put the same contents in a .graphql file, I get the expected result.

Vim Version

I'm using a neovim at some random commit (NVIM v0.7.0-dev+nightly-1261-gb4fbb9dcf), but can repro under a normal version of vim or neovim if this isn't something that trivially reproduces!

zenazn commented 2 years ago

A potentially related bug:

import gql from 'graphql-tag';

export default gql`
  type Query {
    # ('oh no')
    foo(id: String): String!
    bar(id: String): String
  }
`;

If I /foo<CR>o# XXX (i.e., open a new line after the foo definition and type # XXX), I get:

import gql from 'graphql-tag';

export default gql`
  type Query {
    # ('oh no')
    foo(id: String): String!
  # XXX
    bar(id: String): String
  }
`;

If I /bar<CR>o# XXX (i.e., open a new line after the bar definition and type # XXX), I get:

import gql from 'graphql-tag';

export default gql`
  type Query {
    # ('oh no')
    foo(id: String): String!
    bar(id: String): String
# XXX
  }
`;

The parens with quotes (either single or double work) in the "oh no" comment are important (but the "oh no" is not important). When either is missing, I get the behavior from the original issue

jparise commented 2 years ago

@zenazn I've been looking at this as I've had free time, and I'm unfortunately not able to reproduce either of these issues. I'm using vim 8.2 (1-4600) and the vim's built-in support for TypeScript (i.e. no plugins). Some potentially relevant settings for this buffer:

indentexpr=GetTypescriptGraphQLIndent()
indentkeys=0{,0},0),0],0,,!^F,o,O,e

Could you compare that to what you have and also let me know if you're using a TypeScript language plugin which might be introducing different behavior?

It's also possible there's something more we need to do beyond 51c5b178cce145c3aaa22fbc37fd5d64065baebc to handle an environment like yours.

jparise commented 2 years ago

Please reopen this issue if new information becomes available.