Skip to content

Commit

Permalink
Make the plugin to be compatible with Python 3.
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 0973c5179e504ffbd74a38d6557bb49fe3bf8b5d
Author: Hong Xu <hong@topbug.net>
Date:   Sun Jan 10 00:11:11 2016 -0800

    Some minor corrections

commit ca17e97
Author: Shunsuke Shimizu <grafi@grafi.jp>
Date:   Sun Jan 10 15:24:13 2016 +0900

    vim 7.3 support by using `py[3]` command instead of `py[3]eval()` function

commit c51ae80
Author: grafi <grafi@grafi.jp>
Date:   Sun Jan 10 14:42:38 2016 +0900

    use print_function on python2

commit 401a948
Author: grafi <grafi@grafi.jp>
Date:   Sun Jan 10 14:33:25 2016 +0900

    assure that sys.path is cleaned

commit f3bf442
Author: grafi <grafi@grafi.jp>
Date:   Sun Jan 10 14:22:45 2016 +0900

    python3 style print

commit 8e05937
Author: Shunsuke Shimizu <grafi@grafi.jp>
Date:   Sat Dec 26 07:54:49 2015 +0900

    Support python3
  • Loading branch information
grafi-tt authored and xuhdev committed Jan 10, 2016
1 parent ac5ec0a commit c2b7a10
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
42 changes: 42 additions & 0 deletions plugin/editorconfig.py
@@ -0,0 +1,42 @@
from __future__ import print_function

try:
try:
import vim
import sys
except:
vim.command('let l:ret = 2')
raise

try:
sys.path.insert(0, vim.eval('a:editorconfig_core_py_dir'))

import editorconfig
import editorconfig.exceptions as editorconfig_except
except:
vim.command('let l:ret = 3')
raise
finally:
del sys.path[0]

# `ec_` prefix is used in order to keep clean Python namespace
ec_data = {}

def ec_UseConfigFiles():
ec_data['filename'] = vim.eval("expand('%:p')")
ec_data['conf_file'] = ".editorconfig"

try:
ec_data['options'] = editorconfig.get_properties(ec_data['filename'])
except editorconfig_except.EditorConfigError as e:
if int(vim.eval('g:EditorConfig_verbose')) != 0:
print(str(e), file=sys.stderr)
vim.command('let l:ret = 1')
return

for key, value in ec_data['options'].items():
vim.command("let l:config['" + key.replace("'", "''") + "'] = " +
"'" + value.replace("'", "''") + "'")

except:
pass
80 changes: 19 additions & 61 deletions plugin/editorconfig.vim
Expand Up @@ -36,6 +36,8 @@ let g:loaded_EditorConfig = 1
let s:saved_cpo = &cpo
set cpo&vim

let s:pyscript_path = expand('<sfile>:p:r') . '.py'

" variables {{{1
if !exists('g:EditorConfig_exec_path')
let g:EditorConfig_exec_path = ''
Expand Down Expand Up @@ -231,48 +233,23 @@ function! s:InitializePythonBuiltin(editorconfig_core_py_dir) " {{{2

let s:builtin_python_initialized = 1

let l:ret = 0

if !has('python')
if has('python')
let s:pyfile_cmd = 'pyfile'
let s:py_cmd = 'py'
elseif has('python3')
let s:pyfile_cmd = 'py3file'
let s:py_cmd = 'py3'
else
return 1
endif

python << EEOOFF

try:
import vim
import sys
except:
vim.command('let l:ret = 2')

EEOOFF

if l:ret != 0
return l:ret
endif

python << EEOOFF

try:
sys.path.insert(0, vim.eval('a:editorconfig_core_py_dir'))

import editorconfig
import editorconfig.exceptions as editorconfig_except

except:
vim.command('let l:ret = 3')

del sys.path[0]

ec_data = {} # used in order to keep clean Python namespace

EEOOFF

if l:ret != 0
return l:ret
endif
let l:ret = 0
" The following line modifies l:ret. This is a bit confusing but
" unfortunately to be compatible with Vim 7.3, we cannot use pyeval. This
" should be changed in the future.
execute s:pyfile_cmd s:pyscript_path

return 0
return l:ret
endfunction

" Do some initalization for the case that the user has specified core mode {{{1
Expand Down Expand Up @@ -388,41 +365,22 @@ augroup END
function! s:UseConfigFiles_Python_Builtin() " {{{2
" Use built-in python to run the python EditorConfig core

let l:config = {}
let l:ret = 0

" ignore buffers that do not have a file path associated
if empty(expand('%:p'))
return 0
endif

python << EEOOFF

ec_data['filename'] = vim.eval("expand('%:p')")
ec_data['conf_file'] = ".editorconfig"

try:
ec_data['options'] = editorconfig.get_properties(ec_data['filename'])
except editorconfig_except.EditorConfigError as e:
if int(vim.eval('g:EditorConfig_verbose')) != 0:
print >> sys.stderr, str(e)
vim.command('let l:ret = 1')
let l:config = {}

EEOOFF
let l:ret = 0
execute s:py_cmd 'ec_UseConfigFiles()'
if l:ret != 0
return l:ret
endif

python << EEOOFF
for key, value in ec_data['options'].items():
vim.command("let l:config['" + key.replace("'", "''") + "'] = " +
"'" + value.replace("'", "''") + "'")

EEOOFF

call s:ApplyConfig(l:config)

return 0
return l:ret
endfunction

function! s:UseConfigFiles_Python_External() " {{{2
Expand Down

0 comments on commit c2b7a10

Please sign in to comment.