Skip to content

Commit

Permalink
Add support for completions based on jedi (pyodide#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom authored Aug 14, 2019
1 parent cb4a56d commit 5db6017
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 5 deletions.
28 changes: 24 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ LDFLAGS=\
SIX_ROOT=six/six-1.11.0/build/lib
SIX_LIBS=$(SIX_ROOT)/six.py

JEDI_ROOT=jedi/jedi-0.15.1/jedi
JEDI_LIBS=$(JEDI_ROOT)/__init__.py

PARSO_ROOT=parso/parso-0.5.1/parso
PARSO_LIBS=$(PARSO_ROOT)/__init__.py

SITEPACKAGES=root/lib/python$(PYMINOR)/site-packages

all: build/pyodide.asm.js \
Expand Down Expand Up @@ -133,6 +139,8 @@ clean:
rm -fr src/*.bc
make -C packages clean
make -C six clean
make -C jedi clean
make -C parso clean
echo "The Emsdk, CPython and CLAPACK are not cleaned. cd into those directories to do so."


Expand All @@ -155,6 +163,8 @@ build/test.data: $(CPYTHONLIB)
root/.built: \
$(CPYTHONLIB) \
$(SIX_LIBS) \
$(JEDI_LIBS) \
$(PARSO_LIBS) \
src/sitecustomize.py \
src/webbrowser.py \
src/pyodide.py \
Expand All @@ -163,6 +173,8 @@ root/.built: \
mkdir -p root/lib
cp -a $(CPYTHONLIB)/ root/lib
cp $(SIX_LIBS) $(SITEPACKAGES)
cp -r $(JEDI_ROOT) $(SITEPACKAGES)
cp -r $(PARSO_ROOT) $(SITEPACKAGES)
cp src/sitecustomize.py $(SITEPACKAGES)
cp src/webbrowser.py root/lib/python$(PYMINOR)
cp src/_testcapi.py root/lib/python$(PYMINOR)
Expand All @@ -181,9 +193,9 @@ $(PYODIDE_EMCC):
mkdir -p $(PYODIDE_ROOT)/ccache ; \
if test ! -h $@; then \
if hash ccache &>/dev/null; then \
ln -s `which ccache` $@ ; \
ln -s `which ccache` $@ ; \
else \
ln -s emsdk/emsdk/emscripten/tag-$(EMSCRIPTEN_VERSION)/emcc $@; \
ln -s emsdk/emsdk/emscripten/tag-$(EMSCRIPTEN_VERSION)/emcc $@; \
fi; \
fi

Expand All @@ -194,9 +206,9 @@ $(PYODIDE_CXX):
if hash ccache &>/dev/null; then \
ln -s `which ccache` $@ ; \
else \
ln -s emsdk/emsdk/emscripten/tag-$(EMSCRIPTEN_VERSION)/em++ $@; \
ln -s emsdk/emsdk/emscripten/tag-$(EMSCRIPTEN_VERSION)/em++ $@; \
fi; \
fi
fi


$(CPYTHONLIB): emsdk/emsdk/.complete $(PYODIDE_EMCC) $(PYODIDE_CXX)
Expand All @@ -211,6 +223,14 @@ $(SIX_LIBS): $(CPYTHONLIB)
make -C six


$(JEDI_LIBS): $(CPYTHONLIB)
make -C jedi


$(PARSO_LIBS): $(CPYTHONLIB)
make -C parso


$(CLAPACK): $(CPYTHONLIB)
make -C CLAPACK

Expand Down
30 changes: 30 additions & 0 deletions jedi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
PYODIDE_ROOT=$(abspath ..)
include ../Makefile.envs

JEDIVERSION=0.15.1

ROOT=$(abspath .)

SRC=$(ROOT)/jedi-$(JEDIVERSION)
BUILD=$(SRC)/build/lib/jedi
TARBALL=$(ROOT)/downloads/jedi-$(JEDIVERSION).tar.gz
URL=https://files.pythonhosted.org/packages/85/03/cd5a6e44a5753b4d539288d9d1f9645caac889c17dd2950292a8818f86b2/jedi-$(JEDIVERSION).tar.gz


all: $(SRC)/setup.py


clean:
-rm -fr downloads
-rm -fr $(SRC)


$(TARBALL):
[ -d $(ROOT)/downloads ] || mkdir $(ROOT)/downloads
wget -q -O $@ $(URL)
sha256sum --quiet --check checksums || (rm $@; false)


$(SRC)/setup.py: $(TARBALL)
tar -C . -xf $(TARBALL)
touch $(SRC)/setup.py
1 change: 1 addition & 0 deletions jedi/checksums
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ba859c74fa3c966a22f2aeebe1b74ee27e2a462f56d3f5f7ca4a59af61bfe42e downloads/jedi-0.15.1.tar.gz
30 changes: 30 additions & 0 deletions parso/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
PYODIDE_ROOT=$(abspath ..)
include ../Makefile.envs

PARSOVERSION=0.5.1

ROOT=$(abspath .)

SRC=$(ROOT)/parso-$(PARSOVERSION)
BUILD=$(SRC)/build/lib/parso
TARBALL=$(ROOT)/downloads/parso-$(PARSOVERSION).tar.gz
URL=https://files.pythonhosted.org/packages/21/40/615957db4d178b7504c87b1a5b85fa5945b0b4fa5f5a845e31fc7aad6018/parso-$(PARSOVERSION).tar.gz


all: $(SRC)/setup.py


clean:
-rm -fr downloads
-rm -fr $(SRC)


$(TARBALL):
[ -d $(ROOT)/downloads ] || mkdir $(ROOT)/downloads
wget -q -O $@ $(URL)
sha256sum --quiet --check checksums || (rm $@; false)


$(SRC)/setup.py: $(TARBALL)
tar -C . -xf $(TARBALL)
touch $(SRC)/setup.py
1 change: 1 addition & 0 deletions parso/checksums
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
666b0ee4a7a1220f65d367617f2cd3ffddff3e205f3f16a0284df30e774c2a9c downloads/parso-0.5.1.tar.gz
44 changes: 43 additions & 1 deletion src/pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,46 @@ def as_nested_list(obj):
return obj


__all__ = ['open_url', 'eval_code', 'find_imports', 'as_nested_list']
def get_completions(code, cursor=None):
"""
Get code autocompletion candidates.
Follows the completion API in Jupyter outlined here:
https://jupyter-client.readthedocs.io/en/stable/messaging.html#completion
"""
import jedi

if cursor is None:
cursor = len(code)
code = code[:cursor]
interp = jedi.Interpreter(source=code, namespaces=[globals()])
completions = interp.completions()

if len(completions) == 0:
return {
'matches': [],
'cursor_start': cursor,
'cursor_end': cursor,
'metadata': {},
'status': 'ok'
}

c = completions[0]
delta = len(c.name_with_symbols) - len(c.complete)
return {
'matches': [x.full_name for x in completions],
'cursor_start': cursor - delta,
'cursor_end': cursor,
'metadata': {},
'status': 'ok'
}


__all__ = [
'open_url',
'eval_code',
'find_imports',
'as_nested_list',
'get_completions'
]
10 changes: 10 additions & 0 deletions test/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,13 @@ def test_unknown_attribute(selenium):
assert "asdf" in str(e)
"""
)


def test_completions(selenium):
result = selenium.run(
"""
import pyodide
pyodide.get_completions('import sys\\nsys.v')
"""
)
assert result['matches'] == ['sys.version', 'sys.version_info']

0 comments on commit 5db6017

Please sign in to comment.