mirror of
https://github.com/We-Dont-Byte/Mind_Reader.git
synced 2024-11-15 03:35:59 +00:00
text.ts - added types
This commit is contained in:
parent
641997b620
commit
c087f1666a
@ -1,13 +1,10 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", {
|
import { CommandEntry } from './commandEntry';
|
||||||
value: true
|
|
||||||
});
|
|
||||||
exports.textCommands = void 0;
|
|
||||||
|
|
||||||
const vscode = require("vscode");
|
const vscode = require("vscode");
|
||||||
const pl = require("../pylex");
|
const pl = require("../pylex");
|
||||||
|
|
||||||
exports.textCommands = [
|
export const textCommands: CommandEntry[] = [
|
||||||
{
|
{
|
||||||
name: 'mind-reader.getLineNumber',
|
name: 'mind-reader.getLineNumber',
|
||||||
callback: getLineNumber,
|
callback: getLineNumber,
|
||||||
@ -33,52 +30,53 @@ exports.textCommands = [
|
|||||||
/* 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) {
|
function fetchLineNumber(editor: any): number {
|
||||||
return editor.selection.active.line + 1;
|
return editor.selection.active.line + 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) {
|
function fetchTextLine(editor: any): string {
|
||||||
return editor.document.lineAt(fetchLineNumber(editor) - 1);
|
return editor.document.lineAt(fetchLineNumber(editor) - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function that outputs the current line number the cursor is on
|
// Function that outputs the current line number the cursor is on
|
||||||
function getLineNumber() {
|
function getLineNumber(): void {
|
||||||
let editor = vscode.window.activeTextEditor;
|
let editor: any = vscode.window.activeTextEditor;
|
||||||
|
|
||||||
if (editor) {
|
if (editor) {
|
||||||
let lineNum = fetchLineNumber(editor);
|
let lineNum: number = fetchLineNumber(editor);
|
||||||
|
|
||||||
vscode.window.showInformationMessage(`Line ${lineNum.toString()}`);
|
vscode.window.showInformationMessage(`Line ${lineNum.toString()}`);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
vscode.window.showErrorMessage('No document currently active');
|
vscode.window.showErrorMessage('No document currently active');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getIndent() {
|
function getIndent(): void {
|
||||||
let editor = vscode.window.activeTextEditor;
|
let editor: any = vscode.window.activeTextEditor;
|
||||||
|
|
||||||
if (editor) {
|
if (editor) {
|
||||||
let lineNum = fetchLineNumber(editor);
|
const lineNum: number = fetchLineNumber(editor);
|
||||||
let textLine = fetchTextLine(editor);
|
const textLine: any = 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
|
||||||
let tabFmt = {
|
let tabFmt: any = {
|
||||||
size: editor.options.tabSize,
|
size: editor.options.tabSize,
|
||||||
hard: !editor.options.insertSpaces
|
hard: !editor.options.insertSpaces
|
||||||
};
|
};
|
||||||
let i = pl.Lexer.getIndent(textLine.text, tabFmt);
|
let i: number = pl.Lexer.getIndent(textLine.text, tabFmt);
|
||||||
|
|
||||||
/* Ternary operator to change the tense of 'indent' to 'indents' for the output if i is 0 or greater than 1 */
|
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 {
|
}
|
||||||
|
else {
|
||||||
vscode.window.showErrorMessage('No document currently active');
|
vscode.window.showErrorMessage('No document currently active');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -99,24 +97,25 @@ function getIndent() {
|
|||||||
*
|
*
|
||||||
* TO-USE: set calculateLeadingSpaces to false
|
* TO-USE: set calculateLeadingSpaces to false
|
||||||
*/
|
*/
|
||||||
function getLeadingSpaces() {
|
function getLeadingSpaces(): void {
|
||||||
let editor = vscode.window.activeTextEditor;
|
let editor: any = vscode.window.activeTextEditor;
|
||||||
|
|
||||||
if (editor) {
|
if (editor) {
|
||||||
const lineNum = fetchLineNumber(editor);
|
const lineNum: number = fetchLineNumber(editor);
|
||||||
const textLine = fetchTextLine(editor);
|
const textLine: any = fetchTextLine(editor);
|
||||||
|
|
||||||
if (textLine.isEmptyOrWhitespace) {
|
if (textLine.isEmptyOrWhitespace) {
|
||||||
vscode.window.showInformationMessage(`Line ${lineNum.toString()} is empty`);
|
vscode.window.showInformationMessage(`Line ${lineNum.toString()} is empty`);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
/*
|
/*
|
||||||
* set true to use method 1: find the number of leading spaces through arithmetic
|
* set true to use method 1: find the number of leading spaces through arithmetic
|
||||||
* set false to use method 2: find the index position of the first non-whitespace character in a 0-index
|
* set false to use method 2: find the index position of the first non-whitespace character in a 0-index
|
||||||
*
|
*
|
||||||
* default: false
|
* default: false
|
||||||
*/
|
*/
|
||||||
const calculateLeadingSpaces = false; // change boolean value to change method
|
const calculateLeadingSpaces: boolean = false; // change boolean value to change method
|
||||||
const numSpaces = (calculateLeadingSpaces === true) ?
|
const numSpaces: number = (calculateLeadingSpaces) ?
|
||||||
pl.Lexer.getLeadingSpacesByArithmetic(textLine) :
|
pl.Lexer.getLeadingSpacesByArithmetic(textLine) :
|
||||||
pl.Lexer.getLeadingSpacesByIndex(textLine);
|
pl.Lexer.getLeadingSpacesByIndex(textLine);
|
||||||
|
|
||||||
@ -125,50 +124,53 @@ function getLeadingSpaces() {
|
|||||||
vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${numSpaces.toString()} spaces`):
|
vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${numSpaces.toString()} spaces`):
|
||||||
vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${numSpaces.toString()} space`);
|
vscode.window.showInformationMessage(`Line ${lineNum.toString()}: ${numSpaces.toString()} space`);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
vscode.window.showErrorMessage('No document currently active');
|
vscode.window.showErrorMessage('No document currently active');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function runLineContext() {
|
function runLineContext(): void {
|
||||||
let editor = vscode.window.activeTextEditor;
|
let editor: any = vscode.window.activeTextEditor;
|
||||||
|
|
||||||
if (editor) {
|
if (editor) {
|
||||||
// current text and line number
|
// current text and line number
|
||||||
let editorText = editor.document.getText();
|
let editorText: string = editor.document.getText();
|
||||||
let line = editor.selection.active.line;
|
let line: number = editor.selection.active.line;
|
||||||
// get tab info settings
|
// get tab info settings
|
||||||
let size = parseInt(editor.options.tabSize);
|
let size: number = parseInt(editor.options.tabSize);
|
||||||
let hard = !editor.options.insertSpaces;
|
let hard: boolean = !editor.options.insertSpaces;
|
||||||
// initialize parser
|
// initialize parser
|
||||||
let parser = new pl.Parser(editorText, {
|
let parser: any = new pl.Parser(editorText, {
|
||||||
size,
|
size,
|
||||||
hard
|
hard
|
||||||
});
|
});
|
||||||
|
|
||||||
parser.parse();
|
parser.parse();
|
||||||
let context = parser.context(line);
|
let context: any = parser.context(line);
|
||||||
// build text
|
// build text
|
||||||
let contentString = createContextString(context, line);
|
let contentString: string = createContextString(context, line);
|
||||||
|
|
||||||
vscode.window.showInformationMessage(contentString);
|
vscode.window.showInformationMessage(contentString);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
vscode.window.showErrorMessage('No document currently active');
|
vscode.window.showErrorMessage('No document currently active');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createContextString(context, line) {
|
function createContextString(context: any, 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');
|
||||||
}
|
}
|
||||||
|
|
||||||
let contextString = 'Line ' + (line + 1); // 1 based
|
let contextString: string = `Line ${line + 1}`; // 1 based
|
||||||
|
|
||||||
if (context[0].token && context[0].token.attr) {
|
if (context[0].token && context[0].token.attr) {
|
||||||
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 = 1; i < context.length; i++) {
|
||||||
let node = context[i];
|
let node: any = context[i];
|
||||||
|
|
||||||
if (node.label === 'root') {
|
if (node.label === 'root') {
|
||||||
// root
|
// root
|
||||||
@ -184,59 +186,64 @@ function createContextString(context, line) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return contextString;
|
return contextString;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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() {
|
function runCursorContext(): void {
|
||||||
let editor = vscode.window.activeTextEditor;
|
let editor: any = 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 = editor.selection.active;
|
const cursorPos: any = editor.selection.active;
|
||||||
const text = editor.document.lineAt(cursorPos).text;
|
const text: string = editor.document.lineAt(cursorPos).text;
|
||||||
const windowSize = vscode.workspace.getConfiguration('mindReader').get('reader.contextWindow');
|
const windowSize: number = vscode.workspace.getConfiguration('mindReader').get('reader.contextWindow');
|
||||||
let trimmedText = text.trimStart(); // trim leading whitespace
|
let trimmedText: string = text.trimStart(); // trim leading whitespace
|
||||||
let leadingWS = text.length - trimmedText.length; // # of characters of leading whitespace
|
let leadingWS: number = text.length - trimmedText.length; // # of characters of leading whitespace
|
||||||
trimmedText = trimmedText.trimEnd(); // trim trailing whitespace
|
let pos: number = leadingWS;
|
||||||
let pos = leadingWS;
|
let maxPos: number = text.length;
|
||||||
let maxPos = text.length;
|
|
||||||
// clamp cursor start/end to new range
|
// clamp cursor start/end to new range
|
||||||
let col = cursorPos.character; // effective column of the cursor position
|
let col: number = cursorPos.character; // effective column of the cursor position
|
||||||
|
|
||||||
|
trimmedText = trimmedText.trimEnd(); // trim trailing whitespace
|
||||||
|
|
||||||
if (col < leadingWS) {
|
if (col < leadingWS) {
|
||||||
// move effective start to first non-whitespace character in the line
|
// move effective start to first non-whitespace character in the line
|
||||||
col = leadingWS;
|
col = leadingWS;
|
||||||
} else if (col > leadingWS + trimmedText.length - 1) {
|
}
|
||||||
|
else if (col > leadingWS + trimmedText.length - 1) {
|
||||||
// move effective end to last non-whitespace character in the line
|
// move effective end to last non-whitespace character in the line
|
||||||
col = leadingWS + trimmedText.length - 1;
|
col = leadingWS + trimmedText.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 = [];
|
let spaceWords: any[] = [];
|
||||||
|
|
||||||
while (pos < maxPos && trimmedText.length > 0) {
|
while (pos < maxPos && trimmedText.length > 0) {
|
||||||
let word = trimmedText.replace(/ .*/, '');
|
let word: string = trimmedText.replace(/ .*/, '');
|
||||||
|
|
||||||
spaceWords.push({
|
spaceWords.push({
|
||||||
word,
|
word,
|
||||||
start: pos,
|
start: pos,
|
||||||
end: pos + word.length
|
end: pos + word.length
|
||||||
});
|
});
|
||||||
|
|
||||||
// remove processed word from trimmed text
|
// remove processed word from trimmed text
|
||||||
const oldText = trimmedText;
|
const oldText: string = trimmedText;
|
||||||
trimmedText = trimmedText.replace(/[^ ]+/, '').trimStart();
|
trimmedText = trimmedText.replace(/[^ ]+/, '').trimStart();
|
||||||
// update pos to start of next word
|
// update pos to start of next word
|
||||||
pos += oldText.length - trimmedText.length;
|
pos += oldText.length - trimmedText.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
// find word the user is in
|
// find word the user is in
|
||||||
let contextStart = -1,
|
let contextStart: number = -1;
|
||||||
contextEnd = -1;
|
let contextEnd: number = -1;
|
||||||
|
|
||||||
for (let i = 0; i < spaceWords.length; i++) {
|
for (let i = 0; i < spaceWords.length; i++) {
|
||||||
if (col >= spaceWords[i].start && col <= spaceWords[i].end) {
|
if (col >= spaceWords[i].start && col <= spaceWords[i].end) {
|
||||||
@ -244,7 +251,7 @@ function runCursorContext() {
|
|||||||
contextStart = Math.max(0, i - windowSize); // clamp start index
|
contextStart = Math.max(0, i - windowSize); // clamp start index
|
||||||
contextEnd = Math.min(spaceWords.length, i + windowSize + 1); // clamp end index
|
contextEnd = Math.min(spaceWords.length, i + windowSize + 1); // clamp end index
|
||||||
// construct cursor context string
|
// construct cursor context string
|
||||||
let contextString = '';
|
let contextString: string = '';
|
||||||
|
|
||||||
for (let i = contextStart; i < contextEnd; i++) {
|
for (let i = contextStart; i < contextEnd; i++) {
|
||||||
contextString += spaceWords[i].word + ' ';
|
contextString += spaceWords[i].word + ' ';
|
||||||
|
Loading…
Reference in New Issue
Block a user