added new function

added fetchNumberOfSelectedLines and getNumberOfSelectedLines
This commit is contained in:
tel0065 2022-04-27 12:00:59 -05:00 committed by GitHub
parent 16869aa5b6
commit 5d3dc7acea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,10 @@ export const textCommands: CommandEntry[] = [
name: 'mind-reader.getLeadingSpaces',
callback: getLeadingSpaces,
},
{
name: 'mind-reader.getNumberOfSelectedLines',
callback: getNumberOfSelectedLines,
},
{
name: 'mind-reader.runLineContext',
callback: runLineContext,
@ -26,6 +30,19 @@ export const textCommands: CommandEntry[] = [
}
];
/* Helper Function
* This function returns the number of selected lines in the active text editor window
*/
function fetchNumberOfSelectedLines(editor: vscode.TextEditor | undefined): number {
let numberOfSelectedLines: number = 0;
if (editor) {
numberOfSelectedLines = editor.selections.reduce((prev, curr) => prev + (curr.end.line - curr.start.line), 1);
}
return numberOfSelectedLines;
}
/* Helper Function
* This function returns the line number of the active text editor window
*/
@ -40,7 +57,28 @@ function fetchTextLine(editor: any): string {
return editor.document.lineAt(fetchLineNumber(editor) - 1);
}
// Function that outputs the current line number the cursor is on
/** Function
* Function to return the number of selected (highlighted) lines
* Changes output to 'Line' for 1 line and 'Lines' for all other instances
*/
function getNumberOfSelectedLines(): void {
const editor: any = vscode.window.activeTextEditor;
if (editor) {
const numberOfSelectedLines: number = fetchNumberOfSelectedLines(editor);
(numberOfSelectedLines !== 1)
? vscode.window.showInformationMessage(`${numberOfSelectedLines.toString()} Lines Selected`)
: vscode.window.showInformationMessage(`${numberOfSelectedLines.toString()} Line Selected`);
}
else {
vscode.window.showErrorMessage('No document currently active');
}
}
/** Function
* Outputs the current line number the cursor is on
*/
function getLineNumber(): void {
const editor: any = vscode.window.activeTextEditor;
@ -54,6 +92,9 @@ function getLineNumber(): void {
}
}
/** Function
* Used to get the number of indents on a line
*/
function getIndent(): void {
const editor: any = vscode.window.activeTextEditor;
@ -72,7 +113,9 @@ function getIndent(): void {
};
const i: number = pl.Lexer.getIndent(textLine.text, tabFmt);
vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${i.toString()} indents`);
(i !== 1)
? vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${i.toString()} indents`)
: vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${i.toString()} indent`);
}
}
else {
@ -114,14 +157,14 @@ function getLeadingSpaces(): void {
* default: false
*/
const calculateLeadingSpaces: boolean = false; // change boolean value to change method
const numSpaces: number = (calculateLeadingSpaces) ?
pl.Lexer.getLeadingSpacesByArithmetic(textLine) :
pl.Lexer.getLeadingSpacesByIndex(textLine);
const numSpaces: number = (calculateLeadingSpaces)
? pl.Lexer.getLeadingSpacesByArithmetic(textLine)
: pl.Lexer.getLeadingSpacesByIndex(textLine);
/* Ternary operator to change the tense of 'space' to 'spaces' for the output if numSpaces is 0 or greater than 1 */
(numSpaces !== 1) ?
vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${numSpaces.toString()} spaces`):
vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${numSpaces.toString()} space`);
(numSpaces !== 1)
? vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${numSpaces.toString()} spaces`)
: vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${numSpaces.toString()} space`);
}
}
else {