Skip to content

Commit

Permalink
Merge pull request atom#223 from deprint/scoped-settings
Browse files Browse the repository at this point in the history
Allow scoped settings
  • Loading branch information
as-cii committed Mar 31, 2016
2 parents f55f660 + 58a029f commit 2d8b7ed
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
24 changes: 16 additions & 8 deletions lib/bracket-matcher.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class BracketMatcher
@subscriptions.add atom.commands.add editorElement, 'bracket-matcher:remove-brackets-from-selection', (event) =>
event.abortKeyBinding() unless @removeBrackets()

@subscriptions.add atom.config.observe 'bracket-matcher.autocompleteSmartQuotes', (newValue) =>
@toggleQuotes(newValue)

@subscriptions.add @editor.onDidDestroy => @unsubscribe()

insertText: (text, options) =>
return true unless text
return true if options?.select or options?.undo is 'skip'

@toggleQuotes(@getScopedSetting('bracket-matcher.autocompleteSmartQuotes'))

return false if @wrapSelectionInBrackets(text)
return true if @editor.hasMultipleCursors()

Expand All @@ -63,11 +63,11 @@ class BracketMatcher
hasEscapeSequenceBeforeCursor = previousCharacters.match(/\\/g)?.length >= 1 # To guard against the "\\" sequence

if text is '#' and @isCursorOnInterpolatedString()
autoCompleteOpeningBracket = atom.config.get('bracket-matcher.autocompleteBrackets') and not hasEscapeSequenceBeforeCursor
autoCompleteOpeningBracket = @getScopedSetting('bracket-matcher.autocompleteBrackets') and not hasEscapeSequenceBeforeCursor
text += '{'
pair = '}'
else
autoCompleteOpeningBracket = atom.config.get('bracket-matcher.autocompleteBrackets') and @isOpeningBracket(text) and not hasWordAfterCursor and not (@isQuote(text) and (hasWordBeforeCursor or hasQuoteBeforeCursor)) and not hasEscapeSequenceBeforeCursor
autoCompleteOpeningBracket = @getScopedSetting('bracket-matcher.autocompleteBrackets') and @isOpeningBracket(text) and not hasWordAfterCursor and not (@isQuote(text) and (hasWordBeforeCursor or hasQuoteBeforeCursor)) and not hasEscapeSequenceBeforeCursor
pair = @pairedCharacters[text]

skipOverExistingClosingBracket = false
Expand All @@ -91,6 +91,8 @@ class BracketMatcher
return if @editor.hasMultipleCursors()
return unless @editor.getLastSelection().isEmpty()

@toggleQuotes(@getScopedSetting('bracket-matcher.autocompleteSmartQuotes'))

cursorBufferPosition = @editor.getCursorBufferPosition()
previousCharacters = @editor.getTextInBufferRange([cursorBufferPosition.traverse([0, -2]), cursorBufferPosition])
nextCharacter = @editor.getTextInBufferRange([cursorBufferPosition, cursorBufferPosition.traverse([0, 1])])
Expand All @@ -102,7 +104,7 @@ class BracketMatcher
@editor.transact =>
@editor.insertText "\n\n"
@editor.moveUp()
if atom.config.get('editor.autoIndent')
if @getScopedSetting('editor.autoIndent')
cursorRow = @editor.getCursorBufferPosition().row
@editor.autoIndentBufferRows(cursorRow, cursorRow + 1)
false
Expand All @@ -111,14 +113,16 @@ class BracketMatcher
return if @editor.hasMultipleCursors()
return unless @editor.getLastSelection().isEmpty()

@toggleQuotes(@getScopedSetting('bracket-matcher.autocompleteSmartQuotes'))

cursorBufferPosition = @editor.getCursorBufferPosition()
previousCharacters = @editor.getTextInBufferRange([cursorBufferPosition.traverse([0, -2]), cursorBufferPosition])
nextCharacter = @editor.getTextInBufferRange([cursorBufferPosition, cursorBufferPosition.traverse([0, 1])])

previousCharacter = previousCharacters.slice(-1)

hasEscapeSequenceBeforeCursor = previousCharacters.match(/\\/g)?.length >= 1 # To guard against the "\\" sequence
if (@pairedCharacters[previousCharacter] is nextCharacter) and not hasEscapeSequenceBeforeCursor and atom.config.get('bracket-matcher.autocompleteBrackets')
if (@pairedCharacters[previousCharacter] is nextCharacter) and not hasEscapeSequenceBeforeCursor and @getScopedSetting('bracket-matcher.autocompleteBrackets')
@editor.transact =>
@editor.moveLeft()
@editor.delete()
Expand All @@ -127,6 +131,7 @@ class BracketMatcher

removeBrackets: ->
bracketsRemoved = false
@toggleQuotes(@getScopedSetting('bracket-matcher.autocompleteSmartQuotes'))
@editor.mutateSelectedText (selection) =>
return unless @selectionIsWrappedByMatchingBrackets(selection)

Expand All @@ -145,7 +150,7 @@ class BracketMatcher
bracketsRemoved

wrapSelectionInBrackets: (bracket) ->
return false unless atom.config.get('bracket-matcher.wrapSelectionsInBrackets')
return false unless @getScopedSetting('bracket-matcher.wrapSelectionsInBrackets')

if bracket is '#'
return false unless @isCursorOnInterpolatedString()
Expand Down Expand Up @@ -218,3 +223,6 @@ class BracketMatcher

unsubscribe: ->
@subscriptions.dispose()

getScopedSetting: (key) ->
atom.config.get(key, scope: @editor.getLastCursor().getScopeDescriptor())
41 changes: 38 additions & 3 deletions spec/bracket-matcher-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ describe "bracket matching", ->

expect(editor.buffer.getText()).toBe "a(b"

describe "when autocompleteBrackets configuration is disabled", ->
describe "when autocompleteBrackets configuration is disabled globally", ->
it "does not insert a matching bracket", ->
atom.config.set 'bracket-matcher.autocompleteBrackets', false
editor.buffer.setText("}")
Expand All @@ -536,6 +536,16 @@ describe "bracket matching", ->
expect(buffer.lineForRow(0)).toBe "{}"
expect(editor.getCursorBufferPosition()).toEqual([0, 1])

describe "when autocompleteBrackets configuration is disabled in scope", ->
it "does not insert a matching bracket", ->
atom.config.set 'bracket-matcher.autocompleteBrackets', true
atom.config.set 'bracket-matcher.autocompleteBrackets', false, scopeSelector: '.source.js'
editor.buffer.setText("}")
editor.setCursorBufferPosition([0, 0])
editor.insertText '{'
expect(buffer.lineForRow(0)).toBe "{}"
expect(editor.getCursorBufferPosition()).toEqual([0, 1])

describe "when there are multiple cursors", ->
it "inserts ) at each cursor", ->
editor.buffer.setText("()\nab\n[]\n12")
Expand Down Expand Up @@ -656,7 +666,7 @@ describe "bracket matching", ->
expect(editor.getSelectedBufferRange()).toEqual [[0, 1], [0, 5]]
expect(editor.getLastSelection().isReversed()).toBeTruthy()

describe "when the bracket-matcher.wrapSelectionsInBrackets is falsy", ->
describe "when the bracket-matcher.wrapSelectionsInBrackets is falsy globally", ->
it "does not wrap the selection in brackets", ->
atom.config.set('bracket-matcher.wrapSelectionsInBrackets', false)
editor.setText 'text'
Expand All @@ -667,6 +677,18 @@ describe "bracket matching", ->
expect(buffer.getText()).toBe '('
expect(editor.getSelectedBufferRange()).toEqual [[0, 1], [0, 1]]

describe "when the bracket-matcher.wrapSelectionsInBrackets is falsy in scope", ->
it "does not wrap the selection in brackets", ->
atom.config.set('bracket-matcher.wrapSelectionsInBrackets', true)
atom.config.set('bracket-matcher.wrapSelectionsInBrackets', false, scopeSelector: '.source.js')
editor.setText 'text'
editor.moveToBottom()
editor.selectToTop()
editor.selectAll()
editor.insertText '('
expect(buffer.getText()).toBe '('
expect(editor.getSelectedBufferRange()).toEqual [[0, 1], [0, 1]]

describe "when there is text selected on multiple lines", ->
it "wraps the selection with brackets", ->
editor.insertText 'text\nabcd'
Expand Down Expand Up @@ -911,7 +933,7 @@ describe "bracket matching", ->
editor.backspace()
expect(buffer.lineForRow(0)).toBe ""

it "does not delete end bracket even if it directly precedes a begin bracket if autocomplete is turned off", ->
it "does not delete end bracket even if it directly precedes a begin bracket if autocomplete is turned off globally", ->
atom.config.set 'bracket-matcher.autocompleteBrackets', false
buffer.setText("")
editor.setCursorBufferPosition([0, 0])
Expand All @@ -923,6 +945,19 @@ describe "bracket matching", ->
editor.backspace()
expect(buffer.lineForRow(0)).toBe "}"

it "does not delete end bracket even if it directly precedes a begin bracket if autocomplete is turned off in scope", ->
atom.config.set 'bracket-matcher.autocompleteBrackets', true
atom.config.set 'bracket-matcher.autocompleteBrackets', false, scopeSelector: '.source.js'
buffer.setText("")
editor.setCursorBufferPosition([0, 0])
editor.insertText "{"
expect(buffer.lineForRow(0)).toBe "{"
editor.insertText "}"
expect(buffer.lineForRow(0)).toBe "{}"
editor.setCursorBufferPosition([0, 1])
editor.backspace()
expect(buffer.lineForRow(0)).toBe "}"

describe 'bracket-matcher:close-tag', ->
beforeEach ->
waitsForPromise ->
Expand Down

0 comments on commit 2d8b7ed

Please sign in to comment.