Skip to content

wolfj123/PolicyModelsPlugin

Repository files navigation

Policy Models Support for Visual Studio Code

Build Status

Overview

This plugin adds support for Policy Model languages in Visual Studio Code (more about Policy Models can be found here). This project is composed from an LSP Client and LSP Server. Both the Client and Server use Tree-Sitter Parsers to parse the languages (the client uses it for syntax highlighting and the server for the rest of the language features).

Table of contents

Installation

Pre-requisites

Instructions

  • Download the .vsix file from our releases.
  • Installing .vsix files can be done with the following command: code --install-extension [NAME-OF-INSTALLATION-FILE].vsix
  • Requires a reload of VSCode if it is running during the installation.

Features

Supported File Types

The plugin supports:

  • Decision graph files with .dg extension
  • Value inferrence files with .vi extension
  • Policy space files with .ps or .pspace .ts extension
    Files with .ts extensions can cause some unexpected behaviour. Look here for instructions using them correctly.

Syntax Highlighting

syntax highlighting

Go To References

We support references of Decision Graph nodes (that have an ID), Policy Space slots and Policy Space slot-values.

go to references

Go To Definition

We support definitions of Decision Graph nodes (that have an ID), Policy Space slots and Policy Space slot-values.

go to definition

Auto-Complete

Press ctrl + space (default VSCode shortcut) to recieve a completion list. The completion list includes keywords dependant on the file type, Decision Graph nodes (from imported graph files), slots and slot-values. autocomplete

Create New Model

Press the New Model button:

run model

Next fill out the necessary information:

run model

Running Model

Press the Run Model button:

run model

This will run the CLI and load the model into it:

run model

Localization

Pressing the Localization will open our Localization GUI. If a Localization folder does not already exist, you will promped to first create one. To Learn more about how these files are managed, see here.

localization button

The Localization GUI allows for simple localization files editing. You will get a structured editor for "answers.txt" and "space.md" files:

localization answers gui

localization space gui

And a free markdown editor for the rest of the .md files

localization space gui

We also provide a Markdown preview for the markdown files, in any available language

localization space gui

To create new language localization press the + button

localization space gui

Graphviz Visualization

It is possible to create a graphical visualization of the model using Graphviz (must be pre-installed).

There are 2 kinds of visualizations:

  • Decision Graph visualization
  • Policy Space visualization

Each has correpsonding button in the plugin:

visualization buttons

After pressing the button, provide the path to the graphviz application (dot file), Example paths:

  • Windows C:/Program Files (x86)/Graphviz2.38/bin/dot.exe
  • macOS /usr/local/bin/dot

graphviz path

The provided path will be save in a file on the path .../PolicyModelsPlugin/client/out/Graphviz/graphvizConfig.txt and is necessary configuration for the feature to work.

After that, provide file name and format (check here for all available Graphviz formats).

This will generate the file under a new visualization/type folder:

graphviz path

Policy Space Graphviz graph output example:

graphviz path

Supported Settings

in order to enable or disable any of the options below, you will need to have a folder named .vscode in the project folder. Inside this folder you will need a file named settings.json. If the file exists use the existing one. After making changes to this file you need to reopen VScode.

Syntax Errors Highlighter (Code Diagnostics)

This ability is disabled by default. To enable add to settings.json this: "PolicyModelsServer.Diagnostics": true

For more information please refer to this guide.

An example of error highlighting:

supported settings

Plugin Logging

Logging of the LSP server has 2 domains:

  1. Server trace - all the messaged sent between the client and server. they can be seen in VS-Code consle. To enable add: "PolicyModelsServer.trace.server": "verbose"
  2. Logging - this is a logging domain used by our code and writes the information to files saved in the plugin location on the computer under Logs folder. To enable add: "PolicyModelsServer.Logging": true

setting file example with all options enabled (can be seen here):

supported settings

For more information about logging look here.

Understanding Errors

For information regarding possible errors encountered in this plugin, please refer to this guide.

Development and Contribution

For contributing to this project, please refer to this guide.

Related

Policy Models

This plugin is designed to streamline and simplify the process of creating Policy Model projects. Learn more about Policy Models.

DataTaggingLibrary Project

Some of the features in this project are provided by an existing open-source project, the DataTaggingLibrary Project, that provides a Command Line Interface for creating and running Policy Model projects.

Language Server Protocol

Implementing support for features like autocomplete, goto definition, or documentation on hover for a programming language is a significant effort. Traditionally this work must be repeated for each development tool, as each provides different APIs for implementing the same features. The idea behind a Language Server is to provide the language-specific smarts inside a server that can communicate with development tooling over a protocol that enables inter-process communication. The idea behind the Language Server Protocol (LSP) is to standardize the protocol for how tools and servers communicate, so a single Language Server can be re-used in multiple development tools, and tools can support languages with minimal effort.

Learn more about LSP.

VSCode Language Extensions

This plugin is a VSCode Language Extension. These extensions provide additional support for more languages in the VSCode editor. Learn more about VSCode Language Extensions.

Tree-Sitter

Tree-sitter is a parser generator tool and an incremental parsing library. It can build a concrete syntax tree for a source file and efficiently update the syntax tree as the source file is edited. Tree-sitter aims to be: General enough to parse any programming language Fast enough to parse on every keystroke in a text editor Robust enough to provide useful results even in the presence of syntax errors Dependency-free so that the runtime library (which is written in pure C) can be embedded in any application

Learn more about Tree-Sitter.

For this project we have created 3 new Tree-Sitter parsers, one for each language in Policy Models. These parsers are standalone npm packages and can be used in other projects freely.

Decision Graph Parser

This parser can be found at https://www.npmjs.com/package/tree-sitter-decisiongraph.

Policy Space Parser

This parser can be found at https://www.npmjs.com/package/tree-sitter-policyspace.

Value Inference Parser

This parser can be found at https://www.npmjs.com/package/tree-sitter-valueinference.

Syntax Highlighting in VSCode using Tree-Sitter

At the time of this writing, LSP does not fully support Syntax Highlighting. Therefore we implemented this on the Client's side. We chose to use the Tree-Sitter parsers for the syntax highlighting, instead of the native Text-Mate support in VSCode, to allow easier maintainability. This was based on the wonderful work in this project.