Merge remote-tracking branch 'origin/master' into gutter

Conflicts:
	src/atom/highlighter.coffee
This commit is contained in:
Nathan Sobo
2012-03-07 10:42:21 -07:00
12 changed files with 104 additions and 26 deletions

View File

@@ -0,0 +1,21 @@
Range = require 'range'
module.exports =
class AceOutdentAdaptor
constructor: (@buffer, @editor) ->
getLine: (row) ->
@buffer.lineForRow(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) ->
{row, column} = @editor.getCursorBufferPosition()
start = range.start
end = {row: range.start.row, column: range.start.column + atom.tabText.length}
@buffer.change(new Range(start, end), "")
@editor.setCursorBufferPosition({row, column: column - atom.tabText.length})

View File

@@ -6,15 +6,17 @@ module.exports =
class App
keymap: null
windows: null
tabText: null
constructor: (@loadPath, nativeMethods)->
@windows = []
@setupKeymap()
@tabText = " "
setupKeymap: ->
@keymap = new GlobalKeymap()
$(document).on 'keydown', (e) => @keymap.handleKeyEvent(e)
@keymap.bindDefaultKeys()
open: (url) ->
$native.open url

View File

@@ -95,9 +95,10 @@ class Buffer
if not @path then throw new Error("Tried to save buffer with no url")
fs.write @path, @getText()
modeName: ->
getMode: ->
return @mode if @mode
extension = if @path then @path.split('/').pop().split('.').pop() else null
switch extension
modeName = switch extension
when 'js' then 'javascript'
when 'coffee' then 'coffee'
when 'rb', 'ru' then 'ruby'
@@ -106,4 +107,6 @@ class Buffer
when 'css' then 'css'
else 'text'
@mode = new (require("ace/mode/#{modeName}").Mode)
_.extend(Buffer.prototype, EventEmitter)

View File

@@ -1,4 +1,5 @@
{View, $$} = require 'space-pen'
AceOutdentAdaptor = require 'ace-outdent-adaptor'
Buffer = require 'buffer'
Cursor = require 'cursor'
Gutter = require 'gutter'
@@ -32,6 +33,7 @@ class Editor extends View
highlighter: null
renderer: null
undoManager: null
autoIndent: null
initialize: () ->
requireStylesheet 'editor.css'
@@ -40,6 +42,7 @@ class Editor extends View
@buildCursorAndSelection()
@handleEvents()
@setBuffer(new Buffer)
@autoIndent = true
bindKeys: ->
window.keymap.bindKeys '*:not(.editor *)',
@@ -70,7 +73,7 @@ class Editor extends View
@on 'select-left', => @selectLeft()
@on 'select-up', => @selectUp()
@on 'select-down', => @selectDown()
@on 'newline', => @insertNewline()
@on 'newline', => @insertText("\n")
@on 'backspace', => @backspace()
@on 'delete', => @delete()
@on 'cut', => @cutSelection()
@@ -297,12 +300,34 @@ class Editor extends View
selectToBufferPosition: (position) ->
@selection.selectToBufferPosition(position)
insertText: (text) -> @selection.insertText(text)
insertNewline: -> @selection.insertNewline()
insertText: (text) ->
{ text, shouldOutdent } = @autoIndentText(text)
@selection.insertText(text)
@autoOutdentText() if shouldOutdent
autoIndentText: (text) ->
if @autoIndent
state = @renderer.lineForRow(@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
console.log text
{text, shouldOutdent}
autoOutdentText: ->
state = @renderer.lineForRow(@getCursorRow()).state
@buffer.mode.autoOutdent(state, new AceOutdentAdaptor(@buffer, this), @getCursorRow())
cutSelection: -> @selection.cut()
copySelection: -> @selection.copy()
paste: -> @selection.insertText($native.readFromPasteboard())
paste: -> @insertText($native.readFromPasteboard())
foldSelection: -> @selection.fold()

View File

@@ -9,15 +9,15 @@ class GlobalKeymap
constructor: ->
@bindingSets = []
bindDefaultKeys: ->
@bindKeys "*",
'meta-n': 'newWindow'
'meta-o': 'open'
$(document).on 'newWindow', => $native.newWindow()
$(document).on 'open', =>
$(document).on 'open', =>
url = $native.openDialog()
atom.open(url) if url
bindKeys: (selector, bindings) ->
@bindingSets.unshift(new BindingSet(selector, bindings))
@@ -29,7 +29,7 @@ class GlobalKeymap
handleKeyEvent: (event) ->
event.keystroke = @keystrokeStringForEvent(event)
currentNode = $(event.target)
currentNode = $(event.target)
while currentNode.length
candidateBindingSets = @bindingSets.filter (set) -> currentNode.is(set.selector)
candidateBindingSets.sort (a, b) -> b.specificity - a.specificity

View File

@@ -5,18 +5,12 @@ EventEmitter = require 'event-emitter'
module.exports =
class Highlighter
buffer: null
tokenizer: null
screenLines: []
constructor: (@buffer) ->
@buildTokenizer()
@screenLines = @buildLinesForScreenRows('start', 0, @buffer.lastRow())
@buffer.on 'change', (e) => @handleBufferChange(e)
buildTokenizer: ->
Mode = require("ace/mode/#{@buffer.modeName()}").Mode
@tokenizer = (new Mode).getTokenizer()
handleBufferChange: (e) ->
oldRange = e.oldRange.copy()
newRange = e.newRange.copy()
@@ -56,8 +50,9 @@ class Highlighter
screenLine
buildLineForScreenRow: (state, row) ->
tokenizer = @buffer.getMode().getTokenizer()
line = @buffer.lineForRow(row)
{tokens, state} = @tokenizer.getLineTokens(line, state)
{tokens, state} = tokenizer.getLineTokens(line, state)
new ScreenLineFragment(tokens, line, [1, 0], [1, 0], { state })
lineForScreenRow: (row) ->

View File

@@ -81,9 +81,6 @@ class Selection extends View
insertText: (text) ->
@editor.buffer.change(@getBufferRange(), text)
insertNewline: ->
@insertText('\n')
delete: ->
range = @getBufferRange()
@editor.buffer.change(range, '') unless range.isEmpty()

View File

@@ -17,11 +17,11 @@ windowAdditions =
startup: (url) ->
@setupKeymap()
@attachRootView(url)
$(window).on 'close', =>
$(window).on 'close', =>
@shutdown()
@close()
$(window).focus()
atom.windowOpened this
@@ -33,7 +33,7 @@ windowAdditions =
setupKeymap: ->
@keymap = new GlobalKeymap()
@keymap.bindDefaultKeys()
$(document).on 'keydown', (e) => @keymap.handleKeyEvent(e)
attachRootView: (url) ->