piersolenski / telescope-import.nvim

Import modules with ease
186 stars 8 forks source link

FR: make `insert_at_top` configurable per filetype or via function #22

Closed chrisgrieser closed 3 weeks ago

chrisgrieser commented 8 months ago

Whether imports should be added to the top or somewhere else often also depends on the language. In python, for example, it's convention to always put imports at the top, while in lua, it is often preferable to put imports inside a function due to lua's lazy evaluation (enabling lazy-loading of modules).

I could see two solutions for this:

  1. allow for insert_at_top to be a function, which can dynamically determine where to add the import
  2. allow for insert_at_top to be a table, where you assign a boolean to a filetype.
piersolenski commented 8 months ago

This is technically a dupe of https://github.com/piersolenski/telescope-import.nvim/issues/9 but I'll move the conversation here as the title covers more use cases.

piersolenski commented 8 months ago

@chrisgrieser, any thoughts on the approaches in https://github.com/piersolenski/telescope-import.nvim/issues/9 - and @thenbe, do you have any on the one above?

chrisgrieser commented 8 months ago

hmmm, the solutions proposed in #9 would also work. Not that big of a difference – the suggestion I made is cleaner if you only want to change insert_at_top, the suggestions in #9 are definitely cleaner if you add custom languages/regexes.

piersolenski commented 6 months ago

@chrisgrieser & @thenbe how about changing insert_at_top to insert_at_line, which can be one of the following:

The default value is 0.

We remove the top level insert_at_top in favour of adding insert_at_line defaults for each language in languages.lua, which can be overridden in the custom_languages section as usual:

{
        {
            regex = [[^\s+import\s+.*from\s+]],
            filetypes = { "svelte" },
            extensions = { "svelte" },
            insert_position = 2
        },
        {
            regex = [[^ ... +]],
            filetypes = { "..." },
            extensions = { "..." },
            insert_position = function()
              return 5
            end
        },
        {
            regex = [[^(?:local (\w+) = require\([\"'](.*?)[\"']\))]],
            filetypes = { "lua" },
            extensions = { "lua" },
            insert_position = nil
            -- Or if insert_at_line is omitted, it defaults to nil anyway
        }
}

In order to cater for people who prefer to import any language at their line of choosing on the fly, we could still have a top level insert_at_line where people can override any of the individually configured languages with the same options of nil, a number, or custom logic.

What do you think?

chrisgrieser commented 6 months ago

it would solve the issue for me, but having functions, nil, and line numbers feels like overcomplicating things to me.

Is there any scenario not covered by "insert at line 0" and "insert at cursor"? I can see "insert at line 1" to deal with modelines/shebangs, but that's about it?

piersolenski commented 6 months ago

I guess one might have situations where they might have multiline comments at the beginning of the file, or more complex situations where you might want to sort the imports alphabetically, or use cases beyond the languages over which you and I primarily have oversight?

If we are going to make a big api change then it'd be worth trying to anticipate all the uses cases, otherwise there might be another issue down the line asking for "insert at line n" and then we have to change it all over again haha.

Also how if "insert at line 0" and "insert at cursor" were two different settings, how would the conflict between them be handled if the latter was set to true? 🤔

JiuRanYa commented 2 months ago

Awesome plugin for me. Is there anyone follow up with this? I'm trying to use this plugin with .vue file.

I want to insert_at_line equals 2.

Maybe when user set insert_at_line, ignore selection value.

piersolenski commented 3 weeks ago

Hey all, I've implemented this over here https://github.com/piersolenski/telescope-import.nvim/pull/27

You can have a play around with it out locally:

{
  "piersolenski/telescope-import.nvim",
  branch = "feature/insert_position",
}

Any thoughts on setting some defaults, especially in regards to Vue?

piersolenski commented 3 weeks ago

Added in https://github.com/piersolenski/telescope-import.nvim/pull/27