Coder Social home page Coder Social logo

j-gann / fragment-editor Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 588 KB

Fragment Editor with included Fragment database

Home Page: https://www.uni-heidelberg.de/

License: MIT License

TypeScript 94.45% Python 2.35% Batchfile 0.49% Shell 2.42% JavaScript 0.29%

fragment-editor's Introduction

frag.edit

A VS Code extension which was created for the course "Programming Tools For Data Science" at Heidelberg University.

Description

This VS Code extension can be used to create, edit and manage code snippets from any language. For the python language, placeholders in code snippets can be calculated alongside with their datatype. A placeholder is considered to be a variable which is used but not declared inside a code snippet.

Features

  • Create an empty Fragment (code snippet along with additional properties)
  • Display a list of Fragments in a Tree View
  • Edit the properties of a Fragment with a simple editor
  • Delete Fragments from the Tree View
  • Parametrize code snippets written in python
  • Calculate datatypes of placeholders for executable python code
  • Sorting of Fragments using folders by assigning tags to each Fragment

Usage

Adding Empty Fragments

When the Extension is installed, a new Tree View Container appears on the left side of the editor. This container displays the Fragment Editor Tree View. This Tree View will be populated by created Fragments. To create an empty Fragment, click on the box on the top right of the Tree View. Enter the name of the Fragment, then press enter. The name of the Fragment now appears in the Tree View on the left. If you click on an entry in the Tree View, an editor opens. With this editor properties of the Fragment can be edited and saved with the button 'save' on the top right.

Add Empty Fragment

Adding Tags

In order to structure Fragments in folders, the tag property of Fragments can be used. A tag, when created in the tag property of a Fragment, will appear as a folder in the Tree View and contains all Fragments which have this tag added to their tag property.

Add Tag

Parametrizing Fragments

In order to create Fragments out of an existing document select the text you want to add as a Fragment, then press the right mouse button. In the appearing menue press 'Add Fragment' and give it a name. If the document has a '.py' ending, the extension will try to determine placeholders and their datatypes. In order to use this feature, the execution of python documents has to be allowed in the settings of the extension. Corresponding information and warning visualisations will appear. It is important that in order to determine datatypes the extension will try to execute the document, which contains the selected code snippet.

Parametrize Fragment

Canceling Execution

If the execution takes too long, it can be cancelled using the button 'Cancel' which appears on the bottom right.

Cancel Execution

To execute the python code the extension uses the (configurable) call statement 'python3'.

Parametrization inside Fragment Editor

If an already saved Fragment should be parametrized, (because it was f.e. added by the frag.Extract Addon) the button 'Parametrize as Python' in the Fragment Editor beneath the body property can be used. This tries to extract placeholders from the Fragment, assuming the Fragment's language is Python. Notice, that datatypes of placeholders can't be determined in this case.

Parametrize in Editor

Extension Settings

Following properties of the extension are configurable

  • The call statement of python code (f.e. python or python2 or python3)

External Libraries

Internal Structure of the Extension

The Extension consists of 5 main Parts:

  • Fragment
  • Database
  • Tree View
  • Parametrization Algorithm
  • Fragment Editor

The Fragment is an object which contains important properties of a code snippet. The database manages creation and storage of Fragments and Tags. The Tree View displays all stored Fragments and Tags. The parametrization algorithm provides the main functionality of the extension: Parametrization of python Fragments. The Fragment Editor enables the user to look at and modify the properties of every Fragment.

The logic of the extension is located in the 'src' file. The Fragment is implemented in the file 'fragments.ts'. It is the goal of the extension to enable the creation, modification and overall management of these Fragments. The database is implemented in the file 'database.ts' and utilises sqlite. The Tree View (the junction of the extension) is implemented in the file 'fragmentProvider.ts'. Most of the functionality the user can access through the GUI is defined here. The parametrization algorithm called PyPa is implemented in the file 'parametrization.ts'.

Fragment

The Fragment contains typical properties of a code snippet like label, description, prefix, body and scope but also contains some additional information for management in the editor like tags, placeholders, keywords and domain.

Database

The database utilises sqlite. It saves Fragments on disk and loads them all into memory on startup. The database is defined to be located at the homedirectory inside a folder called 'fragments'. The corresponding file gets automatically created if it does not exist. The database contains several utility methods for managing Fragments in order to f.e. add Fragments, delete Fragments and update Fragments.

Besides Fragments, the database class stores TreeItems, Tags and Domains (not persistent) and contains several utility functions for these similar to those for Fragments.

Fragment Provider

As the name suggests, this class provides Fragments to the editor, in this case, as entries to the Tree View. What is displayed in the Tree View are not actually the Fragments themselves but an object called treeItem. These treeItems can either appear as a Tag or as a Fragment, depending on how they were instantiated. A treeItem with the contextValue "fragment" appears with the label of it's assigned fragment in the treeView. A treeItem with the contextValue "tag" appears as a folder with it's initialized name in the TreeView. This folder can contain multiple treeItems with contextValue "fragment", which have a fragment assigned that contains the tag of the folder in its tag propertie. The method 'createTreeStructure' creates all necessary treeItems for the stored Fragments dynamically. The method 'getChildren' gets called by the Tree View and displays all TreeItems the function returns.

The Fragment Provider contains several functions which can be called by the user from the GUI:

  • editFragment: Open the editor for the given fragment
  • addEmptyFragment: Add an empty fragment
  • addFragment: Add a fragment by selecting a snippet inside a text document
  • deleteTreeItem: Delete the given treeitem (In case the TreeItem is a tag, the tag gets removed from the Fragment. In case the TreeItem is a Fragment, the Fragment gets deleted)

Parametrization

PyPa (Python Parametrization) computes placeholders and the corresponding datatypes of a code snippet for any executable python script. In order to retrieve placeholders, the AST (abstract syntax tree) of the python code gets created and ported in JSON format (using astexport). The AST includes information about which variables are defined and which are used as parameter. Every variable, which is used as parameter but is not defined within a code snippet is considered a placeholder. In order to find these placeholders, the AST is queried for declared and used variables (using jsonpath). In order to retrieve the datatype of found placeholders, the original python code gets modified and executed. The following code is inserted at the beginning:

def detType(id, x):
     print('{\"id\": \"' + str(id) + '\", \"type\": ' + '\"' + str(type(x)) + '\"' + '}')
     return x

Every placeholder gets replaced by a call of this function with the name of the placeholder (string) as first parameter and the value of the placeholder (any datatype) as second parameter. This function prints the corresponding datatype for each placeholder during runtime. The output is then collected and processed.

Fragment Editor

The Fragment Editor is implemented using a Web View. Through vscode specific API's the communication between Web View and Extension is possible and therefore enables the modification of Fragments through the Web View with according updates in the Database.

TODOs

  • Add Testing (especially for PyPa to find as many cases of declarations and parameters in the AST as possible)
  • Algorithm for parametrizing non-python code snippets (should be no problem f.e for strongly typed languages)
  • Functionality for importing and exporting the database
  • In PyPa: Add recognition of definitions in imported modules (PyPa f.e. assumes all imported functions to be placeholders because they were not defined in the document itself)
  • Updating the sqlite dependency the database uses (updates to usage of promises)
  • In PyPa: Find a workaround for infinite loops to be able to get datatypes even if the code does not terminate
  • Color highlighting of placeholders in body in Fragment Editor / general syntax highlighting

fragment-editor's People

Contributors

jkoenig134 avatar

Stargazers

Artur Andrzejak avatar

Watchers

Jonas Gann avatar

fragment-editor's Issues

Determining Datatype of Placeholders by Execution

A possible solution if the program is executable AND the code snippet gets executed inside the program

  1. Get list of placeholders of the code snippet (including name, line and column)
  2. Get file of the code snippet
  3. Make a copy of the file
  4. Modify the copy:
    4.1) Replace the placeholders with: getType(placeholderName)
    4.2) function 'getType' saves the type of the placeholder and returns the placeholder
  5. Execute the modified copy

Extraction of Placeholders

First version of the parametrisation function:
Use filbert for parsing and placeholder extraction.
Use defiant in order to search for the small number of necessary objects of the JSON. The important objects are:

Objects with "type": "VariableDeclaration"
Object argument with "type": "Identifier"

GUI Testing

  1. Define all possible gui testcases
  2. Execute all the tests

Naming of tags

Error logs appear if a tag is created, with the same name as an existing fragment. Functionality is still there however.

Create a Class for Placeholders

In order to solve bugs of these types: #21 #20 create a class 'placeholder', where each instance represents one placeholder. Each placeholder should be able to include multiple variables with different locations but the same name. To identify if variables are part of the same placeholder, the python function id(variable) can be used in combination with the variable name:

  1. If two variables have the same id, they have the same adress and therefore reference the same object and are part of the same placeholder
  2. In python, for example, two variables having the same string value also have the same adress. To prevent this, the placeholder gets identified not only by the one unique id of its variables but also by the one name of the variables

Identifier of a placeholder: id(variable) + str(variable)

Fragment Filtering

The Feature to filter and search fragments has been deactivated because the code has not been adapted to the new code base. Modify or rewrite the filtering algorithm and activate the feature in the gui.

Adapt PyPa in order to work with FOEF approach

In order to reuse PyPa-created fragments using FOEF, Pypa for example has to create keywords for the fragment. Also FOEF approach has not been adapted to the current code base and needs to be checked.

Concept for a FoEF

A first concept for FOEF (Fragments of Exisiting Fragments)

Create a tree of fragments, where child-fragments must inherit the keywords of the parent. As a result, the further down a fragment is in the tree, the more specialized it is. This means, it has less open parameters but more filled in ones. To a given set of keywords, a path in the tree is walked where the given keywords match the ones of the fragments best.
The advantage of this concept is, that it can be applied to any language and/or framework.
The disadvantage is, that only code that is similar to a already created fragment can be successfully parametrised. Also a existing database of fragments is neccessary and it is important, that the user chooses keywords for newly created fragments wisely.
Concept FoEF

Problems with special charakters in the editor

Charakters (",...) can't be added, for example in the keyword input area of the fragmentEditor. Bug may exist in fragmentEditor.ts or database.ts.
The < charakter in the label of a fragment results in wrong formatting of the editor.

PyPa Misbehaves for Function Definitions

PyPa falsely declares variables inside a function as placeholders:
For example the following snippet

def function(x, y):
x = x+1
y = y+1

results in

def function(x, y):
x = ${0:x} + 1
y = ${1:y} + 1

Because neither x nor y are declared in the snippet, PyPa thinks they are placeholders.

Usage of drop down menus to enter values

Implement that a user can select tags or languages from a drop down menu inside the fragment editor. Additionally implement that the user can add languages that should appear in the drop down menu.

Research Code Testing within Visual Studio Code

Research how tests can be written in a Visual Studio Code Extension. It may not be trivial because in order to test the extension, it may have to be started by the test (for example to test the FragmentProvider and functions in general that rely on the vs code API).

Research Best Approaches for Establishing Datatype of a Placeholder

In order to establish the datatype of a given fragment there are several approaches:

  1. Find a python parser that includes the datatype of variables
  2. Write an algorithm that searches for the definition of the function in which the placeholder gets passed (Maybe usage of transpilers)
  3. Look at how the python interpreter figures out the datatype
  4. Look at the name of the variable to get a hint to its usage / datatype
  5. Ty to execute a modified version of the code in order to determine datatypes during runtime

Exclude while and for loops in PyPa

PyPa misbehaves with loops (while True is deadly)

  • ignore loops, Code should only be runnig once
  • replace loop keywords before runnig the code

Detection of Placeholders in PyPa is only best effort

PyPa detects Placeholders by computing if for every Parameter, there exists a definition with the same name anywhere in the snippet. Obviously, to improve this, python scopes and the order of definition and parameters have to be taken into consideration.

PyPa Problems with Duplicate Variable Names

Pypa can`t handle if there exist variables in different scopes with the same name. Pypa thinks all variables that have the same name are identical.
This problem is not trivial to solve, because PyPa has to look which variables have the same scope in order to determine if they are the same.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.