Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the grammar's definition of nonWordCharacters #189

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/highlighted-area-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ class HighlightedAreaView

if atom.config.get('highlight-selected.onlyHighlightWholeWords')
return unless @isWordSelected(lastSelection)
nonWordCharacters = atom.config.get('editor.nonWordCharacters')
selectionStart = lastSelection.getBufferRange().start
nonWordCharacters = @getNonWordCharacters(editor, selectionStart)
allowedCharactersToSelect = atom.config.get('highlight-selected.allowedCharactersToSelect')
nonWordCharactersToStrip = nonWordCharacters.replace(
new RegExp("[#{allowedCharactersToSelect}]", 'g'), '')
Expand Down Expand Up @@ -253,19 +254,24 @@ class HighlightedAreaView
else
false

isNonWordCharacter: (character) ->
nonWordCharacters = atom.config.get('editor.nonWordCharacters')
new RegExp("[ \t#{escapeRegExp(nonWordCharacters)}]").test(character)
getNonWordCharacters: (editor, point) ->
scopeDescriptor = editor.scopeDescriptorForBufferPosition(point)
nonWordCharacters = atom.config.get('editor.nonWordCharacters', scope: scopeDescriptor)

isNonWord: (editor, range) ->
nonWordCharacters = @getNonWordCharacters(editor, range.start)
text = editor.getTextInBufferRange(range)
new RegExp("[ \t#{escapeRegExp(nonWordCharacters)}]").test(text)

isNonWordCharacterToTheLeft: (selection) ->
selectionStart = selection.getBufferRange().start
range = Range.fromPointWithDelta(selectionStart, 0, -1)
@isNonWordCharacter(@getActiveEditor().getTextInBufferRange(range))
@isNonWord(@getActiveEditor(), range)

isNonWordCharacterToTheRight: (selection) ->
selectionEnd = selection.getBufferRange().end
range = Range.fromPointWithDelta(selectionEnd, 0, 1)
@isNonWordCharacter(@getActiveEditor().getTextInBufferRange(range))
@isNonWord(@getActiveEditor(), range)

setupStatusBar: =>
return if @statusBarElement?
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/sample.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
$test = "$test";
// Some $test @test file
// #test
// Don't highlight thistest, or testthis
?>
17 changes: 14 additions & 3 deletions spec/highlight-selected-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,24 @@ describe "HighlightSelected", ->
expect(editorElement.querySelectorAll(
'.highlight-selected .region')).toHaveLength(3)

describe "being able to highlight variables when not selecting '$'", ->
describe "not being able to highlight variables when not selecting '$'", ->
beforeEach ->
atom.config.set('highlight-selected.onlyHighlightWholeWords', true)
range = new Range(new Point(1, 3), new Point(1, 7))
editor.setSelectedBufferRange(range)
advanceClock(20000)

it "finds 4 regions", ->
it "finds 0 regions", ->
expect(editorElement.querySelectorAll(
'.highlight-selected .region')).toHaveLength(4)
'.highlight-selected .region')).toHaveLength(0)

describe "being able to highlight other strings when not selecting '@'", ->
beforeEach ->
atom.config.set('highlight-selected.onlyHighlightWholeWords', true)
range = new Range(new Point(3, 6), new Point(3, 10))
editor.setSelectedBufferRange(range)
advanceClock(20000)

it "finds 0 regions", ->
expect(editorElement.querySelectorAll(
'.highlight-selected .region')).toHaveLength(2)