Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profile completion #826

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 47 additions & 12 deletions pyls/plugins/jedi_completion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Copyright 2017 Palantir Technologies, Inc.
import os
import logging
import os.path as osp
from datetime import datetime, timedelta
from collections import defaultdict

import parso

Expand Down Expand Up @@ -49,10 +52,12 @@
# Types of parso node for errors
_ERRORS = ('error_node', )


profile_dump = open('/tmp/pyls_completions.prof', 'w')
@hookimpl
def pyls_completions(config, document, position):
"""Get formatted completions for current code position"""
global profile_dump
t1 = datetime.now()
settings = config.plugin_settings('jedi_completion', document_path=document.path)
code_position = _utils.position_to_jedi_linecolumn(document, position)

Expand All @@ -62,6 +67,7 @@ def pyls_completions(config, document, position):
if not completions:
return None

# t15 = datetime.now()
completion_capabilities = config.capabilities.get('textDocument', {}).get('completion', {})
snippet_support = completion_capabilities.get('completionItem', {}).get('snippetSupport')

Expand All @@ -71,8 +77,10 @@ def pyls_completions(config, document, position):
include_params = snippet_support and should_include_params and use_snippets(document, position)
include_class_objects = snippet_support and should_include_class_objects and use_snippets(document, position)

# t17 = datetime.now()
format_profile_ranges = defaultdict(timedelta)
ready_completions = [
_format_completion(c, include_params)
_format_completion(c, include_params, format_profile_ranges)
for c in completions
]

Expand All @@ -84,6 +92,17 @@ def pyls_completions(config, document, position):
completion_dict['label'] += ' object'
ready_completions.append(completion_dict)

t2 = datetime.now()
print(
# 'pid', os.getpid(),
'pyls_completions',
'total', t2 - t1,
'format_profile_ranges', format_profile_ranges,
# 'jedi', t15 - t1,
# 'before_format', t17 - t15,
# 'format', t2 - t17,
file=profile_dump)
profile_dump.flush()
return ready_completions or None


Expand Down Expand Up @@ -137,24 +156,36 @@ def use_snippets(document, position):
not (expr_type in _ERRORS and 'import' in code))


def _format_completion(d, include_params=True):
def _format_completion(d, include_params=True, profile_ranges=None):
t1 = datetime.now()
completion = {
'label': _label(d),
'kind': _TYPE_MAP.get(d.type),
'detail': _detail(d),
'documentation': _utils.format_docstring(d.docstring()),
'sortText': _sort_text(d),
'insertText': d.name
}
t2 = datetime.now()
sig = d.get_signatures()
completion.update({
'label': _label(d, sig),
'kind': _TYPE_MAP.get(d.type),
'sortText': _sort_text(d),
'insertText': d.name,
'detail': _detail(d),
})
t3 = datetime.now()

if d.type == 'path':
path = osp.normpath(d.name)
path = path.replace('\\', '\\\\')
path = path.replace('/', '\\/')
completion['insertText'] = path

sig = d.get_signatures()
if (include_params and sig and not is_exception_class(d.name)):
profile_ranges['documentation'] += (t2 - t1)
profile_ranges['update'] += (t3 - t2)
profile_ranges['total'] += (t3 - t1)
if not include_params:
return completion

if sig and not is_exception_class(d.name):
completion['label'] = _label(d, sig)
positional_args = [param for param in sig[0].params
if '=' not in param.description and
param.name not in {'/', '*'}]
Expand All @@ -174,12 +205,16 @@ def _format_completion(d, include_params=True):
completion['insertText'] = d.name + '($0)'
else:
completion['insertText'] = d.name + '()'
# t4 = datetime.now()

# profile_ranges['path'] += (t3 - t2)
# profile_ranges['forks'] += (t4 - t3)

return completion


def _label(definition):
sig = definition.get_signatures()
def _label(definition, sig):
# sig = definition.get_signatures()
if definition.type in ('function', 'method') and sig:
params = ', '.join(param.name for param in sig[0].params)
return '{}({})'.format(definition.name, params)
Expand Down
11 changes: 10 additions & 1 deletion pyls/python_ls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2017 Palantir Technologies, Inc.
from datetime import datetime
from functools import partial
import logging
import os
Expand All @@ -16,6 +17,7 @@
log = logging.getLogger(__name__)


profile_dump = open('/tmp/PythonLanguageServer.completions.prof', 'w')
LINT_DEBOUNCE_S = 0.5 # 500 ms
PARENT_PROCESS_WATCH_INTERVAL = 10 # 10 s
MAX_WORKERS = 64
Expand Down Expand Up @@ -153,7 +155,9 @@ def _hook(self, hook_name, doc_uri=None, **kwargs):
workspace = self._match_uri_to_workspace(doc_uri)
doc = workspace.get_document(doc_uri) if doc_uri else None
hook_handlers = self.config.plugin_manager.subset_hook_caller(hook_name, self.config.disabled_plugins)
return hook_handlers(config=self.config, workspace=workspace, document=doc, **kwargs)
ret = hook_handlers(config=self.config, workspace=workspace, document=doc, **kwargs)

return ret

def capabilities(self):
server_capabilities = {
Expand Down Expand Up @@ -237,7 +241,12 @@ def code_lens(self, doc_uri):
return flatten(self._hook('pyls_code_lens', doc_uri))

def completions(self, doc_uri, position):
global profile_dump
t1 = datetime.now()
completions = self._hook('pyls_completions', doc_uri, position=position)
t2 = datetime.now()
print('completions', t2 - t1, file=profile_dump)
profile_dump.flush()
return {
'isIncomplete': False,
'items': flatten(completions)
Expand Down