Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit c3ff056

Browse files
committed
Put misspelling decorations in their owm marker layer
1 parent 492fb3b commit c3ff056

File tree

6 files changed

+97
-101
lines changed

6 files changed

+97
-101
lines changed

lib/corrections-view.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CorrectionsView extends SelectListView
2424
@cancel()
2525
return unless correction
2626
@editor.transact =>
27-
@editor.selectMarker(@marker)
27+
@editor.setSelectedBufferRange(@marker.getRange())
2828
@editor.insertText(correction)
2929

3030
cancelled: ->

lib/main.coffee

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ module.exports =
1414
description: 'List of scopes for languages which will be checked for misspellings. See [the README](https://github.com/atom/spell-check#spell-check-package-) for more information on finding the correct scope for a specific language.'
1515

1616
activate: ->
17-
@disposable = atom.workspace.observeTextEditors(addViewToEditor)
17+
@viewsByEditor = new WeakMap
18+
@disposable = atom.workspace.observeTextEditors (editor) =>
19+
SpellCheckView ?= require './spell-check-view'
20+
@viewsByEditor.set(editor, new SpellCheckView(editor))
21+
22+
misspellingMarkersForEditor: (editor) ->
23+
@viewsByEditor.get(editor).markerLayer.getMarkers()
1824

1925
deactivate: ->
2026
@disposable.dispose()
21-
22-
addViewToEditor = (editor) ->
23-
SpellCheckView ?= require './spell-check-view'
24-
new SpellCheckView(editor)

lib/misspelling-view.coffee

Lines changed: 0 additions & 44 deletions
This file was deleted.

lib/spell-check-handler.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
SpellChecker = require 'spellchecker'
22

33
module.exports = ({id, text}) ->
4+
console.log("SPELL CHECKING!!", id)
5+
46
SpellChecker.add("GitHub")
57
SpellChecker.add("github")
68

lib/spell-check-view.coffee

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
_ = require 'underscore-plus'
22
{CompositeDisposable} = require 'atom'
3-
MisspellingView = require './misspelling-view'
43
SpellCheckTask = require './spell-check-task'
54

5+
CorrectionsView = null
6+
SpellChecker = null
7+
68
module.exports =
79
class SpellCheckView
810
@content: ->
911
@div class: 'spell-check'
1012

1113
constructor: (@editor) ->
1214
@disposables = new CompositeDisposable
13-
@views = []
1415
@task = new SpellCheckTask()
16+
@initializeMarkerLayer()
17+
18+
@correctMisspellingCommand = atom.commands.add atom.views.getView(@editor), 'spell-check:correct-misspelling', =>
19+
if marker = @markerLayer.findMarkers({containsPoint: @editor.getCursorBufferPosition()})[0]
20+
CorrectionsView ?= require './corrections-view'
21+
@correctionsView?.destroy()
22+
@correctionsView = new CorrectionsView(@editor, @getCorrections(marker), marker)
1523

1624
@task.onDidSpellCheck (misspellings) =>
17-
@destroyViews()
18-
@addViews(misspellings) if @buffer?
25+
@detroyMarkers()
26+
@addMarkers(misspellings) if @buffer?
1927

2028
@disposables.add @editor.onDidChangePath =>
2129
@subscribeToBuffer()
@@ -33,13 +41,25 @@ class SpellCheckView
3341

3442
@disposables.add @editor.onDidDestroy(@destroy.bind(this))
3543

44+
initializeMarkerLayer: ->
45+
@markerLayer = @editor.getBuffer().addMarkerLayer()
46+
@markerLayerDecoration = @editor.decorateMarkerLayer(@markerLayer, {
47+
type: 'highlight',
48+
class: 'spell-check-misspelling',
49+
deprecatedRegionClass: 'misspelling'
50+
})
51+
3652
destroy: ->
3753
@unsubscribeFromBuffer()
3854
@disposables.dispose()
3955
@task.terminate()
56+
@markerLayer.destroy()
57+
@markerLayerDecoration.destroy()
58+
@correctMisspellingCommand.dispose()
59+
@correctionsView?.remove()
4060

4161
unsubscribeFromBuffer: ->
42-
@destroyViews()
62+
@detroyMarkers()
4363

4464
if @buffer?
4565
@bufferDisposable.dispose()
@@ -57,18 +77,28 @@ class SpellCheckView
5777
grammar = @editor.getGrammar().scopeName
5878
_.contains(atom.config.get('spell-check.grammars'), grammar)
5979

60-
destroyViews: ->
61-
while view = @views.shift()
62-
view.destroy()
80+
detroyMarkers: ->
81+
@markerLayer.destroy()
82+
@markerLayerDecoration.destroy()
83+
@initializeMarkerLayer()
6384

64-
addViews: (misspellings) ->
85+
addMarkers: (misspellings) ->
6586
for misspelling in misspellings
66-
view = new MisspellingView(misspelling, @editor)
67-
@views.push(view)
87+
@markerLayer.markRange(misspelling,
88+
invalidate: 'touch',
89+
replicate: 'false',
90+
persistent: false,
91+
maintainHistory: false,
92+
)
6893

6994
updateMisspellings: ->
7095
# Task::start can throw errors atom/atom#3326
7196
try
7297
@task.start(@buffer.getText())
7398
catch error
7499
console.warn('Error starting spell check task', error.stack ? error)
100+
101+
getCorrections: (marker) ->
102+
SpellChecker ?= require 'spellchecker'
103+
misspelling = @editor.getTextInBufferRange(marker.getRange())
104+
corrections = SpellChecker.getCorrectionsForMisspelling(misspelling)

spec/spell-check-spec.coffee

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
describe "Spell check", ->
2-
[workspaceElement, editor, editorElement] = []
2+
[workspaceElement, editor, editorElement, spellCheckModule] = []
33

4-
textForDecoration = ({marker}) ->
5-
editor.getTextInBufferRange(marker.getBufferRange())
4+
textForMarker = (marker) ->
5+
editor.getTextInBufferRange(marker.getRange())
6+
7+
getMisspellingMarkers = ->
8+
spellCheckModule.misspellingMarkersForEditor(editor)
69

710
beforeEach ->
811
workspaceElement = atom.views.getView(atom.workspace)
@@ -20,7 +23,8 @@ describe "Spell check", ->
2023
atom.workspace.open('sample.js')
2124

2225
waitsForPromise ->
23-
atom.packages.activatePackage('spell-check')
26+
atom.packages.activatePackage('spell-check').then ({mainModule}) ->
27+
spellCheckModule = mainModule
2428

2529
runs ->
2630
jasmine.attachToDOM(workspaceElement)
@@ -31,67 +35,67 @@ describe "Spell check", ->
3135
editor.setText("This middle of thiss\nsentencts\n\nhas issues and the \"edn\" 'dsoe' too")
3236
atom.config.set('spell-check.grammars', ['source.js'])
3337

34-
decorations = null
35-
38+
misspellingMarkers = null
3639
waitsFor ->
37-
decorations = editor.getHighlightDecorations(class: 'spell-check-misspelling')
38-
decorations.length > 0
39-
40+
misspellingMarkers = getMisspellingMarkers()
41+
misspellingMarkers.length > 0
4042

4143
runs ->
42-
expect(decorations.length).toBe 4
43-
expect(textForDecoration(decorations[0])).toEqual "thiss"
44-
expect(textForDecoration(decorations[1])).toEqual "sentencts"
45-
expect(textForDecoration(decorations[2])).toEqual "edn"
46-
expect(textForDecoration(decorations[3])).toEqual "dsoe"
44+
expect(misspellingMarkers.length).toBe 4
45+
expect(textForMarker(misspellingMarkers[0])).toEqual "thiss"
46+
expect(textForMarker(misspellingMarkers[1])).toEqual "sentencts"
47+
expect(textForMarker(misspellingMarkers[2])).toEqual "edn"
48+
expect(textForMarker(misspellingMarkers[3])).toEqual "dsoe"
4749

4850
it "doesn't consider our company's name to be a spelling error", ->
4951
editor.setText("GitHub (aka github): Where codez are built.")
5052
atom.config.set('spell-check.grammars', ['source.js'])
5153

52-
decorations = null
53-
54+
misspellingMarkers = null
5455
waitsFor ->
55-
decorations = editor.getHighlightDecorations(class: 'spell-check-misspelling')
56-
decorations.length > 0
56+
misspellingMarkers = getMisspellingMarkers()
57+
misspellingMarkers.length > 0
5758

5859
runs ->
59-
expect(decorations.length).toBe 1
60-
expect(textForDecoration(decorations[0])).toBe "codez"
60+
expect(misspellingMarkers.length).toBe 1
61+
expect(textForMarker(misspellingMarkers[0])).toBe "codez"
6162

6263
it "hides decorations when a misspelled word is edited", ->
6364
editor.setText('notaword')
6465
advanceClock(editor.getBuffer().getStoppedChangingDelay())
6566
atom.config.set('spell-check.grammars', ['source.js'])
6667

67-
decorations = null
68+
misspellingMarkers = null
6869
waitsFor ->
69-
decorations = editor.getHighlightDecorations(class: 'spell-check-misspelling')
70-
decorations.length > 0
70+
misspellingMarkers = getMisspellingMarkers()
71+
misspellingMarkers.length > 0
7172

7273
runs ->
73-
expect(decorations.length).toBe 1
74+
expect(misspellingMarkers.length).toBe 1
7475
editor.moveToEndOfLine()
7576
editor.insertText('a')
7677
advanceClock(editor.getBuffer().getStoppedChangingDelay())
77-
decorations = editor.getHighlightDecorations(class: 'spell-check-misspelling')
78-
expect(decorations.length).toBe 1
79-
expect(decorations[0].marker.isValid()).toBe false
78+
79+
misspellingMarkers = getMisspellingMarkers()
80+
81+
expect(misspellingMarkers.length).toBe 1
82+
expect(misspellingMarkers[0].isValid()).toBe false
8083

8184
describe "when spell checking for a grammar is removed", ->
8285
it "removes all the misspellings", ->
8386
editor.setText('notaword')
8487
advanceClock(editor.getBuffer().getStoppedChangingDelay())
8588
atom.config.set('spell-check.grammars', ['source.js'])
8689

87-
decorations = null
90+
misspellingMarkers = null
8891
waitsFor ->
89-
editor.getHighlightDecorations(class: 'spell-check-misspelling').length > 0
92+
misspellingMarkers = getMisspellingMarkers()
93+
misspellingMarkers.length > 0
9094

9195
runs ->
92-
expect(editor.getHighlightDecorations(class: 'spell-check-misspelling').length).toBe 1
96+
expect(getMisspellingMarkers().length).toBe 1
9397
atom.config.set('spell-check.grammars', [])
94-
expect(editor.getHighlightDecorations(class: 'spell-check-misspelling').length).toBe 0
98+
expect(getMisspellingMarkers().length).toBe 0
9599

96100
describe "when the editor's grammar changes to one that does not have spell check enabled", ->
97101
it "removes all the misspellings", ->
@@ -100,12 +104,12 @@ describe "Spell check", ->
100104
atom.config.set('spell-check.grammars', ['source.js'])
101105

102106
waitsFor ->
103-
editor.getHighlightDecorations(class: 'spell-check-misspelling').length > 0
107+
getMisspellingMarkers().length > 0
104108

105109
runs ->
106-
expect(editor.getHighlightDecorations(class: 'spell-check-misspelling').length).toBe 1
110+
expect(getMisspellingMarkers().length).toBe 1
107111
editor.setGrammar(atom.grammars.selectGrammar('.txt'))
108-
expect(editor.getHighlightDecorations(class: 'spell-check-misspelling').length).toBe 0
112+
expect(getMisspellingMarkers().length).toBe 0
109113

110114
describe "when 'spell-check:correct-misspelling' is triggered on the editor", ->
111115
describe "when the cursor touches a misspelling that has corrections", ->
@@ -115,9 +119,11 @@ describe "Spell check", ->
115119
atom.config.set('spell-check.grammars', ['source.js'])
116120

117121
waitsFor ->
118-
editor.getHighlightDecorations(class: 'spell-check-misspelling').length > 0
122+
getMisspellingMarkers().length is 1
119123

120124
runs ->
125+
expect(getMisspellingMarkers()[0].isValid()).toBe true
126+
121127
atom.commands.dispatch editorElement, 'spell-check:correct-misspelling'
122128

123129
correctionsElement = editorElement.querySelector('.corrections')
@@ -129,8 +135,8 @@ describe "Spell check", ->
129135

130136
expect(editor.getText()).toBe 'together'
131137
expect(editor.getCursorBufferPosition()).toEqual [0, 8]
132-
advanceClock(editor.getBuffer().getStoppedChangingDelay())
133-
expect(editorElement.querySelectorAll('.spell-check-misspelling').length).toBe 0
138+
139+
expect(getMisspellingMarkers()[0].isValid()).toBe false
134140
expect(editorElement.querySelector('.corrections')).toBeNull()
135141

136142
describe "when the cursor touches a misspelling that has no corrections", ->
@@ -140,7 +146,7 @@ describe "Spell check", ->
140146
atom.config.set('spell-check.grammars', ['source.js'])
141147

142148
waitsFor ->
143-
editor.getHighlightDecorations(class: 'spell-check-misspelling').length > 0
149+
getMisspellingMarkers().length > 0
144150

145151
runs ->
146152
atom.commands.dispatch editorElement, 'spell-check:correct-misspelling'
@@ -154,8 +160,8 @@ describe "Spell check", ->
154160
atom.config.set('spell-check.grammars', ['source.js'])
155161

156162
waitsFor ->
157-
editor.getHighlightDecorations(class: 'spell-check-misspelling').length > 0
163+
getMisspellingMarkers().length > 0
158164

159165
runs ->
160166
editor.destroy()
161-
expect(editor.getMarkers().length).toBe 0
167+
expect(getMisspellingMarkers().length).toBe 0

0 commit comments

Comments
 (0)