onivim / oni

Oni: Modern Modal Editing - powered by Neovim
https://www.onivim.io
MIT License
11.34k stars 299 forks source link

Can we have keyword completion for file type without language support? #1475

Open coeuvre opened 6 years ago

coeuvre commented 6 years ago

Currently, without touching neovim config file, only file type with language support like JS can use auto completion. When typing in other file types, no completion popup will show up.

Can we have keyword completion working out of box?

oni-bot[bot] commented 6 years ago

Hello and welcome to the Oni repository! Thanks for opening your first issue here. To help us out, please make sure to include as much detail as possible - including screenshots and logs, if possible.

TalAmuyal commented 6 years ago

I think that a "general purpose" LSP can do the trick. After a quick search I didn't find one, but maybe there is. If not, I guess it shouldn't be hard to base on an existing one and just collect words and rank them...

badosu commented 6 years ago

I think this is a hard one, because one must ponder between adding bloat that might not be useful for all users and also having a 'batteries included' approach.

This could be achieved by analysing which would be the languages that are most used and perhaps bundling their respective LSPs, not a huge fan of this approach though.

Perhaps we could create oni plugins that bundle the LSP and configure oni automatically, this would make it straightforward enough so that it would work similar to how one installs sublime text plugins, without bloating the codebase.

Initially we could just add a directive on config.js to download and use the respective npm package and later on provide a UI to do this in a friendly manner.

bryphe commented 6 years ago

Thanks for the suggestion, @coeuvre ! I'd love to have this behavior - I miss it in files like markdown or plain text files, powershell scripts, etc. We could set it up to be enabled when there isn't a language service completion strategy available. Having it available and enabled by default fits in with Oni's mission of lowering the bar and a great out-of-box experience ('batteries included'). Of course, have it configurable, so that it can be opt-out - something like editor.completion.keywordCompletion.enabled?

Perhaps we could create oni plugins that bundle the LSP and configure oni automatically, this would make it straightforward enough so that it would work similar to how one installs sublime text plugins, without bloating the codebase.

👍 , I like the idea of having bundles for languages. It's definitely not feasible to bundle every language's completions strategy / highlighting / etc. And we could do some nice things like guide the user to a plugin if they are using a language that we don't have installed. But I think this is orthogonal to a keyword completion strategy that is available in general or as a fallback.

We actually have an undocumented API that could be potentially used for this - an API for providing custom completion providers. This is intended for adding general completions outside of an LSP. It's still a WIP (which is why it's undocumented). It's lighter weight than writing an LSP, and can be used in conjunction with an LSP completion strategy. (Other potential applications: showing snippets in completion, #1221 , etc)

You can actually test it out in your config.js today:

const activate = (oni) => {
    console.log("config activated")

    // A simple array of months...
    const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

    const MonthsCompletionProvider = {
        // Method to return completions, based on a language/filePath/line/column
        // This is the important piece - returns an array of completion items
        getCompletions: async (language, filePath, line, column) => {
            const createCompletionItem = (str) => {
                return {
                    label: str,
                    detail: "A calendar month",
                }
            }

            return months.map(createCompletionItem)
        },
        // This allows for getting more details for a particular item.
        getCompletionDetails: async (language, filePath, completionItem) => completionItem,
    }

    // Tell Oni about your completion provider..
    oni.completions.registerCompletionProvider("months", MonthsCompletionProvider)
...
}

And you'll see the new completion options: image (I'm in a javascript file - there's a bug where if there isn't already completion the provider won't get picked up...)

To implement this, we'd just need to create a completion provider that listens for buffer updates, grabs keywords, and keeps them around.

I think that a "general purpose" LSP can do the trick.

A general purpose / keyword-completion LSP is a really interesting idea too @TalAmuyal .. The benefit a LSP would have over the lightweight completion strategy above is that it is run in a separate process (the lightweight completion could block the UI if parsing keywords is slow, for example). If we can't find a LSP for a particular file, we could fall-back to this general LSP. In addition, it's something that could be generally applicable outside of Oni, too. As you mentioned I didn't see any here... but it is an interesting idea for sure!

Piping commented 6 years ago

@coeuvre @bryphe use the completor.vim plugin, you can have a very decent auto-completion behavior that supports all vim, neovim and oni without LSP (oni's external ui will take cares of it surprisingly well ) Python support is required.

    Plug 'maralla/completor.vim'
    inoremap <expr> <Tab>   pumvisible() ? "\<C-n>" : "\<Tab>"
    inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
    inoremap <expr> <Cr>    pumvisible() ? "\<C-y>" : "\<Cr>"
    inoremap <expr> <C-Y>   pumvisible() ? "\<C-y>" : '\<C-R>"'
feltech commented 6 years ago

This is one of the things I miss from YouCompleteMe - the keyword/buffer ("ID") completions complement the semantic completions nicely. I've found you can get this in Oni by tapping <C-n>, and the keyword completions render nicely in Oni's pretty popup menu. But it would indeed be nice if I didn't have to do that.

Piping commented 6 years ago

@feltech you can definitely create a mapping/some scripts for this functionality.