Skip to content

Commit

Permalink
Improved autocomplete hints
Browse files Browse the repository at this point in the history
  • Loading branch information
bornova authored Jun 24, 2024
1 parent d172857 commit 0849300
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,34 +147,31 @@ Object.getOwnPropertyNames(math).forEach((f) => {
})

Object.keys(formulajs).forEach((f) => {
numaraHints.push({ text: f, className: 'cm-excel' })
numaraHints.push({ text: 'xls.' + f, className: 'cm-excel' })
})

CodeMirror.commands.autocomplete = (cm) => {
CodeMirror.showHint(cm, CodeMirror.hint.numaraHints, {
completeSingle: false,
extraKeys: { Enter: 'newline' }
})
}

CodeMirror.registerHelper('hint', 'numaraHints', (editor) => {
const cmCursor = editor.getCursor()
const cmCursorLine = editor.getLine(cmCursor.line)

let start = cmCursor.ch
let end = start

while (end < cmCursorLine.length && /[\w$]/.test(cmCursorLine.charAt(end))) {
while (end < cmCursorLine.length && /[\w.$]/.test(cmCursorLine.charAt(end))) {
++end
}

while (start && /[\w$]/.test(cmCursorLine.charAt(start - 1))) {
while (start && /[\w.$]/.test(cmCursorLine.charAt(start - 1))) {
--start
}

const curWord = start !== end && cmCursorLine.slice(start, end)
let curStr = cmCursorLine.slice(start, end)
let curWord = start !== end && curStr

const curWordRegex = new RegExp('^' + curWord, 'i')

curWord = !curStr.endsWith('.') || curStr === 'xls.'

return {
list: !curWord
? []
Expand All @@ -184,6 +181,13 @@ CodeMirror.registerHelper('hint', 'numaraHints', (editor) => {
}
})

CodeMirror.commands.autocomplete = (cm) => {
CodeMirror.showHint(cm, CodeMirror.hint.numaraHints, {
completeSingle: false,
extraKeys: { Enter: 'newline', Tab: () => {} }
})
}

// Codemirror handlers
cm.on('changes', calculate)

Expand Down

0 comments on commit 0849300

Please sign in to comment.