Refactor Ace specific code into its own adapter class.

This commit is contained in:
Corey Johnson
2012-03-06 16:16:49 -08:00
parent f00632a96e
commit fe07e45ebc
3 changed files with 37 additions and 25 deletions

View File

@@ -0,0 +1,18 @@
Range = require 'range'
module.exports =
class AceOutdentAdaptor
constructor: (@buffer) ->
getLine: (row) ->
@buffer.getLine(row)
# We don't care where the bracket is; we always outdent one level
findMatchingBracket: ({row, column}) ->
{row: 0, column: 0}
# Does not actually replace text, just line at range.start outdents one level
replace: (range, text) ->
start = range.start
end = {row: range.start.row, column: range.start.column + atom.tabText.length}
@buffer.change(new Range(start, end), "")

View File

@@ -105,14 +105,4 @@ class Buffer
@mode = new (require("ace/mode/#{modeName}").Mode)
# API to match Ace's Document class
findMatchingBracket: ({row, column}) ->
{row: 0, column: 0}
replace:(range, text) ->
# Only used to outdent lines
start = range.start
end = {row: range.start.row, column: range.start.column + atom.tabText.length}
@change(new Range(start, end), "")
_.extend(Buffer.prototype, EventEmitter)

View File

@@ -1,4 +1,5 @@
{View, $$} = require 'space-pen'
AceOutdentAdaptor = require 'ace-outdent-adaptor'
Buffer = require 'buffer'
Point = require 'point'
Cursor = require 'cursor'
@@ -33,7 +34,6 @@ class Editor extends View
undoManager: null
autoIndent: null
initialize: () ->
requireStylesheet 'editor.css'
requireStylesheet 'theme/twilight.css'
@@ -304,23 +304,27 @@ class Editor extends View
@selection.selectToBufferPosition(position)
insertText: (text) ->
unless @autoIndent
@selection.insertText(text)
return
state = @lineWrapper.lineForScreenRow(@getCursorRow()).state
shouldOutdent = false
if text[0] == "\n"
indent = @buffer.mode.getNextLineIndent(state, @getCurrentLine(), atom.tabText)
text = text[0] + indent + text[1..]
else if @buffer.mode.checkOutdent(state, @getCurrentLine(), text)
shouldOutdent = true
{ text, shouldOutdent } = @autoIndentText(text)
@selection.insertText(text)
if shouldOutdent
@buffer.mode.autoOutdent(state, @buffer, @getCursorRow())
@autoOutdentText() if shouldOutdent
autoIndentText: (text) ->
if @autoIndent
state = @lineWrapper.lineForScreenRow(@getCursorRow()).state
if text[0] == "\n"
indent = @buffer.mode.getNextLineIndent(state, @getCurrentLine(), atom.tabText)
text = text[0] + indent + text[1..]
else if @buffer.mode.checkOutdent(state, @getCurrentLine(), text)
shouldOutdent = true
{text, shouldOutdent}
autoOutdentText: ->
state = @lineWrapper.lineForScreenRow(@getCursorRow()).state
@buffer.mode.autoOutdent(state, new AceOutdentAdaptor(@buffer), @getCursorRow())
cutSelection: -> @selection.cut()
copySelection: -> @selection.copy()