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

Add code action for implementing auto-import #403

Closed
eyalk11 opened this issue Jul 11, 2023 · 10 comments · Fixed by #471
Closed

Add code action for implementing auto-import #403

eyalk11 opened this issue Jul 11, 2023 · 10 comments · Fixed by #471
Labels
enhancement New feature or request
Milestone

Comments

@eyalk11
Copy link

eyalk11 commented Jul 11, 2023

Hi,
while the auto-import feature is great, it is not enough. And a code action to do import of a certain class from module could be a very good improvement.

@eyalk11
Copy link
Author

eyalk11 commented Jul 13, 2023

FYI, there is a vim plugin called NayvyImports that does it basically. But it is less good.

@eyalk11 eyalk11 changed the title Add code action for implementing import Add code action for implementing auto-import Sep 1, 2023
@eyalk11
Copy link
Author

eyalk11 commented Sep 12, 2023

Any response? I wasn't clear enough?

@krassowski
Copy link
Contributor

Feel free to submit a pull request. If you need some inspiration, code actions are already implemented in pylsp-ruff.

@tkrabel-db
Copy link
Contributor

tkrabel-db commented Oct 22, 2023

FYI: I'm ready to tackle this. I think about the following:

  • add the pylsp_code_actions hook to rope_autoimport
  • define the settings a bit differently: memory for example is a shared field between completions and quickfix

I think about redefining settings to:

@hookimpl
def pylsp_settings() -> Dict[str, Dict[str, Dict[str, Any]]]:
    # Default rope_completion to disabled
    return {
        "plugins": {
            "rope_autoimport": {
                "memory": False,
                "completions": {
                    "enabled": False, 
                },
                "code_actions": {
                    "enabled": False,
                },
            }
        }
    }

@krassowski @ccordoba12 changing the settings name likely means a major release, right? I'd really keep code actions and completions under the single rope_autoimport file as both share a lot of functionalities.

@ccordoba12
Copy link
Member

ccordoba12 commented Oct 22, 2023

changing the settings name likely means a major release, right?

Not major but minor, i.e. from 1.8.x to 1.9.0

I'd really keep code actions and completions under the single rope_autoimport file as both share a lot of functionalities.

Sounds good to me.

@ccordoba12 ccordoba12 added this to the v1.9.0 milestone Oct 26, 2023
@ccordoba12 ccordoba12 added the enhancement New feature or request label Oct 26, 2023
@Lewenhaupt
Copy link

How do I enable this? I've tried both the config above (adapted to lua) and the config in the readme but I get no code-action alternative to import.

@tkrabel-db
Copy link
Contributor

@Lewenhaupt can you explain exactly what you did?

I use this when sending the initialize message and it works

export const INITIALIZATION_OPTIONS = {
  pylsp: {
    plugins: {
      rope_autoimport: {
        memory: false,
        enabled: true,
        code_actions: { enabled: true },
        completions: { enabled: false },
      },
    },
  },
};

@Lewenhaupt
Copy link

This is my current config. I'm using NVChad as my base config.

lspconfig.pylsp.setup({
	on_attach = on_attach,
	capabilities = capabilities,
	flags = {
		debounce_text_changes = 200,
	},
	settings = {
		plugins = {
			rope_completion = { enabled = true },
			rope_autoimport = {
				enabled = true,
			},
		},
	},
})

@tkrabel-db
Copy link
Contributor

Does actvating any other plugin work? E.g. can you enable flake8?

@Lewenhaupt
Copy link

I'll check tonight 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants