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

Supporting Custom autocompletion functions using a custom-defined api #65

Closed
zeyadhany98 opened this issue Mar 2, 2023 · 3 comments
Closed
Assignees

Comments

@zeyadhany98
Copy link

Hello @matkuki ,
#1: I would like to support custom autocompletion functions to be enabled while using the editor, could you tell me where can i edit it in the code?
I am guessing it will be in the "customeditor.py" file right?

#2: I have read in the documentation that you only make the function using a keyword and when using brackets it will be called through the call tip, isn't there a way to modify it?
Example of my wanted function style to be supported:-
Supported function appearing to be selected: ndif2(abstol, reltol, file1, file2);
Supported call tip to appear after using "(" : ndif2("1e-5","5e-2","sim.nd","$ENV{REF}/sim.nd");
my goal is to use the call tip to support a written example as a guide for me while I am writing

Are there any suggestions?

Thanks & Regards

@matkuki matkuki self-assigned this Mar 2, 2023
@zeyadhany98
Copy link
Author

Hello @matkuki,
Any updates for this issue ?

@matkuki
Copy link
Owner

matkuki commented Apr 8, 2023

I'm on it, will post details tomorrow.

@matkuki
Copy link
Owner

matkuki commented Apr 9, 2023

I would like to support custom autocompletion functions to be enabled while using the editor, could you tell me where can i edit it in the code?
I am guessing it will be in the "customeditor.py" file right?

Correct, this can be done in the gui/customeditor.py module in the enable_autocompletions method in line 2167.
If you want a guide to custom autocompletions, check out my documentation on QScintilla chapter 3.10. Autocompletions - Advanced: https://github.com/matkuki/qscintilla_docs , and also Kristof Mulier's great online version of the same documentation: https://qscintilla.com/#autocompletion (I would recommend this one)

I have read in the documentation that you only make the function using a keyword and when using brackets it will be called through the call tip, isn't there a way to modify it?
Example of my wanted function style to be supported:-
Supported function appearing to be selected: ndif2(abstol, reltol, file1, file2);
Supported call tip to appear after using "(" : ndif2("1e-5","5e-2","sim.nd","$ENV{REF}/sim.nd");
my goal is to use the call tip to support a written example as a guide for me while I am writing

From what I understand, you would want the autocompletion to show this: ndif2(abstol, reltol, file1, file2),
but when you would type the opening parenthesis of ndif2(,
it would change and show: ndif2("1e-5","5e-2","sim.nd","$ENV{REF}/sim.nd")? Am I understanding this correctly?

If I am, then this ndif2(abstol, reltol, file1, file2) cannot be shown as a autocompletion, because the parenthesis are automatically parsed as a function/method. That is why you should probably use UserLists, with which you can show any string you wish in the same way as an autocompletion list:
image
But the downside is, you will have to disable autocompletions and implement all the completion and calltip displaying logic yourself. This will probably be quite tedious if you wish to create calltips like the Scintilla built-in ones, when the color changes when you are at a certain argument, but with Qt/PyQt it can be done with some effort.
Here is a simple example of displaying a userlist:

# Import everything we need
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import *

# Create the main PyQt application object
application = QApplication(sys.argv)

# Create a QScintila editor instance
editor = QsciScintilla()
# Disable autocompletion
editor.setAutoCompletionSource(QsciScintilla.AcsNone)
# Connect UserList signal
def userListCallback(_id, _string):
    print(_id, _string)
editor.userListActivated.connect(userListCallback)

# Show an example UserList
def showUserListExample(*args):
    editor.showUserList(1, ["ndif2(abstol, reltol, file1, file2)", "two", "three"])
QTimer.singleShot(500, showUserListExample)

# Create the main window
main_window = QMainWindow()
main_window.setCentralWidget(editor)
main_window.resize(800, 600)
main_window.show()

# Execute the application
application.exec_()

With the above UserList method you are in full control of what's happening, but the alternative is to use autocompletions and adjust calltips on the fly manually. That means intercepting the '(' key-press, then adjusting the call tips to the value elements (i.e.: "1e-5","5e-2","sim.nd","$ENV{REF}/sim.nd") and reseting the autocompletions.

Hope it helps

@matkuki matkuki closed this as completed Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants