Move handling of nonWordCharacters setting into TextEditorRegistry

This commit is contained in:
Max Brunsfeld
2016-07-12 11:30:41 -07:00
parent 5167dbca6f
commit 58ec44f8ec
5 changed files with 78 additions and 26 deletions

View File

@@ -18,7 +18,7 @@ class Cursor extends Model
visible: true
# Instantiated by a {TextEditor}
constructor: ({@editor, @marker, @config, id}) ->
constructor: ({@editor, @marker, id}) ->
@emitter = new Emitter
@assignId(id)
@@ -160,8 +160,8 @@ class Cursor extends Model
[before, after] = @editor.getTextInBufferRange(range)
return false if /\s/.test(before) or /\s/.test(after)
nonWordCharacters = @config.get('editor.nonWordCharacters', scope: @getScopeDescriptor()).split('')
_.contains(nonWordCharacters, before) isnt _.contains(nonWordCharacters, after)
nonWordCharacters = @getNonWordCharacters()
nonWordCharacters.includes(before) isnt nonWordCharacters.includes(after)
# Public: Returns whether this cursor is between a word's start and end.
#
@@ -608,9 +608,7 @@ class Cursor extends Model
#
# Returns a {RegExp}.
wordRegExp: (options) ->
scope = @getScopeDescriptor()
nonWordCharacters = _.escapeRegExp(@config.get('editor.nonWordCharacters', {scope}))
nonWordCharacters = _.escapeRegExp(@getNonWordCharacters())
source = "^[\t ]*$|[^\\s#{nonWordCharacters}]+"
if options?.includeNonWordCharacters ? true
source += "|" + "[#{nonWordCharacters}]+"
@@ -624,7 +622,7 @@ class Cursor extends Model
#
# Returns a {RegExp}.
subwordRegExp: (options={}) ->
nonWordCharacters = @config.get('editor.nonWordCharacters', scope: @getScopeDescriptor())
nonWordCharacters = @getNonWordCharacters()
lowercaseLetters = 'a-z\\u00DF-\\u00F6\\u00F8-\\u00FF'
uppercaseLetters = 'A-Z\\u00C0-\\u00D6\\u00D8-\\u00DE'
snakeCamelSegment = "[#{uppercaseLetters}]?[#{lowercaseLetters}]+"
@@ -647,6 +645,14 @@ class Cursor extends Model
Section: Private
###
getNonWordCharacters: ->
(
@editor
.scopedSettingsDelegate
?.getNonWordCharacters?(@getScopeDescriptor().getScopesArray()) ?
@editor.getNonWordCharacters()
)
changePosition: (options, fn) ->
@clearSelection(autoscroll: false)
fn()

View File

@@ -18,6 +18,7 @@ const EDITOR_SETTER_NAMES_BY_SETTING_KEY = [
['editor.autoIndentOnPaste', 'setAutoIndentOnPaste'],
['editor.scrollPastEnd', 'setScrollPastEnd'],
['editor.undoGroupingInterval', 'setUndoGroupingInterval'],
['editor.nonWordCharacters', 'setNonWordCharacters'],
]
// Experimental: This global registry tracks registered `TextEditors`.
@@ -39,6 +40,7 @@ export default class TextEditorRegistry {
this.emitter = new Emitter()
this.scopesWithConfigSubscriptions = new Set()
this.editorsWithMaintainedConfig = new Set()
this.scopedSettingsDelegate = new ScopedSettingsDelegate(config)
}
destroy () {
@@ -90,6 +92,7 @@ export default class TextEditorRegistry {
maintainConfig (editor) {
this.editorsWithMaintainedConfig.add(editor)
this.subscribeToSettingsForEditorScope(editor)
editor.setScopedSettingsDelegate(this.scopedSettingsDelegate)
const configOptions = {scope: editor.getRootScopeDescriptor()}
for (const [settingKey, setterName] of EDITOR_SETTER_NAMES_BY_SETTING_KEY) {
@@ -165,3 +168,13 @@ function shouldEditorUseSoftTabs (editor, tabType, softTabs) {
}
}
}
class ScopedSettingsDelegate {
constructor (config) {
this.config = config
}
getNonWordCharacters(scope) {
return this.config.get('editor.nonWordCharacters', {scope: scope})
}
}

View File

@@ -154,6 +154,7 @@ class TextEditor extends Model
@autoIndent ?= true
@autoIndentOnPaste ?= true
@undoGroupingInterval ?= 300
@nonWordCharacters ?= "/\\()\"':,.;<>~!@#$%^&*|+=[]{}`?-…"
@buffer ?= new TextBuffer
@tokenizedBuffer ?= new TokenizedBuffer({
@@ -2094,7 +2095,7 @@ class TextEditor extends Model
# Add a cursor based on the given {DisplayMarker}.
addCursor: (marker) ->
cursor = new Cursor(editor: this, marker: marker, config: @config)
cursor = new Cursor(editor: this, marker: marker)
@cursors.push(cursor)
@cursorsByMarkerId.set(marker.id, cursor)
@decorateMarker(marker, type: 'line-number', class: 'cursor-line')
@@ -3328,6 +3329,10 @@ class TextEditor extends Model
Section: Config
###
setScopedSettingsDelegate: (@scopedSettingsDelegate) ->
getScopedSettingsDelegate: -> @scopedSettingsDelegate
setAutoIndent: (@autoIndent) ->
setAutoIndentOnPaste: (@autoIndentOnPaste) ->
@@ -3344,6 +3349,10 @@ class TextEditor extends Model
getUndoGroupingInterval: -> @undoGroupingInterval
setNonWordCharacters: (@nonWordCharacters) ->
getNonWordCharacters: -> @nonWordCharacters
###
Section: Event Handlers
###