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

Selecting a spell suggestion now uses z{index}= #3022

Closed
wants to merge 1 commit into from

Conversation

ilan-schemoul
Copy link

Selecting a spell suggestion now uses z{index}= instead of ciw.
This fixes spellrepall command not working
z{index}= is the same than typing z= and then typing the index with the native spell plugin

Description

#3019

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Open a file such as a.md with "Hey yelterdai yelterdai was good"
Telescope spell_suggest on the first mistyped yesterday choose one element then type :spellrepall and it will apply to the second element (and all others if applicable)

Configuration:

  • Neovim version (nvim --version):
    NVIM v0.10.0-dev-2723+g981301d11
  • Operating system and version:
    windows 11 WSL ubuntu 22

Checklist:

  • My code follows the style guidelines of this project (stylua)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (lua annotations)

@jamestrew
Copy link
Contributor

I don't think this works depending of the spellcapcheck setting and whether you're on the first word of the sentence.
z= seems to check for matches against spellcapcheck but we're just calling spellsuggest straight up (basically capital=false) so if spellcapcheck matches, the results from z= and vim.fn.spellsuggest would differ.

So we'd need to check the <cword> against spellcapcheck pattern and pass capital=true based on that result for this approach to work effectively. Trouble is, spellcapcheck needs more context that just the <cword> to work properly I think...

@ilan-schemoul
Copy link
Author

ilan-schemoul commented Mar 30, 2024

Oh so you mean
"againn you did it" I do "z=" suggestion is "Again" I do telescope spell_checks and it offers "again" and you want to avoid that user select "again" and is left with "Again" because of my fix ?
I mean personally I don't care for my use case but I see your point. If you have a suggestion I can implement I can try otherwise I guess I'lll have to maintain my fork (damn tinkering with my neovim config now requires me to maintain like 5 forks :( )
I guess what we'd really need is neovim to make a nice API around spell. I do not know why there's none.

@jamestrew
Copy link
Contributor

Yeah but it's worse than that cuz it's not just a difference of capitalized "Again" versus "again". Maybe some of the first few words match but the order and the list of words could be completely different depending on the word.

For example, if I have "hillo world" and I z= on "hillo", option 9 is "Hill o" but option 9 in spellsuggest("hillo") is "halloo". Completely different words.

I don't have any great solutions for this. I think for z=, internally vim check the progressively checks prior lines if it matches spellcapcheck and gives the suggestion based on this.
I don't really want to transpile this C code into Telescope...

But fyi

otherwise I guess I'lll have to maintain my fork

you don't need to maintain a fork of telescope for this or other similar minor changes. You can just copy/paste the code for the picker into your own config and tweak things to your hearts content.

@ilan-schemoul
Copy link
Author

Hmm yeah it's a tricky thing. It could be documented that to the :spellr command working you need to set nospellcapcheck=no and then copy the snippet in your config
And yeah the c function that make the spell check with the full context so it can check with the cap option etc. is complex so ofc reimplementation is a nogo
Ideally there should be a neovim function which could be named spellsuggestwithcontext() that would take the position in the buffer of a word as an argument so it can provide suggestion contextually.

@ilan-schemoul
Copy link
Author

By the way for the record this is what I added to my config (+ the vim cmd set spellcapcheck=no)

vim.api.nvim_create_user_command('CustomTelescopeSpellSuggest', function()
  require('telescope.builtin').spell_suggest({
    attach_mappings = function(prompt_bufnr)
      local actions = require("telescope.actions")
      local action_state = require("telescope.actions.state")
      local utils = require("telescope.utils")

      actions.select_default:replace(function()
        local selection = action_state.get_selected_entry()
        if selection == nil then
          utils.__warn_no_selection("builtin.spell_suggest")
          return
        end

        action_state.get_current_picker(prompt_bufnr)._original_mode = "i"
        actions.close(prompt_bufnr)
        vim.cmd("normal! " .. selection.index .. "z=")
        vim.cmd("stopinsert")
      end)
      return true
    end,
  })
end, {})

@jamestrew
Copy link
Contributor

Glad you got something working for yourself.
I'll close this PR until we come with a more robust solution. Thanks for trying though.

@jamestrew jamestrew closed this Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants