Merge pull request #124 from github/auto-indent-config

Auto-indent config
This commit is contained in:
Corey Johnson
2013-01-10 11:02:54 -08:00
7 changed files with 107 additions and 57 deletions

View File

@@ -33,11 +33,10 @@ class EditSession
anchorRanges: null
cursors: null
selections: null
autoIndent: false # TODO: re-enabled auto-indent after fixing the rest of tokenization
softTabs: true
softWrap: false
constructor: ({@project, @buffer, tabLength, @autoIndent, softTabs, @softWrap }) ->
constructor: ({@project, @buffer, tabLength, softTabs, @softWrap }) ->
@softTabs = @buffer.usesSoftTabs() ? softTabs ? true
@languageMode = new LanguageMode(this, @buffer.getExtension())
@displayBuffer = new DisplayBuffer(@buffer, { @languageMode, tabLength })
@@ -93,7 +92,6 @@ class EditSession
getScrollLeft: -> @scrollLeft
setSoftWrapColumn: (@softWrapColumn) -> @displayBuffer.setSoftWrapColumn(@softWrapColumn)
setAutoIndent: (@autoIndent) ->
setSoftTabs: (@softTabs) ->
getSoftWrap: -> @softWrap
@@ -159,18 +157,26 @@ class EditSession
getCursorScopes: -> @getCursor().getScopes()
logScreenLines: (start, end) -> @displayBuffer.logLines(start, end)
insertText: (text, options) ->
shouldAutoIndent: ->
config.get("editor.autoIndent")
shouldAutoIndentPastedText: ->
config.get("editor.autoIndentOnPaste")
insertText: (text, options={}) ->
options.autoIndent ?= @shouldAutoIndent()
@mutateSelectedText (selection) -> selection.insertText(text, options)
insertNewline: ->
@insertText('\n', autoIndent: true)
@insertText('\n')
insertNewlineBelow: ->
@moveCursorToEndOfLine()
@insertNewline()
indent: ->
@mutateSelectedText (selection) -> selection.indent()
indent: (options={})->
options.autoIndent ?= @shouldAutoIndent()
@mutateSelectedText (selection) -> selection.indent(options)
backspace: ->
@mutateSelectedText (selection) -> selection.backspace()
@@ -217,9 +223,14 @@ class EditSession
selection.copy(maintainPasteboard)
maintainPasteboard = true
pasteText: ->
pasteText: (options={}) ->
options.normalizeIndent ?= true
options.autoIndent ?= @shouldAutoIndentPastedText()
[text, metadata] = pasteboard.read()
@insertText(text, _.extend(metadata ? {}, normalizeIndent: true))
_.extend(options, metadata) if metadata
@insertText(text, options)
undo: ->
@buffer.undo(this)

View File

@@ -17,6 +17,8 @@ class Editor extends View
fontSize: 20
showInvisibles: false
autosave: false
autoIndent: true
autoIndentOnPaste: false
@content: (params) ->
@div class: @classes(params), tabindex: -1, =>
@@ -80,7 +82,6 @@ class Editor extends View
buffer: new Buffer()
softWrap: false
tabLength: 2
autoIndent: false
softTabs: true
@editSessions.push editSession
@@ -244,7 +245,7 @@ class Editor extends View
insertText: (text, options) -> @activeEditSession.insertText(text, options)
insertNewline: -> @activeEditSession.insertNewline()
insertNewlineBelow: -> @activeEditSession.insertNewlineBelow()
indent: -> @activeEditSession.indent()
indent: (options) -> @activeEditSession.indent(options)
indentSelectedRows: -> @activeEditSession.indentSelectedRows()
outdentSelectedRows: -> @activeEditSession.outdentSelectedRows()
cutSelection: -> @activeEditSession.cutSelectedText()
@@ -381,7 +382,7 @@ class Editor extends View
@selectOnMousemoveUntilMouseup()
@on "textInput", (e) =>
@insertText(e.originalEvent.data, autoIndent: true)
@insertText(e.originalEvent.data)
false
@scrollView.on 'mousewheel', (e) =>

View File

@@ -16,7 +16,6 @@ class Project
new Project(state.path, state.grammarOverridesByPath)
tabLength: 2
autoIndent: true
softTabs: true
softWrap: false
rootDirectory: null
@@ -93,9 +92,6 @@ class Project
relativize: (fullPath) ->
fullPath.replace(@getPath(), "").replace(/^\//, '')
getAutoIndent: -> @autoIndent
setAutoIndent: (@autoIndent) ->
getSoftTabs: -> @softTabs
setSoftTabs: (@softTabs) ->
@@ -116,7 +112,6 @@ class Project
defaultEditSessionOptions: ->
tabLength: @tabLength
autoIndent: @getAutoIndent()
softTabs: @getSoftTabs()
softWrap: @getSoftWrap()

View File

@@ -101,6 +101,10 @@ class RootView extends View
config.set("editor.showInvisibles", !config.get("editor.showInvisibles"))
@command 'window:toggle-ignored-files', =>
config.set("core.hideGitIgnoredFiles", not config.core.hideGitIgnoredFiles)
@command 'window:toggle-auto-indent', =>
config.set("editor.autoIndent", !config.get("editor.autoIndent"))
@command 'window:toggle-auto-indent-on-paste', =>
config.set("editor.autoIndentOnPaste", !config.get("editor.autoIndentOnPaste"))
afterAttach: (onDom) ->
@focus() if onDom

View File

@@ -179,13 +179,13 @@ class Selection
else
@cursor.setBufferPosition(newBufferRange.end, skipAtomicTokens: true) if wasReversed
if @editSession.autoIndent and options.autoIndent
if options.autoIndent
if text == '\n'
@editSession.autoIndentBufferRow(newBufferRange.end.row)
else
@editSession.autoDecreaseIndentForRow(newBufferRange.start.row)
indent: ->
indent: ({ autoIndent }={})->
{ row, column } = @cursor.getBufferPosition()
if @isEmpty()
@@ -193,7 +193,7 @@ class Selection
desiredIndent = @editSession.suggestedIndentForBufferRow(row)
delta = desiredIndent - @cursor.getIndentLevel()
if @editSession.autoIndent and delta > 0
if autoIndent and delta > 0
@insertText(@editSession.buildIndentString(delta))
else
@insertText(@editSession.getTabText())
@@ -221,7 +221,7 @@ class Selection
if insideExistingLine
desiredBasis = @editSession.indentationForBufferRow(currentBufferRow)
else if @editSession.autoIndent
else if options.autoIndent
desiredBasis = @editSession.suggestedIndentForBufferRow(currentBufferRow)
else
desiredBasis = @cursor.getIndentLevel()