Sublime Forum

Autocompletion of "phrases" (allowed to contain whitespace characters) without manually adding sublime-completion entries - possible?

#1

I’d like to set up my sublime so that it can complete ‘phrases’ for me, not just words. The difference is that phrases are allowed to contain whitespace characters.

This is already possible via sublime-completions files:

Ideally though I’d like something similar to how word completions are currently implemented, i.e. you don’t have to manually maintain anything, you just keep typing and the suggestions automatically pop up based on the text already in the buffer.

Is such a thing possible?

As you can see in the screenshot, I’m wrapping these phrases in special unicode brackets. So one idea would be to write a custom syntax that captures the string within (and including) these brackets as (indexed) symbols. This will allow them to be highlighted as well. But what’s the next step? Is it possible to set it up so that all the symbols of the current file appear in completions?

Would be so grateful for any help!

1 Like

#2

you could probably write a plugin that uses an event listener for on_query_completions that would return the indexed symbols using view.symbols().

2 Likes

#3

Thanks for the pointer and the good news, this is exactly what I wanted know.
I’ll go check out the docs (and source for plugins like AllAutocomplete) to try hack together something for this purpose.

1 Like

#4

I did a 5 part video series on my YouTube channel back in September that may be of help if you’re new to this sort of thing. In the series I go through creating a plugin for the manipulation of a simple notes file (based on a question that was asked on stack overflow) from the ground up.

The videos are fairly long, but this video covers creating a simple syntax and this video talks about creating an event handler that injects completions (links are for the relevant parts of the videos).

Video descriptions should have links to the code in each one so that you don’t have to transcribe it by hand.

2 Likes

#5

Thanks OdatNurd! I actually a just finished watching the 1st video two days ago and have the others coming up on my playlist. The 2nd link looks really relevant, I’ll go check it out now.

As an update to my original question, I just found this thread

where it sounds as though there’s no API yet for getting the indexed symbol list. The return value of view.symbols() is probably good enough for me, though (and at worst I can probably always set up some background process to grep the relevant directory and update the sublime-completions file every minute, lol)

1 Like

#6

To follow up on this, here’s a plugin (by adapting the All Autocomplete extension) that add all the symbols in all active views to autocomplete suggestions. This isn’t quite as good as autocompleting all indexed symbols, since it requires you to have the relevant files open, but for my purposes at least it’s almost as good. Here’s a screenshot and the code is below.

import sublime, sublime_plugin

class SymbolComplete(sublime_plugin.EventListener):
    def on_query_completions(self, view, prefix, locations):
        ReturnValue = [] 
        ListOfSymbolsInAllViews = []
        ListOfAllActiveViews = [view] + [v for v in sublime.active_window().views() if v.id != view.id]
        for TheViewCurrentlyUnderConsideration in ListOfAllActiveViews:
            ListOfSymbolsInAllViews.extend([i[1] for i in TheViewCurrentlyUnderConsideration.symbols()])
        ListOfSymbolsInAllViews = list(set(ListOfSymbolsInAllViews))
        for SymbolTextLiteral in ListOfSymbolsInAllViews:
            SymbolTextSanitized = SymbolTextLiteral.replace('$', '\\$')
            ReturnValue.append((SymbolTextLiteral + "\t(symbol)",SymbolTextSanitized))
        return ReturnValue
1 Like