Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ The below keyboard shortcuts are for managing the haskedit window.

The remaining shortcuts are for quickly performing some of the haskedit commands.
- CTRL + S -> saves the open file
- CTRL + I -> launches a ghci environment in a new cmd.exe (for the current file).
- CTRL + I -> launches a ghci environment in a new cmd.exe (for the current file).
- CTRL + B -> builds the current file with ghc
- CTRL + R -> builds and then runs the current file with ghc
99 changes: 42 additions & 57 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ window.historyIndex = 0;
*/
window.editorExists = false;

/**
* Enum like structure for code readability.
*
* @type {Object}
*/
const format = {
FORMATTED: 'FORMATTING',
NOT_FORMATTED: 'NOT_FORMATTED',
FORMATTING: 'FORMATTING',
}

/**
* Overriding the cls command and replacing it with one that will clear the
* html terminal.
Expand Down Expand Up @@ -109,16 +120,7 @@ cmd.registerCustomCommand('save', (args) => {
let editor = document.getElementById('editor');

if (args.length === 0) {
if (file.getFilePath() === undefined) {
writeStderr('file has not been previously saved, must give a filename');
} else {
replaceTabsWithSpaces();
file.saveFile(editor.value, 'utf-8');
window.saved = true;
updateSavedIndicator();
updateFileNameDisplay();
formatFile();
}
savePreviouslySavedFile(editor.value);
showCommandPrompt();
return;
}
Expand Down Expand Up @@ -224,31 +226,22 @@ cmd.registerCustomCommand('help', () => {
});

/**
* Generates the element that will be nested into the hljs code view that will
* contain the absolutely positioned elements;
* save a previously saved file
*
* @returns {HTMLDivElement}
* @param {string} value the new value of the file
*/
function generateHighlightsElement() {
let element = document.createElement('div');
element.id = 'haskedit-highlights';
element.classList.add('haskedit-highlights');
return element;
}

/**
* Shows the highlighted element at the top of the hljs code.
*/
function showHighlightedElement() {
document.getElementsByClassName('hljs')[0]
.prepend(generateHighlightsElement());
}
function savePreviouslySavedFile(value) {
if (file.getFilePath() === undefined) {
writeStderr('file has not been previously saved, must give a filename');
showCommandPromptRegion();
return;
}

/**
* Removes the highlighted element at the top of the hljs code.
*/
function hideHighlightedElement() {
document.getElementById('haskedit-highlights').remove();
file.saveFile(replaceTabsWithSpaces(value), 'utf-8');
window.saved = true;
updateSavedIndicator();
updateFileNameDisplay();
formatFile();
}

/**
Expand Down Expand Up @@ -293,7 +286,6 @@ async function openFileInGui(filePath) {
}

document.getElementById('editor').value = text;
window.saved = true;
updateSavedIndicator();
updateFileNameDisplay();
checkFileFormatting();
Expand Down Expand Up @@ -492,17 +484,17 @@ function updateFormattingIndicator(stage) {
let formattedIndicator = document.getElementById('formatted-indicator');

switch (stage) {
case 'NOT_FORMATTED':
case format.NOT_FORMATTED:
formattedIndicator.innerText = 'Not Formatted';
formattedIndicator.classList.remove('true', 'formatting');
formattedIndicator.classList.add('false');
break;
case 'FORMATTING':
case format.FORMATTING:
formattedIndicator.innerText = 'Formatting';
formattedIndicator.classList.remove('true', 'false');
formattedIndicator.classList.add('formatting');
break;
case 'FORMATTED':
case format.FORMATTED:
formattedIndicator.innerText = 'Formatted';
formattedIndicator.classList.remove('false', 'formatting');
formattedIndicator.classList.add('true');
Expand All @@ -518,7 +510,10 @@ function formatFile() {
return;
}

updateFormattingIndicator('FORMATTING');
// TODO: needs refactoring to reduce file load and ensure curosr is always
// in the same position after a file is saved and formatted.

updateFormattingIndicator(format.FORMATTING);

console.info('starting automatic formatting process');
let command = parser.parse(config["on-save"]["formatter"]["script"]);
Expand All @@ -533,9 +528,10 @@ function formatFile() {
file.saveFile(formattedText.toString(), 'utf-8');
console.log('formatted file successfully');

updateFormattingIndicator('FORMATTED');
updateFormattingIndicator(format.FORMATTED);
} catch (e) {
writeStderr(`failed to format ${file.getLocalName()} with command "${command}"`);
updateFormattingIndicator(format.NOT_FORMATTED);
showCommandPromptRegion();
}
}
Expand All @@ -548,34 +544,34 @@ function checkFileFormatting() {
return;
}

updateFormattingIndicator('FORMATTING');
updateFormattingIndicator(format.FORMATTING);

console.info('checking file formatting');
console.info('starting automatic formatting process');
let command = parser.parse(config["on-save"]["formatter"]["script"]);

try {
let formattedText = execSync(command);

if (document.getElementById('editor').value.trim() ===
formattedText.toString().trim().replaceAll(/\r\n/g, '\n')) {
updateFormattingIndicator('FORMATTED');
updateFormattingIndicator(format.FORMATTED);
return;
}

updateFormattingIndicator('NOT_FORMATTED');
} catch (e) {
writeStderr(`format checker failed to format ${file.getLocalName()} with command "${command}"`);
showCommandPromptRegion();
}

updateFormattingIndicator(format.NOT_FORMATTED);
}

/**
* Replaces all tabs in the editor with the current tab-size worth of spaces.
*/
function replaceTabsWithSpaces() {
function replaceTabsWithSpaces(value) {
let editor = document.getElementById('editor');
let tabSize = parseInt(editor.getAttribute('tab-size'));
editor.value = editor.value.replace(/\t/g, ' '.repeat(tabSize));
return value.replace(/\t/g, ' '.repeat(tabSize));
}

/**
Expand Down Expand Up @@ -870,18 +866,7 @@ document.addEventListener('keydown', (e) => {
}

if (bindings.save(e)) {
let editor = document.getElementById('editor');
if (file.getFilePath() === undefined) {
writeStderr('file has not been previously saved, must give a filename');
showCommandPromptRegion();
} else {
replaceTabsWithSpaces();
file.saveFile(editor.value, 'utf-8');
window.saved = true;
updateSavedIndicator();
updateFileNameDisplay();
formatFile();
}
savePreviouslySavedFile(document.getElementById('editor').value);
}

if (bindings.interactive(e)) {
Expand Down