mirror of
https://github.com/We-Dont-Byte/Mind_Reader.git
synced 2024-11-15 03:35:59 +00:00
text.ts -> type error fixes, lets -> const, import
fixed type errors, changed most let variables to const, changed const require to imports
This commit is contained in:
parent
3fed2aa4ad
commit
50ff693d16
@ -1,8 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
import { CommandEntry } from './commandEntry';
|
import { CommandEntry } from './commandEntry';
|
||||||
|
import vscode = require("vscode");
|
||||||
const vscode = require("vscode");
|
import pl = require("../pylex");
|
||||||
const pl = require("../pylex");
|
|
||||||
|
|
||||||
export const textCommands: CommandEntry[] = [
|
export const textCommands: CommandEntry[] = [
|
||||||
{
|
{
|
||||||
@ -43,10 +42,10 @@ function fetchTextLine(editor: any): string {
|
|||||||
|
|
||||||
// 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 {
|
||||||
let editor: any = vscode.window.activeTextEditor;
|
const editor: any = vscode.window.activeTextEditor;
|
||||||
|
|
||||||
if (editor) {
|
if (editor) {
|
||||||
let lineNum: number = fetchLineNumber(editor);
|
const lineNum: number = fetchLineNumber(editor);
|
||||||
|
|
||||||
vscode.window.showInformationMessage(`Line ${lineNum.toString()}`);
|
vscode.window.showInformationMessage(`Line ${lineNum.toString()}`);
|
||||||
}
|
}
|
||||||
@ -56,7 +55,7 @@ function getLineNumber(): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getIndent(): void {
|
function getIndent(): void {
|
||||||
let editor: any = vscode.window.activeTextEditor;
|
const editor: any = vscode.window.activeTextEditor;
|
||||||
|
|
||||||
if (editor) {
|
if (editor) {
|
||||||
const lineNum: number = fetchLineNumber(editor);
|
const lineNum: number = fetchLineNumber(editor);
|
||||||
@ -67,11 +66,11 @@ function getIndent(): void {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Grab tab format from open document
|
// Grab tab format from open document
|
||||||
let tabFmt: any = {
|
const tabFmt: any = {
|
||||||
size: editor.options.tabSize,
|
size: editor.options.tabSize,
|
||||||
hard: !editor.options.insertSpaces
|
hard: !editor.options.insertSpaces
|
||||||
};
|
};
|
||||||
let i: number = pl.Lexer.getIndent(textLine.text, tabFmt);
|
const i: number = pl.Lexer.getIndent(textLine.text, tabFmt);
|
||||||
|
|
||||||
vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${i.toString()} indents`);
|
vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${i.toString()} indents`);
|
||||||
}
|
}
|
||||||
@ -98,7 +97,7 @@ function getIndent(): void {
|
|||||||
* TO-USE: set calculateLeadingSpaces to false
|
* TO-USE: set calculateLeadingSpaces to false
|
||||||
*/
|
*/
|
||||||
function getLeadingSpaces(): void {
|
function getLeadingSpaces(): void {
|
||||||
let editor: any = vscode.window.activeTextEditor;
|
const editor: any = vscode.window.activeTextEditor;
|
||||||
|
|
||||||
if (editor) {
|
if (editor) {
|
||||||
const lineNum: number = fetchLineNumber(editor);
|
const lineNum: number = fetchLineNumber(editor);
|
||||||
@ -131,25 +130,25 @@ function getLeadingSpaces(): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function runLineContext(): void {
|
function runLineContext(): void {
|
||||||
let editor: any = vscode.window.activeTextEditor;
|
const editor: any = vscode.window.activeTextEditor;
|
||||||
|
|
||||||
if (editor) {
|
if (editor) {
|
||||||
// current text and line number
|
// current text and line number
|
||||||
let editorText: string = editor.document.getText();
|
const editorText: string = editor.document.getText();
|
||||||
let line: number = editor.selection.active.line;
|
const line: string = editor.selection.active.line;
|
||||||
// get tab info settings
|
// get tab info settings
|
||||||
let size: number = parseInt(editor.options.tabSize);
|
const size: number = parseInt(editor.options.tabSize);
|
||||||
let hard: boolean = !editor.options.insertSpaces;
|
const hard: boolean = !editor.options.insertSpaces;
|
||||||
// initialize parser
|
// initialize parser
|
||||||
let parser: any = new pl.Parser(editorText, {
|
const parser: any = new pl.Parser(editorText, {
|
||||||
size,
|
size,
|
||||||
hard
|
hard
|
||||||
});
|
});
|
||||||
|
|
||||||
parser.parse();
|
parser.parse();
|
||||||
let context: any = parser.context(line);
|
const context: string = parser.context(line);
|
||||||
// build text
|
// build text
|
||||||
let contentString: string = createContextString(context, line);
|
const contentString: string = createContextString(context, line);
|
||||||
|
|
||||||
vscode.window.showInformationMessage(contentString);
|
vscode.window.showInformationMessage(contentString);
|
||||||
}
|
}
|
||||||
@ -158,7 +157,7 @@ function runLineContext(): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createContextString(context: any, line: number): string {
|
function createContextString(context: any, line: string): 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,8 +168,8 @@ function createContextString(context: any, line: number): string {
|
|||||||
contextString += ': ' + context[0].token.type.toString() + ' ' + context[0].token.attr.toString();
|
contextString += ': ' + context[0].token.type.toString() + ' ' + context[0].token.attr.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 1; i < context.length; i++) {
|
for (let i: number = 1; i < context.length; i++) {
|
||||||
let node: any = context[i];
|
const node: any = context[i];
|
||||||
|
|
||||||
if (node.label === 'root') {
|
if (node.label === 'root') {
|
||||||
// root
|
// root
|
||||||
@ -193,7 +192,7 @@ function createContextString(context: any, line: number): 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 {
|
||||||
let editor: any = vscode.window.activeTextEditor;
|
const editor: any = vscode.window.activeTextEditor;
|
||||||
|
|
||||||
if (!editor) {
|
if (!editor) {
|
||||||
vscode.window.showErrorMessage('RunCursorContext: No Active Editor');
|
vscode.window.showErrorMessage('RunCursorContext: No Active Editor');
|
||||||
@ -201,12 +200,12 @@ function runCursorContext(): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const cursorPos: any = editor.selection.active;
|
const cursorPos: any = editor.selection.active;
|
||||||
const text: any = editor.document.lineAt(cursorPos).text;
|
const text: string = editor.document.lineAt(cursorPos).text;
|
||||||
const windowSize: number = vscode.workspace.getConfiguration('mindReader').get('reader.contextWindow');
|
const windowSize: any = vscode.workspace.getConfiguration('mindReader').get('reader.contextWindow');
|
||||||
let trimmedText: any = text.trimStart(); // trim leading whitespace
|
let trimmedText: string = text.trimStart(); // trim leading whitespace
|
||||||
let leadingWS: number = text.length - trimmedText.length; // # of characters of leading whitespace
|
const leadingWS: number = text.length - trimmedText.length; // # of characters of leading whitespace
|
||||||
let pos: number = leadingWS;
|
let pos: number = leadingWS;
|
||||||
let maxPos: number = text.length;
|
const maxPos: number = text.length;
|
||||||
// clamp cursor start/end to new range
|
// clamp cursor start/end to new range
|
||||||
let col: number = cursorPos.character; // effective column of the cursor position
|
let col: number = cursorPos.character; // effective column of the cursor position
|
||||||
|
|
||||||
@ -223,10 +222,10 @@ function runCursorContext(): void {
|
|||||||
|
|
||||||
// generate list of space separate words with range data (start, end)
|
// generate list of space separate words with range data (start, end)
|
||||||
// TODO: can find user position to be done in one pass
|
// TODO: can find user position to be done in one pass
|
||||||
let spaceWords: any[] = [];
|
const spaceWords: any[] = [];
|
||||||
|
|
||||||
while (pos < maxPos && trimmedText.length > 0) {
|
while (pos < maxPos && trimmedText.length > 0) {
|
||||||
let word: string = trimmedText.replace(/ .*/, '');
|
const word: string = trimmedText.replace(/ .*/, '');
|
||||||
|
|
||||||
spaceWords.push({
|
spaceWords.push({
|
||||||
word,
|
word,
|
||||||
@ -245,7 +244,7 @@ function runCursorContext(): void {
|
|||||||
let contextStart: number = -1;
|
let contextStart: number = -1;
|
||||||
let contextEnd: number = -1;
|
let contextEnd: number = -1;
|
||||||
|
|
||||||
for (let i = 0; i < spaceWords.length; i++) {
|
for (let i: number = 0; i < spaceWords.length; i++) {
|
||||||
if (col >= spaceWords[i].start && col <= spaceWords[i].end) {
|
if (col >= spaceWords[i].start && col <= spaceWords[i].end) {
|
||||||
// found the word
|
// found the word
|
||||||
contextStart = Math.max(0, i - windowSize); // clamp start index
|
contextStart = Math.max(0, i - windowSize); // clamp start index
|
||||||
@ -253,7 +252,7 @@ function runCursorContext(): void {
|
|||||||
// construct cursor context string
|
// construct cursor context string
|
||||||
let contextString: string = '';
|
let contextString: string = '';
|
||||||
|
|
||||||
for (let i = contextStart; i < contextEnd; i++) {
|
for (let i: any = contextStart; i < contextEnd; i++) {
|
||||||
contextString += spaceWords[i].word + ' ';
|
contextString += spaceWords[i].word + ' ';
|
||||||
}
|
}
|
||||||
// output cursor context string
|
// output cursor context string
|
||||||
|
Loading…
Reference in New Issue
Block a user