mirror of
https://github.com/We-Dont-Byte/Mind_Reader.git
synced 2024-11-15 03:35:59 +00:00
added getLeadingSpaces() function
Added function that would return the number of leading spaces on a line
This commit is contained in:
parent
30248966bf
commit
cac31ceaa9
@ -1,168 +1,166 @@
|
|||||||
import * as vscode from 'vscode';
|
"use strict";
|
||||||
import * as pl from '../pylex';
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.textCommands = void 0;
|
||||||
import { CommandEntry } from './commandEntry';
|
const vscode = require("vscode");
|
||||||
|
const pl = require("../pylex");
|
||||||
export const textCommands: CommandEntry[] = [
|
exports.textCommands = [
|
||||||
{
|
|
||||||
name: 'mind-reader.getIndent',
|
|
||||||
callback: getIndent,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
name: 'mind-reader.runLineContext',
|
|
||||||
callback: runLineContext,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
name: 'mind-reader.runCursorContext',
|
|
||||||
callback: runCursorContext
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
function getIndent(): void {
|
|
||||||
let editor = vscode.window.activeTextEditor;
|
|
||||||
if(editor)
|
|
||||||
{
|
|
||||||
let lineNum = editor.selection.active.line + 1;
|
|
||||||
let textLine = editor.document.lineAt(lineNum - 1);
|
|
||||||
if(textLine.isEmptyOrWhitespace)
|
|
||||||
{
|
{
|
||||||
vscode.window.showInformationMessage("Line number " + lineNum.toString() + " Is Empty");
|
name: 'mind-reader.getIndent',
|
||||||
}
|
callback: getIndent,
|
||||||
else
|
},
|
||||||
{
|
{
|
||||||
// Grab tab format from open document
|
name: 'mind-reader.getLeadingSpaces',
|
||||||
let tabFmt = {
|
callback: getLeadingSpaces,
|
||||||
size: editor.options.tabSize as number,
|
},
|
||||||
hard: !editor.options.insertSpaces
|
{
|
||||||
};
|
name: 'mind-reader.runLineContext',
|
||||||
let i = pl.Lexer.getIndent(textLine.text, tabFmt);
|
callback: runLineContext,
|
||||||
vscode.window.showInformationMessage("Line Number " + lineNum.toString() + " Indentation " + i.toString());
|
},
|
||||||
|
{
|
||||||
|
name: 'mind-reader.runCursorContext',
|
||||||
|
callback: runCursorContext
|
||||||
|
}
|
||||||
|
];
|
||||||
|
function getIndent() {
|
||||||
|
let editor = vscode.window.activeTextEditor;
|
||||||
|
if (editor) {
|
||||||
|
let lineNum = editor.selection.active.line + 1;
|
||||||
|
let textLine = editor.document.lineAt(lineNum - 1);
|
||||||
|
if (textLine.isEmptyOrWhitespace) {
|
||||||
|
vscode.window.showInformationMessage("Line number " + lineNum.toString() + " Is Empty");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Grab tab format from open document
|
||||||
|
let tabFmt = {
|
||||||
|
size: editor.options.tabSize,
|
||||||
|
hard: !editor.options.insertSpaces
|
||||||
|
};
|
||||||
|
let i = pl.Lexer.getIndent(textLine.text, tabFmt);
|
||||||
|
vscode.window.showInformationMessage("Line Number " + lineNum.toString() + " Indentation " + i.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
vscode.window.showErrorMessage('No document currently active');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else{
|
|
||||||
vscode.window.showErrorMessage('No document currently active');
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
function getLeadingSpaces() {
|
||||||
|
let editor = vscode.window.activeTextEditor;
|
||||||
|
|
||||||
function runLineContext(): void {
|
if (editor) {
|
||||||
let editor = vscode.window.activeTextEditor;
|
let lineNum = editor.selection.active.line + 1;
|
||||||
if (editor){
|
let textLine = editor.document.lineAt(lineNum - 1);
|
||||||
// current text and line number
|
if(textLine.isEmptyOrWhitespace) {
|
||||||
let editorText = editor.document.getText();
|
vscode.window.showInformationMessage("Line number " + lineNum.toString() + " Is Empty");
|
||||||
let line = editor.selection.active.line;
|
}
|
||||||
|
else {
|
||||||
// get tab info settings
|
let numSpaces = textLine.firstNonWhitespaceCharacterIndex;
|
||||||
let size = parseInt(editor.options.tabSize as string);
|
vscode.window.showInformationMessage("Line Number " + lineNum.toString() + numSpaces.toString()) + " Leading Spaces ";
|
||||||
let hard = !editor.options.insertSpaces;
|
}
|
||||||
|
}
|
||||||
// initialize parser
|
else {
|
||||||
let parser = new pl.Parser(editorText, {size, hard});
|
vscode.window.showErrorMessage('No document currently active');
|
||||||
parser.parse();
|
}
|
||||||
|
|
||||||
let context = parser.context(line);
|
|
||||||
|
|
||||||
// build text
|
|
||||||
let contentString = createContextString(context, line);
|
|
||||||
vscode.window.showInformationMessage(contentString);
|
|
||||||
} else {
|
|
||||||
vscode.window.showErrorMessage('No document currently active');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
function runLineContext() {
|
||||||
function createContextString(context: pl.LexNode[], line: number): string {
|
let editor = vscode.window.activeTextEditor;
|
||||||
|
if (editor) {
|
||||||
|
// current text and line number
|
||||||
|
let editorText = editor.document.getText();
|
||||||
|
let line = editor.selection.active.line;
|
||||||
|
// get tab info settings
|
||||||
|
let size = parseInt(editor.options.tabSize);
|
||||||
|
let hard = !editor.options.insertSpaces;
|
||||||
|
// initialize parser
|
||||||
|
let parser = new pl.Parser(editorText, { size, hard });
|
||||||
|
parser.parse();
|
||||||
|
let context = parser.context(line);
|
||||||
|
// build text
|
||||||
|
let contentString = createContextString(context, line);
|
||||||
|
vscode.window.showInformationMessage(contentString);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
vscode.window.showErrorMessage('No document currently active');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function createContextString(context, line) {
|
||||||
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 = '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 = context[i];
|
||||||
if (node.label === 'root') {
|
if (node.label === 'root') {
|
||||||
// root
|
// root
|
||||||
contextString += ' in the Document Root';
|
contextString += ' in the Document Root';
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (node.token.type !== pl.PylexSymbol.EMPTY &&
|
||||||
if (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) {
|
contextString += ' ' + node.token.attr.toString();
|
||||||
contextString += ' ' + node.token!.attr.toString();
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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(): void {
|
function runCursorContext() {
|
||||||
let editor = vscode.window.activeTextEditor;
|
let editor = 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: vscode.Position = 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 leadingWS = text.length - trimmedText.length; // # of characters of leading whitespace
|
||||||
let trimmedText = text.trimStart(); // trim leading whitespace
|
trimmedText = trimmedText.trimEnd(); // trim trailing whitespace
|
||||||
let leadingWS = text.length - trimmedText.length; // # of characters of leading whitespace
|
let pos = leadingWS;
|
||||||
trimmedText = trimmedText.trimEnd(); // trim trailing whitespace
|
let maxPos = text.length;
|
||||||
let pos = leadingWS;
|
// clamp cursor start/end to new range
|
||||||
let maxPos = text.length;
|
let col = cursorPos.character; // effective column of the cursor position
|
||||||
|
if (col < leadingWS) {
|
||||||
// clamp cursor start/end to new range
|
// move effective start to first non-whitespace character in the line
|
||||||
let col = cursorPos.character; // effective column of the cursor position
|
col = leadingWS;
|
||||||
if (col < leadingWS) {
|
}
|
||||||
// move effective start to first non-whitespace character in the line
|
else if (col > leadingWS + trimmedText.length - 1) {
|
||||||
col = leadingWS;
|
// move effective end to last non-whitespace character in the line
|
||||||
} else if (col > leadingWS + trimmedText.length - 1) {
|
col = leadingWS + trimmedText.length - 1;
|
||||||
// move effective end to last non-whitespace character in the line
|
}
|
||||||
col = leadingWS + trimmedText.length - 1;
|
// generate list of space separate words with range data (start, end)
|
||||||
}
|
// TODO: can find user position to be done in one pass
|
||||||
|
let spaceWords = [];
|
||||||
// generate list of space separate words with range data (start, end)
|
while (pos < maxPos && trimmedText.length > 0) {
|
||||||
// TODO: can find user position to be done in one pass
|
let word = trimmedText.replace(/ .*/, '');
|
||||||
let spaceWords: {word: string, start: number, end: number}[] = [];
|
spaceWords.push({ word, start: pos, end: pos + word.length });
|
||||||
while (pos < maxPos && trimmedText.length > 0) {
|
// remove processed word from trimmed text
|
||||||
let word = trimmedText.replace(/ .*/, '');
|
const oldText = trimmedText;
|
||||||
spaceWords.push({word, start: pos, end: pos+word.length});
|
trimmedText = trimmedText.replace(/[^ ]+/, '').trimStart();
|
||||||
|
// update pos to start of next word
|
||||||
// remove processed word from trimmed text
|
pos += oldText.length - trimmedText.length;
|
||||||
const oldText = trimmedText;
|
}
|
||||||
trimmedText = trimmedText.replace(/[^ ]+/, '').trimStart();
|
// find word the user is in
|
||||||
|
let contextStart = -1, contextEnd = -1;
|
||||||
// update pos to start of next word
|
for (let i = 0; i < spaceWords.length; i++) {
|
||||||
pos += oldText.length - trimmedText.length;
|
if (col >= spaceWords[i].start && col <= spaceWords[i].end) {
|
||||||
}
|
// found the word
|
||||||
|
contextStart = Math.max(0, i - windowSize); // clamp start index
|
||||||
// find word the user is in
|
contextEnd = Math.min(spaceWords.length, i + windowSize + 1); // clamp end index
|
||||||
let contextStart: number = -1, contextEnd: number = -1;
|
// construct cursor context string
|
||||||
for (let i = 0; i < spaceWords.length; i++) {
|
let contextString = '';
|
||||||
if (col >= spaceWords[i].start && col <= spaceWords[i].end) {
|
for (let i = contextStart; i < contextEnd; i++) {
|
||||||
// found the word
|
contextString += spaceWords[i].word + ' ';
|
||||||
contextStart = Math.max(0, i - windowSize); // clamp start index
|
}
|
||||||
contextEnd = Math.min(spaceWords.length, i + windowSize + 1); // clamp end index
|
// output cursor context string
|
||||||
|
vscode.window.showInformationMessage(contextString);
|
||||||
// construct cursor context string
|
return;
|
||||||
let contextString = '';
|
}
|
||||||
for (let i = contextStart; i < contextEnd; i++) {
|
|
||||||
contextString += spaceWords[i].word + ' ';
|
|
||||||
}
|
|
||||||
|
|
||||||
// output cursor context string
|
|
||||||
vscode.window.showInformationMessage(contextString);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
//# sourceMappingURL=text.js.map
|
||||||
|
Loading…
Reference in New Issue
Block a user