syntaxcomplete#Complete() and nested filetypes

13 views
Skip to first unread message

Lifepillar

unread,
Jun 21, 2021, 6:03:29 AM6/21/21
to vim...@googlegroups.com
I would like to use syntaxcomplete#Complete() in a filetype that
includes a nested filetype. The only way I have found so far is to
define my own completion function that checks whether the current
position is within the nested filetype, then calls OmniSyntaxList() with
the appropriate regular expression. Something along these lines:

if synIDattr(...) =~ '^nestedfiletype'
matches = OmniSyntaxList(['^nestedfiletype\w\+'])
else
matches = OmniSyntaxList(['^mainfiletype\w\+'])
endif

This is simple enough, but (1) it does not benefit from
syntaxcomplete#Complete() caching, and (2) it requires my own function.

Perhaps, there is a more canonical way to achieve what I want?

Ideally, I would like to set omnifunc=syntaxcomplete#Complete(), and
have Vim figure out the filetype to use for the current completion.
I see that there are some global variables to tweak the completion
behaviour (g:omni_syntax_group_include, ...), but I have not found how
to use them for the my purpose.

Thanks,
Life.

Lifepillar

unread,
Jun 21, 2021, 6:23:25 AM6/21/21
to vim...@googlegroups.com
On 2021-06-21, Lifepillar <lifep...@lifepillar.me> wrote:
> I would like to use syntaxcomplete#Complete() in a filetype that
> includes a nested filetype.

This is what I have so far, which appears to be working in filetype
`foobar` with nested filetype `xyz`:

def foobarcomplete#Complete(findstart: number, base: string): any
if findstart == 1
syntaxcomplete#OmniSyntaxClearCache()

if len(synstack(line("."), 1)) > 0 &&
synIDattr(synstack(line("."), 1)[0], "name") ==# '^xyz'
g:omni_syntax_group_include_foobar = 'xyz\w\+'
else
unlet! g:omni_syntax_group_include_foobar
endif
endif

return syntaxcomplete#Complete(findstart, base)
enddef

If you have better ideas, let me know! I especially do not like that
I have to clear the cache.

Thanks,
Life.

David Fishburn

unread,
Jun 21, 2021, 9:09:51 PM6/21/21
to vim_use
Life, 

I am not sure I quite understand your use case for the plugin.

:h ft-syntax-omni

In the example I provide there, I am editing a PHP file, which has embedded HTML and JavaScript.
So I need syntaxComplete to also provide JavaScript functions, not just PHP functions.

That examples demonstrates how to use the syntaxcomplete variables like:
    let g:omni_syntax_group_include_php = 'php\w\+,javaScript\w\+,html\w\+'

I can help if you give me a bit more to go on with a concrete example of commands and what you expected / want to see.

David


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/sappac%243eh%242%40ciao.gmane.io.

Lifepillar

unread,
Jun 22, 2021, 5:14:30 AM6/22/21
to vim...@googlegroups.com
On 2021-06-22, David Fishburn <dfishb...@gmail.com> wrote:
> Life,
>
> I am not sure I quite understand your use case for the plugin.

Thanks for the feedback. My goal is to have some sort of
context-sensitive completion. My use case is ConTeXt, which can include
MetaPost blocks:

\starttext
TeX code here: filetype is `context`
\startMPcode
% Nested filetype here is `mp`.
% MetaPost code here.
% Only `mpSomething` syntax groups should be used for
% completion.
\stopMPcode
TeX code again: filetype is again `context`.
Only `contextSomething` syntax groups should be used.
\stoptext

Within the ...MPcode block, where a nested filetype is active, I'd like
completion only for that filetype. AFAICT, I can add MetaPost syntax
groups using omni_syntax_group_include_context, but they will be active
throughout the buffer.

The snippet I have posted allows me to do that sort of context-sensitive
completion, but maybe there is a better/more efficient way.

Thanks,
Life.

Reply all
Reply to author
Forward
0 new messages