added function, fixed bug

Added function for selectLeadingSpaces, fixed bug which was reporting the wrong line number on some functions, added window focusing to most of the functions
This commit is contained in:
tel0065 2022-05-04 19:27:17 -05:00 committed by GitHub
parent c17b718ce6
commit cc1c23a16f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -64,8 +64,7 @@ function fetchNumberOfLeadingSpaces(editor: TextEditor | undefined): number {
* default: false * default: false
*/ */
const calculateLeadingSpaces: boolean = false; // change boolean value to change method const calculateLeadingSpaces: boolean = false; // change boolean value to change method
const lineNum: number = (fetchLineNumber(editor) - 1); // We want the line index, so we remove the 1 we added to the result in fetchLineNumber const line : any = fetchLine(editor);
const line : any = editor.document.lineAt(lineNum);
/* If true, calculate by arithmetic otherwise get index */ /* If true, calculate by arithmetic otherwise get index */
numSpaces = (calculateLeadingSpaces) numSpaces = (calculateLeadingSpaces)
@ -126,6 +125,8 @@ function getNumberOfSelectedLines(): void {
else { else {
window.showErrorMessage('No document currently active'); window.showErrorMessage('No document currently active');
} }
window.showTextDocument(editor.document); // After the selection is made, the editor loses focus. We need to re-focus the editor so typing isn't interrupted
} }
/* Function /* Function
@ -142,6 +143,8 @@ function getLineNumber(): void {
else { else {
window.showErrorMessage('No document currently active'); window.showErrorMessage('No document currently active');
} }
window.showTextDocument(editor.document); // After the selection is made, the editor loses focus. We need to re-focus the editor so typing isn't interrupted
} }
/* Function /* Function
@ -151,14 +154,14 @@ function getIndent(): void {
const editor: any = window.activeTextEditor; const editor: any = window.activeTextEditor;
if (editor) { if (editor) {
const lineNum: number = (fetchLineNumber(editor) - 1); // We want the line index, so we remove the 1 we added to the result in fetchLineNumber const lineNum: number = (fetchLineNumber(editor));
const line : any = editor.document.lineAt(lineNum); const line : any = fetchLine(editor);
if (line.isEmptyOrWhitespace) { if (line.isEmptyOrWhitespace) {
window.showInformationMessage(`Line ${lineNum.toString()} is Empty`); window.showInformationMessage(`Line ${lineNum.toString()} is Empty`);
} }
else { else {
// Grab tab format from open document /* Grab tab format from open document */
const tabFmt: any = { const tabFmt: any = {
size: editor.options.tabSize, size: editor.options.tabSize,
hard: !editor.options.insertSpaces hard: !editor.options.insertSpaces
@ -173,6 +176,8 @@ function getIndent(): void {
else { else {
window.showErrorMessage('No document currently active'); window.showErrorMessage('No document currently active');
} }
window.showTextDocument(editor.document); // After the selection is made, the editor loses focus. We need to re-focus the editor so typing isn't interrupted
} }
/* Function /* Function
@ -200,6 +205,8 @@ function getLeadingSpaces(): void {
else { else {
window.showErrorMessage('No document currently active'); window.showErrorMessage('No document currently active');
} }
window.showTextDocument(editor.document); // After the selection is made, the editor loses focus. We need to re-focus the editor so typing isn't interrupted
} }
/* Function /* Function
@ -207,22 +214,21 @@ function getLeadingSpaces(): void {
* This feature was a request from Senior Design Day Spring 2022 * This feature was a request from Senior Design Day Spring 2022
*/ */
function selectLeadingWhitespace(): void { function selectLeadingWhitespace(): void {
const editor : any = window.activeTextEditor; const editor : any = window.activeTextEditor;
if (editor) { if (editor) {
const numSpaces = fetchNumberOfLeadingSpaces(editor); // This will be used for the output message const numSpaces = fetchNumberOfLeadingSpaces(editor); // This will be used for the output message
const lineNum : number = (fetchLineNumber(editor) - 1); // We want the line index, so we remove the 1 we added to the result in fetchLineNumber const lineNum : number = (fetchLineNumber(editor)); // Get the displayed line number
/* If numSpaces isn't greater than 1, then there is no leading whitespace to select */ /* If numSpaces isn't greater than 1, then there is no leading whitespace to select */
if (numSpaces >= 1) { if (numSpaces >= 1) {
const line : any = editor.document.lineAt(lineNum); // Get our line const line : any = fetchLine(editor);
const startPos: any = line.range.start.character; // Start at the starting character position const startPos: any = line.range.start.character; // Start at the starting character position
const endPos : any = line.firstNonWhitespaceCharacterIndex; // End at the first non whitespace character index const endPos : any = line.firstNonWhitespaceCharacterIndex; // End at the first non whitespace character index
/* Apply our selection */ /* Apply our selection */
editor.selection = new Selection(new Position(lineNum, startPos), new Position(lineNum, endPos)); /* We need to subtract 1 from lineNum because we added 1 during the fetchLineNumber above and we want the 0-index for position, so remove it */
/* After the selection is made, the editor loses focus. We need to re-focus the editor so typing isn't interrupted */ editor.selection = new Selection(new Position((lineNum - 1), startPos), new Position((lineNum - 1), endPos));
window.showTextDocument(editor.document);
/* Ternary operator to change the tense of 'space' to 'spaces' for the output if numSpaces is 0 or greater than 1 */ /* Ternary operator to change the tense of 'space' to 'spaces' for the output if numSpaces is 0 or greater than 1 */
@ -232,12 +238,13 @@ function selectLeadingWhitespace(): void {
} }
else { else {
window.showErrorMessage(`Line ${lineNum.toString()}: No leading spaces to select!`); // No whitespace to select window.showErrorMessage(`Line ${lineNum.toString()}: No leading spaces to select!`); // No whitespace to select
window.showTextDocument(editor.document); // Refocus editor
} }
} }
else { else {
window.showErrorMessage('No document currently active'); // No active document window.showErrorMessage('No document currently active'); // No active document
} }
window.showTextDocument(editor.document); // After the selection is made, the editor loses focus. We need to re-focus the editor so typing isn't interrupted
} }
function runLineContext(): void { function runLineContext(): void {