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

Automatic newline and indentation for template literals in JavaScript #61

Closed
marlun opened this issue Oct 29, 2023 · 6 comments
Closed

Comments

@marlun
Copy link

marlun commented Oct 29, 2023

This might be connected to #59 or at least similar. I'm not sure if this is something that this plugin should do or not. When writing for example a web component in JavaScript I might start with:

class MyComponent extends HTMLElement {|}

Pressing CR will now give me

class MyComponent extends HTMLElement {
  |
}

Then I continue writing

class MyComponent extends HTMLElement {
  render() {
    this.innerHTML = `|`
  }
}

Now when I press CR it gives me

class MyComponent extends HTMLElement {
  render() {
    this.innerHTML = `
    |`
  }
}

Would it possible to have the same logic here as with {}? I'm not sure if it's all ultimate-autopair doing the "extra newline and indentation" or if it's a combination of ultimate-autopair and Treesitter indentation but it feels so smooth and nice with {}.

@altermo
Copy link
Owner

altermo commented Oct 29, 2023

Yes:

require('ultimate-autopair').setup{
  config_internal_pairs={
    {'`','`',newline=true}
  }
}

@marlun
Copy link
Author

marlun commented Oct 29, 2023

With that

class MyComponent extends HTMLElement {
  render() {
    this.innerHTML = `|`;
  }
}

still results in

class MyComponent extends HTMLElement {
  render() {
    this.innerHTML = `
    |`;
  }
}

However

class MyComponent extends HTMLElement {
  render() {
    const test = `|`;
  }
}

results in

class MyComponent extends HTMLElement {
  render() {
  const test = `
|
  `
  }
}

I'm guessing that last one with the caret on the first column has to do with the treesitter indentation because if I try to format the code it stays in the first column instead of being indented as I expect.

Also, with tagged template literals sometimes you will write

return html`|`

That will result in

class MyComponent extends HTMLElement {
  render() {
    const test = html`
    |`;
  }
}

@altermo
Copy link
Owner

altermo commented Oct 29, 2023

It's treesitter, specifically that the plugin isolates text inside injected blocks of code.
So if you don't care about this feature (or string isolation) then disable the extension:

require('ultimate-autopair').setup{
  config_internal_pairs={
    {'`','`',newline=true}
  },
  extensions={
    tsnode=false
  },
}

@marlun
Copy link
Author

marlun commented Oct 29, 2023

It's treesitter, specifically that the plugin isolates text inside injected blocks of code.
So if you don't care about this feature (or string isolation) then disable the extension:

I'm not sure what the isolation part means?

Disabling tsnode I do get the cursor on its own line but the cursor is left on the first column of the line instead of one extra indentation like with {|}. Should I look into TS indentation to fix that?

@altermo
Copy link
Owner

altermo commented Oct 29, 2023

When I mean isolated, I mean that the multi line open pair detection and all other detection will only be done in a region/outside a region.

And for the indentation, yes (as this plugin uses whatever indentation system neovim is set up with).

@marlun
Copy link
Author

marlun commented Oct 30, 2023

I don't really understand the region part but I'll keep it like this for now and continue researching why the indentation is not the way I want with Treesitter. I'll close the issue, thanks for the help!

@marlun marlun closed this as completed Oct 30, 2023
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