From cfcbfd43fc29d5ce5ee359e93fd6df698f268872 Mon Sep 17 00:00:00 2001 From: tel0065 <77864718+tel0065@users.noreply.github.com> Date: Wed, 23 Mar 2022 18:20:44 -0500 Subject: [PATCH] added fetchLineNumber(editor) and getLineNumber() added helper function fetchLineNumber(editor) to return the line number that the cursor is on and refactored the code to make use of this helper function in various places. Also added function getLineNumber() which will output the current line number the cursor is on. --- src/commands/text.ts | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/commands/text.ts b/src/commands/text.ts index 511220c..99eb15a 100755 --- a/src/commands/text.ts +++ b/src/commands/text.ts @@ -4,6 +4,10 @@ exports.textCommands = void 0; const vscode = require("vscode"); const pl = require("../pylex"); exports.textCommands = [ + { + name: 'mind-reader.getLineNumber', + callback: getLineNumber, + }, { name: 'mind-reader.getIndent', callback: getIndent, @@ -21,10 +25,30 @@ exports.textCommands = [ callback: runCursorContext } ]; +/* Helper Function + * This function returns the line number of the active text editor window + */ +function fetchLineNumber(editor) { + return editor.selection.active.line + 1; +} + +// Function that outputs the current line number the cursor is on +function getLineNumber() { + let editor = vscode.window.activeTextEditor; + + if (editor) { + let lineNum = fetchLineNumber(editor); + vscode.window.showInformationMessage("Line " + lineNum.toString()); + } + else { + vscode.window.showErrorMessage('No document currently active'); + } +} + function getIndent() { let editor = vscode.window.activeTextEditor; if (editor) { - let lineNum = editor.selection.active.line + 1; + let lineNum = fetchLineNumber(editor); let textLine = editor.document.lineAt(lineNum - 1); if (textLine.isEmptyOrWhitespace) { vscode.window.showInformationMessage("Line number " + lineNum.toString() + " Is Empty"); @@ -43,18 +67,22 @@ function getIndent() { vscode.window.showErrorMessage('No document currently active'); } } +/* Function -> Returns the number of leading spaces on the line the cursor is on + * This is done by returning the index of the first non whitespace character + * Since this index is a 0-index, the returned number will correspond to the number of leading spaces + */ function getLeadingSpaces() { let editor = vscode.window.activeTextEditor; if (editor) { - let lineNum = editor.selection.active.line + 1; + let lineNum = fetchLineNumber(editor); let textLine = editor.document.lineAt(lineNum - 1); if(textLine.isEmptyOrWhitespace) { vscode.window.showInformationMessage("Line number " + lineNum.toString() + " Is Empty"); } else { let numSpaces = textLine.firstNonWhitespaceCharacterIndex; - vscode.window.showInformationMessage("Line Number " + lineNum.toString() + ": " + numSpaces.toString() + " Leading Spaces "); + vscode.window.showInformationMessage("Line Number " + lineNum.toString() + " Has " + numSpaces.toString()+ " Leading Spaces "); } } else {