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 command to switch language #12

Open
Fymyte opened this issue Aug 24, 2022 · 7 comments
Open

Add command to switch language #12

Fymyte opened this issue Aug 24, 2022 · 7 comments
Labels
enhancement New feature or request

Comments

@Fymyte
Copy link
Contributor

Fymyte commented Aug 24, 2022

At setup, add a command to change the cheched language

Provide a command like

LTextSwitchLang <lang>

with lang autocompletion

I have this in my config

    local function setup_ltex(lang)
      local config = {
        on_attach = function(client, bufnr)
          custom_attach(client, bufnr)
          require('ltex_extra').setup {
            load_langs = { 'en-US', 'fr-FR' },
            -- init_check = true,
            path = vim.fn.stdpath 'config' .. '/spell/dictionaries',
          }
          vim.api.nvim_create_user_command("LtexSwitchLang", function(args)
            local splited_args = vim.split(args.args, " ", {trimemtpy=true})
            setup_ltex(splited_args[1])
          end, { nargs = 1 })
        end,
        settings = {
          ['ltex'] = {
            configurationTarget = {
              dictionary = 'user',
              disabledRules = 'workspaceFolderExternalFile',
              hiddenFalsePositives = 'workspaceFolderExternalFile',
            },
            language = lang,
          },
        },
      }
      require'lspconfig'['ltex'].setup(config)
    end
    setup_ltex('en-US')

It is not perfect but a good starting point as it provides the the Command only when ltex is setup
It does not remove it once ltex dies though,and produce a warning saying that the previous instance of ltex died

Available languages from ltex: https://valentjn.github.io/ltex/settings.html#ltexlanguage

@Fymyte
Copy link
Contributor Author

Fymyte commented Aug 24, 2022

I will probably do a PR, but I am not sure how to implement this properly. I think we should do as rust-tools do: call the setup(config) ourselves so we know the config to re-instanciate the client.
Also here is the autocompletion part:

local ltex_languages = {
  "auto",
  "ar",
  "ast-ES",
  "be-BY",
  "br-FR",
  "ca-ES",
  "ca-ES-valencia",
  "da-DK",
  "de",
  "de-AT",
  "de-CH",
  "de-DE",
  "de-DE-x-simple-language",
  "el-GR",
  "en",
  "en-AU",
  "en-CA",
  "en-GB",
  "en-NZ",
  "en-US",
  "en-ZA",
  "eo",
  "es",
  "es-AR",
  "fa",
  "fr",
  "ga-IE",
  "gl-ES",
  "it",
  "ja-JP",
  "km-KH",
  "nl",
  "nl-BE",
  "pl-PL",
  "pt",
  "pt-AO",
  "pt-BR",
  "pt-MZ",
  "pt-PT",
  "ro-RO",
  "ru-RU",
  "sk-SK",
  "sl-SI",
  "sv",
  "ta-IN",
  "tl-PH",
  "uk-UA",
  "zh-CN",
}

vim.api.nvim_create_user_command("LtexSwitchLang", function(args)
    local splited_args = vim.split(args.args, " ", {trimemtpy=true})
    local ltex_clients = vim.lsp.get_active_clients({bufnr=0, name="ltex"})
    for _, ltex_client in ipairs(ltex_clients) do
      vim.lsp.stop_client(ltex_client.id, false)
   end
   setup_ltex(splited_args[1])
 end, {
    nargs = 1,
    complete = function (ArgLead, _, _)
      return vim.tbl_filter(function(el) return el:find(ArgLead, 1, true) end, ltex_languages)
    end
  })
end,

@barreiroleo
Copy link
Owner

Hi @Fymyte, how are you?
This is a really good idea, I like it.

I think that it will be useful for previewing and temporary things, I would like to extend this feat (or fork in similar) to offer "magic comments" depending on the type of file.
I believe that it's not necessary re-instantiate the setup, just needs to notify the ltex server. This should resolve the errors too.
I would like to work on it soon, I'll open a branch.

Thank you very much.

@barreiroleo barreiroleo added the enhancement New feature or request label Aug 24, 2022
@ayoubelmhamdi
Copy link

my idea is to use some env variable for each project, with some function or apps like direnv

echo export PROJECT_LANG=fr > .envrc

in lspconfig.lua

local lang = os.getenv 'PROJECT_LANG' or 'en'

require('lspconfig').ltex.setup {
  settings = {
    ltex = {
      language = lang,
    }
  }
}

@fhfuih
Copy link
Contributor

fhfuih commented Jan 26, 2023

Hi everyone, I just encountered this wonder plugin from ltex issue board, and I am very happy using it so far!

I also love this idea, but after a glimpse at the repo, I think maybe it comes with an (optional but elegant to have) cost of refactoring the current code.
Currently the setup is done in on_attach function, which is more like a place for buffer-specific additional configs, e.g., adding key bindings and custom lsp commands. It seems a little bit unintuitive to add custom commands in lsp on_attach.

I noticed that many other lsp extension plugins inverses the control and do something like

-- from https://github.com/jose-elias-alvarez/typescript.nvim
require("typescript").setup({
    -- additional behaviors here
    disable_commands = false, -- prevent the plugin from creating Vim commands
    debug = false, -- enable debug logging for commands
    go_to_source_definition = {
        fallback = true, -- fall back to standard LSP definition on failure
    },
    -- lspconfig arguments here, passed to internal calls of lspconfig setup()
    server = { -- pass options to lspconfig's setup method
        on_attach = ..., -- This on_attach function is often some one-size-fit-all stuff provided by user or neovim distros. We can extend on_attach in the implementation of `require("ltexextra").setup` function. 
    },
})

The same pattern is used in deno, clangd, dart, rust plugins
And after inversing the control, it seems a more reasonable place to configure the additional features of ltex (and we can both extend on_attach behaviors to define lsp commands as usual, as well as add new vim commands like a language switch)

I would like to extend this feat (or fork in similar) to offer "magic comments" depending on the type of file.

I think this is less necessary. Magic comments or modlines should apply to specific files instead of a filetype, right? ltex already offers per-file config via magic comments, markdown front matters, and latex babel calls. For per-filetype configs, maybe vim autocmd is sufficient? Or adding another config field in ltex_extra will work, which does not require peeking into the file content.

@barreiroleo
Copy link
Owner

barreiroleo commented Jan 29, 2023

Yeah, it's part of the things I want to do with architecture. Especially for giving better compatibility with the flow of 'ftplugin' or 'Lazy.nvim'. It's not soo complex, someday I will get the time.

The "magic comments" it's not my happiest idea, but I had some trouble exchanging commands through Ltex server. I don't know what expect in the future.

@jkorb
Copy link

jkorb commented Apr 24, 2023

First off: thanks so much for #the plugin @barreiroleo , it's cleared a lot of headaches for me and I'm using it daily :)

I was wondering what the current plans are for implementing this feature? I saw the branch 12-switch-langs but as far as I can see it doesn't contain any code to implement the feature, right?

Now, I don't have much experience with lua, nvim plugins, and/or development in general (I'm an academic researcher) but I'd be interested in contributing if that's wanted.

I'm currently daily-driving a simple (hopefully unproblematic) implementation of the feature (ad2aa19 and 30f808b): a simple switchLanguage function that uses the same methodology as addToDictionary et al. (it updates the server spec and notifies the server of the change), which I expose to the user (moi) in the setup function via nvim_create_user_command. It uses some ideas from above and works, as far as I can see. But as I said, I'm inexperienced.

If you're interested in this, I'd definitely be happy to work on a PR. No worries, if not, of course. In that case just: thanks again :)

@barreiroleo
Copy link
Owner

Hi @jkorb, it's nice to hear that.

I've frozen the implementation of that because I don't use this very much and I'm not sure about the more comfortable flow.
My perspective of Vim flow has changed quite a lot since that moment and now I think the best approach is to add a code action to trigger a selector but there are other perspectives.

As always, the PRs are very welcome. Particularly for me, I don't care if is a full implementation or very clean code. Something to start is nice to have.

Sorry for the delay, I missed the notification.

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

No branches or pull requests

5 participants