Skip to content

Commit

Permalink
Updated autocomplete
Browse files Browse the repository at this point in the history
Case-insensitive, inserts text at cursor if word is prefix of autocomplete word
  • Loading branch information
EntityPlantt committed Aug 24, 2023
1 parent d2a4a95 commit d75c596
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ addEventListener("DOMContentLoaded", () => {
}
}, 5000);
}
if (/^(\w|Backspace|Arrow(Up|Down)|Tab|\t)$/.test(event.key) && !(event.ctrlKey || event.altKey || event.metaKey)) {
if (/^(\w|Backspace)$/.test(event.key)) {
if (/^(\w|Backspace|Arrow(Up|Down)|Tab|\t|Shift)$/.test(event.key) && !(event.ctrlKey || event.altKey || event.metaKey)) {
if (/^(\w|Backspace|Shift)$/.test(event.key)) {
const word = editor.session.getTextRange(getEditorWordRange());
var autocomp = editor.getValue().split(/\b/);
autocomp = [...new Set(autocomp.filter(x => /^\w+$/.test(x)))].sort();
autocompleteOptions(autocomp.filter(x => new RegExp(word, "i").test(x) && x != word));
if (word.length) {
var autocomp = editor.getValue().split(/\b/);
autocomp = [...new Set(autocomp.filter(x => /^\w+$/.test(x)))].sort();
autocompleteOptions(autocomp.filter(x => new RegExp(word, "i").test(x) && x != word));
}
}
}
else {
Expand Down Expand Up @@ -184,7 +186,13 @@ function setEditorValue(data, keepHistory) {
}
function autocomplete(text) {
if (!text) text = document.querySelector("#autocomplete > li.selected").innerText;
editor.session.replace(getEditorWordRange(), text);
var wrange = getEditorWordRange();
if (editor.session.getTextRange(wrange) == text.substring(0, wrange.end.column - wrange.start.column)) {
editor.insert(text.substring(wrange.end.column - wrange.start.column));
}
else {
editor.session.replace(wrange, text);
}
}
function autocompleteOptions(arr) {
document.getElementById("autocomplete").innerHTML = arr.map(x => `<li>${x}</li>`).join("");
Expand Down

0 comments on commit d75c596

Please sign in to comment.