Skip to content

Commit

Permalink
Remove support for Python2 (#1129)
Browse files Browse the repository at this point in the history
Python 3 is > 10 years old and Python 2 will no longer be supported in a few months. Time to get rid of it, simplifying the code base (slightly) and opening the opportunities to use the Py3 quality of life improvements.
  • Loading branch information
SirVer authored Nov 5, 2019
1 parent 7dc30c5 commit d2f42d6
Show file tree
Hide file tree
Showing 38 changed files with 169 additions and 390 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.pyc
*.swp
doc/tags
.ropeproject/
/.ropeproject/
/.mypy_cache/
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ sudo: required
services: docker

env:
- VIM_VERSION="7.4" PYTHON_IMAGE=2.7-stretch TAG=vim_74_py2
- VIM_VERSION="8.0" PYTHON_IMAGE=2.7-stretch TAG=vim_80_py2
- VIM_VERSION="8.1" PYTHON_IMAGE=2.7-stretch TAG=vim_81_py2
- VIM_VERSION="git" PYTHON_IMAGE=2.7-stretch TAG=vim_git_py2
- VIM_VERSION="7.4" PYTHON_IMAGE=3.6-stretch TAG=vim_74_py36
- VIM_VERSION="8.0" PYTHON_IMAGE=3.6-stretch TAG=vim_80_py36
- VIM_VERSION="8.1" PYTHON_IMAGE=3.6-stretch TAG=vim_81_py36
Expand Down
8 changes: 0 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MAKEFILE_DIR := $(dir ${MAKEFILE_PATH})

# Test images as run on CI.
image_vim_74_py2:
docker build -t ultisnips:$@ --build-arg PYTHON_IMAGE=2.7-stretch --build-arg VIM_VERSION=7.4 .
image_vim_80_py2:
docker build -t ultisnips:$@ --build-arg PYTHON_IMAGE=2.7-stretch --build-arg VIM_VERSION=8.0 .
image_vim_81_py2:
docker build -t ultisnips:$@ --build-arg PYTHON_IMAGE=2.7-stretch --build-arg VIM_VERSION=8.1 .
image_vim_git_py2:
docker build -t ultisnips:$@ --build-arg PYTHON_IMAGE=2.7-stretch --build-arg VIM_VERSION=git .
image_vim_74_py36:
docker build -t ultisnips:$@ --build-arg PYTHON_IMAGE=3.6-stretch --build-arg VIM_VERSION=7.4 .
image_vim_80_py36:
Expand Down
2 changes: 1 addition & 1 deletion after/plugin/UltiSnips_after.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
" Called after everything else to reclaim keys (Needed for Supertab)

if exists("b:did_after_plugin_ultisnips_after") || !exists("g:_uspy")
if exists("b:did_after_plugin_ultisnips_after")
finish
endif
let b:did_after_plugin_ultisnips_after = 1
Expand Down
56 changes: 28 additions & 28 deletions autoload/UltiSnips.vim
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
if exists("b:did_autoload_ultisnips") || !exists("g:_uspy")
if exists("b:did_autoload_ultisnips")
finish
endif
let b:did_autoload_ultisnips = 1

" Also import vim as we expect it to be imported in many places.
exec g:_uspy "import vim"
exec g:_uspy "from UltiSnips import UltiSnips_Manager"
py3 import vim
py3 from UltiSnips import UltiSnips_Manager

function! s:compensate_for_pum() abort
""" The CursorMovedI event is not triggered while the popup-menu is visible,
""" and it's by this event that UltiSnips updates its vim-state. The fix is
""" to explicitly check for the presence of the popup menu, and update
""" the vim-state accordingly.
if pumvisible()
exec g:_uspy "UltiSnips_Manager._cursor_moved()"
py3 UltiSnips_Manager._cursor_moved()
endif
endfunction

Expand All @@ -23,7 +23,7 @@ function! UltiSnips#Edit(bang, ...) abort
else
let type = ""
endif
exec g:_uspy "vim.command(\"let file = '%s'\" % UltiSnips_Manager._file_to_edit(vim.eval(\"type\"), vim.eval('a:bang')))"
py3 vim.command("let file = '%s'" % UltiSnips_Manager._file_to_edit(vim.eval("type"), vim.eval('a:bang')))

if !len(file)
return
Expand All @@ -48,7 +48,7 @@ function! UltiSnips#Edit(bang, ...) abort
endfunction

function! UltiSnips#AddFiletypes(filetypes) abort
exec g:_uspy "UltiSnips_Manager.add_buffer_filetypes('" . a:filetypes . "')"
py3 UltiSnips_Manager.add_buffer_filetypes('" . a:filetypes . "')
return ""
endfunction

Expand All @@ -69,18 +69,18 @@ function! UltiSnips#FileTypeComplete(arglead, cmdline, cursorpos) abort
endfunction

function! UltiSnips#ExpandSnippet() abort
exec g:_uspy "UltiSnips_Manager.expand()"
py3 UltiSnips_Manager.expand()
return ""
endfunction

function! UltiSnips#ExpandSnippetOrJump() abort
call s:compensate_for_pum()
exec g:_uspy "UltiSnips_Manager.expand_or_jump()"
py3 UltiSnips_Manager.expand_or_jump()
return ""
endfunction

function! UltiSnips#ListSnippets() abort
exec g:_uspy "UltiSnips_Manager.list_snippets()"
py3 UltiSnips_Manager.list_snippets()
return ""
endfunction

Expand All @@ -90,69 +90,69 @@ function! UltiSnips#SnippetsInCurrentScope(...) abort
if all
let g:current_ulti_dict_info = {}
endif
exec g:_uspy "UltiSnips_Manager.snippets_in_current_scope(" . all . ")"
py3 UltiSnips_Manager.snippets_in_current_scope(int(vim.eval("all")))
return g:current_ulti_dict
endfunction

function! UltiSnips#SaveLastVisualSelection() range abort
exec g:_uspy "UltiSnips_Manager._save_last_visual_selection()"
py3 UltiSnips_Manager._save_last_visual_selection()
return ""
endfunction

function! UltiSnips#JumpBackwards() abort
call s:compensate_for_pum()
exec g:_uspy "UltiSnips_Manager.jump_backwards()"
py3 UltiSnips_Manager.jump_backwards()
return ""
endfunction

function! UltiSnips#JumpForwards() abort
call s:compensate_for_pum()
exec g:_uspy "UltiSnips_Manager.jump_forwards()"
py3 UltiSnips_Manager.jump_forwards()
return ""
endfunction

function! UltiSnips#AddSnippetWithPriority(trigger, value, description, options, filetype, priority) abort
exec g:_uspy "trigger = vim.eval(\"a:trigger\")"
exec g:_uspy "value = vim.eval(\"a:value\")"
exec g:_uspy "description = vim.eval(\"a:description\")"
exec g:_uspy "options = vim.eval(\"a:options\")"
exec g:_uspy "filetype = vim.eval(\"a:filetype\")"
exec g:_uspy "priority = vim.eval(\"a:priority\")"
exec g:_uspy "UltiSnips_Manager.add_snippet(trigger, value, description, options, filetype, priority)"
py3 trigger = vim.eval("a:trigger")
py3 value = vim.eval("a:value")
py3 description = vim.eval("a:description")
py3 options = vim.eval("a:options")
py3 filetype = vim.eval("a:filetype")
py3 priority = vim.eval("a:priority")
py3 UltiSnips_Manager.add_snippet(trigger, value, description, options, filetype, priority)
return ""
endfunction

function! UltiSnips#Anon(value, ...) abort
" Takes the same arguments as SnippetManager.expand_anon:
" (value, trigger="", description="", options="")
exec g:_uspy "args = vim.eval(\"a:000\")"
exec g:_uspy "value = vim.eval(\"a:value\")"
exec g:_uspy "UltiSnips_Manager.expand_anon(value, *args)"
py3 args = vim.eval("a:000")
py3 value = vim.eval("a:value")
py3 UltiSnips_Manager.expand_anon(value, *args)
return ""
endfunction

function! UltiSnips#CursorMoved() abort
exec g:_uspy "UltiSnips_Manager._cursor_moved()"
py3 UltiSnips_Manager._cursor_moved()
endf

function! UltiSnips#LeavingBuffer() abort
let from_preview = getwinvar(winnr('#'), '&previewwindow')
let to_preview = getwinvar(winnr(), '&previewwindow')

if !(from_preview || to_preview)
exec g:_uspy "UltiSnips_Manager._leaving_buffer()"
py3 UltiSnips_Manager._leaving_buffer()
endif
endf

function! UltiSnips#LeavingInsertMode() abort
exec g:_uspy "UltiSnips_Manager._leaving_insert_mode()"
py3 UltiSnips_Manager._leaving_insert_mode()
endfunction

function! UltiSnips#TrackChange() abort
exec g:_uspy "UltiSnips_Manager._track_change()"
py3 UltiSnips_Manager._track_change()
endfunction

function! UltiSnips#RefreshSnippets() abort
exec g:_uspy "UltiSnips_Manager._refresh_snippets()"
py3 UltiSnips_Manager._refresh_snippets()
endfunction
" }}}
2 changes: 1 addition & 1 deletion autoload/UltiSnips/map_keys.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if exists("b:did_autoload_ultisnips_map_keys") || !exists("g:_uspy")
if exists("b:did_autoload_ultisnips_map_keys")
finish
endif
let b:did_autoload_ultisnips_map_keys = 1
Expand Down
29 changes: 5 additions & 24 deletions doc/UltiSnips.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ UltiSnips *snippet* *snippets* *UltiSnips*

This plugin only works if 'compatible' is not set.
{Vi does not have any of these features}
{only available when |+python| or |+python3| have been enabled at compile time}
{only available when |+python3| has been enabled at compile time}


==============================================================================
Expand Down Expand Up @@ -89,22 +89,14 @@ http://vimcasts.org/episodes/ultisnips-visual-placeholder/
1.1 Requirements *UltiSnips-requirements*
----------------

This plugin works with Vim version 7.4 or later. It only works if the
'compatible' setting is not set.
This plugin works with Vim version 7.4 or later with Python 3 support enabled.
It only works if the 'compatible' setting is not set.

This plugin is tested against Python 2.7 and 3.6 in Vim 7.4 and 8. All other
combinations are unsupported, but might work.

The Python 2.x or Python 3.x interface must be available. In other words, Vim
must be compiled with either the |+python| feature or the |+python3| feature.
The Python 3.x interface must be available. In other words, Vim
must be compiled with the |+python3| feature.
The following commands show how to test if you have python compiled in Vim.
They print '1' if the python version is compiled in, '0' if not.

Test if Vim is compiled with python version 2.x: >
:echo has("python")
The python version Vim is linked against can be found with: >
:py import sys; print(sys.version)
Test if Vim is compiled with python version 3.x: >
:echo has("python3")
The python version Vim is linked against can be found with: >
Expand All @@ -113,17 +105,6 @@ The python version Vim is linked against can be found with: >
Note that Vim is maybe not using your system-wide installed python version, so
make sure to check the Python version inside of Vim.

UltiSnips attempts to auto-detect which python version is compiled into Vim.
Unfortunately, in some versions of Vim this detection does not work.
In that case you have to explicitly tell UltiSnips which version to use using
the 'UltiSnipsUsePythonVersion' global variable.

To use python version 2.x: >
let g:UltiSnipsUsePythonVersion = 2
To use python version 3.x: >
let g:UltiSnipsUsePythonVersion = 3

1.2 Acknowledgments *UltiSnips-acknowledgments*
-------------------
Expand Down
11 changes: 4 additions & 7 deletions docker/build_vim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@ set -o verbose

cd /src/vim

if [[ $PYTHON_VERSION =~ ^2\. ]]; then
PYTHON_BUILD_CONFIG="--enable-pythoninterp"
else
PYTHON_BUILD_CONFIG="--enable-python3interp"
fi
PYTHON_BUILD_CONFIG="--enable-python3interp"

export CFLAGS="$(python-config --cflags)"
echo $CFLAGS
./configure \
--disable-gpm \
--disable-nls \
--disable-sysmouse \
--disable-gpm \
--enable-gui=no \
--enable-multibyte \
--enable-python3interp \
--with-features=huge \
--with-tlib=ncurses \
--without-x \
$PYTHON_BUILD_CONFIG || cat $(find . -name 'config.log')
|| cat $(find . -name 'config.log')
make install
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
python_version = 3.7
warn_return_any = True
warn_unused_configs = True
mypy_path=pythonx/UltiSnips

[mypy-vim]
ignore_missing_imports = True
Expand Down
30 changes: 0 additions & 30 deletions plugin/UltiSnips.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,6 @@ if version < 704
finish
endif

if !exists("g:UltiSnipsUsePythonVersion")
let g:_uspy=":py3 "
if !has("python3")
if !has("python")
if !exists("g:UltiSnipsNoPythonWarning")
echohl WarningMsg
echom "UltiSnips requires py >= 2.7 or py3"
echohl None
endif
unlet g:_uspy
finish
endif
let g:_uspy=":py "
endif
else
" Use user-provided value, but check if it's available.
" This uses `has()`, because e.g. `exists(":python3")` is always 2.
if g:UltiSnipsUsePythonVersion == 2 && has('python')
let g:_uspy=":python "
elseif g:UltiSnipsUsePythonVersion == 3 && has('python3')
let g:_uspy=":python3 "
endif
if !exists('g:_uspy')
echohl WarningMsg
echom "UltiSnips: the Python version from g:UltiSnipsUsePythonVersion (".g:UltiSnipsUsePythonVersion.") is not available."
echohl None
finish
endif
endif

" The Commands we define.
command! -bang -nargs=? -complete=customlist,UltiSnips#FileTypeComplete UltiSnipsEdit
\ :call UltiSnips#Edit(<q-bang>, <q-args>)
Expand Down
9 changes: 1 addition & 8 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ disable=
too-many-locals,
too-many-public-methods,
too-many-return-statements,
too-many-statements,
useless-object-inheritance,
too-many-statements



Expand Down Expand Up @@ -145,12 +144,6 @@ max-module-lines=1000
indent-string=' '


[MISCELLANEOUS]

# List of note tags to take in consideration, separated by a comma.
notes=TODO


[SIMILARITIES]

# Minimum lines number of a similarity.
Expand Down
12 changes: 4 additions & 8 deletions pythonx/UltiSnips/buffer_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import vim
from UltiSnips import vim_helper
from UltiSnips.compatibility import as_unicode, as_vimencoding
from UltiSnips.position import Position
from UltiSnips.diff import diff

Expand Down Expand Up @@ -96,11 +95,11 @@ def __setitem__(self, key, value):
changes and applies them to the current snippet stack.
"""
if isinstance(key, slice):
value = [as_vimencoding(line) for line in value]
value = [line for line in value]
changes = list(self._get_diff(key.start, key.stop, value))
self._buffer[key.start : key.stop] = [line.strip("\n") for line in value]
else:
value = as_vimencoding(value)
value = value
changes = list(self._get_line_diff(key, self._buffer[key], value))
self._buffer[key] = value

Expand All @@ -122,10 +121,7 @@ def __getitem__(self, key):
"""
Just passing call to the vim.current.window.buffer.__getitem__.
"""
if isinstance(key, slice):
return [as_unicode(l) for l in self._buffer[key.start : key.stop]]
else:
return as_unicode(self._buffer[key])
return self._buffer[key]

def __getslice__(self, i, j):
"""
Expand All @@ -147,7 +143,7 @@ def append(self, line, line_number=-1):
line_number = len(self)
if not isinstance(line, list):
line = [line]
self[line_number:line_number] = [as_vimencoding(l) for l in line]
self[line_number:line_number] = [l for l in line]

def __delitem__(self, key):
if isinstance(key, slice):
Expand Down
Loading

0 comments on commit d2f42d6

Please sign in to comment.