Skip to content

Commit

Permalink
Merge pull request #52 from estruyf/issue/50
Browse files Browse the repository at this point in the history
Add toggle selection highlight command and corresponding icons #50
  • Loading branch information
estruyf authored Feb 15, 2025
2 parents 7cf7456 + 80ba31f commit 0b10134
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [0.0.71] - 2025-02-xx

- [#50](https://github.com/estruyf/vscode-demo-time/issues/50): Added the `Demo Time: Toggle highlight of current line or selection` command to toggle the highlight of the current line or selection

## [0.0.70] - 2025-02-14

- Fix in `setSetting` action where the setting was not updated in the workspace configuration
Expand Down
35 changes: 23 additions & 12 deletions assets/icons/highlight-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions assets/icons/selection-highlight-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions assets/icons/selection-highlight-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 24 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@
"type": "number",
"default": 3710,
"description": "Port number for the API"
},
"demoTime.hideEditorActions": {
"type": "boolean",
"default": false,
"description": "Hide the editor actions"
}
}
},
Expand Down Expand Up @@ -263,6 +268,15 @@
"category": "Demo Time",
"icon": "$(vm)"
},
{
"command": "demo-time.toggleSelectionHighlight",
"title": "Toggle highlight of current line or selection",
"category": "Demo Time",
"icon": {
"light": "assets/icons/selection-highlight-light.svg",
"dark": "assets/icons/selection-highlight-dark.svg"
}
},
{
"command": "demo-time.toggleHighlight",
"title": "Toggle highlight",
Expand Down Expand Up @@ -417,14 +431,19 @@
"when": "(editorLangId == jsonc || editorLangId == json) && resourceDirname =~ /\\.demo$/",
"group": "navigation@1"
},
{
"command": "demo-time.toggleSelectionHighlight",
"when": "activeEditor == 'workbench.editors.files.textFileEditor' && config.demoTime.hideEditorActions === false",
"group": "navigation@1"
},
{
"command": "demo-time.toggleHighlight",
"when": "activeEditor == 'workbench.editors.files.textFileEditor' && demo-time.hasCodeHighlighting == true",
"group": "navigation@1"
},
{
"command": "demo-time.addToStep",
"when": "activeEditor == 'workbench.editors.files.textFileEditor' && !(resourceDirname =~ /\\.demo$/)",
"when": "activeEditor == 'workbench.editors.files.textFileEditor' && !(resourceDirname =~ /\\.demo$/) && config.demoTime.hideEditorActions === false",
"group": "navigation@1"
}
]
Expand Down Expand Up @@ -455,6 +474,10 @@
"command": "demo-time.previous",
"key": "left",
"when": "demo-time.presentation && demo-time.previousEnabled"
},
{
"command": "demo-time.toggleSelectionHighlight",
"key": "alt+h"
}
]
},
Expand Down
1 change: 1 addition & 0 deletions src/constants/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const COMMAND = {
togglePresentationMode: `${EXTENSION_NAME}.togglePresentationMode`,
reset: `${EXTENSION_NAME}.reset`,
toggleHighlight: `${EXTENSION_NAME}.toggleHighlight`,
toggleSelectionHighlight: `${EXTENSION_NAME}.toggleSelectionHighlight`,
// Creator
initialize: `${EXTENSION_NAME}.initialize`,
openDemoFile: `${EXTENSION_NAME}.openDemoFile`,
Expand Down
59 changes: 56 additions & 3 deletions src/services/DemoRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class DemoRunner {
subscriptions.push(commands.registerCommand(COMMAND.runById, DemoRunner.runById));
subscriptions.push(commands.registerCommand(COMMAND.reset, DemoRunner.reset));
subscriptions.push(commands.registerCommand(COMMAND.toggleHighlight, DemoRunner.toggleHighlight));
subscriptions.push(commands.registerCommand(COMMAND.toggleSelectionHighlight, DemoRunner.toggleSelectionHighlight));

window.onDidChangeActiveTextEditor(async (editor) => {
if (editor && editor.document.fileName === DemoRunner.crntFilePath) {
Expand Down Expand Up @@ -912,6 +913,17 @@ export class DemoRunner {
await DemoRunner.saveFile();
}

/**
* Toggles the highlight decoration in the active text editor.
*
* This method checks if there is an active text editor and a current highlight range.
* If the editor or range is not available, it exits early.
*
* If the text is not currently decorated, it highlights the specified range in the active editor.
* Otherwise, it removes the highlight decoration.
*
* @returns {Promise<void>} A promise that resolves when the toggle operation is complete.
*/
public static async toggleHighlight(): Promise<void> {
const activeEditor = window.activeTextEditor;
const range = DemoRunner.crntHighlightRange;
Expand All @@ -929,6 +941,44 @@ export class DemoRunner {
}
}

/**
* Toggles the highlight of the current text selection in the active editor.
*
* If there is no active editor, the function returns immediately.
* If the selection is a single line, it highlights the selection.
* If the selection spans multiple lines, it highlights the entire range.
* If the selection starts at the beginning of the line and ends at the end of the line,
* it highlights the whole line.
*
* If the text is not currently decorated, it highlights the selection.
* If the text is already decorated, it removes the highlight.
*
* @returns {Promise<void>} A promise that resolves when the operation is complete.
*/
public static async toggleSelectionHighlight(): Promise<void> {
const activeEditor = window.activeTextEditor;
if (!activeEditor) {
return;
}

const selection = activeEditor.selection;
const range = selection.isSingleLine ? selection : new Range(selection.start, selection.end);
const endLineText = activeEditor.document.lineAt(selection.end).text;
let highlightWholeLine = range.start.character === 0 && range.end.character === endLineText.length;
if (range.start.line === range.end.line && range.start.character === range.end.character) {
highlightWholeLine = true;
}

// Remove the text selection
activeEditor.selection = new Selection(range.start, range.start);

if (!DecoratorService.isDecorated()) {
DemoRunner.highlight(activeEditor, range, undefined, undefined, highlightWholeLine, false);
} else {
DemoRunner.unselect(activeEditor);
}
}

/**
* Highlights the specified range or position in the given text editor.
* @param textEditor - The text editor in which to highlight the range or position.
Expand All @@ -940,7 +990,8 @@ export class DemoRunner {
range: Range | undefined,
position: Position | undefined,
zoomLevel?: number,
highlightWholeLine?: boolean
highlightWholeLine?: boolean,
keepInMemory = true
): Promise<void> {
if (!range && !position) {
return;
Expand All @@ -955,8 +1006,10 @@ export class DemoRunner {
}

if (range) {
DemoRunner.setCrntHighlighting(textEditor.document.fileName, range, zoomLevel, highlightWholeLine);
await setContext(ContextKeys.hasCodeHighlighting, true);
if (keepInMemory) {
DemoRunner.setCrntHighlighting(textEditor.document.fileName, range, zoomLevel, highlightWholeLine);
await setContext(ContextKeys.hasCodeHighlighting, true);
}

DecoratorService.hightlightLines(textEditor, range, zoomLevel, highlightWholeLine);
textEditor.revealRange(range, TextEditorRevealType.InCenter);
Expand Down

0 comments on commit 0b10134

Please sign in to comment.