|
|
@@ -4,9 +4,7 @@ define [
|
|
|
Range = ace.require("ace/range").Range
|
|
|
|
|
|
class Highlight
|
|
|
- constructor: (options) ->
|
|
|
- @row = options.row
|
|
|
- @column = options.column
|
|
|
+ constructor: (@markerId, @range, options) ->
|
|
|
@word = options.word
|
|
|
@suggestions = options.suggestions
|
|
|
|
|
|
@@ -15,56 +13,53 @@ define [
|
|
|
@reset()
|
|
|
|
|
|
reset: () ->
|
|
|
- @highlights?.rows.forEach (highlight) =>
|
|
|
+ @highlights?.forEach (highlight) =>
|
|
|
@editor.getSession().removeMarker(highlight.markerId)
|
|
|
- @highlights = rows: []
|
|
|
-
|
|
|
- addHighlight: (highlight) ->
|
|
|
- unless highlight instanceof Highlight
|
|
|
- highlight = new Highlight(highlight)
|
|
|
+ @highlights = []
|
|
|
|
|
|
+ addHighlight: (options) ->
|
|
|
session = @editor.getSession()
|
|
|
doc = session.getDocument()
|
|
|
# Set up Range that will automatically update it's positions when the
|
|
|
# document changes
|
|
|
range = new Range()
|
|
|
range.start = doc.createAnchor({
|
|
|
- row: highlight.row,
|
|
|
- column: highlight.column
|
|
|
+ row: options.row,
|
|
|
+ column: options.column
|
|
|
})
|
|
|
range.end = doc.createAnchor({
|
|
|
- row: highlight.row,
|
|
|
- column: highlight.column + highlight.word.length
|
|
|
+ row: options.row,
|
|
|
+ column: options.column + options.word.length
|
|
|
})
|
|
|
|
|
|
- highlight.markerId = session.addMarker range, "spelling-highlight", 'text', false
|
|
|
- @highlights.rows[highlight.row] ||= []
|
|
|
- @highlights.rows[highlight.row].push highlight
|
|
|
+ markerId = session.addMarker range, "spelling-highlight", 'text', false
|
|
|
+
|
|
|
+ @highlights.push new Highlight(markerId, range, options)
|
|
|
|
|
|
removeHighlight: (highlight) ->
|
|
|
@editor.getSession().removeMarker(highlight.markerId)
|
|
|
- for h, i in @highlights.rows[highlight.row]
|
|
|
- if h == highlight
|
|
|
- @highlights.rows[highlight.row].splice(i, 1)
|
|
|
+ @highlights = @highlights.filter (hl) ->
|
|
|
+ hl != highlight
|
|
|
|
|
|
clearRow: (row) ->
|
|
|
- row = @highlights.rows[row]
|
|
|
- for highlight in (row || []).slice()
|
|
|
- @removeHighlight highlight
|
|
|
+ @highlights.filter (highlight) ->
|
|
|
+ highlight.range.start.row == row
|
|
|
+ .forEach (highlight) =>
|
|
|
+ @removeHighlight(highlight)
|
|
|
|
|
|
findHighlightWithinRange: (range) ->
|
|
|
- rows = @highlights.rows.slice(range.start.row, range.end.row + 1)
|
|
|
- for row in rows
|
|
|
- for highlight in (row || [])
|
|
|
- if @_doesHighlightOverlapRange(highlight, range.start, range.end)
|
|
|
- return highlight
|
|
|
- return null
|
|
|
+ @highlights.find (highlight) =>
|
|
|
+ @_doesHighlightOverlapRange highlight, range.start, range.end
|
|
|
|
|
|
_doesHighlightOverlapRange: (highlight, start, end) ->
|
|
|
+ highlightRow = highlight.range.start.row
|
|
|
+ highlightStartColumn = highlight.range.start.column
|
|
|
+ highlightEndColumn = highlight.range.end.column
|
|
|
+
|
|
|
highlightIsAllBeforeRange =
|
|
|
- highlight.row < start.row or
|
|
|
- (highlight.row == start.row and highlight.column + highlight.word.length <= start.column)
|
|
|
+ highlightRow < start.row or
|
|
|
+ (highlightRow == start.row and highlightEndColumn <= start.column)
|
|
|
highlightIsAllAfterRange =
|
|
|
- highlight.row > end.row or
|
|
|
- (highlight.row == end.row and highlight.column >= end.column)
|
|
|
+ highlightRow > end.row or
|
|
|
+ (highlightRow == end.row and highlightStartColumn >= end.column)
|
|
|
!(highlightIsAllBeforeRange or highlightIsAllAfterRange)
|