Skip to content

Commit

Permalink
fix: [ultisnips] script type detection should be correct in line's end
Browse files Browse the repository at this point in the history
  • Loading branch information
hikerpig committed Feb 4, 2020
1 parent af29aab commit a8f6e6b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
8 changes: 8 additions & 0 deletions packages/ultisnips/__tests__/index.spec.ts
Expand Up @@ -116,6 +116,14 @@ describe('parse ultisnips', () => {
code: 'date',
},
})

const defNewLine = definitions.find(def => def.trigger === 'test_newline')
expect(defNewLine.placeholders[0]).toMatchObject<PartialPlaceholder>({
scriptInfo: {
scriptType: 'js',
code: 'new Date()',
},
})
})
})
})
Expand Down
14 changes: 10 additions & 4 deletions packages/ultisnips/src/parse/tokenizer.ts
Expand Up @@ -446,16 +446,16 @@ export class ScriptCodeToken extends Token {
iter.next() // `
const nextChars = iter.peek(5)
let scriptType: ScriptType
if (nextChars.substr(0, 2) === '! ') {
if (this.containsWord(nextChars, '!')) {
scriptType = 'shell'
iter.next(2)
} else if (nextChars.substr(0, 3) === '!p ') {
} else if (this.containsWord(nextChars, '!p')) {
scriptType = 'python'
iter.next(3)
} else if (nextChars.substr(0, 3) === '!v ') {
} else if (this.containsWord(nextChars, '!v')) {
scriptType = 'vim'
iter.next(3)
} else if (nextChars.substr(0, 4) === '!js ') {
} else if (this.containsWord(nextChars, '!js')) {
scriptType = 'js'
iter.next(4)
}
Expand All @@ -465,6 +465,12 @@ export class ScriptCodeToken extends Token {
this.scriptCode = content
}
}

protected containsWord(chars: string, word: string, validSeperator = [' ', '\n']) {
const charsToCheck = chars.substr(0, word.length + 1)
const lastChar = charsToCheck[charsToCheck.length - 1]
return charsToCheck.startsWith(word) && validSeperator.includes(lastChar)
}
}

export class EndOfTextToken extends Token {
Expand Down
6 changes: 6 additions & 0 deletions tools/test-tool/snippets/ultisnips/scripts.snippets
Expand Up @@ -13,3 +13,9 @@ endsnippet
snippet test_js "javascript code"
should print date: `!js new Date()`
endsnippet

# should not contain \n
snippet test_newline "line ends with !js"
`!js
new Date()`
endsnippet

0 comments on commit a8f6e6b

Please sign in to comment.