Skip to content

Commit

Permalink
Do not depend on UltiSnips internals
Browse files Browse the repository at this point in the history
Use UltiSnips#SnippetsInCurrentScope to fetch snippets.
Add an entry in the FAQ about the :UltiSnipsAddFiletypes command.
  • Loading branch information
micbou committed Sep 11, 2016
1 parent e804705 commit 5dca552
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 43 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2648,6 +2648,16 @@ g:UltiSnipsJumpForwardTrigger
g:UltiSnipsJumpBackwardTrigger
```

### Snippets added with `:UltiSnipsAddFiletypes` do not appear in the popup menu

For efficiency, YCM only fetches UltiSnips snippets in specific scenarios like
visiting a buffer or setting its filetype. You can force YCM to retrieve them by
manually triggering the `FileType` autocommand:

```viml
:doautocmd FileType
```

### Why isn't YCM just written in plain VimScript, FFS?

Because of the identifier completion engine and subsequence-based filtering.
Expand Down
38 changes: 24 additions & 14 deletions doc/youcompleteme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,24 @@ Contents ~
19. YCM does not read identifiers from my tags files |youcompleteme-ycm-does-not-read-identifiers-from-my-tags-files|
20. 'CTRL-U' in insert mode does not work |CTRL-sub-U|
21. YCM conflicts with UltiSnips TAB key usage |youcompleteme-ycm-conflicts-with-ultisnips-tab-key-usage|
22. Why isn't YCM just written in plain VimScript, FFS? |youcompleteme-why-isnt-ycm-just-written-in-plain-vimscript-ffs|
23. Why does YCM demand such a recent version of Vim? |youcompleteme-why-does-ycm-demand-such-recent-version-of-vim|
24. I get annoying messages in Vim's status area when I type |youcompleteme-i-get-annoying-messages-in-vims-status-area-when-i-type|
25. Nasty bugs happen if I have the 'vim-autoclose' plugin installed |vim-sub-autoclose|
26. Is there some sort of YCM mailing list? I have questions |youcompleteme-is-there-sort-of-ycm-mailing-list-i-have-questions|
27. I get an internal compiler error when installing |youcompleteme-i-get-an-internal-compiler-error-when-installing|
28. I get weird errors when I press 'Ctrl-C' in Vim |Ctrl-sub-C|
29. Why did YCM stop using Syntastic for diagnostics display? |youcompleteme-why-did-ycm-stop-using-syntastic-for-diagnostics-display|
30. Completion doesn't work with the C++ standard library headers |youcompleteme-completion-doesnt-work-with-c-standard-library-headers|
31. When I open a JavaScript file, I get an annoying warning about '.tern-project'
22. Snippets added with |:UltiSnipsAddFiletypes| do not appear in the popup menu
23. Why isn't YCM just written in plain VimScript, FFS? |youcompleteme-why-isnt-ycm-just-written-in-plain-vimscript-ffs|
24. Why does YCM demand such a recent version of Vim? |youcompleteme-why-does-ycm-demand-such-recent-version-of-vim|
25. I get annoying messages in Vim's status area when I type |youcompleteme-i-get-annoying-messages-in-vims-status-area-when-i-type|
26. Nasty bugs happen if I have the 'vim-autoclose' plugin installed |vim-sub-autoclose|
27. Is there some sort of YCM mailing list? I have questions |youcompleteme-is-there-sort-of-ycm-mailing-list-i-have-questions|
28. I get an internal compiler error when installing |youcompleteme-i-get-an-internal-compiler-error-when-installing|
29. I get weird errors when I press 'Ctrl-C' in Vim |Ctrl-sub-C|
30. Why did YCM stop using Syntastic for diagnostics display? |youcompleteme-why-did-ycm-stop-using-syntastic-for-diagnostics-display|
31. Completion doesn't work with the C++ standard library headers |youcompleteme-completion-doesnt-work-with-c-standard-library-headers|
32. When I open a JavaScript file, I get an annoying warning about '.tern-project'
file |.tern-sub-project|
32. When I start vim I get a runtime error saying 'R6034 An application has made an
33. When I start vim I get a runtime error saying 'R6034 An application has made an
attempt to load the C runtime library incorrectly.' |R6034-An-application-has-made-an-attempt-to-load-the-C-runtime-library-incorrectly.|
33. I hear that YCM only supports Python 2, is that true? |youcompleteme-i-hear-that-ycm-only-supports-python-2-is-that-true|
34. On Windows I get "E887: Sorry, this command is disabled, the Python's site
34. I hear that YCM only supports Python 2, is that true? |youcompleteme-i-hear-that-ycm-only-supports-python-2-is-that-true|
35. On Windows I get "E887: Sorry, this command is disabled, the Python's site
module could not be loaded" |E887:-Sorry-this-command-is-disabled-the-Python-s-site-module-could-not-be-loaded|
35. I can't complete python packages in a virtual environment. |youcompleteme-i-cant-complete-python-packages-in-virtual-environment.|
36. I can't complete python packages in a virtual environment. |youcompleteme-i-cant-complete-python-packages-in-virtual-environment.|
12. Contributor Code of Conduct |youcompleteme-contributor-code-of-conduct|
13. Contact |youcompleteme-contact|
14. License |youcompleteme-license|
Expand Down Expand Up @@ -2904,6 +2905,15 @@ options:
g:UltiSnipsJumpForwardTrigger
g:UltiSnipsJumpBackwardTrigger
<
-------------------------------------------------------------------------------
Snippets added with *:UltiSnipsAddFiletypes* do not appear in the popup menu

For efficiency, YCM only fetches UltiSnips snippets in specific scenarios like
visiting a buffer or setting its filetype. You can force YCM to retrieve them
by manually triggering the 'FileType' autocommand:
>
:doautocmd FileType
<
-------------------------------------------------------------------------------
*youcompleteme-why-isnt-ycm-just-written-in-plain-vimscript-ffs*
Why isn't YCM just written in plain VimScript, FFS? ~
Expand Down
43 changes: 14 additions & 29 deletions python/ycm/youcompleteme.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@
EventNotification )
from ycm.client.shutdown_request import SendShutdownRequest

try:
from UltiSnips import UltiSnips_Manager
USE_ULTISNIPS_DATA = True
except ImportError:
USE_ULTISNIPS_DATA = False


def PatchNoProxy():
current_value = os.environ.get('no_proxy', '')
Expand Down Expand Up @@ -319,7 +313,7 @@ def OnBufferVisit( self ):
if not self.IsServerAlive():
return
extra_data = {}
_AddUltiSnipsDataIfNeeded( extra_data )
self._AddUltiSnipsDataIfNeeded( extra_data )
SendEventNotificationAsync( 'BufferVisit', extra_data )


Expand Down Expand Up @@ -690,25 +684,16 @@ def BuildExtraConfData( extra_conf_vim_data ):
extra_conf_vim_data )


def _AddUltiSnipsDataIfNeeded( extra_data ):
if not USE_ULTISNIPS_DATA:
return

try:
# Since UltiSnips may run in a different python interpreter (python 3) than
# YCM, UltiSnips_Manager singleton is not necessary the same as the one
# used by YCM. In particular, it means that we cannot rely on UltiSnips to
# set the current filetypes to the singleton. We need to do it ourself.
UltiSnips_Manager.reset_buffer_filetypes()
UltiSnips_Manager.add_buffer_filetypes(
vimsupport.GetVariableValue( '&filetype' ) )
rawsnips = UltiSnips_Manager._snips( '', True )
except:
return

# UltiSnips_Manager._snips() returns a class instance where:
# class.trigger - name of snippet trigger word ( e.g. defn or testcase )
# class.description - description of the snippet
extra_data[ 'ultisnips_snippets' ] = [
{ 'trigger': x.trigger, 'description': x.description } for x in rawsnips
]
def _AddUltiSnipsDataIfNeeded( self, extra_data ):
# See :h UltiSnips#SnippetsInCurrentScope.
try:
vim.eval( 'UltiSnips#SnippetsInCurrentScope( 1 )' )
except vim.error:
return

snippets = vimsupport.GetVariableValue( 'g:current_ulti_dict_info' )
extra_data[ 'ultisnips_snippets' ] = [
{ 'trigger': trigger,
'description': snippet[ 'description' ] }
for trigger, snippet in iteritems( snippets )
]

9 comments on commit 5dca552

@liuxyc
Copy link

@liuxyc liuxyc commented on 5dca552 Sep 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't have UltiSnips installed, after update the code, i got error "E117: Unknown function: UltiSnips#SnippetsInCurrentScope"

@micbou
Copy link
Collaborator Author

@micbou micbou commented on 5dca552 Sep 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you paste the full error message?

@liuxyc
Copy link

@liuxyc liuxyc commented on 5dca552 Sep 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error detected while processing function 21_OnBufferRead:
line 17:
E117: Unknown function: UltiSnips#SnippetsInCurrentScope
E15: Invalid expression: UltiSnips#SnippetsInCurrentScope( 1 )
Error detected while processing function 85_AcceptSelection..webdevicons#ctrlPOpenFunc..ctrlp#acceptfile:
line 49:
E171: Missing :endif

after I install UltiSnips, this issue no longer exists

@micbou
Copy link
Collaborator Author

@micbou micbou commented on 5dca552 Sep 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still get the error when replacing except vim.error: by except Exception:? I can't reproduce without UltiSnips installed.

@puremourning
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange backtrace:

85_AcceptSelection..webdevicons#ctrlPOpenFunc..ctrlp#acceptfile

What plugin is that? Could it be an incompatibility with yCM?

@micbou
Copy link
Collaborator Author

@micbou micbou commented on 5dca552 Sep 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to reproduce with this Vim but it is not specific to YouCompleteMe. This version of Vim does not throw a Python exception when an error occurs while evaluating a Vim expression. The following code in Vimscript:

python << EOF
import vim
try:
  vim.eval( 'invalid_eval' )
except vim.error:
  pass
EOF

will raise these errors:

E121: Undefined variable: invalid_eval
E15: Invalid expression: invalid_eval

while the documentation (:h python-error) says:

Upon encountering a Vim error, Python raises an exception of type vim.error.

I don't think there is anything we can do to work around this bug. I recommend to use a newer version of Vim on Windows.

@liuxyc
Copy link

@liuxyc liuxyc commented on 5dca552 Sep 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks micbou, i update my vim to 8.0, it works

@sumeetb25
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the same error on vim 7.4 and Ubuntu 14.04

[Output of messages]
Messages maintainer: Bram Moolenaar [email protected]
"youcompleteme.py" 699L, 25188C
Error detected while processing function youcompleteme#Enable..14_OnBufferRead:
line 17:
E117: Unknown function: UltiSnips#SnippetsInCurrentScope
Error detected while processing function youcompleteme#Enable..14_OnBufferRead:
line 17:
E15: Invalid expression: UltiSnips#SnippetsInCurrentScope( 1 )

I tried using Exception instead of vim.error and still see the same error.

[Vim version]
sumeetb@u847beb1328d857c085be:~/.vim/bundle/YouCompleteMe$ vim --version | grep python
+cryptv +linebreak +python +viminfo
+cscope +lispindent -python3 +vreplace
Linking: gcc -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--as-needed -o vim -lm -ltinfo -lnsl -lselinux -lacl -lattr -lgpm -ldl -L/usr/lib/python2.7/config-x86_64-linux-gnu -lpython2.7 -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions

This errors disappear once I have UltiSnips installed.

@vheon
Copy link
Contributor

@vheon vheon commented on 5dca552 Sep 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sumeetb25 if you're using the vim that ships with Ubuntu 14.04 than there is a bug in that version of vim which prevents this for working. There is #2337 with a fix for that version of vim but we're actually thinking of bumping the minimum vim version to 7.4.143.

Please sign in to comment.