You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: code-description.md
+13-94Lines changed: 13 additions & 94 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,7 @@ A tag set:
41
41
42
42
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:
43
43
44
-
```
44
+
```markdown
45
45
This text does not belong to a tag span, {% ifversion some-version-name %}this is the ifversion tag span,
46
46
{% elsif alternative-version %}this is the tag span for an elsif clause, {% else %}this is the tag span for
47
47
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
55
55
56
56
Here's an example of nested versioning:
57
57
58
-
```
58
+
```markdown
59
59
{% ifversion baselevel %}
60
60
61
61
This text is versioned for "baselevel".
@@ -84,7 +84,7 @@ There are 2 main phases:
84
84
85
85
First we find the cursor position within the Markdown file and assign this to the constant `cursorPosition`:
@@ -123,7 +123,7 @@ When we finish parsing the file, we can work backwards through the `currentTagSp
123
123
124
124
For example, let's say we have the following at the beginning of a Markdown file:
125
125
126
-
```
126
+
```markdown
127
127
This text is unversioned, {% ifversion ghes %}this is versioned for ghes{% endif %} and this is unversioned.
128
128
My favorite version is {% ifversion ghec %}GHEC{% elsif fpt %}Free/Pro/Team{% else %}NOT GHES and NOT
129
129
Free/Pro/Team{% endif %}.
@@ -200,7 +200,7 @@ Highlighting is done in the `highlightVersionTags()` function.
200
200
201
201
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:
202
202
203
-
```
203
+
```typescript
204
204
"configuration": {
205
205
"type": "object",
206
206
"title": "Versioning Extension Configuration",
@@ -260,8 +260,6 @@ Finally, within the nesting level loop, we use `activeEditor.setDecorations` to
260
260
261
261
Then, if there's version nesting, we iterate through the loop again, applying another color to the tags in the parent tag set.
262
262
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
-
265
263
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.
266
264
267
265
#### Removing the highlighting
@@ -270,7 +268,7 @@ When the user presses the Escape key, or moves the cursor, we want to remove the
270
268
271
269
The code to do this is in the extension's `activate` function, near the top of the TypeScript file:
272
270
273
-
```
271
+
```typescript
274
272
// Register a command to remove the decorations.
275
273
// The command is defined in package.json and is bound to the escape key
276
274
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
292
290
293
291
We link the Escape keypress to the `version-identifier.removeDecorations` command in the `keybinding` section of the extension's `package.json` file:
@@ -322,7 +320,7 @@ The extension allows you to display the versioning message either as a "toast" p
322
320
323
321
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:
324
322
325
-
```
323
+
```typescript
326
324
const lineNum =cursorPosition.line+1;
327
325
const charNum =cursorPosition.character+1;
328
326
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
336
334
337
335
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:
338
336
339
-
```
337
+
```markdown
340
338
{% ifversion some-feature-based-versioning %}
341
339
... some text here ...
342
340
@@ -366,7 +364,7 @@ Let's take another example, this time with just one level of versioning but wher
366
364
367
365
In the following Markdown, let's say the cursor is within the text "GitHub Code Scanning" in the `else` tag span:
368
366
369
-
```
367
+
```markdown
370
368
{% ifversion ghec or ghes > 3.8 %}
371
369
372
370
... 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.
431
429
- Increment `tagCounter`, to use as a unique ID for this tag. This variable needs to survive from one tag processing to the next.
432
430
- 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:
433
431
434
-
```
432
+
```typescript
435
433
// match.index is the number of the first character of the match
436
434
// within the entire searched string (i.e. the entire Markdown file)
437
435
const openingBracketPos =match.index;
@@ -495,7 +493,7 @@ Note: the cursor can never be within an `endif` tag span, because `endif` tags h
495
493
496
494
We use the following regular expression to find version tags in the Markdown file:
@@ -525,7 +523,7 @@ This regular expression has two capture groups:
525
523
526
524
We use the regular expression like this:
527
525
528
-
```
526
+
```typescript
529
527
let match:RegExpExecArray|null;
530
528
while (match=tagRegEx.exec(text)) {
531
529
...
@@ -537,82 +535,3 @@ The `while` loop will keep running until the regular expression fails to match a
537
535
### The package.json file
538
536
539
537
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:
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