Skip to content

How to add syntax highlighting support for your own favourite language ?

Navdeep Singh Sidhu edited this page Feb 24, 2017 · 3 revisions

Project's Directory Structure

/jsons /**Contains All the rules for langauages**/
/src   /**Contains source code**/
/./ in.co.s13.syntaxareafx  /**package**/
/./ langs       /**Contains java files for generating syntax highlighting rules**/
/./ meta        /**Contains Base Data Structure and tools**/
/./ res         /**Resource Directory**/
/././ json2    /**Stripped files (only special keywords) from json directory but rely on json directory if these doesn't make sense or vice versa**/
/././ css     /**Contains CSS theme for various languages**/
/./././default /** Default theme name and contains all CSS files for corresponding languages**/

Example for adding syntax highlighting support for your favourite language (ansforth94)

Check if SyntaxAreaFX.java FILE_TYPES enum contains the file-extensions from ansforth94.json's globs content under metadata key.

     "metadata": {"property": [
    {
        "name": "mimetypes",
        "content": "text/x-forth"
    },
    {
        "name": "globs",
        "content": "*.4th;*.forth"
    },
    {
        "name": "line-comment-start",
        "content": "\\\\"
    },
    {
        "name": "block-comment-start",
        "content": "\\("
    },
    {
        "name": "block-comment-end",
        "content": "\\)"
    }
]},

which are 4th and forth.

and Check if SyntaxAreaFX.java LANGS enum contains ansforth94 which is also the filename of corresponding file name of CSS file to load with language.

As we can't use "4th" as enum element, so we will add that to ALT_FILE_TYPES array as a String.

   public static String[] ALT_FILE_TYPES = {"4th"};

Now add a case under getFileTypeFromFileExtension method for the index of file extension

private FILE_TYPES getFileTypeFromFileExtension(String fileExtension) {
    FILE_TYPES fileType = null;
    if (Arrays.asList(ALT_FILE_TYPES).contains(fileExtension)) {
        int index = Arrays.asList(ALT_FILE_TYPES).indexOf(fileExtension);
        switch (index) {
            case 0:
                fileType = FILE_TYPES.forth;
                break;
            default:
                break;

        }
    } else {
        fileType = FILE_TYPES.valueOf(fileExtension);

    }

    return fileType;
}

where we added

            case 0:
                fileType = FILE_TYPES.forth;
                break;

and we add a case under getCodingStyleFromFileType() method for FILE_TYPES

       case forth: // element of FILE_TYPES
            language=LANGS.ansforth94; // element of LANGS and assiging to language
            break;

Create Ansforth94.java file under in.co.s13.langs package which implements Language from meta package.

Each language implement all the three methods from Language Interface

From ansforth94.json file, create a new array of String as CORE_KEYWORDS

    String CORE_KEYWORDS[] = new String[]{"!", "#",..... "\\['\\]", "\\[CHAR\\]","\\]"};

and corresponding String representation for Pattern

    String CORE_KEYWORDS_PATTERN;
    CORE_KEYWORDS_PATTERN = "\\b(" + String.join("|", CORE_KEYWORDS) + ")\\b";

and compile this pattern using

   pattern = Pattern.compile(
             "|(?<COREKEYWORDS>" + CORE_KEYWORDS_PATTERN + ")"
            );

Changes under getStyleClass()

   public String getStyleClass(Matcher matcher) {
           return  matcher.group("COREKEYWORDS") != null ? "core-keywords"
                   : null;
       }

where "core-keyword" is the field in "ansforth94.css" file, if it is not present please add and assign color which you think suits best for default theme. (Note: All CSS files and fields are generated randomly by Generator.java)

  .core-keywords {
      -fx-fill:#3D9970;
      -fx-font-weight: bold;
  }

And Last change in Ansforth94.java file is returning CORE_KEYWORDS in a ArrayList under getKeywords method.

@Override
public ArrayList<String> getKeywords() {
    ArrayList<String> keywordList = new ArrayList<>();
    keywordList.addAll(Arrays.asList(CORE_KEYWORDS));
    return keywordList;
}

and last but not least add case for the language under loadLanguage() method as

 case ansforth94:
            syntax= new Syntax(new Ansforth94());
            break;

See Commit for all the changes