Py code completion at a runtime

Hi all,

I need to find a way how to customize python console code completion. Is completioncontributor what i need here? How can i get access to the console context(all the variables already defined there with their values)?

The simplified case is dict keys completion:

a = {'1': 'a', '2': 'b'}

so when user typed a[ and ctrl+space it will suggest him 1 or 2 instead of no suggestions found?

Best Regards,

Artem

0
1 comment
Avatar
Permanently deleted user

Hi all, 

Sorry if i am asking dumb question, but...

We need to find a way how to add python console code completions based on some extra python code(for ex. when user types open("" and press ctrl+space it gets cwd and all the files recursively) 

Currently i have added a CompletionContributor.addCompletions with code like:

    public override fun addCompletions(parameters: CompletionParameters,
context: ProcessingContext,
resultSet: CompletionResultSet) {
val psiFile = parameters.position.containingFile

if (psiFile.language !== PythonLanguage.INSTANCE) {
return
}
val myClient = psiFile.getCopyableUserData(PydevConsoleRunner.CONSOLE_KEY) as PydevConsoleCommunication? ?: return
val waiter = Semaphore(0)

DumbService.getInstance(psiFile.project).smartInvokeLater({
try {
myClient.execInterpreter(commandPy){}
}finally {
waiter.release()
}
})
while (!waiter.tryAcquire(100, TimeUnit.MILLISECONDS)) {
ProgressManager.checkCanceled()
}
var pyresult: PyDebugValue?
pyresult = myClient.evaluate(varName, true, false)
if(pyresult.isContainer){
var builder: LookupElementBuilder?
val childrenList = myClient.loadVariable(pyresult)
for (i in 0..childrenList.size() - 1) {
val value = childrenList.getValue(i) as PyDebugValue
if(value.name == "__len__"){
continue
}

if (value.qualifiedType == "__builtin__.str") {
builder = LookupElementBuilder.create(value.value.replace("\\\\".toRegex(), "/"))
}
else {
builder = LookupElementBuilder.create(value.value)
}
resultSet.addElement(builder)
}
}else{
resultSet.addElement(LookupElementBuilder.create(pyresult.value))
}
resultSet.stopHere()
}
So the idea here is that for different use-cases we can use diff python code to be added to the console, 
that will produce the required completions for us and store them into var.

The slowest place here is:

        DumbService.getInstance(psiFile.project).smartInvokeLater({
try {
myClient.execInterpreter(commandPy){}
}finally {
waiter.release()
}
})
while (!waiter.tryAcquire(100, TimeUnit.MILLISECONDS)) {
ProgressManager.checkCanceled()
}

Is this approach right and is there any way how we can speed up completion(now it takes 3-5 secs
to show the completion box)?

Best Regards,
Artem

 

0

Please sign in to leave a comment.