Skip to content

ClassificationTypes.json and Codist.ct.json

WMJ edited this page Nov 5, 2024 · 11 revisions

Syntax Highlighting Basics

A classification type is used in Visual Studio to denote a syntax highlight element. Each classification type can be associated with their own highlight styles (foreground color, bold style, etc.).

When syntax highlight begins, _tagger_s will be created to mark segments of the code with various classification types, for instance, the first 5 characters in the code have a classification type named "keyword". So, Visual Studio highlights the first 5 characters with the style corresponding to "keyword".

Therefore, classification types are typically used by code taggers. It is of little use for you to define your own classification types without a tagger that you can control. So, comes the Custom Tagger from Codist 8.0.

The Custom Tagger takes a series of rules and matches them against each line of your code. A rule has a regular expression and corresponding classification types. When a piece of your code matches a regular expression, the corresponding classification type will be used to highlight that piece of code.

You may want to set up your own classification type for specific purpose, not only using those already defined in Visual Studio.

So, the ClassificationTypes.json file lets you define your own classification types. And various of Codist.ct.json files let you define your rules on various code files in different places.

ClassificationTypes.json

ClassificationTypes.json is an optional configuration file, which allows you to add extra classification types to Visual Studio.

To define your own classification types, place a file named ClassificationTypes.json into the configuration folder of Codist. The configuration folder could be opened via the "Open Config Folder" button in the Options page.

The following is a sample of ClassificationTypes.json.

{
	"items": [
		{ "name": "Yellow", "background": "#FFCC00", "foreground": "#0000CC" },
		{ "name": "Quotation", "foreground": "#8EADFF" },
		{ "name": "Large", "fontSize": 25 },
		{ "name": "Small", "fontSize": 8 },
		{ "name": "Bold", "bold": true },
		{ "name": "Italic", "italic": true },
		{ "name": "Underline", "underline": true },
		{ "name": "Bold Italic", "baseOn": "Bold, Italic" },
		{ "name": "Bold Underline", "baseOn": "Bold, Underline" },
		{ "name": "Italic Underline", "baseOn": "Italic, Underline" },
		{ "name": "Error", "foreground": "#F89494" },
		{ "name": "Fatal Error", "foreground": "#330000", "background": "#FFFFCC" },
		{ "name": "Warning", "foreground": "#EF9F00" },
		{ "name": "Information", "foreground": "#009FEF" }
	]
}

After that file being placed into the folder, restart Visual Studio, you will see them listed in the Customize Codist Syntax Highlight window.

image

Codist.ct.json

Codist.ct.json an optional configuration file, which allows you to use regular expressions or simple strings to highlight your code. To make it work, place a Codist.ct.json file into the folder where your code files reside.

The following is a sample of Codist.ct.json.

[
	{
		"file": "\\.md$",
		"items": [
			{ "match": "\\[(.+?)\\]", "tag": "Bold" },
			{ "match": "#([^#]+?)#", "groupTags": [ "Underline" ] }
		]
	}
]

The file field defines a Regular Expression which matches code file names. For instance, the above sample matches file names that end with .md.

items defines a series of items of tagger rules for those files having their file names matches the file pattern.

match defines a Regular Expression which is used to match code text.

When the match expression is matched, the corresponding segment of text will be assigned with a classification type named after the tag field. For instance, a piece of code having "xxx[abc]xxx" will match the first rule in the sample configuration file and "[abc]" will be highlighted with the syntax style of "Bold", the classification type defined in the aforementioned ClassificationTypes.json.

groupTags are used for Regular Expressions that have capture groups. For instance, a piece of code having "xxx#abc#xxx" will match the second rule and the first group "abc" (without the # around them) will be highlighted with the syntax style of "Underline".

The file expression is case-insensitive. If there is no file field in a rule set, the items will be applied to all files.

By default, the match expression is case-sensitive. To make it case-insensitive, add a "ignoreCase": true field to the corresponding item.

If the Codist.ct.json file is already in place, saving the file will apply the changes to opened documents. You need to make some modifications to the document or scroll the code window to refresh the changes.

If the Codist.ct.json file was not in place when you open a code file, the custom syntax highlight will not be initialized for that code file, you need to reopen the code file after saving the Codist.ct.json file.


NOTE: Be careful about character escaping in JSON file. You need to double the \ (that is \\), to get a single \ in the Regular Expression. If you forget this, the configuration file will not be loaded.

The highlighter checks the patterns line by line. Thus multi-line expression is not supported. All Regular Expressions are evaluated to each single line in the code. The end of line characters are stripped. Use $ to denote the end of a line.


Highlight contents in the Output windowpane

To highlight contents generated from the Debug, Build, etc. source, put a file named Codist.ct.json into the folder of Codist (the same folder for ClassificationTypes.json). And use "<output>" for the file pattern.

Rules defined in that file will be applied to the Output window.

An example for the configuration is listed below:

[
	{
		"file": "<output>",
		"items": [
			{ "match": "([=\\-]{5,})", "tag": "Large", "groupTags": ["Quotation"], "tagLine": "true" },
			{ "match": "\\b(?:[Ee]rror|[Ss]tack [Tt]race)\\b|[Ee]xception\\b|(?:\\b[1-9]|\\b\\d+\\d) [Ff]ailed\\b|-- FAILED\\b", "tag": "Error", "tagLine": "true" },
			{ "match": "\\b[Ww]arning\\b", "tag": "Warning", "tagLine": "true" },
			{ "match": "Build: \\d+ succeeded, 0 failed, \\d+ up-to-date, \\d+ skipped", "tag": "Information", "tagLine": "true" },
			{ "match": "\\b\\w[\\w _\\.]* -> (.+)", "tag": "Information", "groupTags": [ "Underline" ] },
			{ "match": "\\b([A-Z]{1,7}\\d{3,4})(?=:)", "tag": "Bold", "groupTags": ["Underline"] }
		]
	}
]

The first rule matches at least five = or - characters, usually as the build title. The second and the third rule matches error and warning information from the Build or Debug window. The fourth matches build succeeded information. The fifth matches C++ and C# project build target files. The sixth matches compiler warnings or errors, such as CS0459, C0120, etc.

Here's an example of the build output.

image