Define types where previously any

This commit is contained in:
John 2022-04-07 13:52:08 -05:00
parent d19ad8f885
commit cf0900968f

View File

@ -3,6 +3,8 @@ import { CommandEntry } from './commandEntry';
import vscode = require("vscode"); import vscode = require("vscode");
import pl = require("../pylex"); import pl = require("../pylex");
type TextEditor = vscode.TextEditor | undefined;
export const textCommands: CommandEntry[] = [ export const textCommands: CommandEntry[] = [
{ {
name: 'mind-reader.getLineNumber', name: 'mind-reader.getLineNumber',
@ -29,20 +31,20 @@ export const textCommands: CommandEntry[] = [
/* Helper Function /* Helper Function
* This function returns the line number of the active text editor window * This function returns the line number of the active text editor window
*/ */
function fetchLineNumber(editor: any): number { function fetchLineNumber(editor: TextEditor): number {
return editor.selection.active.line + 1; return editor? editor.selection.active.line + 1: -1;
} }
/* Helper Function /* Helper Function
* This function returns the text from the current line of the active text editor window * This function returns the text from the current line of the active text editor window
*/ */
function fetchTextLine(editor: any): string { function fetchTextLine(editor: TextEditor): vscode.TextLine|undefined {
return editor.document.lineAt(fetchLineNumber(editor) - 1); return editor? editor.document.lineAt(fetchLineNumber(editor) - 1): undefined;
} }
// Function that outputs the current line number the cursor is on // Function that outputs the current line number the cursor is on
function getLineNumber(): void { function getLineNumber(): void {
const editor: any = vscode.window.activeTextEditor; const editor: TextEditor = vscode.window.activeTextEditor;
if (editor) { if (editor) {
const lineNum: number = fetchLineNumber(editor); const lineNum: number = fetchLineNumber(editor);
@ -55,19 +57,19 @@ function getLineNumber(): void {
} }
function getIndent(): void { function getIndent(): void {
const editor: any = vscode.window.activeTextEditor; const editor: TextEditor = vscode.window.activeTextEditor;
if (editor) { if (editor) {
const lineNum: number = fetchLineNumber(editor); const lineNum: number = fetchLineNumber(editor);
const textLine: any = editor.document.lineAt(lineNum - 1); const textLine: vscode.TextLine = editor.document.lineAt(lineNum - 1);
if (textLine.isEmptyOrWhitespace) { if (textLine.isEmptyOrWhitespace) {
vscode.window.showInformationMessage(`Line ${lineNum.toString()} is Empty`); vscode.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: pl.TabInfo = {
size: editor.options.tabSize, size: typeof editor.options.tabSize === 'number'? editor.options.tabSize: 4,
hard: !editor.options.insertSpaces hard: !editor.options.insertSpaces
}; };
const i: number = pl.Lexer.getIndent(textLine.text, tabFmt); const i: number = pl.Lexer.getIndent(textLine.text, tabFmt);
@ -101,9 +103,9 @@ function getLeadingSpaces(): void {
if (editor) { if (editor) {
const lineNum: number = fetchLineNumber(editor); const lineNum: number = fetchLineNumber(editor);
const textLine: any = fetchTextLine(editor); const textLine: vscode.TextLine|undefined = fetchTextLine(editor);
// If there's no line, or the line is empty, say the line is empty
if (textLine.isEmptyOrWhitespace) { if (!textLine || textLine.isEmptyOrWhitespace) {
vscode.window.showInformationMessage(`Line ${lineNum.toString()} is empty`); vscode.window.showInformationMessage(`Line ${lineNum.toString()} is empty`);
} }
else { else {
@ -130,23 +132,23 @@ function getLeadingSpaces(): void {
} }
function runLineContext(): void { function runLineContext(): void {
const editor: any = vscode.window.activeTextEditor; const editor: TextEditor = vscode.window.activeTextEditor;
if (editor) { if (editor) {
// current text and line number // current text and line number
const editorText: string = editor.document.getText(); const editorText: string = editor.document.getText();
const line: string = editor.selection.active.line; const line: number = editor.selection.active.line;
// get tab info settings // get tab info settings
const size: number = parseInt(editor.options.tabSize); const size: number = typeof editor.options.tabSize === 'number'? editor.options.tabSize: 4;
const hard: boolean = !editor.options.insertSpaces; const hard: boolean = !editor.options.insertSpaces;
// initialize parser // initialize parser
const parser: any = new pl.Parser(editorText, { const parser: pl.Parser = new pl.Parser(editorText, {
size, size,
hard hard
}); });
parser.parse(); parser.parse();
const context: string = parser.context(line); const context: pl.LexNode[] = parser.context(line);
// build text // build text
const contentString: string = createContextString(context, line); const contentString: string = createContextString(context, line);
@ -157,7 +159,7 @@ function runLineContext(): void {
} }
} }
function createContextString(context: any, line: string): string { function createContextString(context: pl.LexNode[], line: number): string {
if (context.length < 1) { if (context.length < 1) {
throw new Error('Cannot create context string for empty context'); throw new Error('Cannot create context string for empty context');
} }
@ -169,7 +171,7 @@ function createContextString(context: any, line: string): string {
} }
for (let i: number = 1; i < context.length; i++) { for (let i: number = 1; i < context.length; i++) {
const node: any = context[i]; const node: pl.LexNode = context[i];
if (node.label === 'root') { if (node.label === 'root') {
// root // root
@ -177,7 +179,7 @@ function createContextString(context: any, line: string): string {
continue; continue;
} }
if (node.token.type !== pl.PylexSymbol.EMPTY && if (node.token && node.token.type !== pl.PylexSymbol.EMPTY &&
node.token.type !== pl.PylexSymbol.INDENT) { node.token.type !== pl.PylexSymbol.INDENT) {
contextString += ' inside ' + node.token.type.toString(); contextString += ' inside ' + node.token.type.toString();
if (node.token.attr) { if (node.token.attr) {
@ -192,14 +194,14 @@ function createContextString(context: any, line: string): string {
// find up to `n` words around the cursor, where `n` is // find up to `n` words around the cursor, where `n` is
// the value of `#mindReader.reader.contextWindow` // the value of `#mindReader.reader.contextWindow`
function runCursorContext(): void { function runCursorContext(): void {
const editor: any = vscode.window.activeTextEditor; const editor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
if (!editor) { if (!editor) {
vscode.window.showErrorMessage('RunCursorContext: No Active Editor'); vscode.window.showErrorMessage('RunCursorContext: No Active Editor');
return; return;
} }
const cursorPos: any = editor.selection.active; const cursorPos: vscode.Position = editor.selection.active;
const text: string = editor.document.lineAt(cursorPos).text; const text: string = editor.document.lineAt(cursorPos).text;
const windowSize: any = vscode.workspace.getConfiguration('mindReader').get('reader.contextWindow'); const windowSize: any = vscode.workspace.getConfiguration('mindReader').get('reader.contextWindow');
let trimmedText: string = text.trimStart(); // trim leading whitespace let trimmedText: string = text.trimStart(); // trim leading whitespace
@ -252,7 +254,7 @@ function runCursorContext(): void {
// construct cursor context string // construct cursor context string
let contextString: string = ''; let contextString: string = '';
for (let i: any = contextStart; i < contextEnd; i++) { for (let i: number = contextStart; i < contextEnd; i++) {
contextString += spaceWords[i].word + ' '; contextString += spaceWords[i].word + ' ';
} }
// output cursor context string // output cursor context string