piersolenski / telescope-import.nvim

Import modules with ease
186 stars 8 forks source link

Syntax compatiblity with html-ish languages #9

Closed thenbe closed 8 months ago

thenbe commented 10 months ago

Hi. I'm trying to add svelte as a custom language. This is what I have:

import = {
  insert_at_top = false,
  custom_languages = {
    {
      regex = [[^\s+import\s+.*from\s+]],
      filetypes = { "svelte" },
      extensions = { "svelte" }, -- custom svelte filetype is defined using ripgrep config file: https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md#configuration-file
    },
  },
},

The core functionality is working great. By default however, the imports are inserted at the top line which is incorrect syntax for svelte files. This is because a typical svelte starts like this:

<script>
  import foo from 'bar';
</script>

Imports need to start at the second line.

Solutions

  1. (ideal) allow to declare a custom line number in a custom_language object:

    custom_languages = {
     {
       regex = [[^\s+import\s+.*from\s+]],
       filetypes = { "svelte" },
       extensions = { "svelte" },
       insert_at_line = 2; -- <-- like this
     },
  2. Allow insert_at_top to be configurable on a custom_language level. Currently, you would lose insert_at_top functionality for all languages (even ts, js) if you wanted to be compatible with svelte (or vue, html, etc). It's a shame because it's such a convenient feature.

    custom_languages = {
     {
       regex = [[^\s+import\s+.*from\s+]],
       filetypes = { "svelte" },
       extensions = { "svelte" },
       insert_at_top = false; -- <-- only for svelte
     },
piersolenski commented 10 months ago

Good point! I prefer the first approach if you feel like submitting a PR for it?

piersolenski commented 8 months ago

Move conversation to https://github.com/piersolenski/telescope-import.nvim/issues/22