evanleck / vim-svelte

Vim syntax highlighting and indentation for Svelte 3 components.
BSD 3-Clause "New" or "Revised" License
319 stars 11 forks source link

script attributes like context="module" or lang="ts" break indent #19

Open non25 opened 3 years ago

non25 commented 3 years ago

To reprocude, simply add some attributes to the script tag:


<script lang="ts">
  // Pressing S will place cursor here:
  let obj = {
|  
  }
</script>
<script lang="ts">
  let obj = {<CR>};

  // will place cursor like this:
  let obj = {
  |
  };
</script>

Can't seem to figure out what causes this behavior. I've read indent script and haven't found anything that could mess with it. Perhaps that's another indent/html.vim feature for me.

non25 commented 3 years ago

Yeah... That's it: https://github.com/vim/vim/blob/dc167465f87a4f03f9e085597dfcfdde39fcd2b9/runtime/indent/html.vim#L398-L406

Adding this does the job:

  elseif a:str == "ts" || a:str = "typescript"
    return "typescript"

Can we somehow override functions from indent/html.vim in svelte.vim?

Interesting, after this addition, no matter what modifications I do to the <script> tag, indent works correctly.

cloudhead commented 3 years ago

I have the same issue.

evanleck commented 2 years ago

@cloudhead @non25 sorry for the tremendous delay... I've been trying to improve the HTML indent behavior and found that adding othree/html5.vim fixes some issues. Mind giving that a shot and letting me know how it works? I've updated the README to include html5.vim as a dependency of this plugin as well.

acarl005 commented 2 years ago

@evanleck Giving a little update here. After adding othree/html5.vim, the indent behavior in a <script context="module"> tag still isn't quite right. If I have the following content in a svelte file...

<script context="module">
  function add(a, b) {▌
  }
</script>

...and I press o I get....

<script context="module">
  function add(a, b) {
  ▌
  }
</script>

The new line is indented to 2 spaces when it should be 4 inside the function body. I am using the latest NeoVim

nvim --version
NVIM v0.7.2
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by brew@Monterey

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/local/Cellar/neovim/0.7.2/share/nvim"

And here is my ~/.config/nvim/init.lua. I have junegunn/vim-plug installed as my plugin manager

vim.fn["plug#begin"](vim.fn.stdpath("data") .. "/plugged")

vim.fn["plug#"]("othree/html5.vim") -- depended upon by evanleck/vim-svelte
vim.fn["plug#"]("pangloss/vim-javascript") -- depended upon by evanleck/vim-svelte
vim.fn["plug#"]("evanleck/vim-svelte") -- needed for good svelte file indentation

vim.fn["plug#end"]()

vim.o.number = true -- line numbers

vim.o.expandtab = true -- convert tabs to spaces
vim.o.softtabstop = 2 -- how many spaces to insert for each <tab>
vim.o.tabstop = 2 -- the width to display a <tab> character
vim.o.shiftwidth = 2 -- used by commands like =, >, and < to know how much to indent
vim.o.cindent = true
binarious commented 1 year ago

Same issue here, but I still don't get any indentation even with othree/html5.vim.

Svelte file:

<script lang="ts">
  function test(): void {
  alert('wrong indentation');
  }
</script>

It works without lang="ts". Also doesn't work with lang="typescript".

My init.lua:


require('lazy').setup({
  -- other plugins
  {
    'evanleck/vim-svelte',
    version = 'main',
    ft = 'svelte',
    dependencies = {
      'pangloss/vim-javascript',
      'othree/html5.vim'
    }
  }
})
vim.g.svelte_preprocessors = {'typescript'}