Home
Categories
Dictionary
Download
Project Details
Changes Log
License

Syntax highlighting



The scripts package contains classes which allows to add syntax highlighting to text content.

Supported syntaxes

The default supported syntaxes with syntax highlighting[1]
Except for the text files syntax, which has no syntax highlighting
are:
Supported syntaxes
Syntax Name Alternate names Extensions
text files[2]
This syntax is for plain text documents. It has no syntax highlighting
text - txt
batch files[3]
They are Windows bat files
batch bat -
C++ files c++ cpp cpp, cc, hpp
C# files csharp c# cs
C files c C c, h
fortran files fortran - for, f, ftn, fpp, f90,f95, f03, f08
html files html - html, htm
idl declarations idl IDL idl
JavaScript files jscript js, javascript js
JSON files[4]
JSON files are valid JavaScript files, so the syntax for these files is the same
json - json
Java files java - java
Groovy files[5]
Groovy files are presented by default with the same syntax as Java files, though the syntax can be overriden by syntax packs
groovy - groovy, gvy, gy, gsh
makefiles make - make
php files php - php
properties files props properties properties
Python files python py py
SQL files sql SQL sql
T-SQL files tsql T-SQL tsql
shell scripts[6]
they are Unix or Linux shell scripts
shell bash bash, sh
XML files xml dtd, DTD, xsd, xsl xml, xsd, xsl, xul, svg, xaml, scxml, fxml, rdf, vxml, wsdl, dae, owl
SPARQL files sparql - sparql
turtle files turtle ttl ttl
TeX files tex - tex
xpath files xpath - xpath
patch files patch - patch
eiffel files eiffel - e
manifest files[7]
it is Java manifest file
manifest - mf
css files css - stylesheet
csv files[8]
Uses commas as delimiters
csv - csv
csv2 files[9]
Uses semi-colons as delimiters
csv2 - -
The definition of the syntaxes is defined in the following XML file[10]
See the structure of the syntax file for more information on how this file is specified

Getting a default syntax

The SyntaxMapper.createDefaultTokenMarker() method allows to get a default TokenMarker.

It is possible to customize which TokenMarker will be returned by this method by calling the SyntaxMapper.setDefaultSyntax(String) method.

Getting a syntax

The code managing the syntax highlighting for a syntax is specified by subclasses of the TokenMarker class. The built-in TokenMarkers are defined in the tokenmarkers package.

There are two ways to create a TokenMarker for a syntax:
  • Either create a new one directly using one of the classes in the tokenmarkers package
  • Or using the SyntaxMapper class to create the TokenMarker associated with a syntax name or file extension

Example

To get the TokenMarker for the java syntax, you can either do:
      TokenMarker tk = new JavaTokenMarker();
or:
      SyntaxMapper mapper = SyntaxMapper.getInstance();
      TokenMarker tk = mapper.createTokenMarker("java");
It is also possible to create a TokenMarker associated with a file extension. For example:
      SyntaxMapper mapper = SyntaxMapper.getInstance();
      Syntax syntax = mapper.getSyntaxForExtension("htm");
      TokenMarker tk = syntax.createTokenMarker();

Adding a syntax pack

Main Article: Adding a syntax pack

It is possible to support other syntaxes by calling the SyntaxMapper.installSyntaxPack(URL, boolean) method.

The jEditor-syntaxes.jar jar file, released with the library, provides syntaxes for the following languages:
  • povray
  • Ruby
  • Groovy

Applying a syntax to a Document

The SyntaxTokenizer class allows to apply a syntax to any Document to produce a StyledDocument.

Note that it is possible to add a syntax to a StyledDocument. In that case the font and color of the input Document will be lost, but the following attributes will be kept in the result:
  • Background color of the text
  • Underline
  • Strikethrough
  • Bold
  • Italic
  • Font family

Example

      Document doc = ...
      SyntaxTokenizer tokenizer = new SyntaxTokenizer("java");
      StyledDocument styledDoc = tokenizer.applySyntax(doc);

Iterating on a SyntaxDocument

It is possible to iterate through the tokens in a SyntaxDocument:

Notes

  1. ^ Except for the text files syntax, which has no syntax highlighting
  2. ^ This syntax is for plain text documents. It has no syntax highlighting
  3. ^ They are Windows bat files
  4. ^ JSON files are valid JavaScript files, so the syntax for these files is the same
  5. ^ Groovy files are presented by default with the same syntax as Java files, though the syntax can be overriden by syntax packs
  6. ^ they are Unix or Linux shell scripts
  7. ^ it is Java manifest file
  8. ^ Uses commas as delimiters
  9. ^ Uses semi-colons as delimiters
  10. ^ See the structure of the syntax file for more information on how this file is specified

Categories: syntax

Copyright 2016-2019 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence