Skip to content

Commit b9e993f

Browse files
committed
Update the code description document
1 parent b9cfb75 commit b9e993f

File tree

1 file changed

+13
-94
lines changed

1 file changed

+13
-94
lines changed

code-description.md

Lines changed: 13 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ A tag set:
4141

4242
A tag span consists of a tag plus the text to which that tag applies. In an un-nested tag set, with the exception of an `endif` tag, a tag span begins with the `{` of the tag and ends with the `{` of the next tag. The `endif` tag has no related text, so it ends with the `}` of the tag itself. For example:
4343

44-
```
44+
```markdown
4545
This text does not belong to a tag span, {% ifversion some-version-name %}this is the ifversion tag span,
4646
{% elsif alternative-version %}this is the tag span for an elsif clause, {% else %}this is the tag span for
4747
an else clause, {% endif %}and this does not belong to a tag span.
@@ -55,7 +55,7 @@ If you put a tag set within a tag span of another tag set, then the inner tag se
5555

5656
Here's an example of nested versioning:
5757

58-
```
58+
```markdown
5959
{% ifversion baselevel %}
6060

6161
This text is versioned for "baselevel".
@@ -84,7 +84,7 @@ There are 2 main phases:
8484

8585
First we find the cursor position within the Markdown file and assign this to the constant `cursorPosition`:
8686

87-
```
87+
```typescript
8888
const cursorPosition = activeEditor.selection.active;
8989
```
9090

@@ -123,7 +123,7 @@ When we finish parsing the file, we can work backwards through the `currentTagSp
123123

124124
For example, let's say we have the following at the beginning of a Markdown file:
125125

126-
```
126+
```markdown
127127
This text is unversioned, {% ifversion ghes %}this is versioned for ghes{% endif %} and this is unversioned.
128128
My favorite version is {% ifversion ghec %}GHEC{% elsif fpt %}Free/Pro/Team{% else %}NOT GHES and NOT
129129
Free/Pro/Team{% endif %}.
@@ -200,7 +200,7 @@ Highlighting is done in the `highlightVersionTags()` function.
200200

201201
First we create and populate the `colorPairs` array which contains the colors to be used to highlight the tag sets. We use an array because we're going to highlight each set of tags, at different nesting levels, in a different color. We get the contents of this array from the `settings.json` file. The array values are written to the `settings.json` file, if they don't already exist, when the extension is first installed. The user can then edit the values in the `settings.json` file to change the colors used for highlighting. This is achieved by specifying the array in the extension's `package.json` file, as follows:
202202

203-
```
203+
```typescript
204204
"configuration": {
205205
"type": "object",
206206
"title": "Versioning Extension Configuration",
@@ -260,8 +260,6 @@ Finally, within the nesting level loop, we use `activeEditor.setDecorations` to
260260

261261
Then, if there's version nesting, we iterate through the loop again, applying another color to the tags in the parent tag set.
262262

263-
The .......... TODO: MENTION PRESSING ESC OR CHANGING THE CURSOR POSITION TO TRIGGER DISPOSING OF THE DECORATION TYPES. LINK TO REF FOR PACKAGE.JSON BELOW.
264-
265263
For more information about applying decorations to text in VS Code, see https://github.com/microsoft/vscode-extension-samples/blob/main/decorator-sample/USAGE.md.
266264

267265
#### Removing the highlighting
@@ -270,7 +268,7 @@ When the user presses the Escape key, or moves the cursor, we want to remove the
270268

271269
The code to do this is in the extension's `activate` function, near the top of the TypeScript file:
272270

273-
```
271+
```typescript
274272
// Register a command to remove the decorations.
275273
// The command is defined in package.json and is bound to the escape key
276274
let removeDecorationsDisposable = vscode.commands.registerCommand(
@@ -292,7 +290,7 @@ Any time the Escape key is pressed, or the cursor is moved, we iterate through t
292290

293291
We link the Escape keypress to the `version-identifier.removeDecorations` command in the `keybinding` section of the extension's `package.json` file:
294292

295-
```
293+
```json
296294
"keybindings": [
297295
{
298296
"command": "version-identifier.runExtensionToast",
@@ -322,7 +320,7 @@ The extension allows you to display the versioning message either as a "toast" p
322320

323321
The message is built up from the `versionDescription` array. Included in the message are details of the cursor position, which we derive from the `cursorPosition` constant. And we create the variable `positionString` to hold the part of the message that describes the cursor position:
324322

325-
```
323+
```typescript
326324
const lineNum = cursorPosition.line + 1;
327325
const charNum = cursorPosition.character + 1;
328326
let positionString = `at the cursor position (line ${lineNum}, character ${charNum})`;
@@ -336,7 +334,7 @@ If the `versionDescription` array is empty then there's no versioning at the cur
336334

337335
If `versionDescription` array is not empty we iterate through the array building up the message in the string variable `versioningString`. Typically versioning is un-nested so there's only one element and the message is simple. The following untypical example contains two levels of nesting. Let's say the cursor is within the text "CodingStars" in the twice-nested `ifversion` tag span:
338336

339-
```
337+
```markdown
340338
{% ifversion some-feature-based-versioning %}
341339
... some text here ...
342340

@@ -366,7 +364,7 @@ Let's take another example, this time with just one level of versioning but wher
366364

367365
In the following Markdown, let's say the cursor is within the text "GitHub Code Scanning" in the `else` tag span:
368366

369-
```
367+
```markdown
370368
{% ifversion ghec or ghes > 3.8 %}
371369

372370
... within the product called {%ifversion ghes = 3.9 %}CodingStars{% elsif ghes = 3.10 %}LGTM{% else %}GitHub Code
@@ -431,7 +429,7 @@ We do the following for every tag we encounter during parsing.
431429
- Increment `tagCounter`, to use as a unique ID for this tag. This variable needs to survive from one tag processing to the next.
432430
- Get the start and end positions of the tag (i.e. the position of `{` and `}`) and assign them to `positionVersionTagStart` and `positionVersionTagEnd`. We do this using the `match` array that contains the tag text (e.g. `{% ifversion ghes %}`) that we found using the regular expression. We do this as follows:
433431

434-
```
432+
```typescript
435433
// match.index is the number of the first character of the match
436434
// within the entire searched string (i.e. the entire Markdown file)
437435
const openingBracketPos = match.index;
@@ -495,7 +493,7 @@ Note: the cursor can never be within an `endif` tag span, because `endif` tags h
495493

496494
We use the following regular expression to find version tags in the Markdown file:
497495

498-
```
496+
```typescript
499497
const tagRegEx = /\{%-?\s*(ifversion|elsif|else|endif)\s+([^%]*)%\}/g;
500498
```
501499

@@ -525,7 +523,7 @@ This regular expression has two capture groups:
525523

526524
We use the regular expression like this:
527525

528-
```
526+
```typescript
529527
let match: RegExpExecArray | null;
530528
while (match = tagRegEx.exec(text)) {
531529
...
@@ -537,82 +535,3 @@ The `while` loop will keep running until the regular expression fails to match a
537535
### The package.json file
538536

539537
This file is located ....... it's used to ...........
540-
541-
542-
543-
==================
544-
545-
================
546-
547-
TODO: FIGURE OUT HOW TO ALLOW USERS TO HAVE A CUSTOM CONFIG FOR:
548-
549-
- The color of the highlight for each nesting level.
550-
- The key bindings for the extension.
551-
552-
TODO: IF I CHANGE THIS, ADD MORE DOCUMENTATION HERE ABOUT THE PACKAGE.JSON FILE.
553-
554-
555-
556-
-------
557-
From Copilot:
558-
559-
You can allow users to set their own custom values for the backgroundColor and color properties by using the vscode.workspace.getConfiguration method to read configuration settings from the user's settings file.
560-
561-
First, you need to define these settings in the contributes.configuration section of your extension's package.json file:
562-
563-
```
564-
"contributes": {
565-
"configuration": {
566-
"title": "My Extension",
567-
"properties": {
568-
"myExtension.decoration.backgroundColor": {
569-
"type": "string",
570-
"default": "red",
571-
"description": "Background color for decoration."
572-
},
573-
"myExtension.decoration.color": {
574-
"type": "string",
575-
"default": "white",
576-
"description": "Text color for decoration."
577-
}
578-
}
579-
}
580-
}
581-
```
582-
583-
Then, in your extension.ts file, you can read these settings and use them when creating the decoration:
584-
585-
```
586-
let config = vscode.workspace.getConfiguration('myExtension.decoration');
587-
let backgroundColor = config.get('backgroundColor', 'red');
588-
let color = config.get('color', 'white');
589-
590-
let decoration = vscode.window.createTextEditorDecorationType({
591-
backgroundColor: backgroundColor,
592-
color: color
593-
});
594-
```
595-
596-
In this code, vscode.workspace.getConfiguration('myExtension.decoration') gets the configuration for your extension. The get method reads a setting, and the second parameter to get is the default value to use if the setting is not found.
597-
598-
Users can configure custom values for decoration.backgroundColor and decoration.color in their user or workspace settings in Visual Studio Code.
599-
600-
Here are the steps:
601-
602-
1. Open the settings: Use the shortcut Ctrl+, on Windows/Linux or Cmd+, on macOS, or go to File > Preferences > Settings.
603-
604-
2. In the search bar at the top, type myExtension.decoration to find the settings for your extension.
605-
606-
3. You should see the settings myExtension.decoration.backgroundColor and myExtension.decoration.color. Click on Edit in settings.json which appears when you hover over the setting.
607-
608-
4. In the settings.json file, you can set the values like this:
609-
610-
```
611-
{
612-
"myExtension.decoration.backgroundColor": "#ff0000",
613-
"myExtension.decoration.color": "#ffffff"
614-
}
615-
```
616-
617-
Save the settings.json file. The new settings will take effect immediately.
618-
The values should be valid CSS color values. They can be keywords (like "red" or "white"), hexadecimal color codes (like "#ff0000" or "#ffffff"), RGB values (like "rgb(255, 0, 0)" or "rgb(255, 255, 255)"), or any other valid CSS color value.

0 commit comments

Comments
 (0)