Skip to content

Commit

Permalink
fix "< >" bug in snippet
Browse files Browse the repository at this point in the history
snippet without variables 'Enter' evetn listener
  • Loading branch information
staniska committed Mar 21, 2021
1 parent 222fa51 commit 690fbf3
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions lib/snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function snippetForm(snip, cursorScreenPosition, cursorPosition) {
snipForm.appendChild(snipForm.name)


if (snip.variables.length) {
if (snip.variables && snip.variables.length) {
snip.variables.forEach((snipVar, idx) => {
snipForm[snipVar.name] = document.createElement('div')
snipForm[snipVar.name].name = document.createElement('div')
Expand All @@ -247,6 +247,7 @@ function snippetForm(snip, cursorScreenPosition, cursorPosition) {
snipForm[snipVar.name].input.addEventListener('input', () => {
generateSnipBody(snip, snipForm)
})

snipForm[snipVar.name].input.addEventListener('keydown', (key) => {
if (key.key === 'Backspace') {
snipForm[snipVar.name].input.value = snipForm[snipVar.name].input.value.slice(0, -1)
Expand Down Expand Up @@ -316,12 +317,40 @@ function snippetForm(snip, cursorScreenPosition, cursorPosition) {
View.sinumerikView.snippetsDiv.input.focus()
View.sinumerikView.snipForm.destroy()
}

if (key.key === 'Enter' && !snip.variables) {
let Editor = atom.workspace.getActiveTextEditor()
Editor.setCursorScreenPosition(cursorPosition)

let text = snipForm.body.innerText.replace(/<br>/g , '\n')

Editor.insertText(text)
atom.workspace.getPanes()[0].activate()


let sinumerikSnippetDiv = document.getElementsByClassName('sinumerikSnippetsMenu')

let snippetDivParent = sinumerikSnippetDiv[0].parentElement
if (snippetDivParent) {
while (sinumerikSnippetDiv[0].children.length) {
sinumerikSnippetDiv[0].removeChild(sinumerikSnippetDiv[0].lastChild)
}
snippetDivParent.removeChild(snippetDivParent.lastChild)
}
if (View.sinumerikView.snipForm) {
View.sinumerikView.snipForm.destroy()
}

View.sinumerikView.snipForm.destroy()
View.sinumerikView.snippetsTopPanel.destroy()
}

})



View.sinumerikView.snipForm = atom.workspace.addTopPanel({item: snipForm})
if (snip.variables.length) {
if (snip.variables && snip.variables.length) {
snipForm[snip.variables[0].name].input.focus()
} else {
snipForm.focus()
Expand Down Expand Up @@ -354,22 +383,28 @@ function generateSnipBody(snip, snipForm) {
expression.match(/(?<=[{])\w+(?=})/g).forEach((varInExpr) => {
let regExp = new RegExp(`{${varInExpr}}`, 'g')
if (snipForm[varInExpr] !== undefined) {
expression = expression.replace(`{${varInExpr}}`, snipForm[varInExpr].input.value)
expression = expression.replace(regExp, snipForm[varInExpr].input.value)
}
})
}
let expressionResult = ''
try {
expressionResult = eval(expression)
str = str.replace(`{${varInStr}}`, expressionResult)
let replaceRegEx = new RegExp(`{${varInStr}}`, 'g')
str = str.replace(replaceRegEx, expressionResult)
} catch (e) {
console.log('Snip expression: ' + expression)
}
}
}
)
}
let regEx = new RegExp('<', 'g')
str = str.replace(regEx,'&lt')
regEx = new RegExp('>', 'g')
str = str.replace(regEx,'&gt')

snipForm.body.innerHTML += '<pre>' + str + '</pre>'

})
}

Expand Down

0 comments on commit 690fbf3

Please sign in to comment.