mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-09-22 00:19:24 +03:00
Snippets now advise the insertTab method on EditSessions instead of intercepting events on Editor
This commit is contained in:
parent
9c02e05051
commit
1bec4c8404
@ -9,9 +9,9 @@ describe "Snippets extension", ->
|
||||
[buffer, editor] = []
|
||||
beforeEach ->
|
||||
rootView = new RootView(require.resolve('fixtures/sample.js'))
|
||||
rootView.activateExtension(Snippets)
|
||||
editor = rootView.activeEditor()
|
||||
buffer = editor.buffer
|
||||
rootView.activateExtension(Snippets)
|
||||
rootView.simulateDomAttachment()
|
||||
|
||||
describe "when 'tab' is triggered on the editor", ->
|
||||
|
@ -1,5 +1,6 @@
|
||||
fs = require 'fs'
|
||||
PEG = require 'pegjs'
|
||||
_ = require 'underscore'
|
||||
|
||||
module.exports =
|
||||
class Snippets
|
||||
@ -8,7 +9,12 @@ module.exports =
|
||||
|
||||
@activate: (@rootView) ->
|
||||
@loadSnippets()
|
||||
rootView.on 'editor-open', (e, editor) => new Snippets(editor)
|
||||
|
||||
project = rootView.project
|
||||
|
||||
new Snippets(editSession) for editSession in project.editSessions
|
||||
project.on 'new-edit-session', (editSession) => new Snippets(editSession)
|
||||
|
||||
|
||||
@loadSnippets: ->
|
||||
snippetsDir = fs.join(atom.configDirPath, 'snippets')
|
||||
@ -22,14 +28,13 @@ module.exports =
|
||||
@evalSnippets: (extension, text) ->
|
||||
@snippetsByExtension[extension] = @snippetsParser.parse(text)
|
||||
|
||||
constructor: (@editor) ->
|
||||
@editor.preempt 'tab', => return false if @expandSnippet()
|
||||
constructor: (@editSession) ->
|
||||
_.adviseBefore @editSession, 'insertTab', => @expandSnippet()
|
||||
|
||||
expandSnippet: ->
|
||||
editSession = @editor.activeEditSession
|
||||
return unless snippets = @constructor.snippetsByExtension[editSession.buffer.getExtension()]
|
||||
prefix = editSession.getLastCursor().getCurrentWordPrefix()
|
||||
return unless snippets = @constructor.snippetsByExtension[@editSession.buffer.getExtension()]
|
||||
prefix = @editSession.getLastCursor().getCurrentWordPrefix()
|
||||
if body = snippets[prefix]?.body
|
||||
editSession.selectToBeginningOfWord()
|
||||
editSession.insertText(body)
|
||||
true
|
||||
@editSession.selectToBeginningOfWord()
|
||||
@editSession.insertText(body)
|
||||
false
|
||||
|
Loading…
Reference in New Issue
Block a user