1

I'm trying to create a Python plugin for VIM that will detect whether or not the current project is an Android Project. Unfortunately, I cannot get it to return boolean values back to VIM. Calling the plugin from within VIM just outputs nothing. The code below uses the print command but I've also tried vim.command("return {value}") and setting vim variables from within the script. Any insight?

I have a plugin file with these contents

if !has('python')
    echo "Error: Required vim compiled with +python"
    finish
endif


" Get local path for the script, so we can import other files
let s:script_folder_path = escape( expand( '<sfile>:p:h' ), '\' )
let s:python_folder_path = s:script_folder_path . '/../python/'

" Start the python file in the scriptdir
function! s:startPyfile(fileName)
    execute "pyfile " . s:python_folder_path . a:fileName
endfunction
command! Detect call Detect()
function! Detect()
  call s:startPyfile("vim_detect.py")
endfunction

which calls vim_detect.py which contains this

#! /usr/bin/env python

import vim
import os
import sys

# Add current scriptdir to import sources
current_script_dir = vim.eval('s:python_folder_path')
sys.path.append(current_script_dir)

class VimDetect:

    def executeCommand(self):
        if self.isAndroidGradleProject():
            print 1
        else:
            print 0


    def isAndroidGradleProject():
        if(isGradleProject() and isAndroidProject()):
            return True
        else:
            return False


    def isGradleProject():
        if findFileInDirectory("build.gradle"):
            return True
        else:
            return False

    def isAndroidProject():
        if findFileInDirectory("AndroidManifest.xml"):
            return True
        else:
            return False

    def findFileInDirectory(filename):
        top = os.getcwd()

        matches = 0
        for root, dirnames, files in os.walk(top):
            for file in fnmatch.filter(files, filename):
                matches = matches + 1
        if matches > 0:
            return True
        else:
            return False
2
  • 2
    Your python file does nothing but import, and # Add current scriptdir to import sources the class was never instantiated. Commented Dec 5, 2014 at 9:24
  • You may want to look at projectionist.vim which seems to have similar goals to yours Commented Dec 5, 2014 at 15:13

1 Answer 1

3

As others have commented, your example code (which you should have condensed to a minimal example) isn't actually invoked. The way I've returned a result from Python is by setting a Vim variable, like this:

strValue = "just a test"
vim.command("let python_result = '%s'" % str(strValue).replace("'", "''"))

For a boolean, just return 0 / 1 as a number, simplifying the logic:

vim.command("let python_result = %d" % int(boolValue))
Sign up to request clarification or add additional context in comments.

2 Comments

Made these changes. Still didn't work. Set proper variable scoping and it now works! Thanks!
@Ingo Karkat Great the 'strValue' solution but I noted that it puts an '^M' symbol at every end of line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.