diff --git a/package.json b/package.json index dc3e1e2..c7c59dc 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,11 @@ "default": true, "description": "Specifies whether or not to show tour markers in the editor gutter." }, + "codetour.adjustOnInsertCodeSnippet": { + "type": "boolean", + "default": true, + "description": "Specifies whether or not to adjust tour step line numbers when a snippet is inserted." + }, "codetour.customTourDirectory": { "type": "string", "default": null, diff --git a/src/player/commands.ts b/src/player/commands.ts index 6bf7bbb..7c3233f 100644 --- a/src/player/commands.ts +++ b/src/player/commands.ts @@ -19,6 +19,10 @@ import { progress } from "../store/storage"; import { readUriContents } from "../utils"; import { CodeTourNode } from "./tree/nodes"; +const adjustOnInsertCodeSnippet = vscode.workspace + .getConfiguration("codetour") + .get("adjustOnInsertCodeSnippet", true); + let terminal: vscode.Terminal | null; export function registerPlayerCommands() { // This is a "private" command that's used exclusively @@ -112,6 +116,12 @@ export function registerPlayerCommands() { const codeSnippet = decodeURIComponent(codeBlock); const step = store.activeTour!.tour.steps[store.activeTour!.step]; + const docText = vscode.window.activeTextEditor?.document.getText(); + + if (docText?.includes(codeSnippet)) { + return; // early out snippet has already been inserted + } + if (step.selection) { await vscode.window.activeTextEditor?.edit(e => { const selection = new vscode.Selection( @@ -130,11 +140,13 @@ export function registerPlayerCommands() { } const lineAdjustment = codeSnippet.split("\n").length - 1; - if (lineAdjustment > 0) { - store.activeTour!.tour.steps[ - store.activeTour!.step - ].line! += lineAdjustment; - + if (adjustOnInsertCodeSnippet && lineAdjustment > 0) { + const step = store.activeTour!.tour.steps[store.activeTour!.step]; + step.line! += lineAdjustment; + if (step.selection) { + step.selection.start.line += lineAdjustment; + step.selection.end.line += lineAdjustment; + } saveTour(store.activeTour!.tour); }