New Stepik.org Plugin for IntelliJ IDEA

    In September, we released a plugin for IntelliJ IDEA with support for Python and Java. We will talk about the development process, and why the plug-in will simplify the life of students programming in Stepik.org, in this article.



    The plugin, as in the case of the Stepik Telegram bot , and mobile applications for iOS and Android, was designed by our new intern Peter Bogdanov , a student at the Computer Science Center in St. Petersburg.

    Purpose of the plugin


    Why did we do it? Of course, in order to simplify the life of students, not to force them to perform their routine operations, to allow them to concentrate fully on the learning process.

    The first difficulty a student faces when taking a course on programming on the platform is the need for copy-paste to the browser from the IDE. Programming in the browser is not easy: you cannot run the code and check it locally, there is no auto-completion and refactoring, and much more. Also, when programming in the IDE, you must constantly peek at the browser to view the conditions of the task or copy the Sample Input.

    The second difficulty is the problem of organizing the code in the project. Students, typing the code in the sandbox, usually solve only the current problem and do not think about where they will solve the next problem. And most likely, the student either does not save the previous decisions, or his files are called like “new_file” “new_file7” “new_file777”. All this complicates navigation and makes code reuse difficult.

    Development process


    In June of this year, we released a plugin that only supported Java, logged in to the project manager, and interacted with Stepik.org using context menus. But he also had significant flaws in architecture.

    As a convenient platform for learning programming, it is important for us to support as many programming languages ​​as possible. A plugin can be conditionally divided into two parts: independent of the supported language (authorization, interaction with the API, settings) and directly the language support (basically, building a project).

    Since the old plugin was monolithic, it was not possible to add support for new languages ​​and write new plugins for other IDEs. Therefore, it was decided to switch to the core of training plugins from the PyCharm team .

    Consider in detail:


    • Saving user data
    • Authorization and network communication
    • Building a project tree

    Saving user data


    To prevent user data from being lost during a restart, you must save it. To do this, IDEA has the PersistentStateComponent interface , with which you can serialize the class of interest to you in .xml. This is not specified in the documentation, but it is understood that the class that implements PersistentStateComponent is a singleton that needs to be instantiated in one of three ways, according to the component level: Application level, Project level and Module level.

    In the first case, the singleton will be the only one in the whole plugin, and it is reasonable to store global settings in it.
    In other cases, there can be many singletones and they will be serialized in ./.idea. It is at the project level that we store user data (name, token, etc.), and almost all meta information about the course.

    The reasonable question is where to store data if no projects are open? For such a case, IDEA has a defaultProject, which can be obtained like this:

    ProjectManager.getInstance().getDefaultProject()

    From there, we take credentials when we create the project, and that is why we ask the user to close all projects before logging in to the settings explicitly.

    You can implement this interface in two ways ( see the documentation for more details ), in addition, you must definitely declare this component in plugin.xml (the most important .xml) in accordance with the level, for example:


    In general, having implemented any interface from the IntelliJ API, it must be declared in plugin.xml.

    And for storing passwords, you can use the built-in password manager PasswordSafe . It is because of the access to it that such windows appear from time to time:




    Authorization and network communication


    Stepik.org supports full OAuth 2 authorization. But in the plugin, only authorization through login and password is implemented so far. There is a problem with other types of authorization in that you have to raise the server to localhost to listen for redirected_url .

    To communicate with Stepik, the core already had the EduStepicConnector class , which contained a large set of static methods, and an HttpClient instance for executing requests. This class was decided to be divided into 4 classes:



    In the Client Init, the HttpClient is initialized and the problem with certificate verification is solved. Stepik has a certificate from Let's Encrypt, and IdenTrus CA was added only in Java 8u101. StepikConnectorLoginresponsible for authorization. And the remaining classes can already make requests that require authorization and not.

    In such a scheme, there can be only one active client, but it is not difficult to expand it by several, creating instances of authorized clients. However, we do not want to indulge students who lead a double life on Stepica :)

    Building a project tree


    The construction of the project tree has changed a lot relative to the first version of the plugin, but weakly relative to the kernel.
    Now the course is downloaded, cached and only then built from the cache. This was realized due to the slow construction of the project: to download the entire course, you need to complete about 100 requests. Here we managed to achieve significant acceleration by combining a lot of id in one request to the API.

    The June version of the plugin was supported by Yandex.Translator to translate the names of the course, lessons and sections into English. This was necessary so that the package names for Java were in English, and the specific file corresponded to the step. And it was a bit uncomfortable. I had to edit the file before sending and imposed additional restrictions.

    Now this is not necessary, because the step level now corresponds to the directory in which source is located. But, nevertheless, directories now have working names (lesson1 / task1), and with the help of TreeStructreProvider there is a fictitious substitution of directory names and they are displayed in the language of the course.


    TreeStructureProvider on / off.

    The TreeStructureProvider also hides the ./src/hide directory, which stores step templates in different languages. And when you change the language, the files are moved.

    Plugin dependency




    The picture shows all the educational plugins from JetBrains known to us, as well as the Stepik plugin and its dependencies. Lilac plugins for PyCharm are shown. It can be seen that our plugin needs 3 more plugins to work. The Python dependency is not yet configured correctly (so you have to install it manually). Interestingly, Community and Ultimate IDEA rely on different plugins. Edu-IntelliJ is just an extension point for a project manager. Perhaps we will remove this dependence.

    And the main dependence on the kernel remains: the current changes in the kernel have not yet been uploaded to the official repository, so you will have to manually install the unstable version. And in this case, the Edu-Java and Edu-Kotlin plugins will not work for you yet.

    New UI


    The first thing we added was authorization in the settings, a la GitHub.



    But, if you still skip this action, you will meet a more intrusive authorization.



    When building a project, you can choose a course from the list of courses you are subscribed to. But this does not mean that they are all compatible with the plugin. In order for a course to be created without errors, it must have at least one step with a programming task.



    In this window you can select the desired programming language, but if it is not available, the most popular one will be selected.

    There is still the opportunity to unload the course by reference, while if you have not previously been subscribed to the course, a subscription takes place. You can enter either the course id or any link from the course in the link field.

    The step status is displayed in the project tree:



    And the main actions are in the Task Description panel on the right.



    Among them: sending a solution, previous / next step, starting over (not yet available), downloading the latest solution and changing the language.

    Compatibility


    Stepik.org does not yet have an API in order to understand whether a particular course is compatible with a third-party application, so it’s useful to imagine how everything works.

    After authorization, the plugin downloads the project tree, for this it collects information about all the steps in the open part of the course. Then it saves data only for supported steps. At the moment, these are only tasks of the “code” type. However, this type of task has a feature - it uses hidden templates. Hidden patterns are pieces of code that are added to the beginning and end of a file. And by definition, they are not available to the student.

    Consider the example of Java. Using templates, the teacher can create tasks for the implementation of a separate method, and the method wraps in the class already on the server. Thus, the student will not compile the correct solution, because all Java code must be in the class.

    But the good news is that there are not so many such tasks. They are most often found in language courses.
    Therefore, the plugin is ideal for a course in algorithms and Hadoop .

    Future plans


    In the near future, we want to develop support for various programming languages. At the moment, it's easy enough to add R, Scala to Stepik Union, but, for example, Haskell support is still too crude. And with C ++ it’s more complicated, here you have to write a separate plugin for CLion.

    In addition, we are considering implementing support for other types of tasks by the plugin.

    Acknowledgments


    Our gratitude to the PyCharm team for the help and high-quality core.

    Conclusion


    We will be happy to answer questions, get feedback, suggestions and constructive criticism of the plugin in the comments and in the discussion with the author of the VK plugin .

    Links: Stepik.org plugins , StepikOrg / intellij-community , Stepik API doc , IntelliJ Platform SDK Documentation , Repository for IntelliJ Platform SDK Documentation .

    Also popular now: