diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index a2ade3753..d50279543 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -2925,6 +2925,17 @@ describe "TextEditor", -> expect(editor.lineTextForBufferRow(0)).toBe "var first = function () {" expect(editor.lineTextForBufferRow(1)).toBe " var first = function(items) {" + it "notifies ::onWillInsertText observers", -> + insertedStrings = [] + editor.onWillInsertText ({text, cancel}) -> + insertedStrings.push(text) + cancel() + + atom.clipboard.write("hello") + editor.pasteText() + + expect(insertedStrings).toEqual ["hello"] + describe "when `autoIndentOnPaste` is true", -> beforeEach -> atom.config.set("editor.autoIndentOnPaste", true) diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 3b8653e3f..152a70ab0 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -772,31 +772,24 @@ class TextEditor extends Model # Returns a {Range} when the text has been inserted # Returns a {Boolean} false when the text has not been inserted insertText: (text, options={}) -> - willInsert = true - cancel = -> willInsert = false - willInsertEvent = {cancel, text} - @emit('will-insert-text', willInsertEvent) if includeDeprecatedAPIs - @emitter.emit 'will-insert-text', willInsertEvent + return false unless @emitWillInsertTextEvent(text) groupingInterval = if options.groupUndo atom.config.get('editor.undoGroupingInterval') else 0 - if willInsert - options.autoIndentNewline ?= @shouldAutoIndent() - options.autoDecreaseIndent ?= @shouldAutoIndent() - @mutateSelectedText( - (selection) => - range = selection.insertText(text, options) - didInsertEvent = {text, range} - @emit('did-insert-text', didInsertEvent) if includeDeprecatedAPIs - @emitter.emit 'did-insert-text', didInsertEvent - range - , groupingInterval - ) - else - false + options.autoIndentNewline ?= @shouldAutoIndent() + options.autoDecreaseIndent ?= @shouldAutoIndent() + @mutateSelectedText( + (selection) => + range = selection.insertText(text, options) + didInsertEvent = {text, range} + @emit('did-insert-text', didInsertEvent) if includeDeprecatedAPIs + @emitter.emit 'did-insert-text', didInsertEvent + range + , groupingInterval + ) # Essential: For each selection, replace the selected text with a newline. insertNewline: -> @@ -2602,6 +2595,8 @@ class TextEditor extends Model # * `options` (optional) See {Selection::insertText}. pasteText: (options={}) -> {text: clipboardText, metadata} = atom.clipboard.readWithMetadata() + return false unless @emitWillInsertTextEvent(clipboardText) + metadata ?= {} options.autoIndent = @shouldAutoIndentOnPaste() @@ -2977,6 +2972,14 @@ class TextEditor extends Model logScreenLines: (start, end) -> @displayBuffer.logLines(start, end) + emitWillInsertTextEvent: (text) -> + result = true + cancel = -> result = false + willInsertEvent = {cancel, text} + @emit('will-insert-text', willInsertEvent) if includeDeprecatedAPIs + @emitter.emit 'will-insert-text', willInsertEvent + result + if includeDeprecatedAPIs TextEditor.delegatesProperties '$lineHeightInPixels', '$defaultCharWidth', '$height', '$width', '$verticalScrollbarWidth', '$horizontalScrollbarHeight', '$scrollTop', '$scrollLeft',