mirror of
https://github.com/atom/atom.git
synced 2026-01-25 06:48:28 -05:00
Snippets matching the cursor's word prefix are inserted on 'tab' events
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
Snippets = require 'snippets'
|
||||
RootView = require 'root-view'
|
||||
Buffer = require 'buffer'
|
||||
Editor = require 'editor'
|
||||
_ = require 'underscore'
|
||||
@@ -6,8 +7,11 @@ _ = require 'underscore'
|
||||
fdescribe "Snippets extension", ->
|
||||
[buffer, editor] = []
|
||||
beforeEach ->
|
||||
buffer = new Buffer(require.resolve('fixtures/sample.js'))
|
||||
editor = new Editor({buffer})
|
||||
rootView = new RootView(require.resolve('fixtures/sample.js'))
|
||||
editor = rootView.activeEditor()
|
||||
buffer = editor.buffer
|
||||
rootView.activateExtension(Snippets)
|
||||
rootView.simulateDomAttachment()
|
||||
|
||||
describe "when 'tab' is triggered on the editor", ->
|
||||
describe "when the letters preceding the cursor are registered as a global extension", ->
|
||||
@@ -16,19 +20,15 @@ fdescribe "Snippets extension", ->
|
||||
snippet te "Test snippet description"
|
||||
this is a test
|
||||
endsnippet
|
||||
|
||||
snippet moo "Moo snippet"
|
||||
Mooooooo!
|
||||
endsnippet
|
||||
"""
|
||||
|
||||
editor.insertText("te")
|
||||
editor.trigger 'tab'
|
||||
|
||||
expect(editor.getCursorScreenPosition()).toEqual [0, 2]
|
||||
expect(buffer.lineForRow(0)).toBe "this is a testvar quicksort = function () {"
|
||||
|
||||
ffdescribe "Snippets parser", ->
|
||||
editor.trigger 'tab'
|
||||
expect(buffer.lineForRow(0)).toBe "this is a testvar quicksort = function () {"
|
||||
expect(editor.getCursorScreenPosition()).toEqual [0, 14]
|
||||
|
||||
describe "Snippets parser", ->
|
||||
it "can parse a snippet", ->
|
||||
snippets = Snippets.snippetsParser.parse """
|
||||
snippet te "Test snippet description"
|
||||
|
||||
@@ -32,6 +32,12 @@ class Buffer
|
||||
getPath: ->
|
||||
@path
|
||||
|
||||
getExtension: ->
|
||||
if @getPath()
|
||||
@getPath().split('/').pop().split('.').pop()
|
||||
else
|
||||
null
|
||||
|
||||
setPath: (path) ->
|
||||
@path = path
|
||||
@trigger "path-change", this
|
||||
|
||||
@@ -155,6 +155,9 @@ class Cursor
|
||||
getCurrentLineBufferRange: ->
|
||||
@editSession.bufferRangeForBufferRow(@getBufferRow())
|
||||
|
||||
getCurrentWordPrefix: ->
|
||||
@editSession.getTextInBufferRange([@getBeginningOfCurrentWordBufferPosition(), @getBufferPosition()])
|
||||
|
||||
isAtBeginningOfLine: ->
|
||||
@getBufferPosition().column == 0
|
||||
|
||||
|
||||
@@ -329,6 +329,9 @@ class EditSession
|
||||
getSelectedText: ->
|
||||
@getLastSelection().getText()
|
||||
|
||||
getTextInBufferRange: (range) ->
|
||||
@buffer.getTextInRange(range)
|
||||
|
||||
moveCursorUp: ->
|
||||
@moveCursors (cursor) -> cursor.moveUp()
|
||||
|
||||
|
||||
@@ -23,8 +23,7 @@ class TokenizedBuffer
|
||||
@aceAdaptor = new AceAdaptor(this)
|
||||
|
||||
requireAceMode: ->
|
||||
extension = if @buffer.getPath() then @buffer.getPath().split('/').pop().split('.').pop() else null
|
||||
modeName = switch extension
|
||||
modeName = switch @buffer.getExtension()
|
||||
when 'js' then 'javascript'
|
||||
when 'coffee' then 'coffee'
|
||||
when 'rb', 'ru' then 'ruby'
|
||||
|
||||
@@ -2,9 +2,23 @@ fs = require 'fs'
|
||||
PEG = require 'pegjs'
|
||||
|
||||
module.exports =
|
||||
name: 'Snippets'
|
||||
snippetsByExtension: {}
|
||||
snippetsParser: PEG.buildParser(fs.read(require.resolve 'extensions/snippets/snippets.pegjs'))
|
||||
|
||||
evalSnippets: (extension, text) ->
|
||||
@snippetsByExtension[extension] = snippetsParser.parse(text)
|
||||
activate: (@rootView) ->
|
||||
rootView.on 'editor-open', (e, editor) =>
|
||||
editor.preempt 'tab', =>
|
||||
return false if @expandSnippet()
|
||||
|
||||
evalSnippets: (extension, text) ->
|
||||
@snippetsByExtension[extension] = @snippetsParser.parse(text)
|
||||
|
||||
expandSnippet: ->
|
||||
editSession = @rootView.activeEditor().activeEditSession
|
||||
return unless snippets = @snippetsByExtension[editSession.buffer.getExtension()]
|
||||
prefix = editSession.getLastCursor().getCurrentWordPrefix()
|
||||
if body = snippets[prefix]?.body
|
||||
editSession.selectToBeginningOfWord()
|
||||
editSession.insertText(body)
|
||||
true
|
||||
|
||||
Reference in New Issue
Block a user