From 96811dd5ed5c432e08be26dd08fd3294a1b88cbe Mon Sep 17 00:00:00 2001 From: Bobby Johnson Date: Thu, 2 Sep 2021 13:20:13 -0700 Subject: [PATCH 1/3] added configuration to disable line adjustments and properly updated selection --- package.json | 5 +++++ src/player/commands.ts | 16 +++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) 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..9ec4da2 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 @@ -130,11 +134,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); } From 897fc48fa195f3caa48de26b21e8c307fc1c8fa7 Mon Sep 17 00:00:00 2001 From: Bobby Johnson Date: Thu, 2 Sep 2021 13:20:58 -0700 Subject: [PATCH 2/3] added check to see if the codeblock has been inserted previously --- src/player/commands.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/player/commands.ts b/src/player/commands.ts index 9ec4da2..bd894fb 100644 --- a/src/player/commands.ts +++ b/src/player/commands.ts @@ -116,6 +116,12 @@ export function registerPlayerCommands() { const codeSnippet = decodeURIComponent(codeBlock); const step = store.activeTour!.tour.steps[store.activeTour!.step]; + const line = vscode.window.activeTextEditor?.document.lineAt(step.line!); + + if (codeBlock.includes(line?.text!)) { + return; // early out block has already been inserted + } + if (step.selection) { await vscode.window.activeTextEditor?.edit(e => { const selection = new vscode.Selection( From 59a001a6e8e4c322101aab5cb8654150f37e2ed3 Mon Sep 17 00:00:00 2001 From: Bobby Johnson Date: Tue, 14 Sep 2021 08:30:38 -0700 Subject: [PATCH 3/3] updated duplicate insertion logic --- src/player/commands.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/player/commands.ts b/src/player/commands.ts index bd894fb..7c3233f 100644 --- a/src/player/commands.ts +++ b/src/player/commands.ts @@ -116,10 +116,10 @@ export function registerPlayerCommands() { const codeSnippet = decodeURIComponent(codeBlock); const step = store.activeTour!.tour.steps[store.activeTour!.step]; - const line = vscode.window.activeTextEditor?.document.lineAt(step.line!); + const docText = vscode.window.activeTextEditor?.document.getText(); - if (codeBlock.includes(line?.text!)) { - return; // early out block has already been inserted + if (docText?.includes(codeSnippet)) { + return; // early out snippet has already been inserted } if (step.selection) {