python2ib
Version:
Convert Python code to IB Pseudocode format
120 lines • 4.08 kB
JavaScript
/**
* Indentation utilities for IB Pseudocode output
*/
/** Indentation manager class */
export class IndentManager {
style;
size;
currentLevel = 0;
constructor(style = 'spaces', size = 4) {
this.style = style;
this.size = size;
}
/** Get current indentation string */
get current() {
return this.getIndent(this.currentLevel);
}
/** Get indentation string for specific level */
getIndent(level) {
if (level < 0)
level = 0;
const unit = this.style === 'spaces' ? ' ' : '\t';
const count = this.style === 'spaces' ? level * this.size : level;
return unit.repeat(count);
}
/** Increase indentation level */
increase() {
this.currentLevel++;
}
/** Decrease indentation level */
decrease() {
if (this.currentLevel > 0) {
this.currentLevel--;
}
}
/** Set specific indentation level */
setLevel(level) {
this.currentLevel = Math.max(0, level);
}
/** Get current indentation level */
getLevel() {
return this.currentLevel;
}
/** Reset indentation to level 0 */
reset() {
this.currentLevel = 0;
}
/** Apply indentation to a line of text */
applyToLine(text, level) {
const indent = level !== undefined ? this.getIndent(level) : this.current;
return indent + text;
}
/** Apply indentation to multiple lines */
applyToLines(lines, level) {
return lines.map(line => this.applyToLine(line, level));
}
}
/** Utility functions for indentation */
export const IndentUtils = {
/** Create indentation string */
create(level, style = 'spaces', size = 4) {
if (level < 0)
level = 0;
const unit = style === 'spaces' ? ' ' : '\t';
const count = style === 'spaces' ? level * size : level;
return unit.repeat(count);
},
/** Count indentation level of a line */
countLevel(line, style = 'spaces', size = 4) {
let count = 0;
const unit = style === 'spaces' ? ' ' : '\t';
for (const char of line) {
if (char === unit) {
count++;
}
else {
break;
}
}
return style === 'spaces' ? Math.floor(count / size) : count;
},
/** Remove indentation from a line */
remove(line) {
return line.replace(/^[\s\t]+/, '');
},
/** Normalize indentation in text block */
normalize(text, style = 'spaces', size = 4) {
const lines = text.split('\n');
const manager = new IndentManager(style, size);
return lines.map(line => {
const trimmed = IndentUtils.remove(line);
if (trimmed === '')
return '';
// Simple heuristic for indentation level based on keywords
if (trimmed.startsWith('IF ') || trimmed.startsWith('WHILE ') ||
trimmed.startsWith('FOR ') || trimmed.startsWith('PROCEDURE ') ||
trimmed.startsWith('FUNCTION ') || trimmed.startsWith('TRY')) {
const result = manager.applyToLine(trimmed);
manager.increase();
return result;
}
else if (trimmed.startsWith('ELSEIF ') || trimmed.startsWith('ELSE') ||
trimmed.startsWith('EXCEPT')) {
manager.decrease();
const result = manager.applyToLine(trimmed);
manager.increase();
return result;
}
else if (trimmed.startsWith('ENDIF') || trimmed.startsWith('ENDWHILE') ||
trimmed.startsWith('NEXT ') || trimmed.startsWith('ENDPROCEDURE') ||
trimmed.startsWith('ENDFUNCTION') || trimmed.startsWith('ENDTRY')) {
manager.decrease();
return manager.applyToLine(trimmed);
}
else {
return manager.applyToLine(trimmed);
}
}).join('\n');
}
};
//# sourceMappingURL=indent.js.map