From 0f2b64339efef6ad7f8eb3579168087401767c4f Mon Sep 17 00:00:00 2001 From: SecretSheppy <62794249+SecretSheppy@users.noreply.github.com> Date: Sun, 24 Nov 2024 13:36:20 +0000 Subject: [PATCH 1/9] refactor: removed unused/old methods --- main.js | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/main.js b/main.js index def46ba..b03b1e2 100644 --- a/main.js +++ b/main.js @@ -223,34 +223,6 @@ cmd.registerCustomCommand('help', () => { showCommandPrompt(); }); -/** - * Generates the element that will be nested into the hljs code view that will - * contain the absolutely positioned elements; - * - * @returns {HTMLDivElement} - */ -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()); -} - -/** - * Removes the highlighted element at the top of the hljs code. - */ -function hideHighlightedElement() { - document.getElementById('haskedit-highlights').remove(); -} - /** * Runs to set up the gui as is specified in the config.json file. */ From 75e79f973441e9278ca24a27aa8452ffb5611efb Mon Sep 17 00:00:00 2001 From: SecretSheppy <62794249+SecretSheppy@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:03:18 +0000 Subject: [PATCH 2/9] refactor: unified file saving --- main.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/main.js b/main.js index b03b1e2..1015028 100644 --- a/main.js +++ b/main.js @@ -109,16 +109,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; } @@ -223,6 +214,26 @@ cmd.registerCustomCommand('help', () => { showCommandPrompt(); }); +/** + * save a previously saved file + * + * @param {string} value the new value of the file + */ +function savePreviouslySavedFile(value) { + if (file.getFilePath() === undefined) { + writeStderr('file has not been previously saved, must give a filename'); + showCommandPromptRegion(); + return; + } + + replaceTabsWithSpaces(); + file.saveFile(value, 'utf-8'); + window.saved = true; + updateSavedIndicator(); + updateFileNameDisplay(); + formatFile(); +} + /** * Runs to set up the gui as is specified in the config.json file. */ @@ -840,18 +851,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)) { From 7db82f1307926e61ef5df97a408bf46d32569641 Mon Sep 17 00:00:00 2001 From: SecretSheppy <62794249+SecretSheppy@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:05:42 +0000 Subject: [PATCH 3/9] refactor: replaceTabsWithSpaces does not update or use editor.value --- main.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/main.js b/main.js index 1015028..1d7d8ec 100644 --- a/main.js +++ b/main.js @@ -226,8 +226,7 @@ function savePreviouslySavedFile(value) { return; } - replaceTabsWithSpaces(); - file.saveFile(value, 'utf-8'); + file.saveFile(replaceTabsWithSpaces(value), 'utf-8'); window.saved = true; updateSavedIndicator(); updateFileNameDisplay(); @@ -553,10 +552,10 @@ function checkFileFormatting() { /** * 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)); } /** From 68f3911e587a673f9fc8681671c7be2df87dcf54 Mon Sep 17 00:00:00 2001 From: SecretSheppy <62794249+SecretSheppy@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:17:12 +0000 Subject: [PATCH 4/9] refactor: introduced format enum like object --- main.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/main.js b/main.js index 1d7d8ec..8dd2fe5 100644 --- a/main.js +++ b/main.js @@ -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. @@ -473,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'); @@ -499,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"]); @@ -514,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(); } } @@ -529,7 +544,7 @@ function checkFileFormatting() { return; } - updateFormattingIndicator('FORMATTING'); + updateFormattingIndicator(format.FORMATTING); console.info('starting automatic formatting process'); let command = parser.parse(config["on-save"]["formatter"]["script"]); @@ -538,15 +553,15 @@ function checkFileFormatting() { let formattedText = execSync(command); if (document.getElementById('editor').value === formattedText.toString()) { - 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); } /** From 921fc3477051756987926587b67f423e97b27b0a Mon Sep 17 00:00:00 2001 From: SecretSheppy <62794249+SecretSheppy@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:32:27 +0000 Subject: [PATCH 5/9] chore: added extra keybindings to help.txt --- help.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/help.txt b/help.txt index 56a259a..088bca3 100644 --- a/help.txt +++ b/help.txt @@ -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). \ No newline at end of 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 \ No newline at end of file From 4eff2ad534b8b6ef00bba96256f435b41de755ee Mon Sep 17 00:00:00 2001 From: SecretSheppy <62794249+SecretSheppy@users.noreply.github.com> Date: Sun, 24 Nov 2024 13:36:20 +0000 Subject: [PATCH 6/9] refactor: removed unused/old methods --- main.js | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/main.js b/main.js index db8e2fc..e7f5a08 100644 --- a/main.js +++ b/main.js @@ -223,34 +223,6 @@ cmd.registerCustomCommand('help', () => { showCommandPrompt(); }); -/** - * Generates the element that will be nested into the hljs code view that will - * contain the absolutely positioned elements; - * - * @returns {HTMLDivElement} - */ -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()); -} - -/** - * Removes the highlighted element at the top of the hljs code. - */ -function hideHighlightedElement() { - document.getElementById('haskedit-highlights').remove(); -} - /** * Runs to set up the gui as is specified in the config.json file. */ From 94aa97c515430fe14ddc60853e64291ee4b20401 Mon Sep 17 00:00:00 2001 From: SecretSheppy <62794249+SecretSheppy@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:03:18 +0000 Subject: [PATCH 7/9] refactor: unified file saving --- main.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/main.js b/main.js index e7f5a08..64c9a65 100644 --- a/main.js +++ b/main.js @@ -109,16 +109,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; } @@ -223,6 +214,26 @@ cmd.registerCustomCommand('help', () => { showCommandPrompt(); }); +/** + * save a previously saved file + * + * @param {string} value the new value of the file + */ +function savePreviouslySavedFile(value) { + if (file.getFilePath() === undefined) { + writeStderr('file has not been previously saved, must give a filename'); + showCommandPromptRegion(); + return; + } + + replaceTabsWithSpaces(); + file.saveFile(value, 'utf-8'); + window.saved = true; + updateSavedIndicator(); + updateFileNameDisplay(); + formatFile(); +} + /** * Runs to set up the gui as is specified in the config.json file. */ @@ -842,18 +853,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)) { From 25f1eae92792af5995ca2a8fbf773ac9aabc8ff8 Mon Sep 17 00:00:00 2001 From: SecretSheppy <62794249+SecretSheppy@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:05:42 +0000 Subject: [PATCH 8/9] refactor: replaceTabsWithSpaces does not update or use editor.value --- main.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/main.js b/main.js index 64c9a65..a3c13b4 100644 --- a/main.js +++ b/main.js @@ -226,8 +226,7 @@ function savePreviouslySavedFile(value) { return; } - replaceTabsWithSpaces(); - file.saveFile(value, 'utf-8'); + file.saveFile(replaceTabsWithSpaces(value), 'utf-8'); window.saved = true; updateSavedIndicator(); updateFileNameDisplay(); @@ -555,10 +554,10 @@ function checkFileFormatting() { /** * 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)); } /** From d5013a61167677cdc1976714f77102f86caa8f8a Mon Sep 17 00:00:00 2001 From: SecretSheppy <62794249+SecretSheppy@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:17:12 +0000 Subject: [PATCH 9/9] refactor: introduced format enum like object --- main.js | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/main.js b/main.js index a3c13b4..6c5651c 100644 --- a/main.js +++ b/main.js @@ -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. @@ -275,7 +286,6 @@ async function openFileInGui(filePath) { } document.getElementById('editor').value = text; - window.saved = true; updateSavedIndicator(); updateFileNameDisplay(); checkFileFormatting(); @@ -474,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'); @@ -500,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"]); @@ -515,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(); } } @@ -530,9 +544,9 @@ 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 { @@ -540,15 +554,15 @@ function checkFileFormatting() { 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); } /**