Files
atom/src/app/range.coffee
Kevin Sawicki 7cce042f03 Change case of prefix and suffix to matched word
Previously an inserted automcomplete match would not
update the case of the prefix or suffix of the match
and instead only insert the text from the matched word
between the prefix and suffix.

Now the entire matched word is inserted as-is replacing
the existing prefix and suffix.
2012-12-26 15:09:07 -08:00

85 lines
2.1 KiB
CoffeeScript

Point = require 'point'
_ = require 'underscore'
module.exports =
class Range
@fromObject: (object) ->
if _.isArray(object)
new Range(object...)
else if object instanceof Range
object
else
new Range(object.start, object.end)
@fromPointWithDelta: (point, rowDelta, columnDelta) ->
pointA = Point.fromObject(point)
pointB = new Point(point.row + rowDelta, point.column + columnDelta)
new Range(pointA, pointB)
constructor: (pointA = new Point(0, 0), pointB = new Point(0, 0)) ->
pointA = Point.fromObject(pointA)
pointB = Point.fromObject(pointB)
if pointA.compare(pointB) <= 0
@start = pointA
@end = pointB
else
@start = pointB
@end = pointA
copy: ->
new Range(@start.copy(), @end.copy())
isEqual: (other) ->
if other instanceof Array and other.length == 2
other = new Range(other...)
other.start.isEqual(@start) and other.end.isEqual(@end)
isSingleLine: ->
@start.row == @end.row
coversSameRows: (other) ->
@start.row == other.start.row && @end.row == other.end.row
inspect: ->
"[#{@start.inspect()} - #{@end.inspect()}]"
add: (point) ->
new Range(@start.add(point), @end.add(point))
intersectsWith: (otherRange) ->
if @start.isLessThanOrEqual(otherRange.start)
@end.isGreaterThanOrEqual(otherRange.start)
else
otherRange.intersectsWith(this)
containsPoint: (point, { exclusive } = {}) ->
point = Point.fromObject(point)
if exclusive
point.isGreaterThan(@start) and point.isLessThan(@end)
else
point.isGreaterThanOrEqual(@start) and point.isLessThanOrEqual(@end)
containsRow: (row) ->
@start.row <= row <= @end.row
union: (otherRange) ->
start = if @start.isLessThan(otherRange.start) then @start else otherRange.start
end = if @end.isGreaterThan(otherRange.end) then @end else otherRange.end
new Range(start, end)
isEmpty: ->
@start.isEqual(@end)
toDelta: ->
rows = @end.row - @start.row
if rows == 0
columns = @end.column - @start.column
else
columns = @end.column
new Point(rows, columns)
getRowCount: ->
@end.row - @start.row + 1